From 83ca3efdef6575072517a24e27428e3c2645944d Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Sun, 21 Jul 2019 17:34:44 +0100 Subject: [PATCH] Test and fix truthiness conversion --- templates/lib/util.cpp | 12 ++++ templates/tests/testbuiltins.cpp | 104 +++++++++++++++++++++++++++++++ 2 files changed, 116 insertions(+) diff --git a/templates/lib/util.cpp b/templates/lib/util.cpp index 62d169dd..2468b0cc 100644 --- a/templates/lib/util.cpp +++ b/templates/lib/util.cpp @@ -45,12 +45,24 @@ bool Grantlee::variantIsTrue(const QVariant &variant) case QVariant::Int: { return variant.value() > 0; } + case QVariant::UInt: { + return variant.value() > 0; + } + case QVariant::LongLong: { + return variant.value() > 0; + } + case QVariant::ULongLong: { + return variant.value() > 0; + } case QVariant::Double: { return variant.value() > 0; } case QMetaType::Float: { return variant.value() > 0; } + case QMetaType::Char: { + return variant.value() > 0; + } case QMetaType::QObjectStar: { auto obj = variant.value(); if (!obj) diff --git a/templates/tests/testbuiltins.cpp b/templates/tests/testbuiltins.cpp index 49dbe0ec..3d881bb6 100644 --- a/templates/tests/testbuiltins.cpp +++ b/templates/tests/testbuiltins.cpp @@ -31,6 +31,7 @@ #include "filterexpression.h" #include "grantlee_paths.h" #include "template.h" +#include "util.h" #include typedef QHash Dict; @@ -168,6 +169,9 @@ private Q_SLOTS: void testObjects(); + void testTruthiness_data(); + void testTruthiness(); + void testRenderAfterError(); void testBasicSyntax_data(); @@ -260,6 +264,106 @@ void TestBuiltinSyntax::testObjects() QMetaType::construct(qMetaTypeId(), 0, 0); } +void TestBuiltinSyntax::testTruthiness_data() +{ + QTest::addColumn("input"); + QTest::addColumn("expected"); + + QTest::newRow("truthtest-01") << QVariant() << false; + QTest::newRow("truthtest-02") << QVariant(false) << false; + QTest::newRow("truthtest-03") << QVariant(true) << true; + + QTest::newRow("truthtest-04") << QVariant(0) << false; + QTest::newRow("truthtest-05") << QVariant(1) << true; + + { + auto falseV = QVariant::fromValue(0); + QTest::newRow("truthtest-06") << falseV << false; + auto trueV = QVariant::fromValue(1); + QTest::newRow("truthtest-07") << trueV << true; + } + { + auto falseV = QVariant::fromValue(0); + QTest::newRow("truthtest-08") << falseV << false; + auto trueV = QVariant::fromValue(1); + QTest::newRow("truthtest-09") << trueV << true; + } + { + auto falseV = QVariant::fromValue(0); + QTest::newRow("truthtest-10") << falseV << false; + auto trueV = QVariant::fromValue(1); + QTest::newRow("truthtest-11") << trueV << true; + } + { + auto falseV = QVariant::fromValue(0); + QTest::newRow("truthtest-12") << falseV << false; + auto trueV = QVariant::fromValue(1); + QTest::newRow("truthtest-13") << trueV << true; + } + { + auto falseV = QVariant::fromValue(0); + QTest::newRow("truthtest-14") << falseV << false; + auto trueV = QVariant::fromValue(1); + QTest::newRow("truthtest-15") << trueV << true; + } + { + auto falseV = QVariant::fromValue(0); + QTest::newRow("truthtest-16") << falseV << false; + auto trueV = QVariant::fromValue(1); + QTest::newRow("truthtest-17") << trueV << true; + } + { + auto falseV = QVariant::fromValue(0); + QTest::newRow("truthtest-18") << falseV << false; + auto trueV = QVariant::fromValue(1); + QTest::newRow("truthtest-19") << trueV << true; + } + + QTest::newRow("truthtest-20") << QVariant::fromValue(QString()) << false; + QTest::newRow("truthtest-21") + << QVariant::fromValue(QStringLiteral("")) << false; + QTest::newRow("truthtest-22") + << QVariant::fromValue(QStringLiteral("false")) << true; + QTest::newRow("truthtest-23") + << QVariant::fromValue(QStringLiteral("true")) << true; + QTest::newRow("truthtest-24") + << QVariant::fromValue(QStringLiteral("anystring")) << true; + + { + QVariantList l; + QTest::newRow("truthtest-25") << QVariant::fromValue(l) << false; + l.append(1); + QTest::newRow("truthtest-26") << QVariant::fromValue(l) << true; + } + { + QVariantHash h; + QTest::newRow("truthtest-27") << QVariant::fromValue(h) << false; + h.insert(QStringLiteral("value"), 1); + QTest::newRow("truthtest-28") << QVariant::fromValue(h) << true; + } + + { + QTest::newRow("truthtest-29") + << QVariant::fromValue(nullptr) << false; + auto plainO = new QObject(this); + QTest::newRow("truthtest-30") << QVariant::fromValue(plainO) << true; + auto trueO = new QObject(this); + trueO->setProperty("__true__", true); + QTest::newRow("truthtest-31") << QVariant::fromValue(trueO) << true; + auto falseO = new QObject(this); + falseO->setProperty("__true__", false); + QTest::newRow("truthtest-32") << QVariant::fromValue(falseO) << false; + } +} + +void TestBuiltinSyntax::testTruthiness() +{ + QFETCH(QVariant, input); + QFETCH(bool, expected); + + QVERIFY(variantIsTrue(input) == expected); +} + void TestBuiltinSyntax::testRenderAfterError() {