Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better support for QVariantMap #28

Merged
merged 1 commit into from
Jul 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion templates/lib/metatype.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,8 @@ QVariant Cutelee::MetaType::lookup(const QVariant &object,

return iter.at(listIndex);
}
if (object.canConvert<QVariantHash>()) {
if (object.canConvert<QVariantHash>() ||
object.canConvert<QVariantMap>()) {

auto iter = object.value<QAssociativeIterable>();

Expand Down
3 changes: 3 additions & 0 deletions templates/lib/util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ bool Cutelee::variantIsTrue(const QVariant &variant)
case QMetaType::QVariantHash: {
return !variant.value<QVariantHash>().isEmpty();
}
case QMetaType::QVariantMap: {
return !variant.value<QVariantMap>().isEmpty();
}
}

return !getSafeString(variant).get().isEmpty();
Expand Down
28 changes: 22 additions & 6 deletions templates/tests/testbuiltins.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -377,18 +377,24 @@ void TestBuiltinSyntax::testTruthiness_data()
h.insert(QStringLiteral("value"), 1);
QTest::newRow("truthtest-28") << QVariant::fromValue(h) << true;
}
{
QVariantMap m;
QTest::newRow("truthtest-29") << QVariant::fromValue(m) << false;
m.insert(QStringLiteral("value"), 1);
QTest::newRow("truthtest-30") << QVariant::fromValue(m) << true;
}

{
QTest::newRow("truthtest-29")
QTest::newRow("truthtest-31")
<< QVariant::fromValue<QObject *>(nullptr) << false;
auto plainO = new QObject(this);
QTest::newRow("truthtest-30") << QVariant::fromValue(plainO) << true;
QTest::newRow("truthtest-32") << QVariant::fromValue(plainO) << true;
auto trueO = new QObject(this);
trueO->setProperty("__true__", true);
QTest::newRow("truthtest-31") << QVariant::fromValue(trueO) << true;
QTest::newRow("truthtest-33") << QVariant::fromValue(trueO) << true;
auto falseO = new QObject(this);
falseO->setProperty("__true__", false);
QTest::newRow("truthtest-32") << QVariant::fromValue(falseO) << false;
QTest::newRow("truthtest-34") << QVariant::fromValue(falseO) << false;
}
}

Expand Down Expand Up @@ -580,11 +586,21 @@ void TestBuiltinSyntax::testBasicSyntax_data()
hash.clear();
hash.insert(QStringLiteral("bar"), QStringLiteral("baz"));
dict.insert(QStringLiteral("foo"), hash);
QTest::newRow("basic-syntax18") << QStringLiteral("{{ foo.bar }}") << dict
QTest::newRow("basic-syntax18a") << QStringLiteral("{{ foo.bar }}") << dict
<< QStringLiteral("baz") << NoError;

// Fail silently when a variable's dictionary key isn't found
QTest::newRow("basic-syntax19")
QTest::newRow("basic-syntax19a")
<< QStringLiteral("{{ foo.spam }}") << dict << QString() << NoError;

QVariantMap map;
map.insert(QStringLiteral("bar"), QStringLiteral("baz"));
dict.insert(QStringLiteral("foo"), map);
QTest::newRow("basic-syntax18b") << QStringLiteral("{{ foo.bar }}") << dict
<< QStringLiteral("baz") << NoError;

// Fail silently when a variable's dictionary key isn't found
QTest::newRow("basic-syntax19a")
<< QStringLiteral("{{ foo.spam }}") << dict << QString() << NoError;

dict.clear();
Expand Down
27 changes: 19 additions & 8 deletions templates/tests/testdefaulttags.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1018,48 +1018,59 @@ void TestDefaultTags::testIfTag_data()
QTest::newRow("if-truthiness04")
<< QStringLiteral("{% if var %}Yes{% else %}No{% endif %}") << dict
<< QStringLiteral("Yes") << NoError;
QVariantMap map;
dict.insert(QStringLiteral("var"), map);
QTest::newRow("if-truthiness05")
<< QStringLiteral("{% if var %}Yes{% else %}No{% endif %}") << dict
<< QStringLiteral("No") << NoError;
map.insert(QStringLiteral("foo"), QStringLiteral("bar"));
dict.insert(QStringLiteral("var"), map);
QTest::newRow("if-truthiness06")
<< QStringLiteral("{% if var %}Yes{% else %}No{% endif %}") << dict
<< QStringLiteral("Yes") << NoError;


QVariant var;
dict.insert(QStringLiteral("var"), var);
QTest::newRow("if-truthiness05")
QTest::newRow("if-truthiness07")
<< QStringLiteral("{% if var %}Yes{% else %}No{% endif %}") << dict
<< QStringLiteral("No") << NoError;
var = QStringLiteral("foo");
dict.insert(QStringLiteral("var"), var);
QTest::newRow("if-truthiness06")
QTest::newRow("if-truthiness08")
<< QStringLiteral("{% if var %}Yes{% else %}No{% endif %}") << dict
<< QStringLiteral("Yes") << NoError;

QString str;
dict.insert(QStringLiteral("var"), str);
QTest::newRow("if-truthiness07")
QTest::newRow("if-truthiness09")
<< QStringLiteral("{% if var %}Yes{% else %}No{% endif %}") << dict
<< QStringLiteral("No") << NoError;
str = QStringLiteral("foo");
dict.insert(QStringLiteral("var"), str);
QTest::newRow("if-truthiness08")
QTest::newRow("if-truthiness10")
<< QStringLiteral("{% if var %}Yes{% else %}No{% endif %}") << dict
<< QStringLiteral("Yes") << NoError;

auto i = 0;
dict.insert(QStringLiteral("var"), i);
QTest::newRow("if-truthiness07")
QTest::newRow("if-truthiness11")
<< QStringLiteral("{% if var %}Yes{% else %}No{% endif %}") << dict
<< QStringLiteral("No") << NoError;
i = 7;
dict.insert(QStringLiteral("var"), i);
QTest::newRow("if-truthiness08")
QTest::newRow("if-truthiness12")
<< QStringLiteral("{% if var %}Yes{% else %}No{% endif %}") << dict
<< QStringLiteral("Yes") << NoError;

auto r = 0.0;
dict.insert(QStringLiteral("var"), r);
QTest::newRow("if-truthiness09")
QTest::newRow("if-truthiness13")
<< QStringLiteral("{% if var %}Yes{% else %}No{% endif %}") << dict
<< QStringLiteral("No") << NoError;
r = 7.1;
dict.insert(QStringLiteral("var"), r);
QTest::newRow("if-truthiness10")
QTest::newRow("if-truthiness14")
<< QStringLiteral("{% if var %}Yes{% else %}No{% endif %}") << dict
<< QStringLiteral("Yes") << NoError;

Expand Down
Loading