aboutsummaryrefslogtreecommitdiffstats
path: root/askama_parser/src/tests.rs
diff options
context:
space:
mode:
authorLibravatar René Kijewski <rene.kijewski@fu-berlin.de>2023-08-01 11:30:06 +0200
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2023-08-01 13:04:41 +0200
commit4315773676527c94646315ac2a3475fd6eb2925a (patch)
treef9be7d81305bfc0210249db28c822b6df6ce25f8 /askama_parser/src/tests.rs
parent32cd2825c4e5be9d0f160f9d112a98dbba24b29d (diff)
downloadaskama-4315773676527c94646315ac2a3475fd6eb2925a.tar.gz
askama-4315773676527c94646315ac2a3475fd6eb2925a.tar.bz2
askama-4315773676527c94646315ac2a3475fd6eb2925a.zip
parser: add type for `Node::Comment`
Diffstat (limited to '')
-rw-r--r--askama_parser/src/tests.rs155
1 files changed, 52 insertions, 103 deletions
diff --git a/askama_parser/src/tests.rs b/askama_parser/src/tests.rs
index 9f0fbd1..dde127b 100644
--- a/askama_parser/src/tests.rs
+++ b/askama_parser/src/tests.rs
@@ -521,112 +521,61 @@ fn test_odd_calls() {
#[test]
fn test_parse_comments() {
- let s = &Syntax::default();
+ fn one_comment_ws(source: &str, ws: Ws) {
+ let s = &Syntax::default();
+ let mut nodes = Ast::from_str(source, s).unwrap().nodes;
+ assert_eq!(nodes.len(), 1, "expected to parse one node");
+ match nodes.pop().unwrap() {
+ Node::Comment(comment) => assert_eq!(comment.ws, ws),
+ node => panic!("expected a comment not, but parsed {:?}", node),
+ }
+ }
- assert_eq!(
- Ast::from_str("{##}", s).unwrap().nodes,
- vec![Node::Comment(Ws(None, None))],
- );
- assert_eq!(
- Ast::from_str("{#- #}", s).unwrap().nodes,
- vec![Node::Comment(Ws(Some(Whitespace::Suppress), None))],
- );
- assert_eq!(
- Ast::from_str("{# -#}", s).unwrap().nodes,
- vec![Node::Comment(Ws(None, Some(Whitespace::Suppress)))],
- );
- assert_eq!(
- Ast::from_str("{#--#}", s).unwrap().nodes,
- vec![Node::Comment(Ws(
- Some(Whitespace::Suppress),
- Some(Whitespace::Suppress)
- ))],
- );
- assert_eq!(
- Ast::from_str("{#- foo\n bar -#}", s).unwrap().nodes,
- vec![Node::Comment(Ws(
- Some(Whitespace::Suppress),
- Some(Whitespace::Suppress)
- ))],
- );
- assert_eq!(
- Ast::from_str("{#- foo\n {#- bar\n -#} baz -#}", s)
- .unwrap()
- .nodes,
- vec![Node::Comment(Ws(
- Some(Whitespace::Suppress),
- Some(Whitespace::Suppress)
- ))],
- );
- assert_eq!(
- Ast::from_str("{#+ #}", s).unwrap().nodes,
- vec![Node::Comment(Ws(Some(Whitespace::Preserve), None))],
- );
- assert_eq!(
- Ast::from_str("{# +#}", s).unwrap().nodes,
- vec![Node::Comment(Ws(None, Some(Whitespace::Preserve)))],
- );
- assert_eq!(
- Ast::from_str("{#++#}", s).unwrap().nodes,
- vec![Node::Comment(Ws(
- Some(Whitespace::Preserve),
- Some(Whitespace::Preserve)
- ))],
- );
- assert_eq!(
- Ast::from_str("{#+ foo\n bar +#}", s).unwrap().nodes,
- vec![Node::Comment(Ws(
- Some(Whitespace::Preserve),
- Some(Whitespace::Preserve)
- ))],
- );
- assert_eq!(
- Ast::from_str("{#+ foo\n {#+ bar\n +#} baz -+#}", s)
- .unwrap()
- .nodes,
- vec![Node::Comment(Ws(
- Some(Whitespace::Preserve),
- Some(Whitespace::Preserve)
- ))],
- );
- assert_eq!(
- Ast::from_str("{#~ #}", s).unwrap().nodes,
- vec![Node::Comment(Ws(Some(Whitespace::Minimize), None))],
- );
- assert_eq!(
- Ast::from_str("{# ~#}", s).unwrap().nodes,
- vec![Node::Comment(Ws(None, Some(Whitespace::Minimize)))],
- );
- assert_eq!(
- Ast::from_str("{#~~#}", s).unwrap().nodes,
- vec![Node::Comment(Ws(
- Some(Whitespace::Minimize),
- Some(Whitespace::Minimize)
- ))],
- );
- assert_eq!(
- Ast::from_str("{#~ foo\n bar ~#}", s).unwrap().nodes,
- vec![Node::Comment(Ws(
- Some(Whitespace::Minimize),
- Some(Whitespace::Minimize)
- ))],
- );
- assert_eq!(
- Ast::from_str("{#~ foo\n {#~ bar\n ~#} baz -~#}", s)
- .unwrap()
- .nodes,
- vec![Node::Comment(Ws(
- Some(Whitespace::Minimize),
- Some(Whitespace::Minimize)
- ))],
+ one_comment_ws("{##}", Ws(None, None));
+ one_comment_ws("{#- #}", Ws(Some(Whitespace::Suppress), None));
+ one_comment_ws("{# -#}", Ws(None, Some(Whitespace::Suppress)));
+ one_comment_ws(
+ "{#--#}",
+ Ws(Some(Whitespace::Suppress), Some(Whitespace::Suppress)),
+ );
+ one_comment_ws(
+ "{#- foo\n bar -#}",
+ Ws(Some(Whitespace::Suppress), Some(Whitespace::Suppress)),
+ );
+ one_comment_ws(
+ "{#- foo\n {#- bar\n -#} baz -#}",
+ Ws(Some(Whitespace::Suppress), Some(Whitespace::Suppress)),
+ );
+ one_comment_ws("{#+ #}", Ws(Some(Whitespace::Preserve), None));
+ one_comment_ws("{# +#}", Ws(None, Some(Whitespace::Preserve)));
+ one_comment_ws(
+ "{#++#}",
+ Ws(Some(Whitespace::Preserve), Some(Whitespace::Preserve)),
+ );
+ one_comment_ws(
+ "{#+ foo\n bar +#}",
+ Ws(Some(Whitespace::Preserve), Some(Whitespace::Preserve)),
+ );
+ one_comment_ws(
+ "{#+ foo\n {#+ bar\n +#} baz -+#}",
+ Ws(Some(Whitespace::Preserve), Some(Whitespace::Preserve)),
+ );
+ one_comment_ws("{#~ #}", Ws(Some(Whitespace::Minimize), None));
+ one_comment_ws("{# ~#}", Ws(None, Some(Whitespace::Minimize)));
+ one_comment_ws(
+ "{#~~#}",
+ Ws(Some(Whitespace::Minimize), Some(Whitespace::Minimize)),
+ );
+ one_comment_ws(
+ "{#~ foo\n bar ~#}",
+ Ws(Some(Whitespace::Minimize), Some(Whitespace::Minimize)),
+ );
+ one_comment_ws(
+ "{#~ foo\n {#~ bar\n ~#} baz -~#}",
+ Ws(Some(Whitespace::Minimize), Some(Whitespace::Minimize)),
);
- assert_eq!(
- Ast::from_str("{# foo {# bar #} {# {# baz #} qux #} #}", s)
- .unwrap()
- .nodes,
- vec![Node::Comment(Ws(None, None))],
- );
+ one_comment_ws("{# foo {# bar #} {# {# baz #} qux #} #}", Ws(None, None));
}
#[test]