aboutsummaryrefslogtreecommitdiffstats
path: root/askama_parser/src/tests.rs
diff options
context:
space:
mode:
authorLibravatar René Kijewski <rene.kijewski@fu-berlin.de>2023-07-31 20:58:23 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-07-31 21:29:35 +0200
commit985eb8955f36e909d18f7ea6eaf4c748e7afd17a (patch)
tree01aca3e735d2c5c698495ce0b3a383911f2563f5 /askama_parser/src/tests.rs
parent107bdfdd7658919691948f629adf2254cd6aa367 (diff)
downloadaskama-985eb8955f36e909d18f7ea6eaf4c748e7afd17a.tar.gz
askama-985eb8955f36e909d18f7ea6eaf4c748e7afd17a.tar.bz2
askama-985eb8955f36e909d18f7ea6eaf4c748e7afd17a.zip
Fix parsing arrays
This change * allows using empty arrays `[]` in expessions, * adds a cut when the leading `[` was encountered, and * fixes the interaction between arrays and boolean OR. IMO the restriction that you couldn't use empty arrays is not needed. The missing cut made error messages slictly worse if you forget to add the closing `]`. Filter expressions must not have white spaces before the pipe `|`. The white space is used to tell a filter expressions, and `std::ops::Or` apart.
Diffstat (limited to 'askama_parser/src/tests.rs')
-rw-r--r--askama_parser/src/tests.rs83
1 files changed, 83 insertions, 0 deletions
diff --git a/askama_parser/src/tests.rs b/askama_parser/src/tests.rs
index 2beeb38..61aa279 100644
--- a/askama_parser/src/tests.rs
+++ b/askama_parser/src/tests.rs
@@ -761,3 +761,86 @@ fn test_missing_space_after_kw() {
"unable to parse template:\n\n\"{%leta=b%}\""
));
}
+
+#[test]
+fn test_parse_array() {
+ let syntax = Syntax::default();
+ assert_eq!(
+ Ast::from_str("{{ [] }}", &syntax).unwrap().nodes,
+ vec![Node::Expr(Ws(None, None), Expr::Array(vec![]))],
+ );
+ assert_eq!(
+ Ast::from_str("{{ [1] }}", &syntax).unwrap().nodes,
+ vec![Node::Expr(
+ Ws(None, None),
+ Expr::Array(vec![Expr::NumLit("1")])
+ )],
+ );
+ assert_eq!(
+ Ast::from_str("{{ [ 1] }}", &syntax).unwrap().nodes,
+ vec![Node::Expr(
+ Ws(None, None),
+ Expr::Array(vec![Expr::NumLit("1")])
+ )],
+ );
+ assert_eq!(
+ Ast::from_str("{{ [1 ] }}", &syntax).unwrap().nodes,
+ vec![Node::Expr(
+ Ws(None, None),
+ Expr::Array(vec![Expr::NumLit("1")])
+ )],
+ );
+ assert_eq!(
+ Ast::from_str("{{ [1,2] }}", &syntax).unwrap().nodes,
+ vec![Node::Expr(
+ Ws(None, None),
+ Expr::Array(vec![Expr::NumLit("1"), Expr::NumLit("2")])
+ )],
+ );
+ assert_eq!(
+ Ast::from_str("{{ [1 ,2] }}", &syntax).unwrap().nodes,
+ vec![Node::Expr(
+ Ws(None, None),
+ Expr::Array(vec![Expr::NumLit("1"), Expr::NumLit("2")])
+ )],
+ );
+ assert_eq!(
+ Ast::from_str("{{ [1, 2] }}", &syntax).unwrap().nodes,
+ vec![Node::Expr(
+ Ws(None, None),
+ Expr::Array(vec![Expr::NumLit("1"), Expr::NumLit("2")])
+ )],
+ );
+ assert_eq!(
+ Ast::from_str("{{ [1,2 ] }}", &syntax).unwrap().nodes,
+ vec![Node::Expr(
+ Ws(None, None),
+ Expr::Array(vec![Expr::NumLit("1"), Expr::NumLit("2")])
+ )],
+ );
+ assert_eq!(
+ Ast::from_str("{{ []|foo }}", &syntax).unwrap().nodes,
+ vec![Node::Expr(
+ Ws(None, None),
+ Expr::Filter("foo", vec![Expr::Array(vec![])])
+ )],
+ );
+ assert_eq!(
+ Ast::from_str("{{ []| foo }}", &syntax).unwrap().nodes,
+ vec![Node::Expr(
+ Ws(None, None),
+ Expr::Filter("foo", vec![Expr::Array(vec![])])
+ )],
+ );
+ assert_eq!(
+ Ast::from_str("{{ [] |foo }}", &syntax).unwrap().nodes,
+ vec![Node::Expr(
+ Ws(None, None),
+ Expr::BinOp(
+ "|",
+ Box::new(Expr::Array(vec![])),
+ Box::new(Expr::Var("foo"))
+ ),
+ )],
+ );
+}