diff options
Diffstat (limited to 'src/quicktestutils')
| -rw-r--r-- | src/quicktestutils/qml/qmlutils.cpp | 35 | ||||
| -rw-r--r-- | src/quicktestutils/qml/qmlutils_p.h | 54 |
2 files changed, 89 insertions, 0 deletions
diff --git a/src/quicktestutils/qml/qmlutils.cpp b/src/quicktestutils/qml/qmlutils.cpp index 5f573e790b..528a843f17 100644 --- a/src/quicktestutils/qml/qmlutils.cpp +++ b/src/quicktestutils/qml/qmlutils.cpp @@ -137,6 +137,41 @@ void gc(QQmlEngine &engine, GCFlags flags) gc(*engine.handle(), flags); } +namespace Syntax { + +auto stringView(const Word &word) -> QLatin1StringView +{ + return std::holds_alternative<Token>(word) ? spellFor(std::get<Token>(word)) + : std::get<QLatin1StringView>(word); +} + +auto toString(const Phrase &phrase) -> QString +{ + QString result; + for (const auto &word : phrase) { + result += stringView(word) + QLatin1Char(' '); + } + return result; +} + +// comfort +auto operator+(const Word &word, const Phrase &phrase) -> Phrase +{ + return Phrase{ word } + phrase; +}; + +auto operator+(const Word &word1, const Word &word2) -> Phrase +{ + return Phrase{ word1, word2 }; +}; + +auto objectDeclaration(Phrase &&objectMember, QLatin1StringView objName) -> Phrase +{ + return Phrase{} << Word(objName) << Word(Token::T_LBRACE) << std::move(objectMember) + << Word(Token::T_RBRACE); +} + +} // namespace Syntax QT_END_NAMESPACE diff --git a/src/quicktestutils/qml/qmlutils_p.h b/src/quicktestutils/qml/qmlutils_p.h index d8d28256ac..d4fc83c270 100644 --- a/src/quicktestutils/qml/qmlutils_p.h +++ b/src/quicktestutils/qml/qmlutils_p.h @@ -22,6 +22,7 @@ #include <QtCore/QStringList> #include <QtTest/QTest> #include <QtCore/private/qglobal_p.h> +#include <private/qqmljsgrammar_p.h> QT_BEGIN_NAMESPACE @@ -122,6 +123,59 @@ void gc(QV4::ExecutionEngine &engine, GCFlags flags = GCFlags::None); bool gcDone(QQmlEngine *engine); void gc(QQmlEngine &engine, GCFlags flags = GCFlags::None); +namespace Syntax { +using Token = QQmlJSGrammar::VariousConstants; +// TODO(QTBUG-138020) +constexpr auto spellFor(Token token) -> QLatin1StringView +{ + switch (token) { + case Token::T_COLON: + return QLatin1StringView(":"); + case Token::T_LBRACE: + return QLatin1StringView("{"); + case Token::T_RBRACE: + return QLatin1StringView("}"); + case Token::T_VAR: + return QLatin1StringView("var"); + case Token::T_PROPERTY: + return QLatin1StringView("property"); + case Token::T_DEFAULT: + return QLatin1StringView("default"); + case Token::T_READONLY: + return QLatin1StringView("readonly"); + case Token::T_REQUIRED: + return QLatin1StringView("required"); + case Token::T_FINAL: + return QLatin1StringView("final"); + case Token::T_VIRTUAL: + return QLatin1StringView("virtual"); + case Token::T_OVERRIDE: + return QLatin1StringView("override"); + default: + break; + } + Q_UNREACHABLE_RETURN({}); +} + +using Word = std::variant<Token, QLatin1StringView>; +auto stringView(const Word &word) -> QLatin1StringView; + +using Phrase = QList<Word>; +auto toString(const Phrase &phrase) -> QString; + +// comfort +auto operator+(const Word &word, const Phrase &phrase) -> Phrase; +auto operator+(const Word &word1, const Word &word2) -> Phrase; + +// if such declaration generating functions start to grow it might be worth creating a dedicated +// more optimized generator + +// can probably be generalized later to accept QList<Phrase> if needed +auto objectDeclaration(Phrase &&objectMember = {}, + QLatin1StringView objName = QLatin1StringView("QtObject")) -> Phrase; + +} // namespace Syntax + QT_END_NAMESPACE #endif // QQMLTESTUTILS_P_H |
