diff options
author | Ulf Hermann <ulf.hermann@qt.io> | 2021-03-17 10:01:42 +0100 |
---|---|---|
committer | Ulf Hermann <ulf.hermann@qt.io> | 2021-03-23 06:51:16 +0100 |
commit | f8f31dd0e1f9425ba272691c79e719ebc4bcfb94 (patch) | |
tree | 1f79039e87d9c43905f678e11fe902766fe72ed1 /tests/auto/qml/qjsprimitivevalue | |
parent | 78aea267209c34abeb4895712dc76c923aa46165 (diff) |
Implement some unary operators on QJSPrimitiveValue
operator++ is not actually the same as += 1, for example.
Change-Id: If1069a9a47170707faee11be05424188027bb0b5
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
Diffstat (limited to 'tests/auto/qml/qjsprimitivevalue')
-rw-r--r-- | tests/auto/qml/qjsprimitivevalue/tst_qjsprimitivevalue.cpp | 212 |
1 files changed, 151 insertions, 61 deletions
diff --git a/tests/auto/qml/qjsprimitivevalue/tst_qjsprimitivevalue.cpp b/tests/auto/qml/qjsprimitivevalue/tst_qjsprimitivevalue.cpp index 4fe68e1f73..7651fa86a6 100644 --- a/tests/auto/qml/qjsprimitivevalue/tst_qjsprimitivevalue.cpp +++ b/tests/auto/qml/qjsprimitivevalue/tst_qjsprimitivevalue.cpp @@ -37,8 +37,11 @@ class tst_QJSPrimitiveValue : public QObject Q_OBJECT private slots: - void operators_data(); - void operators(); + void binaryOperators_data(); + void binaryOperators(); + + void unaryOperators_data(); + void unaryOperators(); void toFromVariant(); @@ -69,12 +72,14 @@ QString toScriptString(T value) else return QStringLiteral("0.0"); } + } else if (value.type() == QJSPrimitiveValue::String) { + return u'"' + value.toString() + u'"'; } return value.toString(); } } -void tst_QJSPrimitiveValue::operators_data() +void tst_QJSPrimitiveValue::binaryOperators_data() { QTest::addColumn<QJSPrimitiveValue>("lhs"); QTest::addColumn<QJSPrimitiveValue>("rhs"); @@ -96,11 +101,11 @@ do {\ #define VERBOSEVERIFY(condition, expression, js) \ QVERIFY2(condition, qPrintable(expression + " -> " + js.toString())) -enum Operator { +enum BinaryOperator { Add, Sub, Mul, Div, Eq, SEq, NEq, SNEq, GT, LT, GEq, LEq, Mod }; -QString toString(Operator op) { +QString toString(BinaryOperator op) { switch (op) { case Add: return "+"; case Sub: return "-"; @@ -121,41 +126,9 @@ QString toString(Operator op) { return QString(); } -template<typename LHS, typename RHS, typename Result, Operator op> -void doTestOperator(QJSEngine *engine, LHS lhs, RHS rhs) +template<typename Result> +void verifyResult(Result result, QJSEngine *engine, const QString &expression) { - Result result; - - if constexpr (op == Add) - result = lhs + rhs; - else if constexpr (op == Sub) - result = lhs - rhs; - else if constexpr (op == Mul) - result = lhs * rhs; - else if constexpr (op == Div) - result = lhs / rhs; - else if constexpr (op == Eq) - result = QJSPrimitiveValue(lhs).equals(rhs); - else if constexpr (op == SEq) - result = lhs == rhs; - else if constexpr (op == NEq) - result = !QJSPrimitiveValue(lhs).equals(rhs); - else if constexpr (op == SNEq) - result = lhs != rhs; - else if constexpr (op == GT) - result = lhs > rhs; - else if constexpr (op == LT) - result = lhs < rhs; - else if constexpr (op == GEq) - result = lhs >= rhs; - else if constexpr (op == LEq) - result = lhs <= rhs; - else if constexpr (op == Mod) - result = lhs % rhs; - else - QFAIL("Unkonwn operator"); - - const QString expression = toScriptString(lhs) + " " + toString(op) + " " + toScriptString(rhs); const QJSValue js = engine->evaluate(expression); if constexpr (std::is_same_v<Result, bool>) { @@ -195,36 +168,153 @@ void doTestOperator(QJSEngine *engine, LHS lhs, RHS rhs) } } +template<typename LHS, typename RHS, typename Result, BinaryOperator op> +void doTestBinaryOperator(QJSEngine *engine, LHS lhs, RHS rhs) +{ + Result result; + + if constexpr (op == Add) + result = lhs + rhs; + else if constexpr (op == Sub) + result = lhs - rhs; + else if constexpr (op == Mul) + result = lhs * rhs; + else if constexpr (op == Div) + result = lhs / rhs; + else if constexpr (op == Eq) + result = QJSPrimitiveValue(lhs).equals(rhs); + else if constexpr (op == SEq) + result = lhs == rhs; + else if constexpr (op == NEq) + result = !QJSPrimitiveValue(lhs).equals(rhs); + else if constexpr (op == SNEq) + result = lhs != rhs; + else if constexpr (op == GT) + result = lhs > rhs; + else if constexpr (op == LT) + result = lhs < rhs; + else if constexpr (op == GEq) + result = lhs >= rhs; + else if constexpr (op == LEq) + result = lhs <= rhs; + else if constexpr (op == Mod) + result = lhs % rhs; + else + QFAIL("Unkonwn operator"); + + const QString expression = toScriptString(lhs) + " " + toString(op) + " " + toScriptString(rhs); + verifyResult(result, engine, expression); +} + template<typename LHS, typename RHS> -void doTestForAllOperators(QJSEngine *engine, LHS lhs, RHS rhs) +void doTestForAllBinaryOperators(QJSEngine *engine, LHS lhs, RHS rhs) { - doTestOperator<LHS, RHS, QJSPrimitiveValue, Add>(engine, lhs, rhs); - doTestOperator<LHS, RHS, QJSPrimitiveValue, Sub>(engine, lhs, rhs); - doTestOperator<LHS, RHS, QJSPrimitiveValue, Mul>(engine, lhs, rhs); - doTestOperator<LHS, RHS, QJSPrimitiveValue, Div>(engine, lhs, rhs); - doTestOperator<LHS, RHS, QJSPrimitiveValue, Mod>(engine, lhs, rhs); - doTestOperator<LHS, RHS, bool, Eq>(engine, lhs, rhs); - doTestOperator<LHS, RHS, bool, SEq>(engine, lhs, rhs); - doTestOperator<LHS, RHS, bool, NEq>(engine, lhs, rhs); - doTestOperator<LHS, RHS, bool, SNEq>(engine, lhs, rhs); - doTestOperator<LHS, RHS, bool, GT>(engine, lhs, rhs); - doTestOperator<LHS, RHS, bool, LT>(engine, lhs, rhs); - doTestOperator<LHS, RHS, bool, GEq>(engine, lhs, rhs); - doTestOperator<LHS, RHS, bool, LEq>(engine, lhs, rhs); + doTestBinaryOperator<LHS, RHS, QJSPrimitiveValue, Add>(engine, lhs, rhs); + doTestBinaryOperator<LHS, RHS, QJSPrimitiveValue, Sub>(engine, lhs, rhs); + doTestBinaryOperator<LHS, RHS, QJSPrimitiveValue, Mul>(engine, lhs, rhs); + doTestBinaryOperator<LHS, RHS, QJSPrimitiveValue, Div>(engine, lhs, rhs); + doTestBinaryOperator<LHS, RHS, QJSPrimitiveValue, Mod>(engine, lhs, rhs); + doTestBinaryOperator<LHS, RHS, bool, Eq>(engine, lhs, rhs); + doTestBinaryOperator<LHS, RHS, bool, SEq>(engine, lhs, rhs); + doTestBinaryOperator<LHS, RHS, bool, NEq>(engine, lhs, rhs); + doTestBinaryOperator<LHS, RHS, bool, SNEq>(engine, lhs, rhs); + doTestBinaryOperator<LHS, RHS, bool, GT>(engine, lhs, rhs); + doTestBinaryOperator<LHS, RHS, bool, LT>(engine, lhs, rhs); + doTestBinaryOperator<LHS, RHS, bool, GEq>(engine, lhs, rhs); + doTestBinaryOperator<LHS, RHS, bool, LEq>(engine, lhs, rhs); } -void tst_QJSPrimitiveValue::operators() +void tst_QJSPrimitiveValue::binaryOperators() { QFETCH(QJSPrimitiveValue, lhs); QFETCH(QJSPrimitiveValue, rhs); - doTestForAllOperators(&engine, lhs, rhs); - doTestForAllOperators(&engine, lhs.toString(), rhs); - doTestForAllOperators(&engine, lhs, rhs.toString()); - doTestForAllOperators(&engine, lhs.toString() + " bar", rhs); - doTestForAllOperators(&engine, lhs, rhs.toString() + " bar"); - doTestForAllOperators(&engine, "foo" + lhs.toString(), rhs); - doTestForAllOperators(&engine, lhs, "foo" + rhs.toString()); + doTestForAllBinaryOperators(&engine, lhs, rhs); + doTestForAllBinaryOperators(&engine, lhs.toString(), rhs); + doTestForAllBinaryOperators(&engine, lhs, rhs.toString()); + doTestForAllBinaryOperators(&engine, lhs.toString() + " bar", rhs); + doTestForAllBinaryOperators(&engine, lhs, rhs.toString() + " bar"); + doTestForAllBinaryOperators(&engine, "foo" + lhs.toString(), rhs); + doTestForAllBinaryOperators(&engine, lhs, "foo" + rhs.toString()); +} + +void tst_QJSPrimitiveValue::unaryOperators_data() +{ + QTest::addColumn<QJSPrimitiveValue>("operand"); + + for (const QJSPrimitiveValue &o : operands) + QTest::newRow(qPrintable(toScriptString(o))) << o; +} + +enum UnaryOperator { + Plus, Minus, PreIncrement, PostIncrement, PreDecrement, PostDecrement +}; + +QString toString(UnaryOperator op) { + switch (op) { + case Plus: + return "+"; + case Minus: + return "-"; + case PreIncrement: + case PostIncrement: + return "++"; + case PreDecrement: + case PostDecrement: + return "--"; + } + + Q_UNREACHABLE(); + return QString(); +} + +template<typename Result, UnaryOperator Operator> +void doTestUnaryOperator(QJSEngine *engine, QJSPrimitiveValue operand) +{ + Result result; + + // Need to get the string here as the operators change the original value + const QString asString = toScriptString(operand); + + if constexpr (Operator == Plus) + result = +operand; + else if constexpr (Operator == Minus) + result = -operand; + else if constexpr (Operator == PreIncrement) + result = ++operand; + else if constexpr (Operator == PostIncrement) + result = operand++; + else if constexpr (Operator == PreDecrement) + result = --operand; + else if constexpr (Operator == PostDecrement) + result = operand--; + else + QFAIL("Unkonwn operator"); + + const QString decl = "var a = " + asString + "; "; + const QString expression = decl + ((Operator == PostIncrement || Operator == PostDecrement) + ? (u'a' + toString(Operator)) + : (toString(Operator) + u'a')); + verifyResult(result, engine, expression); +} + +void doTestForAllUnaryOperators(QJSEngine *engine, QJSPrimitiveValue operand) +{ + doTestUnaryOperator<QJSPrimitiveValue, Plus>(engine, operand); + doTestUnaryOperator<QJSPrimitiveValue, Minus>(engine, operand); + doTestUnaryOperator<QJSPrimitiveValue, PreIncrement>(engine, operand); + doTestUnaryOperator<QJSPrimitiveValue, PostIncrement>(engine, operand); + doTestUnaryOperator<QJSPrimitiveValue, PostDecrement>(engine, operand); +} + +void tst_QJSPrimitiveValue::unaryOperators() +{ + QFETCH(QJSPrimitiveValue, operand); + + doTestForAllUnaryOperators(&engine, operand); + doTestForAllUnaryOperators(&engine, operand.toString()); + doTestForAllUnaryOperators(&engine, operand.toString() + " bar"); + doTestForAllUnaryOperators(&engine, "foo" + operand.toString()); } void tst_QJSPrimitiveValue::toFromVariant() |