File: testscriptabletags.cpp

package info (click to toggle)
kf6-ktexttemplate 6.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,272 kB
  • sloc: cpp: 19,464; javascript: 6,043; python: 297; ruby: 24; makefile: 5
file content (186 lines) | stat: -rw-r--r-- 6,255 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
/*
  This file is part of the KTextTemplate library

  SPDX-FileCopyrightText: 2009, 2010 Stephen Kelly <steveire@gmail.com>

  SPDX-License-Identifier: LGPL-2.1-or-later

*/

#ifndef SCRIPTABLETAGSTEST_H
#define SCRIPTABLETAGSTEST_H

#include <QDebug>
#include <QDir>
#include <QFileInfo>
#include <QTest>

#include "context.h"
#include "engine.h"
#include "ktexttemplate_paths.h"
#include "template.h"

using Dict = QHash<QString, QVariant>;

Q_DECLARE_METATYPE(KTextTemplate::Error)

using namespace KTextTemplate;

class TestScriptableTagsSyntax : public QObject
{
    Q_OBJECT

private Q_SLOTS:
    void initTestCase();

    void testBasicSyntax_data();
    void testBasicSyntax()
    {
        doTest();
    }

    void testResolve_data();
    void testResolve()
    {
        doTest();
    }

    void cleanupTestCase();

private:
    void doTest();

    Engine *m_engine = nullptr;
};

void TestScriptableTagsSyntax::initTestCase()
{
    Q_INIT_RESOURCE(testresource);

    m_engine = new Engine(this);
    auto appDirPath = QFileInfo(QCoreApplication::applicationDirPath()).absoluteDir().path();
    m_engine->setPluginPaths({
        QStringLiteral(KTEXTTEMPLATE_PLUGIN_PATH),
        QStringLiteral(":/plugins/") // For scripteddefaults.qs
    });
    m_engine->addDefaultLibrary(QStringLiteral("ktexttemplate_scriptabletags"));
}

void TestScriptableTagsSyntax::cleanupTestCase()
{
    delete m_engine;
}

void TestScriptableTagsSyntax::doTest()
{
    QFETCH(QString, input);
    QFETCH(Dict, dict);
    QFETCH(QString, output);
    QFETCH(KTextTemplate::Error, error);

    auto t = m_engine->newTemplate(input, QLatin1String(QTest::currentDataTag()));

    if (t->error() != NoError) {
        if (t->error() != error)
            qDebug() << t->errorString();
        QCOMPARE(t->error(), error);
        return;
    }

    Context context(dict);

    auto result = t->render(&context);

    if (t->error() != NoError) {
        if (t->error() != error)
            qDebug() << t->errorString();
        QCOMPARE(t->error(), error);
        return;
    }

    QCOMPARE(t->error(), NoError);

    // Didn't catch any errors, so make sure I didn't expect any.
    QCOMPARE(NoError, error);

    QCOMPARE(t->error(), NoError);

    QCOMPARE(result, output);
}

void TestScriptableTagsSyntax::testBasicSyntax_data()
{
    QTest::addColumn<QString>("input");
    QTest::addColumn<Dict>("dict");
    QTest::addColumn<QString>("output");
    QTest::addColumn<KTextTemplate::Error>("error");

    Dict dict;

    dict.insert(QStringLiteral("boo"), QStringLiteral("Far"));
    dict.insert(QStringLiteral("booList"), QVariantList{QStringLiteral("Tom"), QStringLiteral("Dick"), QStringLiteral("Harry")});

    QTest::newRow("scriptable-tags01") << "{% load scripteddefaults %}{% if2 "
                                          "\"something\\\" stupid\" %}{{ boo "
                                          "}}{% endif2 %}"
                                       << dict << QStringLiteral("Far") << NoError;

    // Nest c++ tags inside scripted tags.
    QTest::newRow("scriptable-tags02") << "{% load scripteddefaults %}{% if2 \"something\\\" stupid\" %}{% "
                                          "for "
                                          "name in booList %}:{{ name }};{% endfor %}{% endif2 %}"
                                       << dict << QStringLiteral(":Tom;:Dick;:Harry;") << NoError;

    // Nest c++ tags inside scripted tags.
    QTest::newRow("scriptable-tags03") << QStringLiteral("{% load scripteddefaults %}{% if2 boo %}yes{% else %}no{% endif2 %}") << dict << QStringLiteral("yes")
                                       << NoError;
    QTest::newRow("scriptable-tags04") << QStringLiteral("{% load scripteddefaults %}{% if2 foo %}yes{% else %}no{% endif2 %}") << dict << QStringLiteral("no")
                                       << NoError;

    QTest::newRow("scriptable-tags05") << QStringLiteral("{% load scripteddefaults %}{{ boo|upper }}") << dict << QStringLiteral("FAR") << NoError;

    dict.insert(QStringLiteral("boo"), QStringLiteral("Far & away"));
    // Variables are escaped ...
    QTest::newRow("scriptable-tags06") << QStringLiteral("{% load scripteddefaults %}{{ boo }}") << dict << QStringLiteral("Far &amp; away") << NoError;
    // and scripted filters can mark output as 'safe'.
    QTest::newRow("scriptable-tags07") << QStringLiteral("{% load scripteddefaults %}{{ boo|safe2 }}") << dict << QStringLiteral("Far & away") << NoError;

    // Filters can take arguments
    QTest::newRow("scriptable-tags08") << "{% load scripteddefaults %}{{ booList|join2:\" \" }}" << dict << QStringLiteral("Tom Dick Harry") << NoError;

    // Literals in tags are compared un-escaped.
    QTest::newRow("scriptable-tags09") << "{% load scripteddefaults %}{% ifequal2 boo \"Far & away\" %}yes{% "
                                          "endifequal2 %}"
                                       << dict << QStringLiteral("yes") << NoError;

    // Literals in arguments to filters are not escaped.
    QTest::newRow("scriptable-tags10") << "{% load scripteddefaults %}{{ booList|join2:\" & \" }}" << dict << QStringLiteral("Tom & Dick & Harry") << NoError;

    // Nor are variables.
    dict.insert(QStringLiteral("amp"), QStringLiteral(" & "));
    QTest::newRow("scriptable-tags11") << QStringLiteral("{% load scripteddefaults %}{{ booList|join2:amp }}") << dict << QStringLiteral("Tom & Dick & Harry")
                                       << NoError;

    QTest::newRow("scriptable-load-error01") << QStringLiteral("{% load %}{{ booList|join2:amp }}") << dict << QString() << TagSyntaxError;

    dict.clear();
}

void TestScriptableTagsSyntax::testResolve_data()
{
    QTest::addColumn<QString>("input");
    QTest::addColumn<Dict>("dict");
    QTest::addColumn<QString>("output");
    QTest::addColumn<KTextTemplate::Error>("error");

    Dict dict;
    dict.insert(QStringLiteral("boo"), QStringLiteral("Far"));
    dict.insert(QStringLiteral("zing"), QStringLiteral("Bang"));

    QTest::newRow("resolve-01") << "{% load scripteddefaults %}{% resolver boo zing %}" << dict << QStringLiteral("Far - Bang") << NoError;
}

QTEST_MAIN(TestScriptableTagsSyntax)
#include "testscriptabletags.moc"

#endif