Changeset View
Changeset View
Standalone View
Standalone View
tests/auto/qmljsc/testpurejavascriptgenerator_integration.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Qml.js Compiler - a QML to JS compiler bringing QML's power to the web. | ||||
3 | * | ||||
4 | * Copyright (C) 2015 Jan Marker <jan@jangmarker.de> | ||||
5 | * | ||||
6 | * This program is free software: you can redistribute it and/or modify | ||||
7 | * it under the terms of the GNU General Public License as published by | ||||
8 | * the Free Software Foundation, either version 3 of the License, or | ||||
9 | * (at your option) any later version. | ||||
10 | * | ||||
11 | * This program is distributed in the hope that it will be useful, | ||||
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
14 | * GNU General Public License for more details. | ||||
15 | * | ||||
16 | * You should have received a copy of the GNU General Public License | ||||
17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
18 | * | ||||
19 | */ | ||||
20 | | ||||
21 | #include "../../../src/qmljsc/purejavascriptgenerator.h" | ||||
22 | | ||||
23 | #include <QtCore/QMetaType> | ||||
24 | #include <QtTest/QTest> | ||||
25 | #include <QtCore/QFile> | ||||
26 | | ||||
27 | #include <private/qqmljsast_p.h> | ||||
28 | #include <private/qqmljsengine_p.h> | ||||
29 | #include <private/qqmljslexer_p.h> | ||||
30 | #include <private/qqmljsparser_p.h> | ||||
31 | | ||||
32 | Q_DECLARE_METATYPE(QQmlJS::AST::Node*) | ||||
33 | | ||||
34 | class TestPureJavaScriptGeneratorIntegration | ||||
35 | : public QObject | ||||
36 | { | ||||
37 | Q_OBJECT | ||||
38 | | ||||
39 | private: | ||||
40 | QQmlJS::AST::Node* astForFile(QString fileName) { | ||||
41 | QString input = fileContent(fileName); | ||||
42 | | ||||
43 | QQmlJS::Engine* engine = new QQmlJS::Engine(); | ||||
44 | QQmlJS::Lexer* lexer = new QQmlJS::Lexer(engine); | ||||
45 | | ||||
46 | lexer->setCode(input, 1, true); | ||||
47 | | ||||
48 | QQmlJS::Parser* parser = new QQmlJS::Parser(engine); | ||||
49 | | ||||
50 | if (!parser->parseProgram()) { | ||||
51 | qDebug() << parser->errorMessage(); | ||||
52 | } | ||||
53 | | ||||
54 | return parser->rootNode(); | ||||
55 | } | ||||
56 | | ||||
57 | QString fileContent(QString fileName) { | ||||
58 | QFile inputFile(fileName); | ||||
59 | Q_ASSERT_X(inputFile.open(QFile::ReadOnly), __FILE__, "File could not be opened. Is it added as resource in the CMakeLists.txt?"); | ||||
60 | return QString(inputFile.readAll()); | ||||
61 | } | ||||
62 | | ||||
63 | void addRowForFileWithCompiled(QString sourceFileName, QString compiledFileName) { | ||||
64 | const QString folder(":/test/%1.js"); | ||||
65 | QTest::newRow(sourceFileName.toLocal8Bit()) << astForFile(folder.arg(sourceFileName)) << fileContent(folder.arg(compiledFileName)); | ||||
66 | | ||||
67 | } | ||||
68 | | ||||
69 | void addRowForFile(QString fileName) { | ||||
70 | addRowForFileWithCompiled(fileName, fileName); | ||||
71 | } | ||||
72 | | ||||
73 | void addRowForFileWithCompiled(QString fileName) { | ||||
74 | addRowForFileWithCompiled(fileName, QString("%1.compiled").arg(fileName)); | ||||
75 | } | ||||
76 | | ||||
77 | private slots: | ||||
78 | | ||||
79 | void test_compileJavaScriptFile_data() { | ||||
80 | QTest::addColumn<QQmlJS::AST::Node*>("ast"); | ||||
81 | QTest::addColumn<QString>("expectedOutput"); | ||||
82 | | ||||
83 | addRowForFileWithCompiled("declarations"); | ||||
84 | addRowForFile("expressions"); | ||||
85 | addRowForFileWithCompiled("functions"); | ||||
86 | addRowForFileWithCompiled("binaryoperations"); | ||||
87 | addRowForFileWithCompiled("otherstatements"); | ||||
88 | addRowForFileWithCompiled("ifstatements"); | ||||
89 | addRowForFileWithCompiled("switchstatements"); | ||||
90 | } | ||||
91 | | ||||
92 | void test_compileJavaScriptFile() { | ||||
93 | // Prepare | ||||
94 | QFETCH(QQmlJS::AST::Node*, ast); | ||||
95 | QFETCH(QString, expectedOutput); | ||||
96 | | ||||
97 | PureJavaScriptGenerator generator; | ||||
98 | | ||||
99 | // Do | ||||
100 | ast->accept(&generator); | ||||
101 | | ||||
102 | // Verify | ||||
103 | QCOMPARE(generator.getGeneratedCode(), expectedOutput); | ||||
104 | } | ||||
105 | | ||||
106 | | ||||
107 | | ||||
108 | }; | ||||
109 | | ||||
110 | QTEST_MAIN(TestPureJavaScriptGeneratorIntegration) | ||||
111 | #include "testpurejavascriptgenerator_integration.moc" |