aboutsummaryrefslogtreecommitdiffstats
path: root/testing/tests/gen_loop_else.py
diff options
context:
space:
mode:
authorLibravatar René Kijewski <kijewski@library.vetmed.fu-berlin.de>2021-11-10 18:59:22 +0100
committerLibravatar Dirkjan Ochtman <dirkjan@ochtman.nl>2021-11-11 15:35:45 +0100
commit393a0ebc368cdf6b2be1de2097092dd1959bda36 (patch)
tree52623471c4ed0f42519d17963cac7cd01157f472 /testing/tests/gen_loop_else.py
parent10b2d9c615460c9dbb241b887d0e6e17e3c001ca (diff)
downloadaskama-393a0ebc368cdf6b2be1de2097092dd1959bda36.tar.gz
askama-393a0ebc368cdf6b2be1de2097092dd1959bda36.tar.bz2
askama-393a0ebc368cdf6b2be1de2097092dd1959bda36.zip
Add exhaustive whitespace tests for for-else
Diffstat (limited to 'testing/tests/gen_loop_else.py')
-rwxr-xr-xtesting/tests/gen_loop_else.py58
1 files changed, 58 insertions, 0 deletions
diff --git a/testing/tests/gen_loop_else.py b/testing/tests/gen_loop_else.py
new file mode 100755
index 0000000..30afe09
--- /dev/null
+++ b/testing/tests/gen_loop_else.py
@@ -0,0 +1,58 @@
+#!/usr/bin/env python3
+
+print(r'''use askama::Template;
+
+#[derive(Template)]
+#[template(
+ source = "{% for v in values %}{{v}}{% else %}empty{% endfor %}",
+ ext = "txt"
+)]
+struct ForElse<'a> {
+ values: &'a [i32],
+}
+
+#[test]
+fn test_for_else() {
+ let t = ForElse { values: &[1, 2, 3] };
+ assert_eq!(t.render().unwrap(), "123");
+
+ let t = ForElse { values: &[] };
+ assert_eq!(t.render().unwrap(), "empty");
+}
+''')
+
+for i in range(2**6):
+ a = '-' if i & 2**0 else ' '
+ b = '-' if i & 2**1 else ' '
+ c = '-' if i & 2**2 else ' '
+ d = '-' if i & 2**3 else ' '
+ e = '-' if i & 2**4 else ' '
+ f = '-' if i & 2**5 else ' '
+ source = fr'a {{%{a}for v in values{b}%}}\t{{{{v}}}}\t{{%{c}else{d}%}}\nX\n{{%{e}endfor{f}%}} b'
+
+ a = '' if i & 2**0 else r' '
+ b = '' if i & 2**1 else r'\t'
+ c = '' if i & 2**2 else r'\t'
+ d = '' if i & 2**3 else r'\n'
+ e = '' if i & 2**4 else r'\n'
+ f = '' if i & 2**5 else r' '
+ some = f'a{a}{b}1{c}{f}b'
+ none = f'a{a}{d}X{e}{f}b'
+
+ print(f'''#[derive(Template)]
+#[template(
+ source = "{source}",
+ ext = "txt"
+)]
+struct LoopElseTrim{i:02}<'a> {{
+ values: &'a [i32],
+}}
+
+#[test]
+fn test_loop_else_trim{i:02}() {{
+ let t = LoopElseTrim{i:02} {{ values: &[1] }};
+ assert_eq!(t.render().unwrap(), "{some}");
+
+ let t = LoopElseTrim{i:02} {{ values: &[] }};
+ assert_eq!(t.render().unwrap(), "{none}");
+}}''')