24 #include <QtCore/QDebug>
25 #include <QtCore/QDir>
26 #include <QtCore/QFileInfo>
27 #include <QtCore/QJsonDocument>
28 #include <QtCore/QJsonObject>
29 #include <QtCore/QJsonArray>
30 #include <QtTest/QTest>
33 #include "coverageobject.h"
35 #include "cutelee_paths.h"
40 typedef QHash<QString, QVariant> Dict;
52 void cleanupTestCase();
54 void testDateBasedFilters_data();
55 void testDateBasedFilters() { doTest(); }
57 void testStringFilters_data();
58 void testStringFilters() { doTest(); }
60 void testListFilters_data();
61 void testListFilters() { doTest(); }
63 void testLogicFilters_data();
64 void testLogicFilters() { doTest(); }
66 void testMiscFilters_data();
67 void testMiscFilters() { doTest(); }
69 void testIntegerFilters_data();
70 void testIntegerFilters() { doTest(); }
75 std::shared_ptr<InMemoryTemplateLoader> loader;
79 void TestFilters::initTestCase()
81 m_engine =
new Engine(
this);
84 m_engine->addTemplateLoader(loader);
87 = QFileInfo(QCoreApplication::applicationDirPath()).absoluteDir().path();
88 m_engine->setPluginPaths({
89 QStringLiteral(CUTELEE_PLUGIN_PATH),
90 appDirPath + QStringLiteral(
"/tests/")
94 void TestFilters::cleanupTestCase() {
delete m_engine; }
96 void TestFilters::doTest()
103 auto t = m_engine->newTemplate(input, QLatin1String(QTest::currentDataTag()));
107 auto result = t->render(&context);
109 if (t->error() != NoError) {
110 if (t->error() != error)
111 qDebug() << t->errorString();
112 QCOMPARE(t->error(), error);
117 QCOMPARE(NoError, error);
119 QCOMPARE(t->error(), NoError);
121 QCOMPARE(result, output);
124 void TestFilters::testDateBasedFilters_data()
126 QTest::addColumn<QString>(
"input");
127 QTest::addColumn<Dict>(
"dict");
128 QTest::addColumn<QString>(
"output");
129 QTest::addColumn<Cutelee::Error>(
"error");
132 auto now = QDateTime::currentDateTimeUtc();
134 dict.insert(QStringLiteral(
"a"), now.addSecs(-70));
136 QTest::newRow(
"filter-timesince01")
137 << QStringLiteral(
"{{ a|timesince }}") << dict
138 << QStringLiteral(
"1 minute") << NoError;
142 dict.insert(QStringLiteral(
"a"), now.addDays(-1).addSecs(-60));
144 QTest::newRow(
"filter-timesince02")
145 << QStringLiteral(
"{{ a|timesince }}") << dict << QStringLiteral(
"1 day")
150 dict.insert(QStringLiteral(
"a"),
151 now.addSecs(-1 * 60 * 60).addSecs(-1 * 25 * 60).addSecs(-1 * 10));
152 QTest::newRow(
"filter-timesince03")
153 << QStringLiteral(
"{{ a|timesince }}") << dict
154 << QStringLiteral(
"1 hour, 25 minutes") << NoError;
160 dict.insert(QStringLiteral(
"a"), now.addDays(-2));
161 dict.insert(QStringLiteral(
"b"), now.addDays(-1));
163 QTest::newRow(
"filter-timesince04")
164 << QStringLiteral(
"{{ a|timesince:b }}") << dict
165 << QStringLiteral(
"1 day") << NoError;
169 dict.insert(QStringLiteral(
"a"), now.addDays(-2).addSecs(-60));
170 dict.insert(QStringLiteral(
"b"), now.addDays(-2));
172 QTest::newRow(
"filter-timesince05")
173 << QStringLiteral(
"{{ a|timesince:b }}") << dict
174 << QStringLiteral(
"1 minute") << NoError;
185 dict.insert(QStringLiteral(
"earlier"), now.addDays(-7));
186 QTest::newRow(
"filter-timesince07")
187 << QStringLiteral(
"{{ earlier|timesince }}") << dict
188 << QStringLiteral(
"1 week") << NoError;
192 dict.insert(QStringLiteral(
"now"), now);
193 dict.insert(QStringLiteral(
"earlier"), now.addDays(-7));
195 QTest::newRow(
"filter-timesince08")
196 << QStringLiteral(
"{{ earlier|timesince:now }}") << dict
197 << QStringLiteral(
"1 week") << NoError;
201 dict.insert(QStringLiteral(
"later"), now.addDays(7));
203 QTest::newRow(
"filter-timesince09")
204 << QStringLiteral(
"{{ later|timesince }}") << dict
205 << QStringLiteral(
"0 minutes") << NoError;
209 dict.insert(QStringLiteral(
"now"), now);
210 dict.insert(QStringLiteral(
"later"), now.addDays(7));
212 QTest::newRow(
"filter-timesince10")
213 << QStringLiteral(
"{{ later|timesince:now }}") << dict
214 << QStringLiteral(
"0 minutes") << NoError;
248 dict.insert(QStringLiteral(
"a"), now);
249 dict.insert(QStringLiteral(
"b"), now);
251 QTest::newRow(
"filter-timesince17")
252 << QStringLiteral(
"{{ a|timesince:b }}") << dict
253 << QStringLiteral(
"0 minutes") << NoError;
257 dict.insert(QStringLiteral(
"a"), now);
258 dict.insert(QStringLiteral(
"b"), now.addDays(1));
260 QTest::newRow(
"filter-timesince18")
261 << QStringLiteral(
"{{ a|timesince:b }}") << dict
262 << QStringLiteral(
"1 day") << NoError;
265 QTest::newRow(
"filter-timesince19") << QStringLiteral(
"{{xx|timesince}}")
266 << dict << QStringLiteral(
"") << NoError;
267 QTest::newRow(
"filter-timesince20") << QStringLiteral(
"{{|timesince}}")
268 << dict << QStringLiteral(
"") << NoError;
273 dict.insert(QStringLiteral(
"a"), now.addSecs(130));
275 QTest::newRow(
"filter-timeuntil01")
276 << QStringLiteral(
"{{ a|timeuntil }}") << dict
277 << QStringLiteral(
"2 minutes") << NoError;
280 dict.insert(QStringLiteral(
"a"), now.addDays(1).addSecs(10));
282 QTest::newRow(
"filter-timeuntil02")
283 << QStringLiteral(
"{{ a|timeuntil }}") << dict << QStringLiteral(
"1 day")
287 dict.insert(QStringLiteral(
"a"), now.addSecs(60 * 60 * 8).addSecs(610));
289 QTest::newRow(
"filter-timeuntil03")
290 << QStringLiteral(
"{{ a|timeuntil }}") << dict
291 << QStringLiteral(
"8 hours, 10 minutes") << NoError;
296 dict.insert(QStringLiteral(
"a"), now.addDays(-1));
297 dict.insert(QStringLiteral(
"b"), now.addDays(-2));
299 QTest::newRow(
"filter-timeuntil04")
300 << QStringLiteral(
"{{ a|timeuntil:b }}") << dict
301 << QStringLiteral(
"1 day") << NoError;
304 dict.insert(QStringLiteral(
"a"), now.addDays(-1));
305 dict.insert(QStringLiteral(
"b"), now.addDays(-1).addSecs(-60));
307 QTest::newRow(
"filter-timeuntil05")
308 << QStringLiteral(
"{{ a|timeuntil:b }}") << dict
309 << QStringLiteral(
"1 minute") << NoError;
312 dict.insert(QStringLiteral(
"earlier"), now.addDays(-7));
314 QTest::newRow(
"filter-timeuntil06")
315 << QStringLiteral(
"{{ earlier|timeuntil }}") << dict
316 << QStringLiteral(
"0 minutes") << NoError;
319 dict.insert(QStringLiteral(
"now"), now);
320 dict.insert(QStringLiteral(
"earlier"), now.addDays(-7));
322 QTest::newRow(
"filter-timeuntil07")
323 << QStringLiteral(
"{{ earlier|timeuntil:now }}") << dict
324 << QStringLiteral(
"0 minutes") << NoError;
327 dict.insert(QStringLiteral(
"later"), now.addDays(7).addSecs(5));
329 QTest::newRow(
"filter-timeuntil08")
330 << QStringLiteral(
"{{ later|timeuntil }}") << dict
331 << QStringLiteral(
"1 week") << NoError;
334 dict.insert(QStringLiteral(
"now"), now);
335 dict.insert(QStringLiteral(
"later"), now.addDays(7));
337 QTest::newRow(
"filter-timeuntil09")
338 << QStringLiteral(
"{{ later|timeuntil:now }}") << dict
339 << QStringLiteral(
"1 week") << NoError;
354 dict.insert(QStringLiteral(
"a"), now);
355 dict.insert(QStringLiteral(
"b"), now);
356 QTest::newRow(
"filter-timeuntil12")
357 << QStringLiteral(
"{{ a|timeuntil:b }}") << dict
358 << QStringLiteral(
"0 minutes") << NoError;
361 dict.insert(QStringLiteral(
"a"), now);
362 dict.insert(QStringLiteral(
"b"), now.addDays(-1));
364 QTest::newRow(
"filter-timeuntil13")
365 << QStringLiteral(
"{{ a|timeuntil:b }}") << dict
366 << QStringLiteral(
"1 day") << NoError;
369 QTest::newRow(
"filter-timeuntil14") << QStringLiteral(
"{{xx|timeuntil}}")
370 << dict << QStringLiteral(
"") << NoError;
371 QTest::newRow(
"filter-timeuntil15") << QStringLiteral(
"{{|timeuntil}}")
372 << dict << QStringLiteral(
"") << NoError;
377 dict.insert(QStringLiteral(
"d"), d);
379 QTest::newRow(
"date01") <<
"{{ d|date:\"MM\" }}" << dict
380 << QStringLiteral(
"01") << NoError;
381 QTest::newRow(
"date02") << QStringLiteral(
"{{ d|date }}") << dict
382 << d.toString(QStringLiteral(
"MMM. d, yyyy"))
386 dict.insert(QStringLiteral(
"d"), QStringLiteral(
"fail_string"));
387 QTest::newRow(
"date03") <<
"{{ d|date:\"MM\" }}" << dict <<
QString()
391 void TestFilters::testStringFilters_data()
393 QTest::addColumn<QString>(
"input");
394 QTest::addColumn<Dict>(
"dict");
395 QTest::addColumn<QString>(
"output");
396 QTest::addColumn<Cutelee::Error>(
"error");
401 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"<a>\'"));
402 dict.insert(QStringLiteral(
"b"),
403 QVariant::fromValue(
markSafe(QStringLiteral(
"<a>\'"))));
405 QTest::newRow(
"filter-addslash01")
406 << QStringLiteral(
"{% autoescape off %}{{ a|addslashes }} {{ "
407 "b|addslashes }}{% endautoescape %}")
408 << dict <<
"<a>\\\' <a>\\\'" << NoError;
411 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"<a>\'"));
412 dict.insert(QStringLiteral(
"b"),
413 QVariant::fromValue(
markSafe(QStringLiteral(
"<a>\'"))));
415 QTest::newRow(
"filter-addslash02")
416 << QStringLiteral(
"{{ a|addslashes }} {{ b|addslashes }}") << dict
417 <<
"<a>\\' <a>\\\'" << NoError;
420 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"fred>"));
421 dict.insert(QStringLiteral(
"b"),
422 QVariant::fromValue(
markSafe(QStringLiteral(
"fred>"))));
424 QTest::newRow(
"filter-capfirst01")
425 << QStringLiteral(
"{% autoescape off %}{{ a|capfirst }} {{ b|capfirst "
426 "}}{% endautoescape %}")
427 << dict << QStringLiteral(
"Fred> Fred>") << NoError;
430 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"fred>"));
431 dict.insert(QStringLiteral(
"b"),
432 QVariant::fromValue(
markSafe(QStringLiteral(
"fred>"))));
434 QTest::newRow(
"filter-capfirst02")
435 << QStringLiteral(
"{{ a|capfirst }} {{ b|capfirst }}") << dict
436 << QStringLiteral(
"Fred> Fred>") << NoError;
442 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"a&b"));
443 dict.insert(QStringLiteral(
"b"),
444 QVariant::fromValue(
markSafe(QStringLiteral(
"a&b"))));
446 QTest::newRow(
"filter-fix_ampersands01")
447 << QStringLiteral(
"{% autoescape off %}{{ a|fix_ampersands }} {{ "
448 "b|fix_ampersands }}{% endautoescape %}")
449 << dict << QStringLiteral(
"a&b a&b") << NoError;
452 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"a&b"));
453 dict.insert(QStringLiteral(
"b"),
454 QVariant::fromValue(
markSafe(QStringLiteral(
"a&b"))));
456 QTest::newRow(
"filter-fix_ampersands02")
457 << QStringLiteral(
"{{ a|fix_ampersands }} {{ b|fix_ampersands }}") << dict
458 << QStringLiteral(
"a&amp;b a&b") << NoError;
461 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"1.42"));
462 dict.insert(QStringLiteral(
"b"),
463 QVariant::fromValue(
markSafe(QStringLiteral(
"1.42"))));
465 QTest::newRow(
"filter-floatformat01")
466 << QStringLiteral(
"{% autoescape off %}{{ a|floatformat }} {{ "
467 "b|floatformat }}{% endautoescape %}")
468 << dict << QStringLiteral(
"1.4 1.4") << NoError;
471 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"1.42"));
472 dict.insert(QStringLiteral(
"b"),
473 QVariant::fromValue(
markSafe(QStringLiteral(
"1.42"))));
475 QTest::newRow(
"filter-floatformat02")
476 << QStringLiteral(
"{{ a|floatformat }} {{ b|floatformat }}") << dict
477 << QStringLiteral(
"1.4 1.4") << NoError;
480 dict.insert(QStringLiteral(
"a"),
double(1234.54321));
481 dict.insert(QStringLiteral(
"b"),
int(1234));
483 QTest::newRow(
"filter-floatformat03")
484 << QStringLiteral(
"{{ a|floatformat }} {{ b|floatformat }}") << dict
485 << QStringLiteral(
"1234.5 1234.0") << NoError;
486 QTest::newRow(
"filter-floatformat04")
487 << QStringLiteral(
"{{ a|floatformat:2 }} {{ b|floatformat:2 }}") << dict
488 << QStringLiteral(
"1234.54 1234.00") << NoError;
489 QTest::newRow(
"filter-floatformat04")
490 << QStringLiteral(
"{{ a|floatformat:0 }} {{ b|floatformat:0 }}") << dict
491 << QStringLiteral(
"1235 1234") << NoError;
497 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"one\n<two>\nthree"));
500 QVariant::fromValue(
markSafe(QStringLiteral(
"one\n<two>\nthree"))));
502 QTest::newRow(
"filter-linenumbers01")
503 << QStringLiteral(
"{{ a|linenumbers }} {{ b|linenumbers }}") << dict
504 <<
"1. one\n2. <two>\n3. three 1. one\n2. <two>\n3. three"
508 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"one\n<two>\nthree"));
511 QVariant::fromValue(
markSafe(QStringLiteral(
"one\n<two>\nthree"))));
512 QTest::newRow(
"filter-linenumbers02")
513 << QStringLiteral(
"{% autoescape off %}{{ a|linenumbers }} {{ "
514 "b|linenumbers }}{% endautoescape %}")
515 << dict <<
"1. one\n2. <two>\n3. three 1. one\n2. <two>\n3. three"
519 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"Apple & banana"));
520 dict.insert(QStringLiteral(
"b"), QVariant::fromValue(
markSafe(
521 QStringLiteral(
"Apple & banana"))));
523 QTest::newRow(
"filter-lower01") << QStringLiteral(
524 "{% autoescape off %}{{ a|lower }} {{ b|lower }}{% endautoescape %}")
527 "apple & banana apple & banana")
531 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"Apple & banana"));
532 dict.insert(QStringLiteral(
"b"), QVariant::fromValue(
markSafe(
533 QStringLiteral(
"Apple & banana"))));
535 QTest::newRow(
"filter-lower02")
536 << QStringLiteral(
"{{ a|lower }} {{ b|lower }}") << dict
537 << QStringLiteral(
"apple & banana apple & banana") << NoError;
543 dict.insert(QStringLiteral(
"a"),
markSafe(QStringLiteral(
"&")));
545 QTest::newRow(
"filter-make_list01") << QStringLiteral(
546 "{% autoescape off %}{{ a|make_list }}{% endautoescape %}")
547 << dict <<
"[u\'&\']" << NoError;
548 QTest::newRow(
"filter-make_list02")
549 << QStringLiteral(
"{{ a|make_list }}") << dict
550 << QStringLiteral(
"[u'&']") << NoError;
552 QTest::newRow(
"filter-make_list03") << QStringLiteral(
553 "{% autoescape off %}{{ a|make_list|stringformat:\"%1\"|safe }}{% "
554 "endautoescape %}") << dict << QStringLiteral(
"[u\'&\']")
556 QTest::newRow(
"filter-make_list04")
557 << QStringLiteral(
"{{ a|make_list|stringformat:\"%1\"|safe }}") << dict
558 << QStringLiteral(
"[u\'&\']") << NoError;
564 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"a & b"));
565 dict.insert(QStringLiteral(
"b"),
markSafe(QStringLiteral(
"a & b")));
567 QTest::newRow(
"filter-slugify01") << QStringLiteral(
568 "{% autoescape off %}{{ a|slugify }} {{ b|slugify }}{% endautoescape %}")
569 << dict << QStringLiteral(
"a-b a-amp-b")
571 QTest::newRow(
"filter-slugify02")
572 << QStringLiteral(
"{{ a|slugify }} {{ b|slugify }}") << dict
573 << QStringLiteral(
"a-b a-amp-b") << NoError;
576 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"Schöne Grüße"));
578 QTest::newRow(
"filter-slugify03") << QStringLiteral(
"{{ a|slugify }}") << dict
579 << QStringLiteral(
"schone-grue") << NoError;
584 QStringLiteral(
"testing\r\njavascript \'string\" <b>escaping</b>"));
585 QTest::newRow(
"escapejs01")
586 << QStringLiteral(
"{{ a|escapejs }}") << dict
587 <<
"testing\\u000D\\u000Ajavascript \\u0027string\\u0022 "
588 "\\u003Cb\\u003Eescaping\\u003C/b\\u003E"
590 QTest::newRow(
"escapejs02")
592 "{% autoescape off %}{{ a|escapejs }}{% endautoescape %}")
594 <<
"testing\\u000D\\u000Ajavascript \\u0027string\\u0022 "
595 "\\u003Cb\\u003Eescaping\\u003C/b\\u003E"
602 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"a<b"));
603 dict.insert(QStringLiteral(
"b"),
604 QVariant::fromValue(
markSafe(QStringLiteral(
"a<b"))));
606 QTest::newRow(
"filter-stringformat01")
607 <<
"{% autoescape off %}.{{ a|stringformat:\"%1\" }}. .{{ "
608 "b|stringformat:\"%2\" }}.{% endautoescape %}"
609 << dict << QStringLiteral(
".a<b. .a<b.") << NoError;
610 QTest::newRow(
"filter-stringformat02")
611 <<
".{{ a|stringformat:\"%1\" }}. .{{ b|stringformat:\"%2\" }}." << dict
612 << QStringLiteral(
".a<b. .a<b.") << NoError;
613 QTest::newRow(
"filter-stringformat03")
614 <<
".{{ a|stringformat:\"foo %1 bar\" }}. .{{ b|stringformat:\"baz %2 "
616 << dict << QStringLiteral(
".foo a<b bar. .baz a<b bat.") << NoError;
619 dict.insert(QStringLiteral(
"path"), QStringLiteral(
"www.cutelee.org"));
620 QTest::newRow(
"filter-stringformat04")
621 <<
"{% with path|stringformat:\"<a href=\\\"%1\\\">%1</a>\"|safe as "
622 "result %}{{ result }}{% endwith %}"
623 << dict <<
"<a href=\"www.cutelee.org\">www.cutelee.org</a>" << NoError;
626 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"JOE\'S CRAB SHACK"));
627 QTest::newRow(
"filter-title01")
628 <<
"{{ a|title }}" << dict << QStringLiteral(
"Joe's Crab Shack")
632 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"555 WEST 53RD STREET"));
633 QTest::newRow(
"filter-title02")
634 <<
"{{ a|title }}" << dict << QStringLiteral(
"555 West 53rd Street")
638 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"alpha & bravo"));
639 dict.insert(QStringLiteral(
"b"), QVariant::fromValue(
markSafe(
640 QStringLiteral(
"alpha & bravo"))));
642 QTest::newRow(
"filter-truncatewords01")
643 <<
"{% autoescape off %}{{ a|truncatewords:\"2\" }} {{ "
644 "b|truncatewords:\"2\"}}{% endautoescape %}"
645 << dict << QStringLiteral(
"alpha & ... alpha & ...") << NoError;
647 QTest::newRow(
"filter-truncatewords02")
648 <<
"{{ a|truncatewords:\"2\" }} {{ b|truncatewords:\"2\"}}" << dict
649 << QStringLiteral(
"alpha & ... alpha & ...") << NoError;
655 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"a & b"));
656 dict.insert(QStringLiteral(
"b"),
657 QVariant::fromValue(
markSafe(QStringLiteral(
"a & b"))));
659 QTest::newRow(
"filter-upper01") << QStringLiteral(
660 "{% autoescape off %}{{ a|upper }} {{ b|upper }}{% endautoescape %}")
661 << dict << QStringLiteral(
"A & B A & B")
663 QTest::newRow(
"filter-upper02")
664 << QStringLiteral(
"{{ a|upper }} {{ b|upper }}") << dict
665 << QStringLiteral(
"A & B A &AMP; B") << NoError;
765 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"a & b"));
766 dict.insert(QStringLiteral(
"b"),
767 QVariant::fromValue(
markSafe(QStringLiteral(
"a & b"))));
769 QTest::newRow(
"filter-wordcount01")
770 << QStringLiteral(
"{% autoescape off %}{{ a|wordcount }} {{ b|wordcount "
771 "}}{% endautoescape %}")
772 << dict << QStringLiteral(
"3 3") << NoError;
774 QTest::newRow(
"filter-wordcount02")
775 << QStringLiteral(
"{{ a|wordcount }} {{ b|wordcount }}") << dict
776 << QStringLiteral(
"3 3") << NoError;
779 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"a & b"));
780 dict.insert(QStringLiteral(
"b"),
781 QVariant::fromValue(
markSafe(QStringLiteral(
"a & b"))));
783 QTest::newRow(
"filter-wordwrap01")
784 << QStringLiteral(
"{% autoescape off %}{{ a|wordwrap:3 }} {{ "
785 "b|wordwrap:3 }}{% endautoescape %}")
786 << dict <<
"a &\nb a &\nb" << NoError;
788 QTest::newRow(
"filter-wordwrap02")
789 << QStringLiteral(
"{{ a|wordwrap:3 }} {{ b|wordwrap:3 }}") << dict
790 <<
"a &\nb a &\nb" << NoError;
793 QTest::newRow(
"filter-wordwrap03")
794 << QStringLiteral(
"{{xx|wordwrap}}") << dict <<
"" << NoError;
796 QTest::newRow(
"filter-wordwrap04")
797 << QStringLiteral(
"{{|wordwrap}}") << dict <<
"" << NoError;
800 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"a&b"));
801 dict.insert(QStringLiteral(
"b"),
802 QVariant::fromValue(
markSafe(QStringLiteral(
"a&b"))));
804 QTest::newRow(
"filter-ljust01")
805 <<
"{% autoescape off %}.{{ a|ljust:\"5\" }}. .{{ b|ljust:\"5\" }}.{% "
807 << dict << QStringLiteral(
".a&b . .a&b .") << NoError;
809 QTest::newRow(
"filter-ljust02")
810 <<
".{{ a|ljust:\"5\" }}. .{{ b|ljust:\"5\" }}." << dict
811 << QStringLiteral(
".a&b . .a&b .") << NoError;
813 QTest::newRow(
"filter-rjust01")
814 <<
"{% autoescape off %}.{{ a|rjust:\"5\" }}. .{{ b|rjust:\"5\" }}.{% "
816 << dict << QStringLiteral(
". a&b. . a&b.") << NoError;
818 QTest::newRow(
"filter-rjust02")
819 <<
".{{ a|rjust:\"5\" }}. .{{ b|rjust:\"5\" }}." << dict
820 << QStringLiteral(
". a&b. . a&b.") << NoError;
822 QTest::newRow(
"filter-center01")
823 <<
"{% autoescape off %}.{{ a|center:\"5\" }}. .{{ b|center:\"5\" "
826 << dict << QStringLiteral(
". a&b . . a&b .") << NoError;
828 QTest::newRow(
"filter-center02")
829 <<
".{{ a|center:\"5\" }}. .{{ b|center:\"5\" }}." << dict
830 << QStringLiteral(
". a&b . . a&b .") << NoError;
833 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"x&y"));
834 dict.insert(QStringLiteral(
"b"),
835 QVariant::fromValue(
markSafe(QStringLiteral(
"x&y"))));
837 QTest::newRow(
"filter-cut01")
838 <<
"{% autoescape off %}{{ a|cut:\"x\" }} {{ "
839 "b|cut:\"x\" }}{% endautoescape %}"
840 << dict << QStringLiteral(
"&y &y") << NoError;
841 QTest::newRow(
"filter-cut02") <<
"{{ a|cut:\"x\" }} {{ b|cut:\"x\" }}" << dict
842 << QStringLiteral(
"&y &y") << NoError;
843 QTest::newRow(
"filter-cut03")
844 <<
"{% autoescape off %}{{ a|cut:\"&\" }} {{ "
845 "b|cut:\"&\" }}{% endautoescape %}"
846 << dict << QStringLiteral(
"xy xamp;y") << NoError;
847 QTest::newRow(
"filter-cut04") <<
"{{ a|cut:\"&\" }} {{ b|cut:\"&\" }}" << dict
848 << QStringLiteral(
"xy xamp;y") << NoError;
853 QTest::newRow(
"filter-cut05")
854 <<
"{% autoescape off %}{{ a|cut:\";\" }} {{ "
855 "b|cut:\";\" }}{% endautoescape %}"
856 << dict << QStringLiteral(
"x&y x&y") << NoError;
857 QTest::newRow(
"filter-cut06")
858 <<
"{{ a|cut:\";\" }} {{ b|cut:\";\" }}" << dict
859 << QStringLiteral(
"x&y x&ampy") << NoError;
865 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"x&y"));
866 dict.insert(QStringLiteral(
"b"),
867 QVariant::fromValue(
markSafe(QStringLiteral(
"x&y"))));
869 QTest::newRow(
"filter-escape01")
870 << QStringLiteral(
"{{ a|escape }} {{ b|escape }}") << dict
871 << QStringLiteral(
"x&y x&y") << NoError;
872 QTest::newRow(
"filter-escape02") << QStringLiteral(
873 "{% autoescape off %}{{ a|escape }} {{ b|escape }}{% endautoescape %}")
874 << dict << QStringLiteral(
"x&y x&y")
881 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"x&y"));
883 QTest::newRow(
"filter-escape03")
885 "{% autoescape off %}{{ a|escape|escape }}{% endautoescape %}")
886 << dict << QStringLiteral(
"x&y") << NoError;
887 QTest::newRow(
"filter-escape04")
888 << QStringLiteral(
"{{ a|escape|escape }}") << dict
889 << QStringLiteral(
"x&y") << NoError;
895 QTest::newRow(
"filter-force-escape01")
897 "{% autoescape off %}{{ a|force_escape }}{% endautoescape %}")
898 << dict << QStringLiteral(
"x&y") << NoError;
899 QTest::newRow(
"filter-force-escape02")
900 << QStringLiteral(
"{{ a|force_escape }}") << dict
901 << QStringLiteral(
"x&y") << NoError;
903 QTest::newRow(
"filter-force-escape03")
904 << QStringLiteral(
"{% autoescape off %}{{ a|force_escape|force_escape "
905 "}}{% endautoescape %}")
906 << dict << QStringLiteral(
"x&amp;y") << NoError;
907 QTest::newRow(
"filter-force-escape04")
908 << QStringLiteral(
"{{ a|force_escape|force_escape }}") << dict
909 << QStringLiteral(
"x&amp;y") << NoError;
914 QTest::newRow(
"filter-force-escape05") << QStringLiteral(
915 "{% autoescape off %}{{ a|force_escape|escape }}{% endautoescape %}")
916 << dict << QStringLiteral(
"x&y")
918 QTest::newRow(
"filter-force-escape06")
919 << QStringLiteral(
"{{ a|force_escape|escape }}") << dict
920 << QStringLiteral(
"x&y") << NoError;
921 QTest::newRow(
"filter-force-escape07") << QStringLiteral(
922 "{% autoescape off %}{{ a|escape|force_escape }}{% endautoescape %}")
923 << dict << QStringLiteral(
"x&y")
925 QTest::newRow(
"filter-force-escape08")
926 << QStringLiteral(
"{{ a|escape|force_escape }}") << dict
927 << QStringLiteral(
"x&y") << NoError;
933 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"x&\ny"));
934 dict.insert(QStringLiteral(
"b"),
markSafe(QStringLiteral(
"x&\ny")));
936 QTest::newRow(
"filter-linebreaks01")
937 << QStringLiteral(
"{{ a|linebreaks }} {{ b|linebreaks }}") << dict
938 << QStringLiteral(
"<p>x&<br />y</p> <p>x&<br />y</p>") << NoError;
939 QTest::newRow(
"filter-linebreaks02")
940 << QStringLiteral(
"{% autoescape off %}{{ a|linebreaks }} {{ "
941 "b|linebreaks }}{% endautoescape %}")
942 << dict << QStringLiteral(
"<p>x&<br />y</p> <p>x&<br />y</p>") << NoError;
943 QTest::newRow(
"filter-linebreaksbr01")
944 << QStringLiteral(
"{{ a|linebreaksbr }} {{ b|linebreaksbr }}") << dict
945 << QStringLiteral(
"x&<br />y x&<br />y") << NoError;
946 QTest::newRow(
"filter-linebreaksbr02")
947 << QStringLiteral(
"{% autoescape off %}{{ a|linebreaksbr }} {{ "
948 "b|linebreaksbr }}{% endautoescape %}")
949 << dict << QStringLiteral(
"x&<br />y x&<br />y") << NoError;
952 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"<b>hello</b>"));
954 QTest::newRow(
"filter-safe01")
955 << QStringLiteral(
"{{ a }} -- {{ a|safe }}") << dict
956 << QStringLiteral(
"<b>hello</b> -- <b>hello</b>") << NoError;
957 QTest::newRow(
"filter-safe02")
959 "{% autoescape off %}{{ a }} -- {{ a|safe }}{% endautoescape %}")
960 << dict << QStringLiteral(
"<b>hello</b> -- <b>hello</b>") << NoError;
963 dict.insert(QStringLiteral(
"a"),
964 QVariantList{QStringLiteral(
"&"), QStringLiteral(
"<")});
966 QTest::newRow(
"filter-safeseq01")
967 <<
"{{ a|join:\", \" }} -- {{ a|safeseq|join:\", \" }}" << dict
968 << QStringLiteral(
"&, < -- &, <") << NoError;
969 QTest::newRow(
"filter-safeseq02")
970 <<
"{% autoescape off %}{{ a|join:\", \" "
971 "}} -- {{ a|safeseq|join:\", \" "
972 "}}{% endautoescape %}"
973 << dict << QStringLiteral(
"&, < -- &, <") << NoError;
976 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"<a>x</a> <p><b>y</b></p>"));
977 dict.insert(QStringLiteral(
"b"), QVariant::fromValue(
markSafe(QStringLiteral(
978 "<a>x</a> <p><b>y</b></p>"))));
980 QTest::newRow(
"filter-removetags01")
981 <<
"{{ a|removetags:\"a b\" }} {{ b|removetags:\"a b\" }}" << dict
982 << QStringLiteral(
"x <p>y</p> x <p>y</p>") << NoError;
983 QTest::newRow(
"filter-removetags02")
984 <<
"{% autoescape off %}{{ a|removetags:\"a b\" }} {{ b|removetags:\"a "
985 "b\" }}{% endautoescape %}"
986 << dict << QStringLiteral(
"x <p>y</p> x <p>y</p>") << NoError;
987 QTest::newRow(
"filter-striptags01")
988 << QStringLiteral(
"{{ a|striptags }} {{ b|striptags }}") << dict
989 << QStringLiteral(
"x y x y") << NoError;
990 QTest::newRow(
"filter-striptags02")
991 << QStringLiteral(
"{% autoescape off %}{{ a|striptags }} {{ b|striptags "
992 "}}{% endautoescape %}")
993 << dict << QStringLiteral(
"x y x y") << NoError;
996 dict.insert(QStringLiteral(
"fs_int_mib"), 1048576);
998 QTest::newRow(
"filter-filesizeformat01")
999 << QStringLiteral(
"{{ fs_int_mib|filesizeformat }}") << dict
1000 << QStringLiteral(
"1.05 MB") << NoError;
1002 QTest::newRow(
"filter-filesizeformat02")
1003 << QStringLiteral(
"{{ fs_int_mib|filesizeformat:\"2\" }}") << dict
1004 << QStringLiteral(
"1.00 MiB") << NoError;
1006 QTest::newRow(
"filter-filesizeformat03")
1007 << QStringLiteral(
"{{ fs_int_mib|filesizeformat:\"10,3\" }}") << dict
1008 << QStringLiteral(
"1.049 MB") << NoError;
1010 QTest::newRow(
"filter-filesizeformat04")
1011 << QStringLiteral(
"{{ fs_int_mib|filesizeformat:\"10,2,1024\" }}") << dict
1012 << QStringLiteral(
"1.07 GB") << NoError;
1015 dict.insert(QStringLiteral(
"fs_float_mib"), 1024.5);
1017 QTest::newRow(
"filter-filesizeformat05")
1018 << QStringLiteral(
"{{ fs_float_mib|filesizeformat:\"10,2,1024\" }}") << dict
1019 << QStringLiteral(
"1.05 MB") << NoError;
1022 dict.insert(QStringLiteral(
"fs_string_mib"), QStringLiteral(
"1024.5"));
1024 QTest::newRow(
"filter-filesizeformat06")
1025 << QStringLiteral(
"{{ fs_string_mib|filesizeformat:\"10,2,1024\" }}") << dict
1026 << QStringLiteral(
"1.05 MB") << NoError;
1029 dict.insert(QStringLiteral(
"fs_bytes"), 999);
1030 dict.insert(QStringLiteral(
"fs_kb"), 1000);
1031 dict.insert(QStringLiteral(
"fs_10kb"), 10 * 1000);
1032 dict.insert(QStringLiteral(
"fs_1000kb"), 1000 * 1000 -1);
1033 dict.insert(QStringLiteral(
"fs_mb"), 1000 * 1000);
1034 dict.insert(QStringLiteral(
"fs_50mb"), 1000 * 1000 * 50);
1035 dict.insert(QStringLiteral(
"fs_1000mb"), 1000 * 1000 * 1000 - 1);
1036 dict.insert(QStringLiteral(
"fs_gb"), 1000 * 1000 * 1000);
1037 dict.insert(QStringLiteral(
"fs_tb"),
static_cast<double>(1000.0 * 1000.0 * 1000.0 * 1000.0));
1038 dict.insert(QStringLiteral(
"fs_pb"),
static_cast<double>(1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0));
1039 dict.insert(QStringLiteral(
"fs_eb"),
static_cast<double>(1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 2000.0));
1040 dict.insert(QStringLiteral(
"fs_zb"),
static_cast<double>(1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0));
1041 dict.insert(QStringLiteral(
"fs_yb"),
static_cast<double>(1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0));
1042 dict.insert(QStringLiteral(
"fs_2000yb"),
static_cast<double>(1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 2000.0));
1043 dict.insert(QStringLiteral(
"fs_0b1"), 0.1);
1044 dict.insert(QStringLiteral(
"fs_0b2"),
QString(QChar(0x03B1)));
1045 dict.insert(QStringLiteral(
"fs_neg_1"), -100);
1046 dict.insert(QStringLiteral(
"fs_neg_2"), -1000 * 1000 *50);
1050 fsInput = QStringLiteral(
"{{ fs_bytes|filesizeformat }} {{ fs_kb|filesizeformat }} {{ fs_10kb|filesizeformat }} {{ fs_1000kb|filesizeformat }} ");
1051 fsInput += QStringLiteral(
"{{ fs_mb|filesizeformat }} {{ fs_50mb|filesizeformat }} {{ fs_1000mb|filesizeformat }} {{ fs_gb|filesizeformat }} ");
1052 fsInput += QStringLiteral(
"{{ fs_tb|filesizeformat }} {{ fs_pb|filesizeformat }} {{ fs_eb|filesizeformat }} {{ fs_zb|filesizeformat }} ");
1053 fsInput += QStringLiteral(
"{{ fs_yb|filesizeformat }} {{ fs_2000yb|filesizeformat }} {{ fs_0b1|filesizeformat }} {{ fs_0b2|filesizeformat }} ");
1054 fsInput += QStringLiteral(
"{{ fs_neg_1|filesizeformat }} {{ fs_neg_2|filesizeformat }}");
1057 fsExpect = QStringLiteral(
"999 bytes 1.00 KB 10.00 KB 1000.00 KB ");
1058 fsExpect += QStringLiteral(
"1.00 MB 50.00 MB 1000.00 MB 1.00 GB ");
1059 fsExpect += QStringLiteral(
"1.00 TB 1.00 PB 2.00 EB 1.00 ZB ");
1060 fsExpect += QStringLiteral(
"1.00 YB 2000.00 YB 0 bytes 0 bytes ");
1061 fsExpect += QStringLiteral(
"-100 bytes -50.00 MB");
1063 QTest::newRow(
"filter-filesizeformat07")
1069 const QString jsonSK = QStringLiteral(
"jsondata");
1070 const QString jsonObjectExpect = QStringLiteral(R
"(<script id="hello-data" type="application/json">{"hello":"world"}</script>)");
1071 const QString jsonArrayExpect = QStringLiteral(R
"(<script id="hello-data" type="application/json">["hello","world"]</script>)");
1073 const QString jsonInput = QStringLiteral(R
"({{ jsondata|json_script:"hello-data" }})");
1075 const QJsonObject jsonObject{{QStringLiteral(
"hello"),QStringLiteral(
"world")}};
1077 dict.insert(jsonSK, jsonObject);
1078 QTest::newRow(
"filter-json_script-JsonObj") << jsonInput << dict << jsonObjectExpect << NoError;
1080 const QJsonDocument jsonDoc(jsonObject);
1082 dict.insert(jsonSK, jsonDoc);
1083 QTest::newRow(
"filter-json_script-JsonDoc") << jsonInput << dict << jsonObjectExpect << NoError;
1085 const QJsonArray jsonArray{QStringLiteral(
"hello"), QStringLiteral(
"world")};
1087 dict.insert(jsonSK, jsonArray);
1088 QTest::newRow(
"filter-json_script-JsonArr") << jsonInput << dict << jsonArrayExpect << NoError;
1090 const QVariantHash jsonVarHash{{QStringLiteral(
"hello"),QStringLiteral(
"world")}};
1092 dict.insert(jsonSK, jsonVarHash);
1093 QTest::newRow(
"filter-json_script-VarHash") << jsonInput << dict << jsonObjectExpect << NoError;
1095 const QVariantMap jsonVarMap{{QStringLiteral(
"hello"),QStringLiteral(
"world")}};
1097 dict.insert(jsonSK, jsonVarMap);
1098 QTest::newRow(
"filter-json_script-VarMap") << jsonInput << dict << jsonObjectExpect << NoError;
1100 const QVariantList jsonVarList{QStringLiteral(
"hello"), QStringLiteral(
"world")};
1102 dict.insert(jsonSK, jsonVarList);
1103 QTest::newRow(
"filter-json_script-VarList") << jsonInput << dict << jsonArrayExpect << NoError;
1105 const QStringList jsonStringList{QStringLiteral(
"hello"), QStringLiteral(
"world")};
1107 dict.insert(jsonSK, jsonStringList);
1108 QTest::newRow(
"filter-json_script-StringList") << jsonInput << dict << jsonArrayExpect << NoError;
1110 const QJsonObject jsonEscapeInput{{QStringLiteral(
"hello"),QStringLiteral(
"world</script>&")}};
1111 const QString jsonEscapeExpect = QStringLiteral(R
"(<script id="hello-data" type="application/json">{"hello":"world\\u003C/script\\u003E\\u0026amp;"}</script>)");
1113 dict.insert(jsonSK, jsonEscapeInput);
1114 QTest::newRow("filter-json_script-escape") << jsonInput << dict << jsonEscapeExpect << NoError;
1117 void TestFilters::testListFilters_data()
1119 QTest::addColumn<QString>(
"input");
1120 QTest::addColumn<Dict>(
"dict");
1121 QTest::addColumn<QString>(
"output");
1122 QTest::addColumn<Cutelee::Error>(
"error");
1126 dict.insert(QStringLiteral(
"a"),
1127 QVariantList{QStringLiteral(
"a&b"), QStringLiteral(
"x")});
1128 dict.insert(QStringLiteral(
"b"),
1129 QVariantList{QVariant::fromValue(
markSafe(QStringLiteral(
"a&b"))),
1130 QStringLiteral(
"x")});
1132 QTest::newRow(
"filter-first01")
1133 << QStringLiteral(
"{{ a|first }} {{ b|first }}") << dict
1134 << QStringLiteral(
"a&b a&b") << NoError;
1135 QTest::newRow(
"filter-first02") << QStringLiteral(
1136 "{% autoescape off %}{{ a|first }} {{ b|first }}{% endautoescape %}")
1137 << dict << QStringLiteral(
"a&b a&b")
1141 dict.insert(QStringLiteral(
"a"),
1142 QVariantList{QStringLiteral(
"x"), QStringLiteral(
"a&b")});
1143 dict.insert(QStringLiteral(
"b"),
1144 QVariantList{QStringLiteral(
"x"), QVariant::fromValue(
markSafe(
1145 QStringLiteral(
"a&b")))});
1147 QTest::newRow(
"filter-last01")
1148 << QStringLiteral(
"{{ a|last }} {{ b|last }}") << dict
1149 << QStringLiteral(
"a&b a&b") << NoError;
1150 QTest::newRow(
"filter-last02")
1152 "{% autoescape off %}{{ a|last }} {{ b|last }}{% endautoescape %}")
1153 << dict << QStringLiteral(
"a&b a&b") << NoError;
1156 dict.insert(QStringLiteral(
"a"),
1157 QVariantList{QStringLiteral(
"a&b"), QStringLiteral(
"a&b")});
1158 dict.insert(QStringLiteral(
"b"),
1160 << QVariant::fromValue(
markSafe(QStringLiteral(
"a&b")))
1161 << QVariant::fromValue(
markSafe(QStringLiteral(
"a&b"))));
1162 QTest::newRow(
"filter-random01")
1163 << QStringLiteral(
"{{ a|random }} {{ b|random }}") << dict
1164 << QStringLiteral(
"a&b a&b") << NoError;
1165 QTest::newRow(
"filter-random02") << QStringLiteral(
1166 "{% autoescape off %}{{ a|random }} {{ b|random }}{% endautoescape %}")
1167 << dict << QStringLiteral(
"a&b a&b")
1171 dict.insert(QStringLiteral(
"empty_list"), QVariantList());
1172 QTest::newRow(
"filter-random03") << QStringLiteral(
"{{empty_list|random}}")
1173 << dict << QStringLiteral(
"") << NoError;
1174 QTest::newRow(
"filter-random04")
1175 << QStringLiteral(
"{{|random}}") << dict << QStringLiteral(
"") << NoError;
1178 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"a&b"));
1179 dict.insert(QStringLiteral(
"b"),
1180 QVariant::fromValue(
markSafe(QStringLiteral(
"a&b"))));
1182 QTest::newRow(
"filter-slice01")
1183 <<
"{{ a|slice:\"1:3\" }} {{ b|slice:\"1:3\" }}" << dict
1184 << QStringLiteral(
"&b &b") << NoError;
1185 QTest::newRow(
"filter-slice02")
1186 <<
"{% autoescape off %}{{ a|slice:\"1:3\" }} {{ b|slice:\"1:3\" }}{% "
1188 << dict << QStringLiteral(
"&b &b") << NoError;
1191 QTest::newRow(
"filter-slice03")
1192 <<
"{{xx|slice}}" << dict << QStringLiteral(
"") << NoError;
1193 QTest::newRow(
"filter-slice04")
1194 <<
"{{|slice}}" << dict << QStringLiteral(
"") << NoError;
1197 QVariantList sublist{QStringLiteral(
"<y")};
1198 dict.insert(QStringLiteral(
"a"),
1199 QVariantList{QStringLiteral(
"x>"),
QVariant(sublist)});
1201 QTest::newRow(
"filter-unordered_list01")
1202 << QStringLiteral(
"{{ a|unordered_list }}") << dict
1203 <<
"\t<li>x>\n\t<ul>\n\t\t<li><y</li>\n\t</ul>\n\t</li>" << NoError;
1204 QTest::newRow(
"filter-unordered_list02")
1206 "{% autoescape off %}{{ a|unordered_list }}{% endautoescape %}")
1207 << dict <<
"\t<li>x>\n\t<ul>\n\t\t<li><y</li>\n\t</ul>\n\t</li>"
1211 sublist = {
markSafe(QStringLiteral(
"<y"))};
1212 dict.insert(QStringLiteral(
"a"),
1213 QVariantList{QStringLiteral(
"x>"),
QVariant(sublist)});
1215 QTest::newRow(
"filter-unordered_list03")
1216 << QStringLiteral(
"{{ a|unordered_list }}") << dict
1217 <<
"\t<li>x>\n\t<ul>\n\t\t<li><y</li>\n\t</ul>\n\t</li>" << NoError;
1218 QTest::newRow(
"filter-unordered_list04")
1220 "{% autoescape off %}{{ a|unordered_list }}{% endautoescape %}")
1221 << dict <<
"\t<li>x>\n\t<ul>\n\t\t<li><y</li>\n\t</ul>\n\t</li>"
1225 sublist = {QStringLiteral(
"<y")};
1226 dict.insert(QStringLiteral(
"a"),
1227 QVariantList{QStringLiteral(
"x>"),
QVariant(sublist)});
1229 QTest::newRow(
"filter-unordered_list05")
1231 "{% autoescape off %}{{ a|unordered_list }}{% endautoescape %}")
1232 << dict <<
"\t<li>x>\n\t<ul>\n\t\t<li><y</li>\n\t</ul>\n\t</li>"
1238 QStringLiteral(
"list"),
1239 QVariantList{QStringLiteral(
"4"),
QVariant(),
true, QVariantHash()});
1241 QTest::newRow(
"length01") << QStringLiteral(
"{{ list|length }}") << dict
1242 << QStringLiteral(
"4") << NoError;
1245 dict.insert(QStringLiteral(
"list"), QVariantList());
1247 QTest::newRow(
"length02") << QStringLiteral(
"{{ list|length }}") << dict
1248 << QStringLiteral(
"0") << NoError;
1251 dict.insert(QStringLiteral(
"string"), QStringLiteral(
""));
1253 QTest::newRow(
"length03") << QStringLiteral(
"{{ string|length }}") << dict
1254 << QStringLiteral(
"0") << NoError;
1257 dict.insert(QStringLiteral(
"string"), QStringLiteral(
"django"));
1259 QTest::newRow(
"length04") << QStringLiteral(
"{{ string|length }}") << dict
1260 << QStringLiteral(
"6") << NoError;
1265 dict.insert(QStringLiteral(
"int"), 7);
1267 QTest::newRow(
"length05")
1268 << QStringLiteral(
"{{ int|length }}") << dict <<
QString() << NoError;
1271 dict.insert(QStringLiteral(
"None"),
QVariant());
1273 QTest::newRow(
"length06")
1274 << QStringLiteral(
"{{ None|length }}") << dict <<
QString() << NoError;
1280 QStringLiteral(
"some_list"),
1281 QVariantList{QStringLiteral(
"4"),
QVariant(),
true, QVariantHash()});
1283 QTest::newRow(
"length_is01")
1284 <<
"{% if some_list|length_is:\"4\" %}Four{% endif %}" << dict
1285 << QStringLiteral(
"Four") << NoError;
1289 QStringLiteral(
"some_list"),
1290 QVariantList{QStringLiteral(
"4"),
QVariant(),
true, QVariantHash(), 17});
1292 QTest::newRow(
"length_is02")
1293 <<
"{% if some_list|length_is:\"4\" %}Four{% else %}Not Four{% endif %}"
1294 << dict << QStringLiteral(
"Not Four") << NoError;
1297 dict.insert(QStringLiteral(
"mystring"), QStringLiteral(
"word"));
1299 QTest::newRow(
"length_is03")
1300 <<
"{% if mystring|length_is:\"4\" %}Four{% endif %}" << dict
1301 << QStringLiteral(
"Four") << NoError;
1304 dict.insert(QStringLiteral(
"mystring"), QStringLiteral(
"Python"));
1306 QTest::newRow(
"length_is04")
1307 <<
"{% if mystring|length_is:\"4\" %}Four{% else %}Not Four{% endif %}"
1308 << dict << QStringLiteral(
"Not Four") << NoError;
1311 dict.insert(QStringLiteral(
"mystring"), QStringLiteral(
""));
1313 QTest::newRow(
"length_is05")
1314 <<
"{% if mystring|length_is:\"4\" %}Four{% else %}Not Four{% endif %}"
1315 << dict << QStringLiteral(
"Not Four") << NoError;
1318 dict.insert(QStringLiteral(
"var"), QStringLiteral(
"django"));
1320 QTest::newRow(
"length_is06") << QStringLiteral(
1321 "{% with var|length as my_length %}{{ my_length }}{% endwith %}")
1322 << dict << QStringLiteral(
"6") << NoError;
1327 QTest::newRow(
"length_is07")
1328 <<
"{% if \"X\"|length_is:0 %}Length is 0{% "
1329 "else %}Length not 0{% endif %}"
1330 << dict << QStringLiteral(
"Length not 0") << NoError;
1331 QTest::newRow(
"length_is08")
1332 <<
"{% if \"X\"|length_is:1 %}Length is 1{% "
1333 "else %}Length not 1{% endif %}"
1334 << dict << QStringLiteral(
"Length is 1") << NoError;
1339 dict.insert(QStringLiteral(
"var"), QStringLiteral(
"django"));
1341 QTest::newRow(
"length_is09")
1342 <<
"{{ var|length_is:\"fish\" }}" << dict <<
QString() << NoError;
1345 dict.insert(QStringLiteral(
"int"), 7);
1347 QTest::newRow(
"length_is10")
1348 <<
"{{ int|length_is:\"1\" }}" << dict <<
QString() << NoError;
1351 dict.insert(QStringLiteral(
"none"),
QVariant());
1353 QTest::newRow(
"length_is11")
1354 <<
"{{ none|length_is:\"1\" }}" << dict <<
QString() << NoError;
1357 dict.insert(QStringLiteral(
"a"), QVariantList{QStringLiteral(
"alpha"),
1358 QStringLiteral(
"beta & me")});
1360 QTest::newRow(
"join01") <<
"{{ a|join:\", \" }}" << dict
1361 << QStringLiteral(
"alpha, beta & me") << NoError;
1362 QTest::newRow(
"join02")
1363 <<
"{% autoescape off %}{{ a|join:\", \" }}{% endautoescape %}" << dict
1364 << QStringLiteral(
"alpha, beta & me") << NoError;
1365 QTest::newRow(
"join03") <<
"{{ a|join:\" & \" }}" << dict
1366 << QStringLiteral(
"alpha & beta & me")
1368 QTest::newRow(
"join04")
1369 <<
"{% autoescape off %}{{ a|join:\" & \" }}{% endautoescape %}"
1370 << dict << QStringLiteral(
"alpha & beta & me") << NoError;
1374 dict.insert(QStringLiteral(
"var"), QStringLiteral(
" & "));
1375 QTest::newRow(
"join05") <<
"{{ a|join:var }}" << dict
1376 << QStringLiteral(
"alpha & beta & me")
1379 QTest::newRow(
"join06") <<
"{{ a|join:var }}" << dict
1380 << QStringLiteral(
"alpha & beta & me") << NoError;
1381 dict.insert(QStringLiteral(
"a"), QVariantList{QStringLiteral(
"Alpha"),
1382 QStringLiteral(
"Beta & Me")});
1383 dict.insert(QStringLiteral(
"var"), QStringLiteral(
" & "));
1384 QTest::newRow(
"join07") <<
"{{ a|join:var|lower }}" << dict
1385 << QStringLiteral(
"alpha & beta & me")
1388 dict.insert(QStringLiteral(
"a"), QVariantList{QStringLiteral(
"Alpha"),
1389 QStringLiteral(
"Beta & Me")});
1391 QTest::newRow(
"join08") <<
"{{ a|join:var|lower }}" << dict
1392 << QStringLiteral(
"alpha & beta & me") << NoError;
1435 QVariantList mapList;
1437 = QStringList{QStringLiteral(
"London"), QStringLiteral(
"Berlin"),
1438 QStringLiteral(
"Paris"), QStringLiteral(
"Dublin")};
1439 Q_FOREACH (
const QString &city, cities) {
1441 map.insert(QStringLiteral(
"city"), city);
1445 dict.insert(QStringLiteral(
"mapList"), mapList);
1447 QTest::newRow(
"dictsort01")
1448 <<
"{% with mapList|dictsort:'city' as result %}{% for item in result "
1449 "%}{{ item.city }},{% endfor %}{% endwith %}"
1450 << dict <<
"Berlin,Dublin,London,Paris," << NoError;
1455 map.insert(QStringLiteral(
"city"), QStringLiteral(
"Berlin"));
1458 dict.insert(QStringLiteral(
"mapList"), mapList);
1460 QTest::newRow(
"dictsort02")
1461 <<
"{% with mapList|dictsort:'city' as result %}{% for item in result "
1462 "%}{{ item.city }},{% endfor %}{% endwith %}"
1463 << dict <<
"Berlin,Berlin,Dublin,London,Paris," << NoError;
1467 QVariantList listList;
1469 const auto countries
1470 = QStringList{QStringLiteral(
"England"), QStringLiteral(
"Germany"),
1471 QStringLiteral(
"France"), QStringLiteral(
"Ireland")};
1473 const auto languages
1474 = QStringList{QStringLiteral(
"English"), QStringLiteral(
"German"),
1475 QStringLiteral(
"French"), QStringLiteral(
"Irish")};
1477 for (
auto i = 0; i < cities.size(); ++i) {
1479 QVariantList{cities.at(i), countries.at(i), languages.at(i)});
1482 dict.insert(QStringLiteral(
"listList"), listList);
1484 QTest::newRow(
"dictsort03")
1485 <<
"{% with listList|dictsort:'0' as result %}{% for item in result "
1487 "item.0 }};{{ item.1 }};{{ item.2 }},{% endfor %}{% endwith %}"
1489 <<
"Berlin;Germany;German,Dublin;Ireland;Irish,London;England;"
1490 "English,Paris;France;French,"
1493 QTest::newRow(
"dictsort04")
1494 <<
"{% with listList|dictsort:'1' as result %}{% for item in result "
1496 "item.0 }};{{ item.1 }};{{ item.2 }},{% endfor %}{% endwith %}"
1498 <<
"London;England;English,Paris;France;French,Berlin;Germany;"
1499 "German,Dublin;Ireland;Irish,"
1503 void TestFilters::testLogicFilters_data()
1505 QTest::addColumn<QString>(
"input");
1506 QTest::addColumn<Dict>(
"dict");
1507 QTest::addColumn<QString>(
"output");
1508 QTest::addColumn<Cutelee::Error>(
"error");
1518 dict.insert(QStringLiteral(
"a"), QStringLiteral(
""));
1520 QTest::newRow(
"filter-default01")
1521 <<
"{{ a|default:\"x<\" }}" << dict << QStringLiteral(
"x<") << NoError;
1522 QTest::newRow(
"filter-default02")
1523 <<
"{% autoescape off %}{{ a|default:\"x<\" }}{% endautoescape %}" << dict
1524 << QStringLiteral(
"x<") << NoError;
1527 dict.insert(QStringLiteral(
"a"),
1528 QVariant::fromValue(
markSafe(QStringLiteral(
"x>"))));
1530 QTest::newRow(
"filter-default03")
1531 <<
"{{ a|default:\"x<\" }}" << dict << QStringLiteral(
"x>") << NoError;
1532 QTest::newRow(
"filter-default04")
1533 <<
"{% autoescape off %}{{ a|default:\"x<\" }}{% endautoescape %}" << dict
1534 << QStringLiteral(
"x>") << NoError;
1537 dict.insert(QStringLiteral(
"a"),
QVariant());
1539 QTest::newRow(
"filter-default_if_none01")
1540 <<
"{{ a|default:\"x<\" }}" << dict << QStringLiteral(
"x<") << NoError;
1541 QTest::newRow(
"filter-default_if_none02")
1542 <<
"{% autoescape off %}{{ a|default:\"x<\" }}{% endautoescape %}" << dict
1543 << QStringLiteral(
"x<") << NoError;
1546 void TestFilters::testMiscFilters_data()
1548 QTest::addColumn<QString>(
"input");
1549 QTest::addColumn<Dict>(
"dict");
1550 QTest::addColumn<QString>(
"output");
1551 QTest::addColumn<Cutelee::Error>(
"error");
1571 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"a < b"));
1572 dict.insert(QStringLiteral(
"b"),
1573 QVariant::fromValue(
markSafe(QStringLiteral(
"a < b"))));
1575 QTest::newRow(
"chaining01")
1576 <<
"{{ a|capfirst|center:\"7\" }}.{{ b|capfirst|center:\"7\" }}" << dict
1577 << QStringLiteral(
" A < b . A < b ") << NoError;
1578 QTest::newRow(
"chaining02")
1579 <<
"{% autoescape off %}{{ a|capfirst|center:\"7\" }}.{{ "
1580 "b|capfirst|center:\"7\" }}{% endautoescape %}"
1581 << dict << QStringLiteral(
" A < b . A < b ") << NoError;
1585 QTest::newRow(
"chaining03")
1586 <<
"{{ a|cut:\"b\"|capfirst }}.{{ b|cut:\"b\"|capfirst }}" << dict
1587 << QStringLiteral(
"A < .A < ") << NoError;
1588 QTest::newRow(
"chaining04")
1589 <<
"{% autoescape off %}{{ a|cut:\"b\"|capfirst }}.{{ "
1590 "b|cut:\"b\"|capfirst }}{% endautoescape %}"
1591 << dict << QStringLiteral(
"A < .A < ") << NoError;
1594 dict.insert(QStringLiteral(
"a"), QStringLiteral(
"a < b"));
1598 QTest::newRow(
"chaining05") << QStringLiteral(
"{{ a|escape|capfirst }}")
1599 << dict << QStringLiteral(
"A < b") << NoError;
1600 QTest::newRow(
"chaining06") << QStringLiteral(
1601 "{% autoescape off %}{{ a|escape|capfirst }}{% endautoescape %}")
1602 << dict << QStringLiteral(
"A < b") << NoError;
1607 QTest::newRow(
"chaining07") <<
"{{ a|force_escape|cut:\";\" }}" << dict
1608 << QStringLiteral(
"a &lt b") << NoError;
1609 QTest::newRow(
"chaining08")
1610 <<
"{% autoescape off %}{{ a|force_escape|cut:\";\" }}{% endautoescape "
1612 << dict << QStringLiteral(
"a < b") << NoError;
1613 QTest::newRow(
"chaining09") <<
"{{ a|cut:\";\"|force_escape }}" << dict
1614 << QStringLiteral(
"a < b") << NoError;
1615 QTest::newRow(
"chaining10")
1616 <<
"{% autoescape off %}{{ a|cut:\";\"|force_escape }}{% endautoescape "
1618 << dict << QStringLiteral(
"a < b") << NoError;
1619 QTest::newRow(
"chaining11")
1620 <<
"{{ a|cut:\"b\"|safe }}" << dict << QStringLiteral(
"a < ") << NoError;
1621 QTest::newRow(
"chaining12")
1622 <<
"{% autoescape off %}{{ a|cut:\"b\"|safe }}{% endautoescape %}" << dict
1623 << QStringLiteral(
"a < ") << NoError;
1624 QTest::newRow(
"chaining13") << QStringLiteral(
"{{ a|safe|force_escape }}")
1625 << dict << QStringLiteral(
"a < b") << NoError;
1626 QTest::newRow(
"chaining14") << QStringLiteral(
1627 "{% autoescape off %}{{ a|safe|force_escape }}{% endautoescape %}")
1628 << dict << QStringLiteral(
"a < b") << NoError;
1655 void TestFilters::testIntegerFilters_data()
1657 QTest::addColumn<QString>(
"input");
1658 QTest::addColumn<Dict>(
"dict");
1659 QTest::addColumn<QString>(
"output");
1660 QTest::addColumn<Cutelee::Error>(
"error");
1664 dict.insert(QStringLiteral(
"i"), 2000);
1666 QTest::newRow(
"add01") << QStringLiteral(
"{{ i|add:5 }}") << dict
1667 << QStringLiteral(
"2005") << NoError;
1668 QTest::newRow(
"add02") << QStringLiteral(
"{{ i|add:\"napis\" }}") << dict
1669 << QStringLiteral(
"2000") << NoError;
1672 dict.insert(QStringLiteral(
"i"), QStringLiteral(
"not_an_int"));
1674 QTest::newRow(
"add03") << QStringLiteral(
"{{ i|add:16 }}") << dict
1675 << QStringLiteral(
"not_an_int") << NoError;
1676 QTest::newRow(
"add04") << QStringLiteral(
"{{ i|add:\"16\" }}") << dict
1677 << QStringLiteral(
"not_an_int16") << NoError;
1680 dict.insert(QStringLiteral(
"l1"), QVariantList{1, 2});
1681 dict.insert(QStringLiteral(
"l2"), QVariantList{3, 4});
1683 QTest::newRow(
"add05") << QStringLiteral(
"{{ l1|add:l2 }}") << dict
1684 << QStringLiteral(
"[1, 2, 3, 4]") << NoError;
1692 QTest::newRow(
"add08") << QStringLiteral(
"{{ 1|add:2 }}") << dict
1693 << QStringLiteral(
"3") << NoError;
1695 QTest::newRow(
"filter-getdigit01") << QStringLiteral(
"{{ 123|get_digit:1 }}")
1696 << dict << QStringLiteral(
"3") << NoError;
1697 QTest::newRow(
"filter-getdigit02") << QStringLiteral(
"{{ 123|get_digit:2 }}")
1698 << dict << QStringLiteral(
"2") << NoError;
1699 QTest::newRow(
"filter-getdigit03") << QStringLiteral(
"{{ 123|get_digit:3 }}")
1700 << dict << QStringLiteral(
"1") << NoError;
1701 QTest::newRow(
"filter-getdigit04")
1702 << QStringLiteral(
"{{ 123|get_digit:4 }}") << dict
1703 << QStringLiteral(
"123") << NoError;
1707 #include "testfilters.moc"
The Context class holds the context to render a Template with.
Cutelee::Engine is the main entry point for creating Cutelee Templates.
The InMemoryTemplateLoader loads Templates set dynamically in memory.
The Cutelee namespace holds all public Cutelee API.
Cutelee::SafeString markSafe(const Cutelee::SafeString &input)
Utility functions used throughout Cutelee.