Changeset View
Changeset View
Standalone View
Standalone View
tests/auto/qmljsc/testcodegeneratorforqml.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) 2016 Anton Kreuzkamp <kde-development@akreuzkamp.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 <QtTest/QTest> | ||||
22 | | ||||
23 | #include <private/qqmljsast_p.h> | ||||
24 | #include "../../../src/qmljsc/utils/error.h" | ||||
25 | #include "../../../src/qmljsc/codegeneration/codegeneratorforqml.h" | ||||
26 | #include "../../../src/qmljsc/ir/module.h" | ||||
27 | | ||||
28 | #define TEST_VISIT(className, scenarioName, instance, stackContent, expectedTopOfStack) \ | ||||
29 | void test_visit_ ## className ## _ ## scenarioName() { \ | ||||
30 | m_generator->m_outputStack.append(QVector<QString>stackContent); \ | ||||
31 | m_generator->endVisit(instance); \ | ||||
32 | QCOMPARE(m_generator->m_outputStack.top(), QLatin1String(expectedTopOfStack)); \ | ||||
33 | QCOMPARE(m_generator->m_outputStack.count(), 1); \ | ||||
34 | } | ||||
35 | | ||||
36 | using namespace QQmlJS; | ||||
37 | | ||||
38 | Q_DECLARE_METATYPE(QmlJSc::IR::Node *); | ||||
39 | | ||||
40 | namespace QmlJSc { | ||||
41 | | ||||
42 | class TestCodeGeneratorForQml | ||||
43 | : public QObject | ||||
44 | { | ||||
45 | Q_OBJECT | ||||
46 | | ||||
47 | public: | ||||
48 | TestCodeGeneratorForQml() | ||||
49 | { | ||||
50 | m_sourceElements.finish(); | ||||
51 | | ||||
52 | m_fullObject.addValueAssignment(&m_property); | ||||
53 | m_fullObject.addSignalHandler(&m_signal); | ||||
54 | m_fullObject.addProperty("someProperty"); | ||||
55 | m_fullObject.addSignal("someSignal"); | ||||
56 | m_fullObject.addSignal("someOtherSignal"); | ||||
57 | m_fullObject.addMethod("someMethod"); | ||||
58 | } | ||||
59 | | ||||
60 | private: | ||||
61 | CodeGeneratorForQml *m_generator; | ||||
62 | | ||||
63 | const QString m_someLabel {"foo"}; | ||||
64 | const QStringRef m_someLabelStringRef {&m_someLabel}; | ||||
65 | | ||||
66 | AST::NumericLiteral m_numericalExpressionPi {3.14}; | ||||
67 | AST::ExpressionStatement m_simpleJsStatement {&m_numericalExpressionPi}; | ||||
68 | AST::IdentifierExpression m_idExpression {m_someLabelStringRef}; | ||||
69 | AST::PreIncrementExpression m_preIncExpression {&m_idExpression}; | ||||
70 | AST::ExpressionStatement m_preIncStatement {&m_preIncExpression}; | ||||
71 | AST::StatementSourceElement m_sourceElement {&m_preIncStatement}; | ||||
72 | AST::SourceElements m_sourceElements {&m_sourceElement}; | ||||
73 | AST::FunctionBody m_functionBody { &m_sourceElements }; | ||||
74 | | ||||
75 | IR::Type m_type { "T" }; | ||||
76 | IR::Property m_property {&m_type, QStringLiteral("propName")}; | ||||
77 | IR::Method m_emptyMethod {QStringLiteral("someMethod")}; | ||||
78 | IR::Method m_methodWithContent {QStringLiteral("m2"), &m_functionBody}; | ||||
79 | IR::Signal m_signal { QStringLiteral("someSignal") }; | ||||
80 | IR::Signal m_paramSignal { QStringLiteral("otherSignal"), {{&m_type, "a"}, {&m_type, "b"}}}; | ||||
81 | IR::Module m_module {"Mod", 1, 0}; | ||||
82 | | ||||
83 | IR::ValueAssignment m_jsAssignment {&m_property, &m_simpleJsStatement, IR::ValueAssignment::JsAssignment}; | ||||
84 | IR::ValueAssignment m_objectAssignment {&m_property, (IR::ObjectSpec*)nullptr}; | ||||
85 | IR::ValueAssignment m_bindingAssignment {&m_property, &m_preIncStatement, IR::ValueAssignment::Binding}; | ||||
86 | IR::ValueAssignment m_emptyObjectListAssignment {&m_property, {}}; | ||||
87 | IR::ValueAssignment m_objectListAssignment {&m_property, {0, 0, 0}}; | ||||
88 | | ||||
89 | IR::SignalHandler m_signalHandler {&m_signal, &m_preIncStatement}; | ||||
90 | | ||||
91 | IR::ObjectSpec m_emptyObject { &m_type }; | ||||
92 | IR::ObjectSpec m_fullObject { &m_type }; | ||||
93 | IR::Import m_import {&m_module, ""}; | ||||
94 | IR::Import m_importLocalPrefix {&m_module, "Foo"}; | ||||
95 | IR::File m_fileWithoutImports { {}, &m_emptyObject }; | ||||
96 | IR::File m_fileWithOneModuleImport { {{&m_module, ""}}, &m_emptyObject }; | ||||
97 | | ||||
98 | private slots: | ||||
99 | void init() { | ||||
100 | m_generator = new CodeGeneratorForQml(); | ||||
101 | }; | ||||
102 | | ||||
103 | void cleanup() { | ||||
104 | delete m_generator; | ||||
105 | } | ||||
106 | | ||||
107 | void testGeneratedCodeIsInitiallyEmpty() { | ||||
108 | QCOMPARE(m_generator->getGeneratedCode(), QStringLiteral("")); | ||||
109 | } | ||||
110 | | ||||
111 | void test_getGeneratedCode_getsTopOfStack() { | ||||
112 | // Prepare | ||||
113 | m_generator->m_outputStack << "1"; | ||||
114 | | ||||
115 | // Verify | ||||
116 | QCOMPARE(m_generator->getGeneratedCode(), QStringLiteral("1")); | ||||
117 | } | ||||
118 | | ||||
119 | void test_getGeneratedCode_throwsError_OnStackSizeGreaterThanOne() { | ||||
120 | // Prepare | ||||
121 | m_generator->m_outputStack << "1" << "2"; | ||||
122 | | ||||
123 | // Verify | ||||
124 | QVERIFY_EXCEPTION_THROWN(m_generator->getGeneratedCode(), QmlJSc::Error *); | ||||
125 | } | ||||
126 | | ||||
127 | TEST_VISIT(ValueAssignment, JsAssignment , &m_jsAssignment , {} , "propName: 3.14;") | ||||
128 | TEST_VISIT(ValueAssignment, Binding , &m_bindingAssignment , {} , "propName: new QmlBinding(\"++foo;\")" ) | ||||
129 | TEST_VISIT(ValueAssignment, ObjectAssignment , &m_objectAssignment , {"{}"} , "propName: {}" ) | ||||
130 | TEST_VISIT(ValueAssignment, EmptyObjectList , &m_emptyObjectListAssignment, ({}) , "propName: []") | ||||
131 | TEST_VISIT(ValueAssignment, ObjectList , &m_objectListAssignment , ({"{}","{}","{}"}) , "propName: [{},{},{}]") | ||||
132 | TEST_VISIT(SignalHandler , SimpleHandler , &m_signalHandler , {} , "onSomeSignal: new QmlBinding(\"++foo;\")") | ||||
133 | TEST_VISIT(ObjectSpec , EmptyObject , &m_emptyObject , {} , "{__proto__:QMLMetaElement.prototype,$class:\"T\"}") | ||||
134 | TEST_VISIT(ObjectSpec , ObjectWithEverything , &m_fullObject , ({"1", "2", "3", "4", "5", "6"}), "{__proto__:QMLMetaElement.prototype,$class:\"T\",1,2,3,4,5,6}") | ||||
135 | TEST_VISIT(Import , ModuleImportNoAs , &m_import , {} , "[\"qmlimport\",\"Mod\",1.0,\"\",true]" ) | ||||
136 | TEST_VISIT(Import , ModuleImportWithAs , &m_importLocalPrefix , {} , "[\"qmlimport\",\"Mod\",1.0,\"Foo\",true]" ) | ||||
137 | TEST_VISIT(File , FileWithoutImport , &m_fileWithoutImports , {"{}"} , "{$class:\"Component\",$children:[{}],$imports:[]}") | ||||
138 | TEST_VISIT(File , FileWithOneModuleImport, &m_fileWithOneModuleImport , ({"[]","{}"}) , "{$class:\"Component\",$children:[{}],$imports:[[]]}") | ||||
139 | TEST_VISIT(Property , SimpleProperty , &m_property , ({}) , "propName: new QMLPropertyDefinition(\"T\")") | ||||
140 | TEST_VISIT(Method , EmptyMethod , &m_emptyMethod , ({}) , "someMethod: new QMLMethod(\"\")") | ||||
141 | TEST_VISIT(Method , MethodWithContent , &m_methodWithContent , ({}) , "m2: new QMLMethod(\"++foo;\")") | ||||
142 | TEST_VISIT(Signal , SimpleSignal , &m_signal , ({}) , "someSignal: new QMLSignalDefinition([])") | ||||
143 | TEST_VISIT(Signal , SignalWithParams , &m_paramSignal , ({}) , "otherSignal: new QMLSignalDefinition([{name:\"a\",type:\"T\"},{name:\"b\",type:\"T\"}])") | ||||
144 | }; | ||||
145 | | ||||
146 | } // namespace QmlJSc | ||||
147 | | ||||
148 | QTEST_MAIN(QmlJSc::TestCodeGeneratorForQml) | ||||
149 | #include "testcodegeneratorforqml.moc" |