File: surveytargetexpressiontest.cpp

package info (click to toggle)
kuserfeedback 1.3.0-9
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 3,496 kB
  • sloc: cpp: 13,251; php: 2,192; xml: 224; yacc: 90; lex: 82; sh: 17; makefile: 8
file content (154 lines) | stat: -rw-r--r-- 6,626 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
/*
    SPDX-FileCopyrightText: 2017 Volker Krause <vkrause@kde.org>

    SPDX-License-Identifier: MIT
*/

#include <common/surveytargetexpression.h>
#include <common/surveytargetexpressionevaluator.h>
#include <common/surveytargetexpressionparser.h>

#include <QDebug>
#include <QtTest/qtest.h>
#include <QObject>

using namespace KUserFeedback;

class SurveyTargetExpressionTest: public QObject, public SurveyTargetExpressionDataProvider
{
    Q_OBJECT
private:
    QHash<QString, QVariant> m_sourceData;
    QVariant sourceData(const QString &sourceName) const override
    {
        return m_sourceData.value(sourceName);
    }

private slots:
    void testValidParse_data()
    {
        QTest::addColumn<QString>("input");
        QTest::newRow("01") << QStringLiteral("true == 12.3");
        QTest::newRow("02") << QStringLiteral("42 >= false");
        QTest::newRow("03") << QStringLiteral("(42 >= 12.3)");
        QTest::newRow("04") << QStringLiteral("(42 >= 12.3) && (42 < 23)");
        QTest::newRow("05") << QStringLiteral("(42 >= 12.3 && 42 < 23) || true != false");
        QTest::newRow("06") << QStringLiteral("42 >= 12.3E-1 && 42 < 23.1e12 || true != false && -3.2 == .3");
        QTest::newRow("07") << QStringLiteral("\"a\" == \"\\\"b\\\"\"");
        QTest::newRow("08") << QStringLiteral("\"true\" == \"\" || \"&&\" != \"()\"");
        QTest::newRow("09") << QStringLiteral("usageTime.value > 3600");
        QTest::newRow("10") << QStringLiteral("3600 <= usageTime.value");
        QTest::newRow("11") << QStringLiteral("startCount.value != usageTime.value");
        QTest::newRow("12") << QStringLiteral("screens[0].dpi <= 240");
        QTest::newRow("13") << QStringLiteral("views[\"view1\"].ratio > 1");
    }

    void testValidParse()
    {
        QFETCH(QString, input);

        SurveyTargetExpressionParser p;
        QVERIFY(p.parse(input));
        QVERIFY(p.expression());
        qDebug() << p.expression();
    }

    void testInvalidParse_data()
    {
        QTest::addColumn<QString>("input");
        QTest::newRow("01") << QStringLiteral("true && 12.3");
        QTest::newRow("02") << QStringLiteral("42 || false");
        QTest::newRow("03") << QStringLiteral("(42)");
        QTest::newRow("04") << QStringLiteral("true == 12.3 >= 24 < (false)");
        QTest::newRow("05") << QStringLiteral("screen[0] == 4");
        QTest::newRow("06") << QStringLiteral("screen[false] == 4");
        QTest::newRow("07") << QStringLiteral("screen[false].dpi == 4");
        QTest::newRow("08") << QStringLiteral("screen[].dpi == 4");
        QTest::newRow("08") << QStringLiteral("screen[].dpi == 4");
        QTest::newRow("09") << QStringLiteral("screen[\"key\"] == 4");
        QTest::newRow("10") << QStringLiteral("\"\\\"\" == \"\\\"");
    }

    void testInvalidParse()
    {
        QFETCH(QString, input);
        SurveyTargetExpressionParser p;
        QVERIFY(!p.parse(input));
    }

