24#include <QtCore/QDebug>
26#include <QtCore/QFileInfo>
27#include <QtTest/QTest>
30#include "coverageobject.h"
32#include "cutelee_paths.h"
49 void cleanupTestCase();
51 void testDateBasedFilters_data();
52 void testDateBasedFilters() { doTest(); }
54 void testStringFilters_data();
55 void testStringFilters() { doTest(); }
57 void testListFilters_data();
58 void testListFilters() { doTest(); }
60 void testLogicFilters_data();
61 void testLogicFilters() { doTest(); }
63 void testMiscFilters_data();
64 void testMiscFilters() { doTest(); }
66 void testIntegerFilters_data();
67 void testIntegerFilters() { doTest(); }
72 std::shared_ptr<InMemoryTemplateLoader> loader;
76void TestFilters::initTestCase()
78 m_engine =
new Engine(
this);
86 QStringLiteral(CUTELEE_PLUGIN_PATH),
87 appDirPath + QStringLiteral(
"/tests/")
91void TestFilters::cleanupTestCase() {
delete m_engine; }
93void TestFilters::doTest()
95 QFETCH(QString, input);
97 QFETCH(QString, output);
100 auto t = m_engine->newTemplate(input, QLatin1String(QTest::currentDataTag()));
102 Context context(dict);
104 auto result = t->render(&context);
106 if (t->error() != NoError) {
107 if (t->error() != error)
108 qDebug() << t->errorString();
109 QCOMPARE(t->error(), error);
114 QCOMPARE(NoError, error);
116 QCOMPARE(t->error(), NoError);
118 QCOMPARE(result, output);
121void TestFilters::testDateBasedFilters_data()
123 QTest::addColumn<QString>(
"input");
124 QTest::addColumn<Dict>(
"dict");
125 QTest::addColumn<QString>(
"output");
126 QTest::addColumn<Cutelee::Error>(
"error");
131 dict.
insert(QStringLiteral(
"a"), now.addSecs(-70));
133 QTest::newRow(
"filter-timesince01")
134 << QStringLiteral(
"{{ a|timesince }}") << dict
135 << QStringLiteral(
"1 minute") << NoError;
139 dict.
insert(QStringLiteral(
"a"), now.addDays(-1).addSecs(-60));
141 QTest::newRow(
"filter-timesince02")
142 << QStringLiteral(
"{{ a|timesince }}") << dict << QStringLiteral(
"1 day")
147 dict.
insert(QStringLiteral(
"a"),
148 now.addSecs(-1 * 60 * 60).addSecs(-1 * 25 * 60).addSecs(-1 * 10));
149 QTest::newRow(
"filter-timesince03")
150 << QStringLiteral(
"{{ a|timesince }}") << dict
151 << QStringLiteral(
"1 hour, 25 minutes") << NoError;
157 dict.
insert(QStringLiteral(
"a"), now.addDays(-2));
158 dict.
insert(QStringLiteral(
"b"), now.addDays(-1));
160 QTest::newRow(
"filter-timesince04")
161 << QStringLiteral(
"{{ a|timesince:b }}") << dict
162 << QStringLiteral(
"1 day") << NoError;
166 dict.
insert(QStringLiteral(
"a"), now.addDays(-2).addSecs(-60));
167 dict.
insert(QStringLiteral(
"b"), now.addDays(-2));
169 QTest::newRow(
"filter-timesince05")
170 << QStringLiteral(
"{{ a|timesince:b }}") << dict
171 << QStringLiteral(
"1 minute") << NoError;
182 dict.
insert(QStringLiteral(
"earlier"), now.addDays(-7));
183 QTest::newRow(
"filter-timesince07")
184 << QStringLiteral(
"{{ earlier|timesince }}") << dict
185 << QStringLiteral(
"1 week") << NoError;
189 dict.
insert(QStringLiteral(
"now"), now);
190 dict.
insert(QStringLiteral(
"earlier"), now.addDays(-7));
192 QTest::newRow(
"filter-timesince08")
193 << QStringLiteral(
"{{ earlier|timesince:now }}") << dict
194 << QStringLiteral(
"1 week") << NoError;
198 dict.
insert(QStringLiteral(
"later"), now.addDays(7));
200 QTest::newRow(
"filter-timesince09")
201 << QStringLiteral(
"{{ later|timesince }}") << dict
202 << QStringLiteral(
"0 minutes") << NoError;
206 dict.
insert(QStringLiteral(
"now"), now);
207 dict.
insert(QStringLiteral(
"later"), now.addDays(7));
209 QTest::newRow(
"filter-timesince10")
210 << QStringLiteral(
"{{ later|timesince:now }}") << dict
211 << QStringLiteral(
"0 minutes") << NoError;
245 dict.
insert(QStringLiteral(
"a"), now);
246 dict.
insert(QStringLiteral(
"b"), now);
248 QTest::newRow(
"filter-timesince17")
249 << QStringLiteral(
"{{ a|timesince:b }}") << dict
250 << QStringLiteral(
"0 minutes") << NoError;
254 dict.
insert(QStringLiteral(
"a"), now);
255 dict.
insert(QStringLiteral(
"b"), now.addDays(1));
257 QTest::newRow(
"filter-timesince18")
258 << QStringLiteral(
"{{ a|timesince:b }}") << dict
259 << QStringLiteral(
"1 day") << NoError;
262 QTest::newRow(
"filter-timesince19") << QStringLiteral(
"{{xx|timesince}}")
263 << dict << QStringLiteral(
"") << NoError;
264 QTest::newRow(
"filter-timesince20") << QStringLiteral(
"{{|timesince}}")
265 << dict << QStringLiteral(
"") << NoError;
270 dict.
insert(QStringLiteral(
"a"), now.addSecs(130));
272 QTest::newRow(
"filter-timeuntil01")
273 << QStringLiteral(
"{{ a|timeuntil }}") << dict
274 << QStringLiteral(
"2 minutes") << NoError;
277 dict.
insert(QStringLiteral(
"a"), now.addDays(1).addSecs(10));
279 QTest::newRow(
"filter-timeuntil02")
280 << QStringLiteral(
"{{ a|timeuntil }}") << dict << QStringLiteral(
"1 day")
284 dict.
insert(QStringLiteral(
"a"), now.addSecs(60 * 60 * 8).addSecs(610));
286 QTest::newRow(
"filter-timeuntil03")
287 << QStringLiteral(
"{{ a|timeuntil }}") << dict
288 << QStringLiteral(
"8 hours, 10 minutes") << NoError;
293 dict.
insert(QStringLiteral(
"a"), now.addDays(-1));
294 dict.
insert(QStringLiteral(
"b"), now.addDays(-2));
296 QTest::newRow(
"filter-timeuntil04")
297 << QStringLiteral(
"{{ a|timeuntil:b }}") << dict
298 << QStringLiteral(
"1 day") << NoError;
301 dict.
insert(QStringLiteral(
"a"), now.addDays(-1));
302 dict.
insert(QStringLiteral(
"b"), now.addDays(-1).addSecs(-60));
304 QTest::newRow(
"filter-timeuntil05")
305 << QStringLiteral(
"{{ a|timeuntil:b }}") << dict
306 << QStringLiteral(
"1 minute") << NoError;
309 dict.
insert(QStringLiteral(
"earlier"), now.addDays(-7));
311 QTest::newRow(
"filter-timeuntil06")
312 << QStringLiteral(
"{{ earlier|timeuntil }}") << dict
313 << QStringLiteral(
"0 minutes") << NoError;
316 dict.
insert(QStringLiteral(
"now"), now);
317 dict.
insert(QStringLiteral(
"earlier"), now.addDays(-7));
319 QTest::newRow(
"filter-timeuntil07")
320 << QStringLiteral(
"{{ earlier|timeuntil:now }}") << dict
321 << QStringLiteral(
"0 minutes") << NoError;
324 dict.
insert(QStringLiteral(
"later"), now.addDays(7).addSecs(5));
326 QTest::newRow(
"filter-timeuntil08")
327 << QStringLiteral(
"{{ later|timeuntil }}") << dict
328 << QStringLiteral(
"1 week") << NoError;
331 dict.
insert(QStringLiteral(
"now"), now);
332 dict.
insert(QStringLiteral(
"later"), now.addDays(7));
334 QTest::newRow(
"filter-timeuntil09")
335 << QStringLiteral(
"{{ later|timeuntil:now }}") << dict
336 << QStringLiteral(
"1 week") << NoError;
351 dict.
insert(QStringLiteral(
"a"), now);
352 dict.
insert(QStringLiteral(
"b"), now);
353 QTest::newRow(
"filter-timeuntil12")
354 << QStringLiteral(
"{{ a|timeuntil:b }}") << dict
355 << QStringLiteral(
"0 minutes") << NoError;
358 dict.
insert(QStringLiteral(
"a"), now);
359 dict.
insert(QStringLiteral(
"b"), now.addDays(-1));
361 QTest::newRow(
"filter-timeuntil13")
362 << QStringLiteral(
"{{ a|timeuntil:b }}") << dict
363 << QStringLiteral(
"1 day") << NoError;
366 QTest::newRow(
"filter-timeuntil14") << QStringLiteral(
"{{xx|timeuntil}}")
367 << dict << QStringLiteral(
"") << NoError;
368 QTest::newRow(
"filter-timeuntil15") << QStringLiteral(
"{{|timeuntil}}")
369 << dict << QStringLiteral(
"") << NoError;
374 dict.
insert(QStringLiteral(
"d"), d);
376 QTest::newRow(
"date01") <<
"{{ d|date:\"MM\" }}" << dict
377 << QStringLiteral(
"01") << NoError;
378 QTest::newRow(
"date02") << QStringLiteral(
"{{ d|date }}") << dict
379 << d.toString(QStringLiteral(
"MMM. d, yyyy"))
383 dict.
insert(QStringLiteral(
"d"), QStringLiteral(
"fail_string"));
384 QTest::newRow(
"date03") <<
"{{ d|date:\"MM\" }}" << dict << QString()
388void TestFilters::testStringFilters_data()
390 QTest::addColumn<QString>(
"input");
391 QTest::addColumn<Dict>(
"dict");
392 QTest::addColumn<QString>(
"output");
393 QTest::addColumn<Cutelee::Error>(
"error");
398 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"<a>\'"));
399 dict.
insert(QStringLiteral(
"b"),
402 QTest::newRow(
"filter-addslash01")
403 << QStringLiteral(
"{% autoescape off %}{{ a|addslashes }} {{ "
404 "b|addslashes }}{% endautoescape %}")
405 << dict <<
"<a>\\\' <a>\\\'" << NoError;
408 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"<a>\'"));
409 dict.
insert(QStringLiteral(
"b"),
412 QTest::newRow(
"filter-addslash02")
413 << QStringLiteral(
"{{ a|addslashes }} {{ b|addslashes }}") << dict
414 <<
"<a>\\' <a>\\\'" << NoError;
417 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"fred>"));
418 dict.
insert(QStringLiteral(
"b"),
421 QTest::newRow(
"filter-capfirst01")
422 << QStringLiteral(
"{% autoescape off %}{{ a|capfirst }} {{ b|capfirst "
423 "}}{% endautoescape %}")
424 << dict << QStringLiteral(
"Fred> Fred>") << NoError;
427 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"fred>"));
428 dict.
insert(QStringLiteral(
"b"),
431 QTest::newRow(
"filter-capfirst02")
432 << QStringLiteral(
"{{ a|capfirst }} {{ b|capfirst }}") << dict
433 << QStringLiteral(
"Fred> Fred>") << NoError;
439 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"a&b"));
440 dict.
insert(QStringLiteral(
"b"),
443 QTest::newRow(
"filter-fix_ampersands01")
444 << QStringLiteral(
"{% autoescape off %}{{ a|fix_ampersands }} {{ "
445 "b|fix_ampersands }}{% endautoescape %}")
446 << dict << QStringLiteral(
"a&b a&b") << NoError;
449 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"a&b"));
450 dict.
insert(QStringLiteral(
"b"),
453 QTest::newRow(
"filter-fix_ampersands02")
454 << QStringLiteral(
"{{ a|fix_ampersands }} {{ b|fix_ampersands }}") << dict
455 << QStringLiteral(
"a&amp;b a&b") << NoError;
458 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"1.42"));
459 dict.
insert(QStringLiteral(
"b"),
462 QTest::newRow(
"filter-floatformat01")
463 << QStringLiteral(
"{% autoescape off %}{{ a|floatformat }} {{ "
464 "b|floatformat }}{% endautoescape %}")
465 << dict << QStringLiteral(
"1.4 1.4") << NoError;
468 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"1.42"));
469 dict.
insert(QStringLiteral(
"b"),
472 QTest::newRow(
"filter-floatformat02")
473 << QStringLiteral(
"{{ a|floatformat }} {{ b|floatformat }}") << dict
474 << QStringLiteral(
"1.4 1.4") << NoError;
477 dict.
insert(QStringLiteral(
"a"),
double(1234.54321));
478 dict.
insert(QStringLiteral(
"b"),
int(1234));
480 QTest::newRow(
"filter-floatformat03")
481 << QStringLiteral(
"{{ a|floatformat }} {{ b|floatformat }}") << dict
482 << QStringLiteral(
"1234.5 1234.0") << NoError;
483 QTest::newRow(
"filter-floatformat04")
484 << QStringLiteral(
"{{ a|floatformat:2 }} {{ b|floatformat:2 }}") << dict
485 << QStringLiteral(
"1234.54 1234.00") << NoError;
486 QTest::newRow(
"filter-floatformat04")
487 << QStringLiteral(
"{{ a|floatformat:0 }} {{ b|floatformat:0 }}") << dict
488 << QStringLiteral(
"1235 1234") << NoError;
494 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"one\n<two>\nthree"));
499 QTest::newRow(
"filter-linenumbers01")
500 << QStringLiteral(
"{{ a|linenumbers }} {{ b|linenumbers }}") << dict
501 <<
"1. one\n2. <two>\n3. three 1. one\n2. <two>\n3. three"
505 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"one\n<two>\nthree"));
509 QTest::newRow(
"filter-linenumbers02")
510 << QStringLiteral(
"{% autoescape off %}{{ a|linenumbers }} {{ "
511 "b|linenumbers }}{% endautoescape %}")
512 << dict <<
"1. one\n2. <two>\n3. three 1. one\n2. <two>\n3. three"
516 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"Apple & banana"));
518 QStringLiteral(
"Apple & banana"))));
520 QTest::newRow(
"filter-lower01") << QStringLiteral(
521 "{% autoescape off %}{{ a|lower }} {{ b|lower }}{% endautoescape %}")
524 "apple & banana apple & banana")
528 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"Apple & banana"));
530 QStringLiteral(
"Apple & banana"))));
532 QTest::newRow(
"filter-lower02")
533 << QStringLiteral(
"{{ a|lower }} {{ b|lower }}") << dict
534 << QStringLiteral(
"apple & banana apple & banana") << NoError;
542 QTest::newRow(
"filter-make_list01") << QStringLiteral(
543 "{% autoescape off %}{{ a|make_list }}{% endautoescape %}")
544 << dict <<
"[u\'&\']" << NoError;
545 QTest::newRow(
"filter-make_list02")
546 << QStringLiteral(
"{{ a|make_list }}") << dict
547 << QStringLiteral(
"[u'&']") << NoError;
549 QTest::newRow(
"filter-make_list03") << QStringLiteral(
550 "{% autoescape off %}{{ a|make_list|stringformat:\"%1\"|safe }}{% "
551 "endautoescape %}") << dict << QStringLiteral(
"[u\'&\']")
553 QTest::newRow(
"filter-make_list04")
554 << QStringLiteral(
"{{ a|make_list|stringformat:\"%1\"|safe }}") << dict
555 << QStringLiteral(
"[u\'&\']") << NoError;
561 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"a & b"));
562 dict.
insert(QStringLiteral(
"b"),
markSafe(QStringLiteral(
"a & b")));
564 QTest::newRow(
"filter-slugify01") << QStringLiteral(
565 "{% autoescape off %}{{ a|slugify }} {{ b|slugify }}{% endautoescape %}")
566 << dict << QStringLiteral(
"a-b a-amp-b")
568 QTest::newRow(
"filter-slugify02")
569 << QStringLiteral(
"{{ a|slugify }} {{ b|slugify }}") << dict
570 << QStringLiteral(
"a-b a-amp-b") << NoError;
573 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"Schöne Grüße"));
575 QTest::newRow(
"filter-slugify03") << QStringLiteral(
"{{ a|slugify }}") << dict
576 << QStringLiteral(
"schone-grue") << NoError;
581 QStringLiteral(
"testing\r\njavascript \'string\" <b>escaping</b>"));
582 QTest::newRow(
"escapejs01")
583 << QStringLiteral(
"{{ a|escapejs }}") << dict
584 <<
"testing\\u000D\\u000Ajavascript \\u0027string\\u0022 "
585 "\\u003Cb\\u003Eescaping\\u003C/b\\u003E"
587 QTest::newRow(
"escapejs02")
589 "{% autoescape off %}{{ a|escapejs }}{% endautoescape %}")
591 <<
"testing\\u000D\\u000Ajavascript \\u0027string\\u0022 "
592 "\\u003Cb\\u003Eescaping\\u003C/b\\u003E"
599 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"a<b"));
600 dict.
insert(QStringLiteral(
"b"),
603 QTest::newRow(
"filter-stringformat01")
604 <<
"{% autoescape off %}.{{ a|stringformat:\"%1\" }}. .{{ "
605 "b|stringformat:\"%2\" }}.{% endautoescape %}"
606 << dict << QStringLiteral(
".a<b. .a<b.") << NoError;
607 QTest::newRow(
"filter-stringformat02")
608 <<
".{{ a|stringformat:\"%1\" }}. .{{ b|stringformat:\"%2\" }}." << dict
609 << QStringLiteral(
".a<b. .a<b.") << NoError;
610 QTest::newRow(
"filter-stringformat03")
611 <<
".{{ a|stringformat:\"foo %1 bar\" }}. .{{ b|stringformat:\"baz %2 "
613 << dict << QStringLiteral(
".foo a<b bar. .baz a<b bat.") << NoError;
616 dict.
insert(QStringLiteral(
"path"), QStringLiteral(
"www.cutelee.org"));
617 QTest::newRow(
"filter-stringformat04")
618 <<
"{% with path|stringformat:\"<a href=\\\"%1\\\">%1</a>\"|safe as "
619 "result %}{{ result }}{% endwith %}"
620 << dict <<
"<a href=\"www.cutelee.org\">www.cutelee.org</a>" << NoError;
623 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"JOE\'S CRAB SHACK"));
624 QTest::newRow(
"filter-title01")
625 <<
"{{ a|title }}" << dict << QStringLiteral(
"Joe's Crab Shack")
629 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"555 WEST 53RD STREET"));
630 QTest::newRow(
"filter-title02")
631 <<
"{{ a|title }}" << dict << QStringLiteral(
"555 West 53rd Street")
635 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"alpha & bravo"));
637 QStringLiteral(
"alpha & bravo"))));
639 QTest::newRow(
"filter-truncatewords01")
640 <<
"{% autoescape off %}{{ a|truncatewords:\"2\" }} {{ "
641 "b|truncatewords:\"2\"}}{% endautoescape %}"
642 << dict << QStringLiteral(
"alpha & ... alpha & ...") << NoError;
644 QTest::newRow(
"filter-truncatewords02")
645 <<
"{{ a|truncatewords:\"2\" }} {{ b|truncatewords:\"2\"}}" << dict
646 << QStringLiteral(
"alpha & ... alpha & ...") << NoError;
652 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"a & b"));
653 dict.
insert(QStringLiteral(
"b"),
656 QTest::newRow(
"filter-upper01") << QStringLiteral(
657 "{% autoescape off %}{{ a|upper }} {{ b|upper }}{% endautoescape %}")
658 << dict << QStringLiteral(
"A & B A & B")
660 QTest::newRow(
"filter-upper02")
661 << QStringLiteral(
"{{ a|upper }} {{ b|upper }}") << dict
662 << QStringLiteral(
"A & B A &AMP; B") << NoError;
762 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"a & b"));
763 dict.
insert(QStringLiteral(
"b"),
766 QTest::newRow(
"filter-wordcount01")
767 << QStringLiteral(
"{% autoescape off %}{{ a|wordcount }} {{ b|wordcount "
768 "}}{% endautoescape %}")
769 << dict << QStringLiteral(
"3 3") << NoError;
771 QTest::newRow(
"filter-wordcount02")
772 << QStringLiteral(
"{{ a|wordcount }} {{ b|wordcount }}") << dict
773 << QStringLiteral(
"3 3") << NoError;
776 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"a & b"));
777 dict.
insert(QStringLiteral(
"b"),
780 QTest::newRow(
"filter-wordwrap01")
781 << QStringLiteral(
"{% autoescape off %}{{ a|wordwrap:3 }} {{ "
782 "b|wordwrap:3 }}{% endautoescape %}")
783 << dict <<
"a &\nb a &\nb" << NoError;
785 QTest::newRow(
"filter-wordwrap02")
786 << QStringLiteral(
"{{ a|wordwrap:3 }} {{ b|wordwrap:3 }}") << dict
787 <<
"a &\nb a &\nb" << NoError;
790 QTest::newRow(
"filter-wordwrap03")
791 << QStringLiteral(
"{{xx|wordwrap}}") << dict <<
"" << NoError;
793 QTest::newRow(
"filter-wordwrap04")
794 << QStringLiteral(
"{{|wordwrap}}") << dict <<
"" << NoError;
797 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"a&b"));
798 dict.
insert(QStringLiteral(
"b"),
801 QTest::newRow(
"filter-ljust01")
802 <<
"{% autoescape off %}.{{ a|ljust:\"5\" }}. .{{ b|ljust:\"5\" }}.{% "
804 << dict << QStringLiteral(
".a&b . .a&b .") << NoError;
806 QTest::newRow(
"filter-ljust02")
807 <<
".{{ a|ljust:\"5\" }}. .{{ b|ljust:\"5\" }}." << dict
808 << QStringLiteral(
".a&b . .a&b .") << NoError;
810 QTest::newRow(
"filter-rjust01")
811 <<
"{% autoescape off %}.{{ a|rjust:\"5\" }}. .{{ b|rjust:\"5\" }}.{% "
813 << dict << QStringLiteral(
". a&b. . a&b.") << NoError;
815 QTest::newRow(
"filter-rjust02")
816 <<
".{{ a|rjust:\"5\" }}. .{{ b|rjust:\"5\" }}." << dict
817 << QStringLiteral(
". a&b. . a&b.") << NoError;
819 QTest::newRow(
"filter-center01")
820 <<
"{% autoescape off %}.{{ a|center:\"5\" }}. .{{ b|center:\"5\" "
823 << dict << QStringLiteral(
". a&b . . a&b .") << NoError;
825 QTest::newRow(
"filter-center02")
826 <<
".{{ a|center:\"5\" }}. .{{ b|center:\"5\" }}." << dict
827 << QStringLiteral(
". a&b . . a&b .") << NoError;
830 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"x&y"));
831 dict.
insert(QStringLiteral(
"b"),
834 QTest::newRow(
"filter-cut01")
835 <<
"{% autoescape off %}{{ a|cut:\"x\" }} {{ "
836 "b|cut:\"x\" }}{% endautoescape %}"
837 << dict << QStringLiteral(
"&y &y") << NoError;
838 QTest::newRow(
"filter-cut02") <<
"{{ a|cut:\"x\" }} {{ b|cut:\"x\" }}" << dict
839 << QStringLiteral(
"&y &y") << NoError;
840 QTest::newRow(
"filter-cut03")
841 <<
"{% autoescape off %}{{ a|cut:\"&\" }} {{ "
842 "b|cut:\"&\" }}{% endautoescape %}"
843 << dict << QStringLiteral(
"xy xamp;y") << NoError;
844 QTest::newRow(
"filter-cut04") <<
"{{ a|cut:\"&\" }} {{ b|cut:\"&\" }}" << dict
845 << QStringLiteral(
"xy xamp;y") << NoError;
850 QTest::newRow(
"filter-cut05")
851 <<
"{% autoescape off %}{{ a|cut:\";\" }} {{ "
852 "b|cut:\";\" }}{% endautoescape %}"
853 << dict << QStringLiteral(
"x&y x&y") << NoError;
854 QTest::newRow(
"filter-cut06")
855 <<
"{{ a|cut:\";\" }} {{ b|cut:\";\" }}" << dict
856 << QStringLiteral(
"x&y x&ampy") << NoError;
862 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"x&y"));
863 dict.
insert(QStringLiteral(
"b"),
866 QTest::newRow(
"filter-escape01")
867 << QStringLiteral(
"{{ a|escape }} {{ b|escape }}") << dict
868 << QStringLiteral(
"x&y x&y") << NoError;
869 QTest::newRow(
"filter-escape02") << QStringLiteral(
870 "{% autoescape off %}{{ a|escape }} {{ b|escape }}{% endautoescape %}")
871 << dict << QStringLiteral(
"x&y x&y")
878 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"x&y"));
880 QTest::newRow(
"filter-escape03")
882 "{% autoescape off %}{{ a|escape|escape }}{% endautoescape %}")
883 << dict << QStringLiteral(
"x&y") << NoError;
884 QTest::newRow(
"filter-escape04")
885 << QStringLiteral(
"{{ a|escape|escape }}") << dict
886 << QStringLiteral(
"x&y") << NoError;
892 QTest::newRow(
"filter-force-escape01")
894 "{% autoescape off %}{{ a|force_escape }}{% endautoescape %}")
895 << dict << QStringLiteral(
"x&y") << NoError;
896 QTest::newRow(
"filter-force-escape02")
897 << QStringLiteral(
"{{ a|force_escape }}") << dict
898 << QStringLiteral(
"x&y") << NoError;
900 QTest::newRow(
"filter-force-escape03")
901 << QStringLiteral(
"{% autoescape off %}{{ a|force_escape|force_escape "
902 "}}{% endautoescape %}")
903 << dict << QStringLiteral(
"x&amp;y") << NoError;
904 QTest::newRow(
"filter-force-escape04")
905 << QStringLiteral(
"{{ a|force_escape|force_escape }}") << dict
906 << QStringLiteral(
"x&amp;y") << NoError;
911 QTest::newRow(
"filter-force-escape05") << QStringLiteral(
912 "{% autoescape off %}{{ a|force_escape|escape }}{% endautoescape %}")
913 << dict << QStringLiteral(
"x&y")
915 QTest::newRow(
"filter-force-escape06")
916 << QStringLiteral(
"{{ a|force_escape|escape }}") << dict
917 << QStringLiteral(
"x&y") << NoError;
918 QTest::newRow(
"filter-force-escape07") << QStringLiteral(
919 "{% autoescape off %}{{ a|escape|force_escape }}{% endautoescape %}")
920 << dict << QStringLiteral(
"x&y")
922 QTest::newRow(
"filter-force-escape08")
923 << QStringLiteral(
"{{ a|escape|force_escape }}") << dict
924 << QStringLiteral(
"x&y") << NoError;
930 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"x&\ny"));
931 dict.
insert(QStringLiteral(
"b"),
markSafe(QStringLiteral(
"x&\ny")));
933 QTest::newRow(
"filter-linebreaks01")
934 << QStringLiteral(
"{{ a|linebreaks }} {{ b|linebreaks }}") << dict
935 << QStringLiteral(
"<p>x&<br />y</p> <p>x&<br />y</p>") << NoError;
936 QTest::newRow(
"filter-linebreaks02")
937 << QStringLiteral(
"{% autoescape off %}{{ a|linebreaks }} {{ "
938 "b|linebreaks }}{% endautoescape %}")
939 << dict << QStringLiteral(
"<p>x&<br />y</p> <p>x&<br />y</p>") << NoError;
940 QTest::newRow(
"filter-linebreaksbr01")
941 << QStringLiteral(
"{{ a|linebreaksbr }} {{ b|linebreaksbr }}") << dict
942 << QStringLiteral(
"x&<br />y x&<br />y") << NoError;
943 QTest::newRow(
"filter-linebreaksbr02")
944 << QStringLiteral(
"{% autoescape off %}{{ a|linebreaksbr }} {{ "
945 "b|linebreaksbr }}{% endautoescape %}")
946 << dict << QStringLiteral(
"x&<br />y x&<br />y") << NoError;
949 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"<b>hello</b>"));
951 QTest::newRow(
"filter-safe01")
952 << QStringLiteral(
"{{ a }} -- {{ a|safe }}") << dict
953 << QStringLiteral(
"<b>hello</b> -- <b>hello</b>") << NoError;
954 QTest::newRow(
"filter-safe02")
956 "{% autoescape off %}{{ a }} -- {{ a|safe }}{% endautoescape %}")
957 << dict << QStringLiteral(
"<b>hello</b> -- <b>hello</b>") << NoError;
960 dict.
insert(QStringLiteral(
"a"),
961 QVariantList{QStringLiteral(
"&"), QStringLiteral(
"<")});
963 QTest::newRow(
"filter-safeseq01")
964 <<
"{{ a|join:\", \" }} -- {{ a|safeseq|join:\", \" }}" << dict
965 << QStringLiteral(
"&, < -- &, <") << NoError;
966 QTest::newRow(
"filter-safeseq02")
967 <<
"{% autoescape off %}{{ a|join:\", \" "
968 "}} -- {{ a|safeseq|join:\", \" "
969 "}}{% endautoescape %}"
970 << dict << QStringLiteral(
"&, < -- &, <") << NoError;
973 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"<a>x</a> <p><b>y</b></p>"));
975 "<a>x</a> <p><b>y</b></p>"))));
977 QTest::newRow(
"filter-removetags01")
978 <<
"{{ a|removetags:\"a b\" }} {{ b|removetags:\"a b\" }}" << dict
979 << QStringLiteral(
"x <p>y</p> x <p>y</p>") << NoError;
980 QTest::newRow(
"filter-removetags02")
981 <<
"{% autoescape off %}{{ a|removetags:\"a b\" }} {{ b|removetags:\"a "
982 "b\" }}{% endautoescape %}"
983 << dict << QStringLiteral(
"x <p>y</p> x <p>y</p>") << NoError;
984 QTest::newRow(
"filter-striptags01")
985 << QStringLiteral(
"{{ a|striptags }} {{ b|striptags }}") << dict
986 << QStringLiteral(
"x y x y") << NoError;
987 QTest::newRow(
"filter-striptags02")
988 << QStringLiteral(
"{% autoescape off %}{{ a|striptags }} {{ b|striptags "
989 "}}{% endautoescape %}")
990 << dict << QStringLiteral(
"x y x y") << NoError;
993 dict.
insert(QStringLiteral(
"fs_int_mib"), 1048576);
995 QTest::newRow(
"filter-filesizeformat01")
996 << QStringLiteral(
"{{ fs_int_mib|filesizeformat }}") << dict
997 << QStringLiteral(
"1.05 MB") << NoError;
999 QTest::newRow(
"filter-filesizeformat02")
1000 << QStringLiteral(
"{{ fs_int_mib|filesizeformat:\"2\" }}") << dict
1001 << QStringLiteral(
"1.00 MiB") << NoError;
1003 QTest::newRow(
"filter-filesizeformat03")
1004 << QStringLiteral(
"{{ fs_int_mib|filesizeformat:\"10,3\" }}") << dict
1005 << QStringLiteral(
"1.049 MB") << NoError;
1007 QTest::newRow(
"filter-filesizeformat04")
1008 << QStringLiteral(
"{{ fs_int_mib|filesizeformat:\"10,2,1024\" }}") << dict
1009 << QStringLiteral(
"1.07 GB") << NoError;
1012 dict.
insert(QStringLiteral(
"fs_float_mib"), 1024.5);
1014 QTest::newRow(
"filter-filesizeformat05")
1015 << QStringLiteral(
"{{ fs_float_mib|filesizeformat:\"10,2,1024\" }}") << dict
1016 << QStringLiteral(
"1.05 MB") << NoError;
1019 dict.
insert(QStringLiteral(
"fs_string_mib"), QStringLiteral(
"1024.5"));
1021 QTest::newRow(
"filter-filesizeformat06")
1022 << QStringLiteral(
"{{ fs_string_mib|filesizeformat:\"10,2,1024\" }}") << dict
1023 << QStringLiteral(
"1.05 MB") << NoError;
1026 dict.
insert(QStringLiteral(
"fs_bytes"), 999);
1027 dict.
insert(QStringLiteral(
"fs_kb"), 1000);
1028 dict.
insert(QStringLiteral(
"fs_10kb"), 10 * 1000);
1029 dict.
insert(QStringLiteral(
"fs_1000kb"), 1000 * 1000 -1);
1030 dict.
insert(QStringLiteral(
"fs_mb"), 1000 * 1000);
1031 dict.
insert(QStringLiteral(
"fs_50mb"), 1000 * 1000 * 50);
1032 dict.
insert(QStringLiteral(
"fs_1000mb"), 1000 * 1000 * 1000 - 1);
1033 dict.
insert(QStringLiteral(
"fs_gb"), 1000 * 1000 * 1000);
1034 dict.
insert(QStringLiteral(
"fs_tb"),
static_cast<double>(1000.0 * 1000.0 * 1000.0 * 1000.0));
1035 dict.
insert(QStringLiteral(
"fs_pb"),
static_cast<double>(1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0));
1036 dict.
insert(QStringLiteral(
"fs_eb"),
static_cast<double>(1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 2000.0));
1037 dict.
insert(QStringLiteral(
"fs_zb"),
static_cast<double>(1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0));
1038 dict.
insert(QStringLiteral(
"fs_yb"),
static_cast<double>(1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0 * 1000.0));
1039 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));
1040 dict.
insert(QStringLiteral(
"fs_0b1"), 0.1);
1041 dict.
insert(QStringLiteral(
"fs_0b2"), QString(QChar(0x03B1)));
1042 dict.
insert(QStringLiteral(
"fs_neg_1"), -100);
1043 dict.
insert(QStringLiteral(
"fs_neg_2"), -1000 * 1000 *50);
1047 fsInput = QStringLiteral(
"{{ fs_bytes|filesizeformat }} {{ fs_kb|filesizeformat }} {{ fs_10kb|filesizeformat }} {{ fs_1000kb|filesizeformat }} ");
1048 fsInput += QStringLiteral(
"{{ fs_mb|filesizeformat }} {{ fs_50mb|filesizeformat }} {{ fs_1000mb|filesizeformat }} {{ fs_gb|filesizeformat }} ");
1049 fsInput += QStringLiteral(
"{{ fs_tb|filesizeformat }} {{ fs_pb|filesizeformat }} {{ fs_eb|filesizeformat }} {{ fs_zb|filesizeformat }} ");
1050 fsInput += QStringLiteral(
"{{ fs_yb|filesizeformat }} {{ fs_2000yb|filesizeformat }} {{ fs_0b1|filesizeformat }} {{ fs_0b2|filesizeformat }} ");
1051 fsInput += QStringLiteral(
"{{ fs_neg_1|filesizeformat }} {{ fs_neg_2|filesizeformat }}");
1054 fsExpect = QStringLiteral(
"999 bytes 1.00 KB 10.00 KB 1000.00 KB ");
1055 fsExpect += QStringLiteral(
"1.00 MB 50.00 MB 1000.00 MB 1.00 GB ");
1056 fsExpect += QStringLiteral(
"1.00 TB 1.00 PB 2.00 EB 1.00 ZB ");
1057 fsExpect += QStringLiteral(
"1.00 YB 2000.00 YB 0 bytes 0 bytes ");
1058 fsExpect += QStringLiteral(
"-100 bytes -50.00 MB");
1060 QTest::newRow(
"filter-filesizeformat07")
1067void TestFilters::testListFilters_data()
1069 QTest::addColumn<QString>(
"input");
1070 QTest::addColumn<Dict>(
"dict");
1071 QTest::addColumn<QString>(
"output");
1072 QTest::addColumn<Cutelee::Error>(
"error");
1076 dict.
insert(QStringLiteral(
"a"),
1077 QVariantList{QStringLiteral(
"a&b"), QStringLiteral(
"x")});
1078 dict.
insert(QStringLiteral(
"b"),
1080 QStringLiteral(
"x")});
1082 QTest::newRow(
"filter-first01")
1083 << QStringLiteral(
"{{ a|first }} {{ b|first }}") << dict
1084 << QStringLiteral(
"a&b a&b") << NoError;
1085 QTest::newRow(
"filter-first02") << QStringLiteral(
1086 "{% autoescape off %}{{ a|first }} {{ b|first }}{% endautoescape %}")
1087 << dict << QStringLiteral(
"a&b a&b")
1091 dict.
insert(QStringLiteral(
"a"),
1092 QVariantList{QStringLiteral(
"x"), QStringLiteral(
"a&b")});
1093 dict.
insert(QStringLiteral(
"b"),
1095 QStringLiteral(
"a&b")))});
1097 QTest::newRow(
"filter-last01")
1098 << QStringLiteral(
"{{ a|last }} {{ b|last }}") << dict
1099 << QStringLiteral(
"a&b a&b") << NoError;
1100 QTest::newRow(
"filter-last02")
1102 "{% autoescape off %}{{ a|last }} {{ b|last }}{% endautoescape %}")
1103 << dict << QStringLiteral(
"a&b a&b") << NoError;
1106 dict.
insert(QStringLiteral(
"a"),
1107 QVariantList{QStringLiteral(
"a&b"), QStringLiteral(
"a&b")});
1108 dict.
insert(QStringLiteral(
"b"),
1112 QTest::newRow(
"filter-random01")
1113 << QStringLiteral(
"{{ a|random }} {{ b|random }}") << dict
1114 << QStringLiteral(
"a&b a&b") << NoError;
1115 QTest::newRow(
"filter-random02") << QStringLiteral(
1116 "{% autoescape off %}{{ a|random }} {{ b|random }}{% endautoescape %}")
1117 << dict << QStringLiteral(
"a&b a&b")
1121 dict.
insert(QStringLiteral(
"empty_list"), QVariantList());
1122 QTest::newRow(
"filter-random03") << QStringLiteral(
"{{empty_list|random}}")
1123 << dict << QStringLiteral(
"") << NoError;
1124 QTest::newRow(
"filter-random04")
1125 << QStringLiteral(
"{{|random}}") << dict << QStringLiteral(
"") << NoError;
1128 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"a&b"));
1129 dict.
insert(QStringLiteral(
"b"),
1132 QTest::newRow(
"filter-slice01")
1133 <<
"{{ a|slice:\"1:3\" }} {{ b|slice:\"1:3\" }}" << dict
1134 << QStringLiteral(
"&b &b") << NoError;
1135 QTest::newRow(
"filter-slice02")
1136 <<
"{% autoescape off %}{{ a|slice:\"1:3\" }} {{ b|slice:\"1:3\" }}{% "
1138 << dict << QStringLiteral(
"&b &b") << NoError;
1141 QTest::newRow(
"filter-slice03")
1142 <<
"{{xx|slice}}" << dict << QStringLiteral(
"") << NoError;
1143 QTest::newRow(
"filter-slice04")
1144 <<
"{{|slice}}" << dict << QStringLiteral(
"") << NoError;
1147 QVariantList sublist{QStringLiteral(
"<y")};
1148 dict.
insert(QStringLiteral(
"a"),
1149 QVariantList{QStringLiteral(
"x>"), QVariant(sublist)});
1151 QTest::newRow(
"filter-unordered_list01")
1152 << QStringLiteral(
"{{ a|unordered_list }}") << dict
1153 <<
"\t<li>x>\n\t<ul>\n\t\t<li><y</li>\n\t</ul>\n\t</li>" << NoError;
1154 QTest::newRow(
"filter-unordered_list02")
1156 "{% autoescape off %}{{ a|unordered_list }}{% endautoescape %}")
1157 << dict <<
"\t<li>x>\n\t<ul>\n\t\t<li><y</li>\n\t</ul>\n\t</li>"
1161 sublist = {
markSafe(QStringLiteral(
"<y"))};
1162 dict.
insert(QStringLiteral(
"a"),
1163 QVariantList{QStringLiteral(
"x>"), QVariant(sublist)});
1165 QTest::newRow(
"filter-unordered_list03")
1166 << QStringLiteral(
"{{ a|unordered_list }}") << dict
1167 <<
"\t<li>x>\n\t<ul>\n\t\t<li><y</li>\n\t</ul>\n\t</li>" << NoError;
1168 QTest::newRow(
"filter-unordered_list04")
1170 "{% autoescape off %}{{ a|unordered_list }}{% endautoescape %}")
1171 << dict <<
"\t<li>x>\n\t<ul>\n\t\t<li><y</li>\n\t</ul>\n\t</li>"
1175 sublist = {QStringLiteral(
"<y")};
1176 dict.
insert(QStringLiteral(
"a"),
1177 QVariantList{QStringLiteral(
"x>"), QVariant(sublist)});
1179 QTest::newRow(
"filter-unordered_list05")
1181 "{% autoescape off %}{{ a|unordered_list }}{% endautoescape %}")
1182 << dict <<
"\t<li>x>\n\t<ul>\n\t\t<li><y</li>\n\t</ul>\n\t</li>"
1188 QStringLiteral(
"list"),
1189 QVariantList{QStringLiteral(
"4"), QVariant(),
true, QVariantHash()});
1191 QTest::newRow(
"length01") << QStringLiteral(
"{{ list|length }}") << dict
1192 << QStringLiteral(
"4") << NoError;
1195 dict.
insert(QStringLiteral(
"list"), QVariantList());
1197 QTest::newRow(
"length02") << QStringLiteral(
"{{ list|length }}") << dict
1198 << QStringLiteral(
"0") << NoError;
1201 dict.
insert(QStringLiteral(
"string"), QStringLiteral(
""));
1203 QTest::newRow(
"length03") << QStringLiteral(
"{{ string|length }}") << dict
1204 << QStringLiteral(
"0") << NoError;
1207 dict.
insert(QStringLiteral(
"string"), QStringLiteral(
"django"));
1209 QTest::newRow(
"length04") << QStringLiteral(
"{{ string|length }}") << dict
1210 << QStringLiteral(
"6") << NoError;
1215 dict.
insert(QStringLiteral(
"int"), 7);
1217 QTest::newRow(
"length05")
1218 << QStringLiteral(
"{{ int|length }}") << dict << QString() << NoError;
1221 dict.
insert(QStringLiteral(
"None"), QVariant());
1223 QTest::newRow(
"length06")
1224 << QStringLiteral(
"{{ None|length }}") << dict << QString() << NoError;
1230 QStringLiteral(
"some_list"),
1231 QVariantList{QStringLiteral(
"4"), QVariant(),
true, QVariantHash()});
1233 QTest::newRow(
"length_is01")
1234 <<
"{% if some_list|length_is:\"4\" %}Four{% endif %}" << dict
1235 << QStringLiteral(
"Four") << NoError;
1239 QStringLiteral(
"some_list"),
1240 QVariantList{QStringLiteral(
"4"), QVariant(),
true, QVariantHash(), 17});
1242 QTest::newRow(
"length_is02")
1243 <<
"{% if some_list|length_is:\"4\" %}Four{% else %}Not Four{% endif %}"
1244 << dict << QStringLiteral(
"Not Four") << NoError;
1247 dict.
insert(QStringLiteral(
"mystring"), QStringLiteral(
"word"));
1249 QTest::newRow(
"length_is03")
1250 <<
"{% if mystring|length_is:\"4\" %}Four{% endif %}" << dict
1251 << QStringLiteral(
"Four") << NoError;
1254 dict.
insert(QStringLiteral(
"mystring"), QStringLiteral(
"Python"));
1256 QTest::newRow(
"length_is04")
1257 <<
"{% if mystring|length_is:\"4\" %}Four{% else %}Not Four{% endif %}"
1258 << dict << QStringLiteral(
"Not Four") << NoError;
1261 dict.
insert(QStringLiteral(
"mystring"), QStringLiteral(
""));
1263 QTest::newRow(
"length_is05")
1264 <<
"{% if mystring|length_is:\"4\" %}Four{% else %}Not Four{% endif %}"
1265 << dict << QStringLiteral(
"Not Four") << NoError;
1268 dict.
insert(QStringLiteral(
"var"), QStringLiteral(
"django"));
1270 QTest::newRow(
"length_is06") << QStringLiteral(
1271 "{% with var|length as my_length %}{{ my_length }}{% endwith %}")
1272 << dict << QStringLiteral(
"6") << NoError;
1277 QTest::newRow(
"length_is07")
1278 <<
"{% if \"X\"|length_is:0 %}Length is 0{% "
1279 "else %}Length not 0{% endif %}"
1280 << dict << QStringLiteral(
"Length not 0") << NoError;
1281 QTest::newRow(
"length_is08")
1282 <<
"{% if \"X\"|length_is:1 %}Length is 1{% "
1283 "else %}Length not 1{% endif %}"
1284 << dict << QStringLiteral(
"Length is 1") << NoError;
1289 dict.
insert(QStringLiteral(
"var"), QStringLiteral(
"django"));
1291 QTest::newRow(
"length_is09")
1292 <<
"{{ var|length_is:\"fish\" }}" << dict << QString() << NoError;
1295 dict.
insert(QStringLiteral(
"int"), 7);
1297 QTest::newRow(
"length_is10")
1298 <<
"{{ int|length_is:\"1\" }}" << dict << QString() << NoError;
1301 dict.
insert(QStringLiteral(
"none"), QVariant());
1303 QTest::newRow(
"length_is11")
1304 <<
"{{ none|length_is:\"1\" }}" << dict << QString() << NoError;
1307 dict.
insert(QStringLiteral(
"a"), QVariantList{QStringLiteral(
"alpha"),
1308 QStringLiteral(
"beta & me")});
1310 QTest::newRow(
"join01") <<
"{{ a|join:\", \" }}" << dict
1311 << QStringLiteral(
"alpha, beta & me") << NoError;
1312 QTest::newRow(
"join02")
1313 <<
"{% autoescape off %}{{ a|join:\", \" }}{% endautoescape %}" << dict
1314 << QStringLiteral(
"alpha, beta & me") << NoError;
1315 QTest::newRow(
"join03") <<
"{{ a|join:\" & \" }}" << dict
1316 << QStringLiteral(
"alpha & beta & me")
1318 QTest::newRow(
"join04")
1319 <<
"{% autoescape off %}{{ a|join:\" & \" }}{% endautoescape %}"
1320 << dict << QStringLiteral(
"alpha & beta & me") << NoError;
1324 dict.
insert(QStringLiteral(
"var"), QStringLiteral(
" & "));
1325 QTest::newRow(
"join05") <<
"{{ a|join:var }}" << dict
1326 << QStringLiteral(
"alpha & beta & me")
1329 QTest::newRow(
"join06") <<
"{{ a|join:var }}" << dict
1330 << QStringLiteral(
"alpha & beta & me") << NoError;
1331 dict.
insert(QStringLiteral(
"a"), QVariantList{QStringLiteral(
"Alpha"),
1332 QStringLiteral(
"Beta & Me")});
1333 dict.
insert(QStringLiteral(
"var"), QStringLiteral(
" & "));
1334 QTest::newRow(
"join07") <<
"{{ a|join:var|lower }}" << dict
1335 << QStringLiteral(
"alpha & beta & me")
1338 dict.
insert(QStringLiteral(
"a"), QVariantList{QStringLiteral(
"Alpha"),
1339 QStringLiteral(
"Beta & Me")});
1341 QTest::newRow(
"join08") <<
"{{ a|join:var|lower }}" << dict
1342 << QStringLiteral(
"alpha & beta & me") << NoError;
1385 QVariantList mapList;
1387 = QStringList{QStringLiteral(
"London"), QStringLiteral(
"Berlin"),
1388 QStringLiteral(
"Paris"), QStringLiteral(
"Dublin")};
1389 Q_FOREACH (
const QString &city, cities) {
1391 map.insert(QStringLiteral(
"city"), city);
1395 dict.
insert(QStringLiteral(
"mapList"), mapList);
1397 QTest::newRow(
"dictsort01")
1398 <<
"{% with mapList|dictsort:'city' as result %}{% for item in result "
1399 "%}{{ item.city }},{% endfor %}{% endwith %}"
1400 << dict <<
"Berlin,Dublin,London,Paris," << NoError;
1405 map.insert(QStringLiteral(
"city"), QStringLiteral(
"Berlin"));
1408 dict.
insert(QStringLiteral(
"mapList"), mapList);
1410 QTest::newRow(
"dictsort02")
1411 <<
"{% with mapList|dictsort:'city' as result %}{% for item in result "
1412 "%}{{ item.city }},{% endfor %}{% endwith %}"
1413 << dict <<
"Berlin,Berlin,Dublin,London,Paris," << NoError;
1417 QVariantList listList;
1419 const auto countries
1420 = QStringList{QStringLiteral(
"England"), QStringLiteral(
"Germany"),
1421 QStringLiteral(
"France"), QStringLiteral(
"Ireland")};
1423 const auto languages
1424 = QStringList{QStringLiteral(
"English"), QStringLiteral(
"German"),
1425 QStringLiteral(
"French"), QStringLiteral(
"Irish")};
1427 for (
auto i = 0; i < cities.size(); ++i) {
1428 listList << QVariant(
1429 QVariantList{cities.at(i), countries.at(i), languages.at(i)});
1432 dict.
insert(QStringLiteral(
"listList"), listList);
1434 QTest::newRow(
"dictsort03")
1435 <<
"{% with listList|dictsort:'0' as result %}{% for item in result "
1437 "item.0 }};{{ item.1 }};{{ item.2 }},{% endfor %}{% endwith %}"
1439 <<
"Berlin;Germany;German,Dublin;Ireland;Irish,London;England;"
1440 "English,Paris;France;French,"
1443 QTest::newRow(
"dictsort04")
1444 <<
"{% with listList|dictsort:'1' as result %}{% for item in result "
1446 "item.0 }};{{ item.1 }};{{ item.2 }},{% endfor %}{% endwith %}"
1448 <<
"London;England;English,Paris;France;French,Berlin;Germany;"
1449 "German,Dublin;Ireland;Irish,"
1453void TestFilters::testLogicFilters_data()
1455 QTest::addColumn<QString>(
"input");
1456 QTest::addColumn<Dict>(
"dict");
1457 QTest::addColumn<QString>(
"output");
1458 QTest::addColumn<Cutelee::Error>(
"error");
1468 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
""));
1470 QTest::newRow(
"filter-default01")
1471 <<
"{{ a|default:\"x<\" }}" << dict << QStringLiteral(
"x<") << NoError;
1472 QTest::newRow(
"filter-default02")
1473 <<
"{% autoescape off %}{{ a|default:\"x<\" }}{% endautoescape %}" << dict
1474 << QStringLiteral(
"x<") << NoError;
1477 dict.
insert(QStringLiteral(
"a"),
1480 QTest::newRow(
"filter-default03")
1481 <<
"{{ a|default:\"x<\" }}" << dict << QStringLiteral(
"x>") << NoError;
1482 QTest::newRow(
"filter-default04")
1483 <<
"{% autoescape off %}{{ a|default:\"x<\" }}{% endautoescape %}" << dict
1484 << QStringLiteral(
"x>") << NoError;
1487 dict.
insert(QStringLiteral(
"a"), QVariant());
1489 QTest::newRow(
"filter-default_if_none01")
1490 <<
"{{ a|default:\"x<\" }}" << dict << QStringLiteral(
"x<") << NoError;
1491 QTest::newRow(
"filter-default_if_none02")
1492 <<
"{% autoescape off %}{{ a|default:\"x<\" }}{% endautoescape %}" << dict
1493 << QStringLiteral(
"x<") << NoError;
1496void TestFilters::testMiscFilters_data()
1498 QTest::addColumn<QString>(
"input");
1499 QTest::addColumn<Dict>(
"dict");
1500 QTest::addColumn<QString>(
"output");
1501 QTest::addColumn<Cutelee::Error>(
"error");
1521 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"a < b"));
1522 dict.
insert(QStringLiteral(
"b"),
1525 QTest::newRow(
"chaining01")
1526 <<
"{{ a|capfirst|center:\"7\" }}.{{ b|capfirst|center:\"7\" }}" << dict
1527 << QStringLiteral(
" A < b . A < b ") << NoError;
1528 QTest::newRow(
"chaining02")
1529 <<
"{% autoescape off %}{{ a|capfirst|center:\"7\" }}.{{ "
1530 "b|capfirst|center:\"7\" }}{% endautoescape %}"
1531 << dict << QStringLiteral(
" A < b . A < b ") << NoError;
1535 QTest::newRow(
"chaining03")
1536 <<
"{{ a|cut:\"b\"|capfirst }}.{{ b|cut:\"b\"|capfirst }}" << dict
1537 << QStringLiteral(
"A < .A < ") << NoError;
1538 QTest::newRow(
"chaining04")
1539 <<
"{% autoescape off %}{{ a|cut:\"b\"|capfirst }}.{{ "
1540 "b|cut:\"b\"|capfirst }}{% endautoescape %}"
1541 << dict << QStringLiteral(
"A < .A < ") << NoError;
1544 dict.
insert(QStringLiteral(
"a"), QStringLiteral(
"a < b"));
1548 QTest::newRow(
"chaining05") << QStringLiteral(
"{{ a|escape|capfirst }}")
1549 << dict << QStringLiteral(
"A < b") << NoError;
1550 QTest::newRow(
"chaining06") << QStringLiteral(
1551 "{% autoescape off %}{{ a|escape|capfirst }}{% endautoescape %}")
1552 << dict << QStringLiteral(
"A < b") << NoError;
1557 QTest::newRow(
"chaining07") <<
"{{ a|force_escape|cut:\";\" }}" << dict
1558 << QStringLiteral(
"a &lt b") << NoError;
1559 QTest::newRow(
"chaining08")
1560 <<
"{% autoescape off %}{{ a|force_escape|cut:\";\" }}{% endautoescape "
1562 << dict << QStringLiteral(
"a < b") << NoError;
1563 QTest::newRow(
"chaining09") <<
"{{ a|cut:\";\"|force_escape }}" << dict
1564 << QStringLiteral(
"a < b") << NoError;
1565 QTest::newRow(
"chaining10")
1566 <<
"{% autoescape off %}{{ a|cut:\";\"|force_escape }}{% endautoescape "
1568 << dict << QStringLiteral(
"a < b") << NoError;
1569 QTest::newRow(
"chaining11")
1570 <<
"{{ a|cut:\"b\"|safe }}" << dict << QStringLiteral(
"a < ") << NoError;
1571 QTest::newRow(
"chaining12")
1572 <<
"{% autoescape off %}{{ a|cut:\"b\"|safe }}{% endautoescape %}" << dict
1573 << QStringLiteral(
"a < ") << NoError;
1574 QTest::newRow(
"chaining13") << QStringLiteral(
"{{ a|safe|force_escape }}")
1575 << dict << QStringLiteral(
"a < b") << NoError;
1576 QTest::newRow(
"chaining14") << QStringLiteral(
1577 "{% autoescape off %}{{ a|safe|force_escape }}{% endautoescape %}")
1578 << dict << QStringLiteral(
"a < b") << NoError;
1605void TestFilters::testIntegerFilters_data()
1607 QTest::addColumn<QString>(
"input");
1608 QTest::addColumn<Dict>(
"dict");
1609 QTest::addColumn<QString>(
"output");
1610 QTest::addColumn<Cutelee::Error>(
"error");
1614 dict.
insert(QStringLiteral(
"i"), 2000);
1616 QTest::newRow(
"add01") << QStringLiteral(
"{{ i|add:5 }}") << dict
1617 << QStringLiteral(
"2005") << NoError;
1618 QTest::newRow(
"add02") << QStringLiteral(
"{{ i|add:\"napis\" }}") << dict
1619 << QStringLiteral(
"2000") << NoError;
1622 dict.
insert(QStringLiteral(
"i"), QStringLiteral(
"not_an_int"));
1624 QTest::newRow(
"add03") << QStringLiteral(
"{{ i|add:16 }}") << dict
1625 << QStringLiteral(
"not_an_int") << NoError;
1626 QTest::newRow(
"add04") << QStringLiteral(
"{{ i|add:\"16\" }}") << dict
1627 << QStringLiteral(
"not_an_int16") << NoError;
1630 dict.
insert(QStringLiteral(
"l1"), QVariantList{1, 2});
1631 dict.
insert(QStringLiteral(
"l2"), QVariantList{3, 4});
1633 QTest::newRow(
"add05") << QStringLiteral(
"{{ l1|add:l2 }}") << dict
1634 << QStringLiteral(
"[1, 2, 3, 4]") << NoError;
1642 QTest::newRow(
"add08") << QStringLiteral(
"{{ 1|add:2 }}") << dict
1643 << QStringLiteral(
"3") << NoError;
1645 QTest::newRow(
"filter-getdigit01") << QStringLiteral(
"{{ 123|get_digit:1 }}")
1646 << dict << QStringLiteral(
"3") << NoError;
1647 QTest::newRow(
"filter-getdigit02") << QStringLiteral(
"{{ 123|get_digit:2 }}")
1648 << dict << QStringLiteral(
"2") << NoError;
1649 QTest::newRow(
"filter-getdigit03") << QStringLiteral(
"{{ 123|get_digit:3 }}")
1650 << dict << QStringLiteral(
"1") << NoError;
1651 QTest::newRow(
"filter-getdigit04")
1652 << QStringLiteral(
"{{ 123|get_digit:4 }}") << dict
1653 << QStringLiteral(
"123") << NoError;
1657#include "testfilters.moc"
Cutelee::Engine is the main entry point for creating Cutelee Templates.
void setPluginPaths(const QStringList &dirs)
void addTemplateLoader(std::shared_ptr< AbstractTemplateLoader > loader)
The InMemoryTemplateLoader loads Templates set dynamically in memory.
The Cutelee namespace holds all public Cutelee API.
Cutelee::SafeString markSafe(const Cutelee::SafeString &input)
QString applicationDirPath()
QDateTime currentDateTimeUtc()
QString path() const const
QDir absoluteDir() const const
iterator insert(const Key &key, const T &value)
QVariant fromValue(const T &value)
Utility functions used throughout Cutelee.