summaryrefslogtreecommitdiffstats
path: root/tests/yaml/tst_yaml.cpp
blob: 448dd690c2007930ff2d48d4229c46fb8af0e4b2 (plain)
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
/****************************************************************************
**
** Copyright (C) 2019 The Qt Company Ltd.
** Copyright (C) 2019 Luxoft Sweden AB
** Copyright (C) 2018 Pelagicore AG
** Contact: https://www.qt.io/licensing/
**
** This file is part of the Qt Application Manager.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT-QTAS$
** Commercial License Usage
** Licensees holding valid commercial Qt Automotive Suite licenses may use
** this file in accordance with the commercial license agreement provided
** with the Software or, alternatively, in accordance with the terms
** contained in a written agreement between you and The Qt Company.  For
** licensing terms and conditions see https://www.qt.io/terms-conditions.
** For further information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/

#include <QtCore>
#include <QtTest>
#include <QThreadPool>

#include "qtyaml.h"
#include "configcache.h"
#include "exception.h"
#include "global.h"

QT_USE_NAMESPACE_AM

class tst_Yaml : public QObject
{
    Q_OBJECT

public:
    tst_Yaml();

private slots:
    void parser();
    void documentParser();
    void cache();
    void mergedCache();
    void parallel();
};


tst_Yaml::tst_Yaml()
{ }

void tst_Yaml::parser()
{
    static const QVariant vnull = QVariant::fromValue(nullptr);

    QVector<QPair<const char *, QVariant>> tests = {
        { "dec", QVariant::fromValue<int>(10) },
        { "hex", QVariant::fromValue<int>(16) },
        { "bin", QVariant::fromValue<int>(2) },
        { "oct", QVariant::fromValue<int>(8) },
        { "float1", QVariant::fromValue<double>(10.1) },
        { "float2", QVariant::fromValue<double>(.1) },
        { "float3", QVariant::fromValue<double>(.1) },
        { "number-separators", QVariant::fromValue<int>(1234567) },
        { "bool-true", true },
        { "bool-yes", true },
        { "bool-false", false },
        { "bool-no", false },
        { "null-literal", vnull },
        { "null-tilde", vnull },
        { "string-unquoted", QVariant::fromValue<QString>("unquoted") },
        { "string-singlequoted", QVariant::fromValue<QString>("singlequoted") },
        { "string-doublequoted", QVariant::fromValue<QString>("doublequoted") },
        { "list-int", QVariantList { 1, 2, 3 } },
        { "list-mixed", QVariantList { 1, "two", QVariantList { true, vnull } } },
        { "map1", QVariantMap { { "a", 1 }, { "b", "two" }, { "c", QVariantList { 1, 2, 3 } } } }
    };

    try {
        QFile f(":/data/test.yaml");
        QVERIFY2(f.open(QFile::ReadOnly), qPrintable(f.errorString()));
        QByteArray ba = f.readAll();
        QVERIFY(!ba.isEmpty());
        YamlParser p(ba);
        auto header = p.parseHeader();

        QCOMPARE(header.first, "testfile");
        QCOMPARE(header.second, 42);

        QVERIFY(p.nextDocument());

        YamlParser::Fields fields;
        for (const auto &pair : tests) {
            YamlParser::FieldType type = YamlParser::Scalar;
            if (pair.second.type() == QVariant::List)
                type = YamlParser::List;
            else if (pair.second.type() == QVariant::Map)
                type = YamlParser::Map;
            QVariant value = pair.second;

            fields.emplace_back(pair.first, true, type, [type, value](YamlParser *p) {
                switch (type) {
                case YamlParser::Scalar: {
                    QVERIFY(p->isScalar());
                    QVariant v = p->parseScalar();
                    QCOMPARE(int(v.type()), int(value.type()));
                    QVERIFY(v == value);
                    break;
                }
                case YamlParser::List: {
                    QVERIFY(p->isList());
                    QVariantList vl = p->parseList();
                    QVERIFY(vl == value.toList());
                    break;
                }
                case YamlParser::Map: {
                    QVERIFY(p->isMap());
                    QVariantMap vm = p->parseMap();
                    QVERIFY(vm == value.toMap());
                    break;
                }
                }
            });
        }
        fields.emplace_back("extended", true, YamlParser::Map, [](YamlParser *p) {
            YamlParser::Fields extFields = {
                { "ext-string", true, YamlParser::Scalar, [](YamlParser *p) {
                      QVERIFY(p->isScalar());
                      QVariant v = p->parseScalar();
                      QCOMPARE(v.type(), QVariant::String);
                      QCOMPARE(v.toString(), "ext string");
                  } }
            };
            p->parseFields(extFields);
        });

        fields.emplace_back("stringlist-string", true, YamlParser::Scalar | YamlParser::List, [](YamlParser *p) {
            QCOMPARE(p->parseStringOrStringList(), QStringList { "string" });
        });
        fields.emplace_back("stringlist-list1", true, YamlParser::Scalar | YamlParser::List, [](YamlParser *p) {
            QCOMPARE(p->parseStringOrStringList(), QStringList { "string" });
        });
        fields.emplace_back("stringlist-list2", true, YamlParser::Scalar | YamlParser::List, [](YamlParser *p) {
            QCOMPARE(p->parseStringOrStringList(), QStringList({ "string1", "string2" }));
        });

        fields.emplace_back("list-of-maps", true, YamlParser::List, [](YamlParser *p) {
            int index = 0;
            p->parseList([&index](YamlParser *p) {
                ++index;
                YamlParser::Fields lomFields = {
                    { "index", true, YamlParser::Scalar, [&index](YamlParser *p) {
                          QCOMPARE(p->parseScalar().toInt(), index);
                      } },
                    { "name", true, YamlParser::Scalar, [&index](YamlParser *p) {
                          QCOMPARE(p->parseScalar().toString(), QString::number(index));
                      } }
                };
                p->parseFields(lomFields);
            });
            QCOMPARE(index, 2);
        });

        p.parseFields(fields);

        QVERIFY(!p.nextDocument());

    } catch (const Exception &e) {
        QVERIFY2(false, e.what());
    }
}

static const QVariant vnull = QVariant::fromValue(nullptr);

static const QVariantMap testHeaderDoc = {
    { "formatVersion", 42 }, { "formatType", "testfile" }
};

static const QVariantMap testMainDoc = {
    { "dec", 10 },
    { "hex", 16 },
    { "bin", 2 },
    { "oct", 8 },
    { "float1", 10.1 },
    { "float2", .1 },
    { "float3", .1 },
    { "number-separators", 1234567 },
    { "bool-true", true },
    { "bool-yes", true },
    { "bool-false", false },
    { "bool-no", false },
    { "null-literal", vnull },
    { "null-tilde", vnull },
    { "string-unquoted", "unquoted" },
    { "string-singlequoted", "singlequoted" },
    { "string-doublequoted", "doublequoted" },
    { "list-int", QVariantList { 1, 2, 3 } },
    { "list-mixed", QVariantList { 1, qSL("two"), QVariantList { true, vnull } } },
    { "map1", QVariantMap { { "a", 1 }, { "b", "two" }, { "c", QVariantList { 1, 2, 3 } } } },


    { "extended", QVariantMap { { "ext-string", "ext string" } } },

    { "stringlist-string", "string" },
    { "stringlist-list1", QVariantList { "string" } },
    { "stringlist-list2", QVariantList { "string1", "string2" } },

    { "list-of-maps", QVariantList { QVariantMap { { "index", 1 }, { "name", "1" } },
                                     QVariantMap { { "index", 2 }, { "name", "2" } } } }
};

void tst_Yaml::documentParser()
{
    try {
        QFile f(":/data/test.yaml");
        QVERIFY2(f.open(QFile::ReadOnly), qPrintable(f.errorString()));
        QByteArray ba = f.readAll();
        QVERIFY(!ba.isEmpty());
        QVector<QVariant> docs = YamlParser::parseAllDocuments(ba);
        QCOMPARE(docs.size(), 2);
        QCOMPARE(docs.at(0).toMap().size(), 2);

        QCOMPARE(testHeaderDoc, docs.at(0).toMap());
        QCOMPARE(testMainDoc, docs.at(1).toMap());

    } catch (const Exception &e) {
        QVERIFY2(false, e.what());
    }
}
struct CacheTest
{
    QString name;
    QString file;
};

// GCC < 7 bug, currently still in RHEL7, https://gcc.gnu.org/bugzilla/show_bug.cgi?id=56480
// this should simply be:
// template<> class QT_PREPEND_NAMESPACE_AM(ConfigCacheAdaptor<CacheTest>)

QT_BEGIN_NAMESPACE_AM
template<> class ConfigCacheAdaptor<CacheTest>
{
public:
    CacheTest *loadFromSource(QIODevice *source, const QString &fileName)
    {
        QScopedPointer<CacheTest> ct(new CacheTest);
        YamlParser p(source->readAll(), fileName);
        p.nextDocument();
        p.parseFields({ { "name", true, YamlParser::Scalar, [&ct](YamlParser *p) {
                          ct->name = p->parseScalar().toString(); } },
                        { "file", true, YamlParser::Scalar, [&ct](YamlParser *p) {
                          ct->file = p->parseScalar().toString(); } }
                      });
        return ct.take();
    }
    CacheTest *loadFromCache(QDataStream &ds)
    {
        CacheTest *ct = new CacheTest;
        ds >> ct->name >> ct->file;
        return ct;
    }
    void saveToCache(QDataStream &ds, const CacheTest *ct)
    {
        ds << ct->name << ct->file;
    }

    void merge(CacheTest *ct1, const CacheTest *ct2)
    {
        ct1->name = ct2->name;
        ct1->file = ct1->file + qSL(",") + ct2->file;
    }
    void preProcessSourceContent(QByteArray &sourceContent, const QString &fileName)
    {
        sourceContent.replace("${FILE}", fileName.toUtf8());
    }
};
QT_END_NAMESPACE_AM

void tst_Yaml::cache()
{
    QStringList files = { ":/data/cache1.yaml", ":/data/cache2.yaml" };

    for (int step = 0; step < 2; ++step) {
        try {
            ConfigCache<CacheTest> cache(files, "cache-test", "CTST", 1,
                                         step == 0 ? AbstractConfigCache::ClearCache
                                                   : AbstractConfigCache::None);
            cache.parse();
            QVERIFY(cache.parseReadFromCache() == (step == 1));
            QVERIFY(cache.parseWroteToCache() == (step == 0));
            CacheTest *ct1 = cache.takeResult(0);
            QVERIFY(ct1);
            QCOMPARE(ct1->name, "cache1");
            QCOMPARE(ct1->file, ":/data/cache1.yaml");
            CacheTest *ct2 = cache.takeResult(1);
            QVERIFY(ct2);
            QCOMPARE(ct2->name, "cache2");
            QCOMPARE(ct2->file, ":/data/cache2.yaml");
        } catch (const Exception &e) {
            QVERIFY2(false, e.what());
        }
    }

    ConfigCache<CacheTest> wrongVersion(files, "cache-test", "CTST", 2, AbstractConfigCache::None);
    QTest::ignoreMessage(QtWarningMsg, "Failed to read cache: failed to parse cache header");
    wrongVersion.parse();
    QVERIFY(!wrongVersion.parseReadFromCache());

    ConfigCache<CacheTest> wrongType(files, "cache-test", "XTST", 1, AbstractConfigCache::None);
    QTest::ignoreMessage(QtWarningMsg, "Failed to read cache: failed to parse cache header");
    wrongType.parse();
    QVERIFY(!wrongType.parseReadFromCache());
}

void tst_Yaml::mergedCache()
{
    QStringList files = { ":/data/cache1.yaml", ":/data/cache2.yaml" };

    for (int step = 0; step < 4; ++step) {
        AbstractConfigCache::Options options = AbstractConfigCache::MergedResult;
        if (step % 2 == 0)
            options |= AbstractConfigCache::ClearCache;
        if (step == 2)
            std::reverse(files.begin(), files.end());

        try {
            ConfigCache<CacheTest> cache(files, "cache-test", "MTST", 1, options);
            cache.parse();
            QVERIFY(cache.parseReadFromCache() == (step % 2 == 1));
            QVERIFY(cache.parseWroteToCache() == (step % 2 == 0));
            CacheTest *ct = cache.takeMergedResult();
            QVERIFY(ct);
            QCOMPARE(ct->name, QFileInfo(files.last()).baseName());
            QCOMPARE(ct->file, files.join(qSL(",")));
        } catch (const Exception &e) {
            QVERIFY2(false, e.what());
        }
    }
}

class YamlRunnable : public QRunnable
{
public:
    YamlRunnable(const QByteArray &yaml, QAtomicInt &success, QAtomicInt &fail)
        : m_yaml(yaml)
        , m_success(success)
        , m_fail(fail)
    { }

    void run() override
    {
        QVector<QVariant> docs;
        try {
            docs = YamlParser::parseAllDocuments(m_yaml);
        } catch (...) {
            docs.clear();
        }
        if ((docs.size() == 2)
            && (docs.at(0).toMap().size() == 2)
            && (testHeaderDoc == docs.at(0).toMap())
            && (testMainDoc == docs.at(1).toMap())) {
            m_success.fetchAndAddOrdered(1);
        } else {
            m_fail.fetchAndAddOrdered(1);
        }
    }
private:
    const QByteArray m_yaml;
    QAtomicInt &m_success;
    QAtomicInt &m_fail;
};

void tst_Yaml::parallel()
{
    QFile f(":/data/test.yaml");
    QVERIFY2(f.open(QFile::ReadOnly), qPrintable(f.errorString()));
    QByteArray ba = f.readAll();
    QVERIFY(!ba.isEmpty());

    constexpr int threadCount = 16;

    QAtomicInt success;
    QAtomicInt fail;

    QThreadPool tp;
    if (tp.maxThreadCount() < threadCount)
        tp.setMaxThreadCount(threadCount);

    for (int i = 0; i < threadCount; ++i)
        tp.start(new YamlRunnable(ba, success, fail));

    QVERIFY(tp.waitForDone(5000));
    QCOMPARE(fail.loadAcquire(), 0);
    QCOMPARE(success.loadAcquire(), threadCount);
}

QTEST_MAIN(tst_Yaml)

#include "tst_yaml.moc"