    void testEval_data()
    {
        QTest::addColumn<QString>("input");
        QTest::addColumn<bool>("expected");
        QTest::newRow("01") << QStringLiteral("1 >= 2") << false;
        QTest::newRow("02") << QStringLiteral("1 < 2.0") << true;
        QTest::newRow("03") << QStringLiteral("true != false") << true;
        QTest::newRow("04") << QStringLiteral("\"abc\" == \"abc\"") << true;
        QTest::newRow("05") << QStringLiteral("1.0 > 1.5 || 1.0 <= 1.5") << true;
        QTest::newRow("06") << QStringLiteral("1.0 >= 1.5 && 1.0 < 1.5") << false;
        QTest::newRow("07") << QStringLiteral("3600 <= usageTime.value") << true;
        QTest::newRow("08") << QStringLiteral("3600 <= usageTime.value || 42 < lazy_eval.non_existing") << true;
        QTest::newRow("09") << QStringLiteral("screens[1].dpi <= 240") << true;
        QTest::newRow("10") << QStringLiteral("screens[2].dpi <= 240") << false;
        QTest::newRow("11") << QStringLiteral("screens.size == 2") << true;
        QTest::newRow("12") << QStringLiteral("1 == \"1\"") << false;
        QTest::newRow("13") << QStringLiteral("\"1\" == 1") << false;
        QTest::newRow("14") << QStringLiteral("screens[0].size == 1920") << true;
        QTest::newRow("15") << QStringLiteral("views[\"view2\"].ratio > 0.35") << true;
        QTest::newRow("16") << QStringLiteral("views[\"view3\"].size == \"???\"") << true;
        QTest::newRow("17") << QStringLiteral("views[\"view4\"].ratio > 0.35") << false;
        QTest::newRow("18") << QStringLiteral("views.size == 3") << true;
        QTest::newRow("19") << QStringLiteral("test.string == \"a\\\"b\\nc\\\\d\\te\\\\f\"") << true;
        QTest::newRow("20") << QStringLiteral("views[\"\"].ratio > 0.35") << false;
        QTest::newRow("21") << QStringLiteral("\"\" == \"\"") << true;
        QTest::newRow("22") << QStringLiteral("views.view1 == 2") << false;
        QTest::newRow("23") << QStringLiteral("views.non_existing == false") << false;
        QTest::newRow("24") << QStringLiteral("1 == 1 || 2 == 2 && 3 != 3") << true;
        QTest::newRow("25") << QStringLiteral("(1 == 1 || 2 == 2) && 3 != 3") << false;
        QTest::newRow("26") << QStringLiteral("1 == 1 && 2 == 2 || 3 != 3") << true;
        QTest::newRow("27") << QStringLiteral("1 == 1 && 2 == 2 || 3 == 3 &&  4!=4") << true;
    }

    void testEval()
    {
        QFETCH(QString, input);
        QFETCH(bool, expected);

        QVariantMap m;
        m.insert(QLatin1String("value"), 3600);
        m_sourceData.insert(QLatin1String("usageTime"), m);

        m.clear();
        m.insert(QLatin1String("dpi"), 200);
        m.insert(QLatin1String("size"), 1920);
        QVariantList l;
        l.push_back(m);
        l.push_back(m);
        m_sourceData.insert(QLatin1String("screens"), l);

        m.clear();
        m.insert(QLatin1String("ratio"), 0.4);
        m.insert(QLatin1String("size"), QLatin1String("???"));
        QVariantMap m2;
        m2.insert(QLatin1String("view1"), m);
        m2.insert(QLatin1String("view2"), m);
        m2.insert(QLatin1String("view3"), m);
        m_sourceData.insert(QLatin1String("views"), m2);

        m.clear();
        m.insert(QLatin1String("string"), QLatin1String("a\"b\nc\\d\te\\f"));
        m_sourceData.insert(QLatin1String("test"), m);

        SurveyTargetExpressionParser p;
        QVERIFY(p.parse(input));
        QVERIFY(p.expression());
        qDebug() << p.expression();
        SurveyTargetExpressionEvaluator eval;
        eval.setDataProvider(this);
        QCOMPARE(eval.evaluate(p.expression()), expected);
    }
};

QTEST_GUILESS_MAIN(SurveyTargetExpressionTest)

#include "surveytargetexpressiontest.moc"