21 #ifndef SCRIPTABLETAGSTEST_H
22 #define SCRIPTABLETAGSTEST_H
24 #include <QtCore/QDebug>
25 #include <QtCore/QDir>
26 #include <QtCore/QFileInfo>
27 #include <QtTest/QTest>
30 #include "coverageobject.h"
32 #include "filterexpression.h"
33 #include "cutelee_paths.h"
36 typedef QHash<QString, QVariant> Dict;
49 void testBasicSyntax_data();
50 void testBasicSyntax() { doTest(); }
52 void testResolve_data();
53 void testResolve() { doTest(); }
55 void cleanupTestCase();
63 void TestScriptableTagsSyntax::initTestCase()
65 Q_INIT_RESOURCE(testresource);
67 m_engine =
new Engine(
this);
68 m_engine->setPluginPaths({
69 QStringLiteral(CUTELEE_PLUGIN_PATH),
70 QStringLiteral(
":/plugins/")
72 m_engine->addDefaultLibrary(QStringLiteral(
"cutelee_scriptabletags"));
75 void TestScriptableTagsSyntax::cleanupTestCase() {
delete m_engine; }
77 void TestScriptableTagsSyntax::doTest()
84 auto t = m_engine->newTemplate(input, QLatin1String(QTest::currentDataTag()));
86 if (t->error() != NoError) {
87 if (t->error() != error)
88 qDebug() << t->errorString();
89 QCOMPARE(t->error(), error);
95 auto result = t->render(&context);
97 if (t->error() != NoError) {
98 if (t->error() != error)
99 qDebug() << t->errorString();
100 QCOMPARE(t->error(), error);
104 QCOMPARE(t->error(), NoError);
107 QCOMPARE(NoError, error);
109 QCOMPARE(t->error(), NoError);
111 QCOMPARE(result, output);
114 void TestScriptableTagsSyntax::testBasicSyntax_data()
116 QTest::addColumn<QString>(
"input");
117 QTest::addColumn<Dict>(
"dict");
118 QTest::addColumn<QString>(
"output");
119 QTest::addColumn<Cutelee::Error>(
"error");
123 dict.insert(QStringLiteral(
"boo"), QStringLiteral(
"Far"));
124 dict.insert(QStringLiteral(
"booList"),
125 QVariantList{QStringLiteral(
"Tom"), QStringLiteral(
"Dick"),
126 QStringLiteral(
"Harry")});
128 QTest::newRow(
"scriptable-tags01")
129 <<
"{% load scripteddefaults %}{% if2 "
130 "\"something\\\" stupid\" %}{{ boo "
132 << dict << QStringLiteral(
"Far") << NoError;
135 QTest::newRow(
"scriptable-tags02")
136 <<
"{% load scripteddefaults %}{% if2 \"something\\\" stupid\" %}{% "
138 "name in booList %}:{{ name }};{% endfor %}{% endif2 %}"
139 << dict << QStringLiteral(
":Tom;:Dick;:Harry;") << NoError;
142 QTest::newRow(
"scriptable-tags03") << QStringLiteral(
143 "{% load scripteddefaults %}{% if2 boo %}yes{% else %}no{% endif2 %}")
144 << dict << QStringLiteral(
"yes")
146 QTest::newRow(
"scriptable-tags04") << QStringLiteral(
147 "{% load scripteddefaults %}{% if2 foo %}yes{% else %}no{% endif2 %}")
148 << dict << QStringLiteral(
"no") << NoError;
150 QTest::newRow(
"scriptable-tags05")
151 << QStringLiteral(
"{% load scripteddefaults %}{{ boo|upper }}") << dict
152 << QStringLiteral(
"FAR") << NoError;
154 dict.insert(QStringLiteral(
"boo"), QStringLiteral(
"Far & away"));
156 QTest::newRow(
"scriptable-tags06")
157 << QStringLiteral(
"{% load scripteddefaults %}{{ boo }}") << dict
158 << QStringLiteral(
"Far & away") << NoError;
160 QTest::newRow(
"scriptable-tags07")
161 << QStringLiteral(
"{% load scripteddefaults %}{{ boo|safe2 }}") << dict
162 << QStringLiteral(
"Far & away") << NoError;
165 QTest::newRow(
"scriptable-tags08")
166 <<
"{% load scripteddefaults %}{{ booList|join2:\" \" }}" << dict
167 << QStringLiteral(
"Tom Dick Harry") << NoError;
170 QTest::newRow(
"scriptable-tags09")
171 <<
"{% load scripteddefaults %}{% ifequal2 boo \"Far & away\" %}yes{% "
173 << dict << QStringLiteral(
"yes") << NoError;
176 QTest::newRow(
"scriptable-tags10")
177 <<
"{% load scripteddefaults %}{{ booList|join2:\" & \" }}" << dict
178 << QStringLiteral(
"Tom & Dick & Harry") << NoError;
181 dict.insert(QStringLiteral(
"amp"), QStringLiteral(
" & "));
182 QTest::newRow(
"scriptable-tags11")
183 << QStringLiteral(
"{% load scripteddefaults %}{{ booList|join2:amp }}")
184 << dict << QStringLiteral(
"Tom & Dick & Harry") << NoError;
186 QTest::newRow(
"scriptable-load-error01")
187 << QStringLiteral(
"{% load %}{{ booList|join2:amp }}") << dict
188 <<
QString() << TagSyntaxError;
193 void TestScriptableTagsSyntax::testResolve_data()
195 QTest::addColumn<QString>(
"input");
196 QTest::addColumn<Dict>(
"dict");
197 QTest::addColumn<QString>(
"output");
198 QTest::addColumn<Cutelee::Error>(
"error");
201 dict.insert(QStringLiteral(
"boo"), QStringLiteral(
"Far"));
202 dict.insert(QStringLiteral(
"zing"), QStringLiteral(
"Bang"));
204 QTest::newRow(
"resolve-01")
205 <<
"{% load scripteddefaults %}{% resolver boo zing %}" << dict
206 << QStringLiteral(
"Far - Bang") << NoError;
210 #include "testscriptabletags.moc"
The Context class holds the context to render a Template with.
Cutelee::Engine is the main entry point for creating Cutelee Templates.
The Cutelee namespace holds all public Cutelee API.