Warning, file /utilities/okteta/kasten/controllers/test/osdparsertest.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /*
0002  *  This file is part of the Okteta Kasten module, made within the KDE community.
0003  *
0004  *  SPDX-FileCopyrightText: 2011, 2012 Alex Richardson <alex.richardson@gmx.de>
0005  *
0006  *  SPDX-License-Identifier: LGPL-2.1-or-later
0007  */
0008 
0009 #include <QTest>
0010 #include <QString>
0011 #include <QScriptEngine>
0012 #include <QScriptValue>
0013 #include "view/structures/parsers/osdparser.hpp"
0014 #include "view/structures/datatypes/primitive/primitivedatainformation.hpp"
0015 #include "view/structures/datatypes/topleveldatainformation.hpp"
0016 
0017 class OsdParserTest : public QObject
0018 {
0019     Q_OBJECT
0020 
0021 private Q_SLOTS:
0022     void testPrimitive();
0023     void testPrimitive_data();
0024     void testScriptFuntion();
0025 };
0026 
0027 namespace {
0028 inline QString arg(const QString& str, const char* argument)
0029 {
0030     return str.arg(QString::fromUtf8(argument));
0031 }
0032 }
0033 void OsdParserTest::testPrimitive_data()
0034 {
0035     QTest::addColumn<QString>("xml");
0036     QTest::addColumn<QString>("secondXml");
0037     QTest::addColumn<int>("expectedType");
0038     QString base(
0039         QStringLiteral(
0040             "<?xml version=\"1.0\" encoding=\"UTF-8\"?><data><primitive name=\"foo\" type=\"%1\" /></data>"));
0041     QTest::newRow("uint8") << arg(base, "uint8") << arg(base, "UInt8") << (int) PrimitiveDataType::UInt8;
0042     QTest::newRow("uint16") << arg(base, "uint16") << arg(base, "UInt16") << (int) PrimitiveDataType::UInt16;
0043     QTest::newRow("uint32") << arg(base, "uint32") << arg(base, "UInt32") << (int) PrimitiveDataType::UInt32;
0044     QTest::newRow("uint64") << arg(base, "uint64") << arg(base, "UInt64") << (int) PrimitiveDataType::UInt64;
0045     QTest::newRow("int8") << arg(base, "int8") << arg(base, "Int8") << (int) PrimitiveDataType::Int8;
0046     QTest::newRow("int16") << arg(base, "int16") << arg(base, "Int16") << (int) PrimitiveDataType::Int16;
0047     QTest::newRow("int32") << arg(base, "int32") << arg(base, "Int32") << (int) PrimitiveDataType::Int32;
0048     QTest::newRow("int64") << arg(base, "int64") << arg(base, "Int64") << (int) PrimitiveDataType::Int64;
0049     QTest::newRow("bool8") << arg(base, "bool8") << arg(base, "Bool8") << (int) PrimitiveDataType::Bool8;
0050     QTest::newRow("bool16") << arg(base, "bool16") << arg(base, "Bool16") << (int) PrimitiveDataType::Bool16;
0051     QTest::newRow("bool32") << arg(base, "bool32") << arg(base, "Bool32") << (int) PrimitiveDataType::Bool32;
0052     QTest::newRow("bool64") << arg(base, "bool64") << arg(base, "Bool64") << (int) PrimitiveDataType::Bool64;
0053     QTest::newRow("char") << arg(base, "char") << arg(base, "Char") << (int) PrimitiveDataType::Char;
0054     QTest::newRow("float") << arg(base, "float") << arg(base, "Float") << (int) PrimitiveDataType::Float;
0055     QTest::newRow("double") << arg(base, "double") << arg(base, "Double") << (int) PrimitiveDataType::Double;
0056 }
0057 
0058 void OsdParserTest::testPrimitive()
0059 {
0060     QFETCH(QString, xml);
0061     QFETCH(QString, secondXml);
0062     QFETCH(int, expectedType);
0063     PrimitiveDataType type(static_cast<PrimitiveDataType>(expectedType));
0064 
0065     OsdParser parser(xml);
0066     QVector<TopLevelDataInformation*> tds = parser.parseStructures();
0067     QCOMPARE(tds.size(), 1);
0068     const TopLevelDataInformation* td = tds.at(0);
0069     DataInformation* data = td->actualDataInformation();
0070     QCOMPARE(data->name(), QStringLiteral("foo"));
0071     PrimitiveDataInformation* prim = data->asPrimitive();
0072     QVERIFY(prim);
0073     QCOMPARE(prim->type(), type);
0074     qDeleteAll(tds);
0075 
0076     // just to ensure comparison is case insensitive
0077     OsdParser parser2(secondXml);
0078     QVector<TopLevelDataInformation*> tds2 = parser2.parseStructures();
0079     QCOMPARE(tds2.size(), 1);
0080     const TopLevelDataInformation* td2 = tds2.at(0);
0081     DataInformation* data2 = td2->actualDataInformation();
0082     QCOMPARE(data2->name(), QStringLiteral("foo"));
0083     PrimitiveDataInformation* prim2 = data2->asPrimitive();
0084     QVERIFY(prim2);
0085     QCOMPARE(prim2->type(), type);
0086     qDeleteAll(tds2);
0087 }
0088 
0089 void OsdParserTest::testScriptFuntion()
0090 {
0091     QScriptEngine engine;
0092     QScriptValue functionWrong1 = engine.evaluate(QStringLiteral("function x() { return 2; }"));
0093     QVERIFY(functionWrong1.isUndefined());
0094     QScriptValue functionWrong2 = engine.evaluate(QStringLiteral("function() { return 2; }"));
0095     QVERIFY(functionWrong2.isError());
0096     QScriptValue function = engine.evaluate(QStringLiteral("x = function() { return 2; }"));
0097     QVERIFY(function.isFunction());
0098     QCOMPARE(function.toString(), QStringLiteral("function () { return 2; }"));
0099     // must wrap in parentheses, see https://bugreports.qt-project.org/browse/QTBUG-5757
0100     QScriptValue betterFunction = engine.evaluate(QStringLiteral("(function() { return 2; })"));
0101     QVERIFY(betterFunction.isFunction());
0102     QCOMPARE(betterFunction.toString(), QStringLiteral("function () { return 2; }"));
0103 }
0104 
0105 QTEST_GUILESS_MAIN(OsdParserTest)
0106 
0107 #include "osdparsertest.moc"