File indexing completed on 2024-05-19 15:46:49

0001 /*
0002     SPDX-FileCopyrightText: 2013 Milian Wolff <mail@milianw.de>
0003     SPDX-FileCopyrightText: 2013 Olivier de Gaalon <olivier.jg@gmail.com>
0004 
0005     SPDX-License-Identifier: GPL-2.0-or-later
0006 */
0007 
0008 #include "test_files.h"
0009 
0010 #include <language/duchain/duchain.h>
0011 #include <language/duchain/problem.h>
0012 #include <language/codegen/coderepresentation.h>
0013 #include <language/backgroundparser/backgroundparser.h>
0014 
0015 #include <tests/testcore.h>
0016 #include <tests/autotestshell.h>
0017 #include <tests/json/declarationvalidator.h>
0018 
0019 #include "testfilepaths.h"
0020 
0021 //Include all used json tests, otherwise "Test not found"
0022 #include <tests/json/jsondeclarationtests.h>
0023 #include <tests/json/jsonducontexttests.h>
0024 #include <tests/json/jsontypetests.h>
0025 #include <interfaces/ilanguagecontroller.h>
0026 
0027 #include <QTest>
0028 
0029 // #include "cppjsontests.h"
0030 
0031 using namespace KDevelop;
0032 
0033 QTEST_MAIN(TestFiles)
0034 
0035 void TestFiles::initTestCase()
0036 {
0037   AutoTestShell::init({"kdevqmljs"});
0038   TestCore::initialize(KDevelop::Core::NoUi);
0039   DUChain::self()->disablePersistentStorage();
0040   Core::self()->languageController()->backgroundParser()->setDelay(0);
0041   CodeRepresentation::setDiskChangesForbidden(true);
0042 }
0043 
0044 void TestFiles::cleanupTestCase()
0045 {
0046   TestCore::shutdown();
0047 }
0048 
0049 void TestFiles::testQMLCustomComponent()
0050 {
0051     // First parse CustomComponent, so that it is visible and can be used
0052     // by CustomComponentUser. Then re-parse CustomComponent and assert that
0053     // it has been used.
0054     parseAndCheck(TEST_FILES_DIR "/custom_component/CustomComponent.qml", false);
0055     parseAndCheck(TEST_FILES_DIR "/custom_component/CustomComponentUser.qml");
0056     parseAndCheck(TEST_FILES_DIR "/custom_component/CustomComponent.qml");
0057 }
0058 
0059 void TestFiles::testQMLTypes()
0060 {
0061     parseAndCheck(TEST_FILES_DIR "/qmltypes/AnItem.qml", true);
0062 }
0063 
0064 void TestFiles::testTypeMismatchFalsePositives()
0065 {
0066     parseAndCheck(TEST_FILES_DIR "/type_mismatch_false_positives/code.js", true);
0067 }
0068 
0069 void TestFiles::testJSUsesBetweenFiles()
0070 {
0071     parseAndCheck(TEST_FILES_DIR "/js_cross_file_uses/js_variable_definition.js", false);
0072     parseAndCheck(TEST_FILES_DIR "/js_cross_file_uses/js_variable_use.js");
0073     parseAndCheck(TEST_FILES_DIR "/js_cross_file_uses/js_variable_definition.js");
0074 }
0075 
0076 void TestFiles::testNodeJS()
0077 {
0078     parseAndCheck(TEST_FILES_DIR "/node_modules/module.js", false); // Ensure that module.js is in the DUChain
0079     parseAndCheck(TEST_FILES_DIR "/node.js/module2.js", false);
0080     parseAndCheck(TEST_FILES_DIR "/node.js/main.js");
0081 }
0082 
0083 void TestFiles::testFiles_data()
0084 {
0085   QTest::addColumn<QString>("fileName");
0086   const QString testDirPath = TEST_FILES_DIR;
0087   const QStringList files = QDir(testDirPath).entryList(QStringList() << QStringLiteral("*.js") << QStringLiteral("*.qml"), QDir::Files);
0088   for (const QString& file : files) {
0089     QTest::newRow(file.toUtf8()) << QString(testDirPath + "/" + file);
0090   }
0091 }
0092 
0093 void TestFiles::testFiles()
0094 {
0095   QFETCH(QString, fileName);
0096   parseAndCheck(fileName);
0097 }
0098 
0099 void TestFiles::parseAndCheck(const QString& fileName, bool check)
0100 {
0101   const IndexedString indexedFileName(fileName);
0102   ReferencedTopDUContext top =
0103       DUChain::self()->waitForUpdate(indexedFileName, KDevelop::TopDUContext::AllDeclarationsContextsAndUses);
0104 
0105   while (!ICore::self()->languageController()->backgroundParser()->isIdle()) {
0106       QTest::qWait(500);
0107   }
0108 
0109   QVERIFY(top);
0110 
0111   if (check) {
0112     DUChainReadLocker lock;
0113     DeclarationValidator validator;
0114     top->visit(validator);
0115     QVERIFY(validator.testsPassed());
0116 
0117     if (!top->problems().isEmpty()) {
0118         foreach(auto p, top->problems()) {
0119             qDebug() << p;
0120         }
0121     }
0122     if (!QTest::currentDataTag() || strcmp("failparse.js", QTest::currentDataTag()) != 0) {
0123         QEXPECT_FAIL("plugins.qml", "not working properly yet", Continue);
0124         QEXPECT_FAIL("qrc_import.qml", "just making sure it does not crash", Continue);
0125         QVERIFY(top->problems().isEmpty());
0126     }
0127   }
0128 }
0129 
0130 #include "moc_test_files.cpp"