Skip to content

Commit

Permalink
Fix a bug with parenless 'is' tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mitsuhiko committed Oct 31, 2024
1 parent 09f868c commit 48cd051
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 11 deletions.
1 change: 0 additions & 1 deletion minijinja/src/compiler/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -854,7 +854,6 @@ impl<'s> Tokenizer<'s> {
Some(b'*') => Some(Token::Mul),
Some(b'/') => Some(Token::Div),
Some(b'%') => Some(Token::Mod),
Some(b'!') => Some(Token::Bang),
Some(b'.') => Some(Token::Dot),
Some(b',') => Some(Token::Comma),
Some(b':') => Some(Token::Colon),
Expand Down
23 changes: 23 additions & 0 deletions minijinja/src/compiler/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -460,6 +460,29 @@ impl<'a> Parser<'a> {
expect_token!(self, Token::Ident(name) => name, "identifier");
let args = if matches_token!(self, Token::ParenOpen) {
ok!(self.parse_args())
} else if matches_token!(
self,
Token::Ident(_)
| Token::Str(_)
| Token::String(_)
| Token::Int(_)
| Token::Int128(_)
| Token::Float(_)
| Token::Plus
| Token::Minus
| Token::BracketOpen
| Token::BraceOpen
) && !matches_token!(
self,
Token::Ident("and")
| Token::Ident("or")
| Token::Ident("else")
| Token::Ident("is")
) {
let span = self.stream.current_span();
let mut expr = ok!(self.parse_unary_only());
expr = ok!(self.parse_postfix(expr, span));
vec![expr]
} else {
Vec::new()
};
Expand Down
3 changes: 0 additions & 3 deletions minijinja/src/compiler/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ pub enum Token<'a> {
Pow,
/// A mod (`%`) operator.
Mod,
/// The bang (`!`) operator.
Bang,
/// A dot operator (`.`)
Dot,
/// The comma operator (`,`)
Expand Down Expand Up @@ -103,7 +101,6 @@ impl<'a> fmt::Display for Token<'a> {
Token::FloorDiv => f.write_str("`//`"),
Token::Pow => f.write_str("`**`"),
Token::Mod => f.write_str("`%`"),
Token::Bang => f.write_str("`!`"),
Token::Dot => f.write_str("`.`"),
Token::Comma => f.write_str("`,`"),
Token::Colon => f.write_str("`:`"),
Expand Down
9 changes: 8 additions & 1 deletion minijinja/tests/inputs/tests.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,9 @@ map: {{ map is mapping }}
string: {{ string is string }}
not-string: {{ mapping is string }}
starts-with-a: {{ string is startingwith('a') }}
starts-with-a-noparen: {{ string is startingwith 'a' }}
ends-with-ha: {{ string is endingwith('ha') }}
ends-with-ha-noparen: {{ string is endingwith 'ha' }}
not-safe: {{ "foo" is safe }}
safe: {{ "foo"|escape is safe }}
is-true: {{ true is true }} | {{ 42 is true }}
Expand All @@ -47,8 +49,13 @@ is-filter: {{ 'escape' is filter }} | {{ 'unknown-filter' is filter }}
is-test: {{ 'safe' is test }} | {{ 'unknown-test' is test }}
is-boolean: {{ true is boolean }} | {{ 42 is boolean }}
is-divisibleby: {{ 42 is divisibleby(2) }} | {{ 41 is divisibleby(2) }}
is-divisibleby-noparen: {{ 42 is divisibleby(2) }} | {{ 41 is divisibleby 2 }}
is-lower: {{ "foo" is lower }} | {{ "FOO" is lower }}
is-upper: {{ "foo" is upper }} | {{ "FOO" is upper }}
seq-same-as: {{ [1, 2, 3] is sameas([1, 2, 3]) }}
seq-same-as-noparen: {{ [1, 2, 3] is sameas [1, 2, 3] }}
const-same-as: {{ true is sameas(true) }}
int-same-as: {{ 1 is sameas(1.0) }}
const-same-as-noparen: {{ true is sameas true }}
int-same-as: {{ 1 is sameas(1.0) }}
int-same-as-noparen: {{ 1 is sameas 1.0 }}
neg-int-same-as-noparen: {{ -1 is sameas -1 }}
2 changes: 1 addition & 1 deletion minijinja/tests/lexer-inputs/operators.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{}
---
{{ (!a + b) * (c - d) / e % f // g ~ h }}
{{ (a + b) * (c - d) / e % f // g ~ h }}
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
---
source: minijinja/tests/test_lexer.rs
description: "{{ (!a + b) * (c - d) / e % f // g ~ h }}"
description: "{{ (a + b) * (c - d) / e % f // g ~ h }}"
input_file: minijinja/tests/lexer-inputs/operators.txt
---
VariableStart
"{{"
ParenOpen
"("
Bang
"!"
Ident("a")
"a"
Plus
Expand Down Expand Up @@ -47,4 +45,3 @@ Ident("h")
"h"
VariableEnd
"}}"

9 changes: 8 additions & 1 deletion minijinja/tests/snapshots/test_templates__vm@tests.txt.snap
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
---
source: minijinja/tests/test_templates.rs
description: "even: {{ two is even }}\nodd: {{ two is odd }}\nundefined: {{ two is undefined }}\ndefined: {{ two is defined }}\nundefined2: {{ ohwell is undefined }}\ndefined2: {{ ohwell is defined }}\nnone: {{ none is none }}\nnot-none: {{ 42 is not none }}\nnumber-int: {{ two is number }}\nnumber-float: {{ two_dot_two is number }}\ninteger-int: {{ 42 is integer }}\ninteger-float: {{ 42.0 is integer }}\nfloat-int: {{ 42 is float }}\nfloat-float: {{ 42.0 is float }}\nnot-seq: {{ two is sequence }}\nseq: {{ seq is sequence }}\nreverse-not-seq: {{ seq|reverse is sequence }}\niterable: {{ seq is iterable }}\niterable-reverse: {{ seq|reverse is iterable }}\nstring-iterable: {{ string is iterable }}\nnot-iterable: {{ two is iterable }}\nnot-map: {{ two is mapping }}\nmap: {{ map is mapping }}\nstring: {{ string is string }}\nnot-string: {{ mapping is string }}\nstarts-with-a: {{ string is startingwith('a') }}\nends-with-ha: {{ string is endingwith('ha') }}\nnot-safe: {{ \"foo\" is safe }}\nsafe: {{ \"foo\"|escape is safe }}\nis-true: {{ true is true }} | {{ 42 is true }}\nis-false: {{ false is false }} | {{ 0 is false }}\nis-filter: {{ 'escape' is filter }} | {{ 'unknown-filter' is filter }}\nis-test: {{ 'safe' is test }} | {{ 'unknown-test' is test }}\nis-boolean: {{ true is boolean }} | {{ 42 is boolean }}\nis-divisibleby: {{ 42 is divisibleby(2) }} | {{ 41 is divisibleby(2) }}\nis-lower: {{ \"foo\" is lower }} | {{ \"FOO\" is lower }}\nis-upper: {{ \"foo\" is upper }} | {{ \"FOO\" is upper }}\nseq-same-as: {{ [1, 2, 3] is sameas([1, 2, 3]) }}\nconst-same-as: {{ true is sameas(true) }}\nint-same-as: {{ 1 is sameas(1.0) }}"
description: "even: {{ two is even }}\nodd: {{ two is odd }}\nundefined: {{ two is undefined }}\ndefined: {{ two is defined }}\nundefined2: {{ ohwell is undefined }}\ndefined2: {{ ohwell is defined }}\nnone: {{ none is none }}\nnot-none: {{ 42 is not none }}\nnumber-int: {{ two is number }}\nnumber-float: {{ two_dot_two is number }}\ninteger-int: {{ 42 is integer }}\ninteger-float: {{ 42.0 is integer }}\nfloat-int: {{ 42 is float }}\nfloat-float: {{ 42.0 is float }}\nnot-seq: {{ two is sequence }}\nseq: {{ seq is sequence }}\nreverse-not-seq: {{ seq|reverse is sequence }}\niterable: {{ seq is iterable }}\niterable-reverse: {{ seq|reverse is iterable }}\nstring-iterable: {{ string is iterable }}\nnot-iterable: {{ two is iterable }}\nnot-map: {{ two is mapping }}\nmap: {{ map is mapping }}\nstring: {{ string is string }}\nnot-string: {{ mapping is string }}\nstarts-with-a: {{ string is startingwith('a') }}\nstarts-with-a-noparen: {{ string is startingwith 'a' }}\nends-with-ha: {{ string is endingwith('ha') }}\nends-with-ha-noparen: {{ string is endingwith 'ha' }}\nnot-safe: {{ \"foo\" is safe }}\nsafe: {{ \"foo\"|escape is safe }}\nis-true: {{ true is true }} | {{ 42 is true }}\nis-false: {{ false is false }} | {{ 0 is false }}\nis-filter: {{ 'escape' is filter }} | {{ 'unknown-filter' is filter }}\nis-test: {{ 'safe' is test }} | {{ 'unknown-test' is test }}\nis-boolean: {{ true is boolean }} | {{ 42 is boolean }}\nis-divisibleby: {{ 42 is divisibleby(2) }} | {{ 41 is divisibleby(2) }}\nis-divisibleby-noparen: {{ 42 is divisibleby(2) }} | {{ 41 is divisibleby 2 }}\nis-lower: {{ \"foo\" is lower }} | {{ \"FOO\" is lower }}\nis-upper: {{ \"foo\" is upper }} | {{ \"FOO\" is upper }}\nseq-same-as: {{ [1, 2, 3] is sameas([1, 2, 3]) }}\nseq-same-as-noparen: {{ [1, 2, 3] is sameas [1, 2, 3] }}\nconst-same-as: {{ true is sameas(true) }}\nconst-same-as-noparen: {{ true is sameas true }}\nint-same-as: {{ 1 is sameas(1.0) }}\nint-same-as-noparen: {{ 1 is sameas 1.0 }}\nneg-int-same-as-noparen: {{ -1 is sameas -1 }}"
info:
two: 2
two_dot_two: 2.2
Expand Down Expand Up @@ -39,7 +39,9 @@ map: true
string: true
not-string: false
starts-with-a: true
starts-with-a-noparen: true
ends-with-ha: true
ends-with-ha-noparen: true
not-safe: false
safe: true
is-true: true | false
Expand All @@ -48,8 +50,13 @@ is-filter: true | false
is-test: true | false
is-boolean: true | false
is-divisibleby: true | false
is-divisibleby-noparen: true | false
is-lower: true | false
is-upper: false | true
seq-same-as: false
seq-same-as-noparen: false
const-same-as: true
const-same-as-noparen: true
int-same-as: false
int-same-as-noparen: false
neg-int-same-as-noparen: true

0 comments on commit 48cd051

Please sign in to comment.