File indexing completed on 2024-05-12 05:55:45

0001 /*
0002  *    This file is part of the Okteta Kasten module, made within the KDE community.
0003  *
0004  *    SPDX-FileCopyrightText: 2012 Alex Richardson <alex.richardson@gmx.de>
0005  *
0006  *    SPDX-License-Identifier: LGPL-2.1-or-later
0007  */
0008 
0009 #ifndef KASTEN_TESTUTILS_HPP
0010 #define KASTEN_TESTUTILS_HPP
0011 
0012 #include <QString>
0013 #include <QTest>
0014 #include <QScriptEngine>
0015 #include "view/structures/datatypes/primitivedatatype.hpp"
0016 #include "view/structures/datatypes/datainformation.hpp"
0017 #include "view/structures/datatypes/primitive/primitivedatainformation.hpp"
0018 #include "view/structures/datatypes/primitive/bitfield/signedbitfielddatainformation.hpp"
0019 #include "view/structures/datatypes/primitive/bitfield/unsignedbitfielddatainformation.hpp"
0020 #include "view/structures/datatypes/primitive/bitfield/boolbitfielddatainformation.hpp"
0021 #include "view/structures/datatypes/topleveldatainformation.hpp"
0022 #include "view/structures/parsers/scriptvalueconverter.hpp"
0023 #include "view/structures/script/scriptengineinitializer.hpp"
0024 
0025 namespace Utils {
0026 
0027 /** Converts a string to a binary number (spaces are ignored)
0028  * @param val the string representing a binary number
0029  * @return
0030  */
0031 template <typename T>
0032 T binary(const char* val)
0033 {
0034     QString value = QString::fromUtf8(val);
0035     value = value.remove(QLatin1Char(' '));
0036     QTEST_ASSERT(unsigned(value.length()) <= sizeof(T) * 8); // otherwise we overflow
0037     bool ok = false;
0038     quint64 result = value.toULongLong(&ok, 2);
0039     QTEST_ASSERT(ok);
0040     return static_cast<T>(result);
0041 }
0042 
0043 DataInformation* evalAndParse(QScriptEngine* eng, const QString& code, ScriptLogger* logger)
0044 {
0045     QScriptValue result = eng->evaluate(code);
0046     if (result.isError()) {
0047         qWarning() << "error parsing" << code << ":" << result.toString();
0048     }
0049     return ScriptValueConverter::convert(result, QStringLiteral("result"), logger);
0050 }
0051 
0052 DataInformation* evalAndParse(QScriptEngine* eng, const char* code, ScriptLogger* logger)
0053 {
0054     return evalAndParse(eng, QString::fromUtf8(code), logger);
0055 }
0056 
0057 TopLevelDataInformation* evalAndParse(const QString& code)
0058 {
0059     auto* l = new ScriptLogger();
0060     l->setLogToStdOut(true);
0061     QScriptEngine* engine = ScriptEngineInitializer::newEngine();
0062     DataInformation* inf = evalAndParse(engine, code, l);
0063     QTEST_ASSERT(inf);
0064     return new TopLevelDataInformation(inf, l, engine);
0065 }
0066 
0067 TopLevelDataInformation* evalAndParse(const char* code)
0068 {
0069     return evalAndParse(QString::fromUtf8(code));
0070 }
0071 
0072 /** The same as engine->evaluate, but if there is an exception return that instead */
0073 QScriptValue evaluate(QScriptEngine* engine, const QString& code)
0074 {
0075     QScriptValue ret = engine->evaluate(code);
0076     if (engine->hasUncaughtException()) {
0077         ret = engine->uncaughtException();
0078         engine->clearExceptions();
0079     }
0080     return ret;
0081 }
0082 
0083 QScriptValue evaluate(QScriptEngine* engine, const char* code)
0084 {
0085     return evaluate(engine, QString::fromUtf8(code));
0086 }
0087 
0088 /** The same as value.property(), but if there is an exception return that instead*/
0089 QScriptValue property(const QScriptValue& value, const char* property)
0090 {
0091     QScriptValue ret = value.property(QString::fromUtf8(property));
0092     if (value.engine()->hasUncaughtException()) {
0093         ret = value.engine()->uncaughtException();
0094         value.engine()->clearExceptions();
0095     }
0096     return ret;
0097 }
0098 
0099 }
0100 
0101 #endif /* KASTEN_TESTUTILS_HPP */