Permalink
Browse files

Fix string and enum comparison in the if tag.

Use the Grantlee::equals method to compare.
1 parent 337023d commit 1e79e4c29f5a12e4cd24dd9c913d99e7267fac24 @steveire committed Apr 14, 2016
Showing with 32 additions and 2 deletions.
  1. +2 −2 templates/defaulttags/if_p.h
  2. +30 −0 templates/tests/testdefaulttags.cpp
@@ -305,9 +305,9 @@ QVariant IfToken::evaluate(Context *c) const
case NotInCode:
return !contains(mArgs.first->evaluate(c), mArgs.second->evaluate(c));
case EqCode:
- return mArgs.first->evaluate(c) == mArgs.second->evaluate(c);
+ return Grantlee::equals(mArgs.first->evaluate(c), mArgs.second->evaluate(c));
case NeqCode:
- return mArgs.first->evaluate(c) != mArgs.second->evaluate(c);
+ return !Grantlee::equals(mArgs.first->evaluate(c), mArgs.second->evaluate(c));
case GtCode:
return mArgs.first->evaluate(c) > mArgs.second->evaluate(c);
case GteCode:
@@ -60,6 +60,17 @@ class FakeTemplateLoader : public Grantlee::InMemoryTemplateLoader
QStringList m_existingMedia;
};
+class Zoo : public QObject
+{
+ Q_OBJECT
+public:
+ Zoo(QObject *parent = 0) : QObject(parent) {}
+
+ enum Animals { Lions, Tigers, Bears };
+
+ Q_ENUMS(Animals)
+};
+
using namespace Grantlee;
class TestDefaultTags : public CoverageObject
@@ -444,6 +455,25 @@ void TestDefaultTags::testIfTag_data()
<< QStringLiteral("{% if foo == \'\' %}yes{% else %}no{% endif %}")
<< dict << QStringLiteral("no") << NoError;
+ dict.clear();
+ dict.insert(QStringLiteral("foostring"), QStringLiteral("foo"));
+ QTest::newRow("if-tag-eq06")
+ << QStringLiteral("{% if foostring == \'foo\' %}yes{% else %}no{% endif %}")
+ << dict << QStringLiteral("yes") << NoError;
+
+ dict.clear();
+ dict.insert(QStringLiteral("foostring"), QStringLiteral("foo"));
+ QTest::newRow("if-tag-eq06")
+ << QStringLiteral("{% if foostring == \'foo\' %}yes{% else %}no{% endif %}")
+ << dict << QStringLiteral("yes") << NoError;
+
+ dict.clear();
+ dict.insert(QStringLiteral("zoo"), QVariant::fromValue(new Zoo(this)));
+ dict.insert(QStringLiteral("tigersEnum"), QVariant::fromValue<int>(Zoo::Tigers));
+ QTest::newRow("if-tag-eq07")
+ << QStringLiteral("{% if tigersEnum == zoo.Tigers %}yes{% else %}no{% endif %}")
+ << dict << QStringLiteral("yes") << NoError;
+
// Comparison
dict.clear();

0 comments on commit 1e79e4c

Please sign in to comment.