Skip to content

Commit 33005c6

Browse files
authored
Merge pull request #35 from google/revert-32-nones
Revert "Explode on operations w/ Nones"
2 parents ba4df6e + 30a1000 commit 33005c6

File tree

2 files changed

+15
-25
lines changed

2 files changed

+15
-25
lines changed

include/minja/minja.hpp

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,11 +1270,6 @@ class BinaryOpExpr : public Expression {
12701270
}
12711271

12721272
auto r = right->evaluate(context);
1273-
if (op != Op::Eq && op != Op::Ne) {
1274-
if (r.is_null() || (l.is_null() && (op != Op::In && op != Op::NotIn))) {
1275-
throw std::runtime_error("unsupported operand type(s)");
1276-
}
1277-
}
12781273
switch (op) {
12791274
case Op::StrConcat: return l.to_str() + r.to_str();
12801275
case Op::Add: return l + r;
@@ -2152,11 +2147,11 @@ class Parser {
21522147
}
21532148

21542149
std::runtime_error unexpected(const TemplateToken & token) const {
2155-
return std::runtime_error("Encountered unknown tag '" + TemplateToken::typeToString(token.type) + "'"
2150+
return std::runtime_error("Unexpected " + TemplateToken::typeToString(token.type)
21562151
+ error_location_suffix(*template_str, token.location.pos));
21572152
}
21582153
std::runtime_error unterminated(const TemplateToken & token) const {
2159-
return std::runtime_error("Unexpected end of template. Jinja was looking for the following tags: '" + TemplateToken::typeToString(token.type) + "'"
2154+
return std::runtime_error("Unterminated " + TemplateToken::typeToString(token.type)
21602155
+ error_location_suffix(*template_str, token.location.pos));
21612156
}
21622157

tests/test-syntax.cpp

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -484,33 +484,28 @@ TEST(SyntaxTest, SimpleCases) {
484484
"",
485485
render("{% if 1 %}{% elif 1 %}{% else %}{% endif %}", {}, {}));
486486

487-
EXPECT_THAT([]() { render(R"({{ 'a' + None }})", {}, {}); }, testing::Throws<std::runtime_error>());
488-
EXPECT_THAT([]() { render(R"({{ None + 'b' }})", {}, {}); }, testing::Throws<std::runtime_error>());
489-
EXPECT_THAT([]() { render(R"({{ 'a' in None }})", {}, {}); }, testing::Throws<std::runtime_error>());
490-
EXPECT_EQ(
491-
"False,True,False",
492-
render(R"({{ None in [] }},{{ None == None }},{{ None != None }})", {}, {}));
493487

494488
if (!getenv("USE_JINJA2")) {
495489
// TODO: capture stderr from jinja2 and test these.
490+
496491
EXPECT_THAT([]() { render("{%- set _ = [].pop() -%}", {}, {}); }, ThrowsWithSubstr("pop from empty list"));
497492
EXPECT_THAT([]() { render("{%- set _ = {}.pop() -%}", {}, {}); }, ThrowsWithSubstr("pop"));
498493
EXPECT_THAT([]() { render("{%- set _ = {}.pop('foooo') -%}", {}, {}); }, ThrowsWithSubstr("foooo"));
499494

500-
EXPECT_THAT([]() { render("{% else %}", {}, {}); }, ThrowsWithSubstr("Encountered unknown tag 'else'"));
495+
EXPECT_THAT([]() { render("{% else %}", {}, {}); }, ThrowsWithSubstr("Unexpected else"));
501496

502-
EXPECT_THAT([]() { render("{% else %}", {}, {}); }, ThrowsWithSubstr("Encountered unknown tag 'else'"));
503-
EXPECT_THAT([]() { render("{% endif %}", {}, {}); }, ThrowsWithSubstr("Encountered unknown tag 'endif'"));
504-
EXPECT_THAT([]() { render("{% elif 1 %}", {}, {}); }, ThrowsWithSubstr("Encountered unknown tag 'elif'"));
505-
EXPECT_THAT([]() { render("{% endfor %}", {}, {}); }, ThrowsWithSubstr("Encountered unknown tag 'endfor'"));
506-
EXPECT_THAT([]() { render("{% endfilter %}", {}, {}); }, ThrowsWithSubstr("Encountered unknown tag 'endfilter'"));
497+
EXPECT_THAT([]() { render("{% else %}", {}, {}); }, ThrowsWithSubstr("Unexpected else"));
498+
EXPECT_THAT([]() { render("{% endif %}", {}, {}); }, ThrowsWithSubstr("Unexpected endif"));
499+
EXPECT_THAT([]() { render("{% elif 1 %}", {}, {}); }, ThrowsWithSubstr("Unexpected elif"));
500+
EXPECT_THAT([]() { render("{% endfor %}", {}, {}); }, ThrowsWithSubstr("Unexpected endfor"));
501+
EXPECT_THAT([]() { render("{% endfilter %}", {}, {}); }, ThrowsWithSubstr("Unexpected endfilter"));
507502

508-
EXPECT_THAT([]() { render("{% if 1 %}", {}, {}); }, ThrowsWithSubstr("Unexpected end of template. Jinja was looking for the following tags: 'if'"));
509-
EXPECT_THAT([]() { render("{% for x in 1 %}", {}, {}); }, ThrowsWithSubstr("Unexpected end of template. Jinja was looking for the following tags: 'for'"));
510-
EXPECT_THAT([]() { render("{% generation %}", {}, {}); }, ThrowsWithSubstr("Unexpected end of template. Jinja was looking for the following tags: 'generation'"));
511-
EXPECT_THAT([]() { render("{% if 1 %}{% else %}", {}, {}); }, ThrowsWithSubstr("Unexpected end of template. Jinja was looking for the following tags: 'if'"));
512-
EXPECT_THAT([]() { render("{% if 1 %}{% else %}{% elif 1 %}{% endif %}", {}, {}); }, ThrowsWithSubstr("Unexpected end of template. Jinja was looking for the following tags: 'if'"));
513-
EXPECT_THAT([]() { render("{% filter trim %}", {}, {}); }, ThrowsWithSubstr("Unexpected end of template. Jinja was looking for the following tags: 'filter'"));
503+
EXPECT_THAT([]() { render("{% if 1 %}", {}, {}); }, ThrowsWithSubstr("Unterminated if"));
504+
EXPECT_THAT([]() { render("{% for x in 1 %}", {}, {}); }, ThrowsWithSubstr("Unterminated for"));
505+
EXPECT_THAT([]() { render("{% generation %}", {}, {}); }, ThrowsWithSubstr("Unterminated generation"));
506+
EXPECT_THAT([]() { render("{% if 1 %}{% else %}", {}, {}); }, ThrowsWithSubstr("Unterminated if"));
507+
EXPECT_THAT([]() { render("{% if 1 %}{% else %}{% elif 1 %}{% endif %}", {}, {}); }, ThrowsWithSubstr("Unterminated if"));
508+
EXPECT_THAT([]() { render("{% filter trim %}", {}, {}); }, ThrowsWithSubstr("Unterminated filter"));
514509
}
515510

516511
EXPECT_EQ(

0 commit comments

Comments
 (0)