File indexing completed on 2024-05-12 04:39:54

0001 /*
0002     This file is part of KDevelop
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "test_generationtest.h"
0008 #include "tests_config.h"
0009 
0010 #include <tests/testcore.h>
0011 #include <tests/autotestshell.h>
0012 
0013 #include <language/codegen/templatesmodel.h>
0014 #include <language/codegen/sourcefiletemplate.h>
0015 #include <language/codegen/documentchangeset.h>
0016 #include <language/codegen/templaterenderer.h>
0017 
0018 #include <util/path.h>
0019 
0020 #include <QStandardPaths>
0021 
0022 using namespace KDevelop;
0023 
0024 
0025 #define COMPARE_FILES(name)                                                 \
0026 do {                                                                        \
0027 QFile actualFile(Path(Path(baseUrl), QStringLiteral(name)).toLocalFile());                        \
0028 QVERIFY(actualFile.open(QIODevice::ReadOnly));                              \
0029 QFile expectedFile(QStringLiteral(TESTS_EXPECTED_DIR "/" name));                  \
0030 QVERIFY(expectedFile.open(QIODevice::ReadOnly));                            \
0031 QCOMPARE(actualFile.size(), expectedFile.size());                           \
0032 QCOMPARE(QString(actualFile.readAll()), QString(expectedFile.readAll()));   \
0033 } while(0)
0034 
0035 void TestGenerationTest::initTestCase()
0036 {
0037     QByteArray xdgData = qgetenv("XDG_DATA_DIRS");
0038     xdgData.prepend(TESTS_DATA_DIR ":");
0039     bool addedDir = qputenv("XDG_DATA_DIRS", xdgData);
0040     QVERIFY(addedDir);
0041 
0042     // avoid translated desktop entries, tests use untranslated strings
0043     QLocale::setDefault(QLocale::c());
0044     AutoTestShell::init();
0045     TestCore::initialize (Core::NoUi);
0046 
0047     TemplatesModel model(QStringLiteral("testgenerationtest"));
0048     model.refresh();
0049 
0050     renderer = new TemplateRenderer;
0051     renderer->setEmptyLinesPolicy(TemplateRenderer::TrimEmptyLines);
0052     renderer->addVariable(QStringLiteral("name"), "TestName");
0053     renderer->addVariable(QStringLiteral("license"), "Test license header\nIn two lines");
0054 
0055     QStringList testCases;
0056     testCases << QStringLiteral("firstTestCase");
0057     testCases << QStringLiteral("secondTestCase");
0058     testCases << QStringLiteral("thirdTestCase");
0059     renderer->addVariable(QStringLiteral("testCases"), testCases);
0060 }
0061 
0062 void TestGenerationTest::cleanupTestCase()
0063 {
0064     delete renderer;
0065     TestCore::shutdown();
0066 }
0067 
0068 void TestGenerationTest::init()
0069 {
0070     dir.reset(new QTemporaryDir);
0071     baseUrl = QUrl::fromLocalFile(dir->path());
0072 }
0073 
0074 void TestGenerationTest::yamlTemplate()
0075 {
0076     QString description = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("testgenerationtest/template_descriptions/test_yaml2.desktop"));
0077     QVERIFY(!description.isEmpty());
0078     SourceFileTemplate file;
0079     file.addAdditionalSearchLocation(QStringLiteral(TESTS_DATA_DIR "/testgenerationtest/templates"));
0080     file.setTemplateDescription(description);
0081 
0082     QCOMPARE(file.name(), QStringLiteral("Testing YAML Template"));
0083     QVERIFY(file.isValid());
0084 
0085     DocumentChangeSet changes = renderer->renderFileTemplate(file, baseUrl, urls(file));
0086     changes.applyAllChanges();
0087 
0088     COMPARE_FILES("testname.yaml");
0089 }
0090 
0091 void TestGenerationTest::cppTemplate()
0092 {
0093     QString description = QStandardPaths::locate(QStandardPaths::GenericDataLocation, QStringLiteral("testgenerationtest/template_descriptions/test_qtestlib.desktop"));
0094     QVERIFY(!description.isEmpty());
0095     SourceFileTemplate file;
0096     file.addAdditionalSearchLocation(QStringLiteral(TESTS_DATA_DIR "/testgenerationtest/templates"));
0097     file.setTemplateDescription(description);
0098 
0099     QCOMPARE(file.name(), QStringLiteral("Testing C++ Template"));
0100     QVERIFY(file.isValid());
0101 
0102     DocumentChangeSet changes = renderer->renderFileTemplate(file, baseUrl, urls(file));
0103     changes.applyAllChanges();
0104 
0105     COMPARE_FILES("testname.h");
0106     COMPARE_FILES("testname.cpp");
0107 }
0108 
0109 QHash< QString, QUrl > TestGenerationTest::urls (const SourceFileTemplate& file)
0110 {
0111     QHash<QString, QUrl> ret;
0112     const auto outputFiles = file.outputFiles();
0113     for (const SourceFileTemplate::OutputFile& output : outputFiles) {
0114         QUrl url = Path(Path(baseUrl), renderer->render(output.outputName).toLower()).toUrl();
0115         ret.insert(output.identifier, url);
0116     }
0117     return ret;
0118 }
0119 
0120 
0121 QTEST_GUILESS_MAIN(TestGenerationTest)
0122 
0123 #include "moc_test_generationtest.cpp"