Repository: djc/askama (rust)

Issue:
Whitespace issue with if conditions
Askama seems to be including whitespace from inside an if condition even when the condition is not fulfilled. Here is my template file:

```jinja
{%- if some_condition -%}
# WARNING

{% endif -%}

# FILE START
```

What I expected is:

- When `some_condition` is `true`:

  ```
  # WARNING
  
  # FILE START
  ```

- When `some_condition` is `false`:

  ```
  # FILE START
  ```

What I got is:

- When `some_condition` is `true`: same as expected output
- When `some_condition` is `false` (notice the blank lines before `FILE START`):

  ```
  
  
  # FILE START
  ```

Is this a bug? If not, is there a way to get the behaviour I described?

Pull request:
Fixed whitespace issue when generating if statement
Fixed #377
Closes #378

I've fixed the issue of whitespace being generated incorrectly in if statements.

I've tested all permutations from 1 to 4 branches. For which I created a Python script that generated all the permutations. I included the script as well, as it allows you to generate and test an arbitrary number of branches. (_I'll use it to check `match`, `macro`, along with nested constructs later._)

I included the test cases for 1 and 2 branches.


Patch:
diff --git a/askama_shared/src/generator.rs b/askama_shared/src/generator.rs
index ad086a3..6a185c1 100644
--- a/askama_shared/src/generator.rs
+++ b/askama_shared/src/generator.rs
@@ -505,8 +505,9 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
         let mut has_else = false;
         for (i, &(cws, ref cond, ref nodes)) in conds.iter().enumerate() {
             self.handle_ws(cws);
-            if arm_sizes.is_empty() {
-                flushed += self.write_buf_writable(buf)?;
+            flushed += self.write_buf_writable(buf)?;
+            if i > 0 {
+                self.locals.pop();
             }
 
             let mut arm_size = 0;
@@ -539,14 +540,14 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
             self.locals.push();
 
             arm_size += self.handle(ctx, nodes, buf, AstLevel::Nested)?;
-            arm_size += self.write_buf_writable(buf)?;
             arm_sizes.push(arm_size);
-
-            self.locals.pop();
         }
         self.handle_ws(ws);
+        flushed += self.write_buf_writable(buf)?;
         buf.writeln("}")?;
 
+        self.locals.pop();
+
         if !has_else {
             arm_sizes.push(0);
         }


Test patch:
diff --git a/testing/tests/gen_ws_tests.py b/testing/tests/gen_ws_tests.py
new file mode 100644
index 0000000..d8c809c
--- /dev/null
+++ b/testing/tests/gen_ws_tests.py
@@ -0,0 +1,112 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+from itertools import product, chain, zip_longest, tee
+
+
+IF, ELSE_IF, ELSE, END_IF = 0, 1, 2, 3
+
+NL = "\\n"
+dash = lambda ws: [" ", "-"][ws]
+
+
+def cond_kind(i, n):
+    i += 1
+    if i == 1:
+        return IF # if
+    elif (i == n) and (i > 1):
+        return ELSE # else
+    elif i > n:
+        return END_IF # endif
+    else:
+        return ELSE_IF # else if
+
+
+# From: https://docs.python.org/3/library/itertools.html#itertools-recipes
+def pairwise(iterable):
+    a, b = tee(iterable)
+    next(b, None)
+    return zip(a, b)
+
+
+def write_cond(conds, active_branch):
+    n = len(conds) - 1
+
+    lits = []
+    for i in range(1, n + 2 + 1):
+        ws1 = "\\n" * i
+        ws2 = "\\r\\n" * i
+        lits.append((ws1, str(i), ws2))
+
+    conds = list(conds)
+    for i, (pws, nws) in enumerate(conds):
+        kind = cond_kind(i, n)
+        b = str(i == active_branch).lower()
+        cond = [f"if {b}", f"else if {b}", "else", "endif"][kind]
+        cond = f"{{%{dash(pws)} {cond} {dash(nws)}%}}"
+        conds[i] = cond
+
+    it = map("".join, lits)
+    it = filter(None, chain.from_iterable(zip_longest(it, conds)))
+    code = "".join(it)
+
+    expected = f"{lits[0][0]}{lits[0][1]}"
+    for i, (cond, (before, after)) in enumerate(zip(conds, pairwise(lits))):
+        kind = cond_kind(i, n)
+        pws = cond.startswith("{%-")
+        nws = cond.endswith("-%}")
+
+        cond = i == active_branch
+        prev_cond = i == (active_branch + 1)
+
+        if prev_cond or (kind == IF):
+            expected += before[2] * (not pws)
+        if cond or (kind == END_IF):
+            expected += after[0] * (not nws)
+            expected += after[1]
+
+    # FIXME: Askama does not include whitespace before eof
+    # expected += lits[-1][2]
+
+    return code, expected
+
+
+if __name__ == "__main__":
+    # The amount of branches to generate
+    n = 2 # branches
+
+
+    with open("ws.rs", "w") as f:
+        f.write("""\
+// This file is auto generated by gen_ws_tests.py
+
+use askama::Template;
+
+macro_rules! test_template {
+    ($source:literal, $rendered:expr) => {{
+        #[derive(Template)]
+        #[template(source = $source, ext = "txt")]
+        struct CondWS;
+
+        assert_eq!(CondWS.render().unwrap(), $rendered);
+    }};
+}
+
+#[rustfmt::skip]
+#[test]
+fn test_cond_ws() {
+""")
+
+        for branches in range(1, n + 1):
+            for x in product([False, True], repeat=(branches+1)*2):
+                # it = iter(x)
+                # conds = list(zip(it, it))
+                conds = list(zip(x[::2], x[1::2]))
+
+                for i in range(branches):
+                    code, expected = write_cond(conds, i)
+                    f.write(f'    test_template!("{code}", "{expected}");\n')
+
+            if branches != n:
+                f.write("\n")
+        f.write("}\n")
diff --git a/testing/tests/ws.rs b/testing/tests/ws.rs
new file mode 100644
index 0000000..1ea6ada
--- /dev/null
+++ b/testing/tests/ws.rs
@@ -0,0 +1,163 @@
+// This file is auto generated by gen_ws_tests.py
+
+use askama::Template;
+
+macro_rules! test_template {
+    ($source:literal, $rendered:expr) => {{
+        #[derive(Template)]
+        #[template(source = $source, ext = "txt")]
+        struct CondWS;
+
+        assert_eq!(CondWS.render().unwrap(), $rendered);
+    }};
+}
+
+#[rustfmt::skip]
+#[test]
+fn test_cond_ws() {
+    test_template!("\n1\r\n{%  if true  %}\n\n2\r\n\r\n{%  endif  %}\n\n\n3\r\n\r\n\r\n", "\n1\r\n\n\n2\r\n\r\n\n\n\n3");
+    test_template!("\n1\r\n{%  if true  %}\n\n2\r\n\r\n{%  endif -%}\n\n\n3\r\n\r\n\r\n", "\n1\r\n\n\n2\r\n\r\n3");
+    test_template!("\n1\r\n{%  if true  %}\n\n2\r\n\r\n{%- endif  %}\n\n\n3\r\n\r\n\r\n", "\n1\r\n\n\n2\n\n\n3");
+    test_template!("\n1\r\n{%  if true  %}\n\n2\r\n\r\n{%- endif -%}\n\n\n3\r\n\r\n\r\n", "\n1\r\n\n\n23");
+    test_template!("\n1\r\n{%  if true -%}\n\n2\r\n\r\n{%  endif  %}\n\n\n3\r\n\r\n\r\n", "\n1\r\n2\r\n\r\n\n\n\n3");
+    test_template!("\n1\r\n{%  if true -%}\n\n2\r\n\r\n{%  endif -%}\n\n\n3\r\n\r\n\r\n", "\n1\r\n2\r\n\r\n3");
+    test_template!("\n1\r\n{%  if true -%}\n\n2\r\n\r\n{%- endif  %}\n\n\n3\r\n\r\n\r\n", "\n1\r\n2\n\n\n3");
+    test_template!("\n1\r\n{%  if true -%}\n\n2\r\n\r\n{%- endif -%}\n\n\n3\r\n\r\n\r\n", "\n1\r\n23");
+    test_template!("\n1\r\n{%- if true  %}\n\n2\r\n\r\n{%  endif  %}\n\n\n3\r\n\r\n\r\n", "\n1\n\n2\r\n\r\n\n\n\n3");
+    test_template!("\n1\r\n{%- if true  %}\n\n2\r\n\r\n{%  endif -%}\n\n\n3\r\n\r\n\r\n", "\n1\n\n2\r\n\r\n3");
+    test_template!("\n1\r\n{%- if true  %}\n\n2\r\n\r\n{%- endif  %}\n\n\n3\r\n\r\n\r\n", "\n1\n\n2\n\n\n3");
+    test_template!("\n1\r\n{%- if true  %}\n\n2\r\n\r\n{%- endif -%}\n\n\n3\r\n\r\n\r\n", "\n1\n\n23");
+    test_template!("\n1\r\n{%- if true -%}\n\n2\r\n\r\n{%  endif  %}\n\n\n3\r\n\r\n\r\n", "\n12\r\n\r\n\n\n\n3");
+    test_template!("\n1\r\n{%- if true -%}\n\n2\r\n\r\n{%  endif -%}\n\n\n3\r\n\r\n\r\n", "\n12\r\n\r\n3");
+    test_template!("\n1\r\n{%- if true -%}\n\n2\r\n\r\n{%- endif  %}\n\n\n3\r\n\r\n\r\n", "\n12\n\n\n3");
+    test_template!("\n1\r\n{%- if true -%}\n\n2\r\n\r\n{%- endif -%}\n\n\n3\r\n\r\n\r\n", "\n123");
+
+    test_template!("\n1\r\n{%  if true  %}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n2\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%  if false  %}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n\n3\r\n\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%  if true  %}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n2\r\n\r\n4");
+    test_template!("\n1\r\n{%  if false  %}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n\n3\r\n\r\n\r\n4");
+    test_template!("\n1\r\n{%  if true  %}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n2\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%  if false  %}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n\n3\n\n\n\n4");
+    test_template!("\n1\r\n{%  if true  %}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n2\r\n\r\n4");
+    test_template!("\n1\r\n{%  if false  %}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n\n34");
+    test_template!("\n1\r\n{%  if true  %}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n2\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%  if false  %}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n3\r\n\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%  if true  %}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n2\r\n\r\n4");
+    test_template!("\n1\r\n{%  if false  %}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n3\r\n\r\n\r\n4");
+    test_template!("\n1\r\n{%  if true  %}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n2\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%  if false  %}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n3\n\n\n\n4");
+    test_template!("\n1\r\n{%  if true  %}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n2\r\n\r\n4");
+    test_template!("\n1\r\n{%  if false  %}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n34");
+    test_template!("\n1\r\n{%  if true  %}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n2\n\n\n\n4");
+    test_template!("\n1\r\n{%  if false  %}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n\n3\r\n\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%  if true  %}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n24");
+    test_template!("\n1\r\n{%  if false  %}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n\n3\r\n\r\n\r\n4");
+    test_template!("\n1\r\n{%  if true  %}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n2\n\n\n\n4");
+    test_template!("\n1\r\n{%  if false  %}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n\n3\n\n\n\n4");
+    test_template!("\n1\r\n{%  if true  %}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n24");
+    test_template!("\n1\r\n{%  if false  %}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n\n34");
+    test_template!("\n1\r\n{%  if true  %}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n2\n\n\n\n4");
+    test_template!("\n1\r\n{%  if false  %}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n3\r\n\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%  if true  %}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n24");
+    test_template!("\n1\r\n{%  if false  %}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n3\r\n\r\n\r\n4");
+    test_template!("\n1\r\n{%  if true  %}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n2\n\n\n\n4");
+    test_template!("\n1\r\n{%  if false  %}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n3\n\n\n\n4");
+    test_template!("\n1\r\n{%  if true  %}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n24");
+    test_template!("\n1\r\n{%  if false  %}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n34");
+    test_template!("\n1\r\n{%  if true -%}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n2\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%  if false -%}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n\n3\r\n\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%  if true -%}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n2\r\n\r\n4");
+    test_template!("\n1\r\n{%  if false -%}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n\n3\r\n\r\n\r\n4");
+    test_template!("\n1\r\n{%  if true -%}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n2\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%  if false -%}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n\n3\n\n\n\n4");
+    test_template!("\n1\r\n{%  if true -%}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n2\r\n\r\n4");
+    test_template!("\n1\r\n{%  if false -%}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n\n34");
+    test_template!("\n1\r\n{%  if true -%}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n2\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%  if false -%}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n3\r\n\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%  if true -%}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n2\r\n\r\n4");
+    test_template!("\n1\r\n{%  if false -%}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n3\r\n\r\n\r\n4");
+    test_template!("\n1\r\n{%  if true -%}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n2\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%  if false -%}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n3\n\n\n\n4");
+    test_template!("\n1\r\n{%  if true -%}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n2\r\n\r\n4");
+    test_template!("\n1\r\n{%  if false -%}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n34");
+    test_template!("\n1\r\n{%  if true -%}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n2\n\n\n\n4");
+    test_template!("\n1\r\n{%  if false -%}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n\n3\r\n\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%  if true -%}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n24");
+    test_template!("\n1\r\n{%  if false -%}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n\n3\r\n\r\n\r\n4");
+    test_template!("\n1\r\n{%  if true -%}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n2\n\n\n\n4");
+    test_template!("\n1\r\n{%  if false -%}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n\n3\n\n\n\n4");
+    test_template!("\n1\r\n{%  if true -%}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n24");
+    test_template!("\n1\r\n{%  if false -%}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n\n\n\n34");
+    test_template!("\n1\r\n{%  if true -%}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n2\n\n\n\n4");
+    test_template!("\n1\r\n{%  if false -%}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n3\r\n\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%  if true -%}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n24");
+    test_template!("\n1\r\n{%  if false -%}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n3\r\n\r\n\r\n4");
+    test_template!("\n1\r\n{%  if true -%}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n2\n\n\n\n4");
+    test_template!("\n1\r\n{%  if false -%}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n3\n\n\n\n4");
+    test_template!("\n1\r\n{%  if true -%}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n24");
+    test_template!("\n1\r\n{%  if false -%}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\r\n34");
+    test_template!("\n1\r\n{%- if true  %}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n2\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%- if false  %}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n\n3\r\n\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%- if true  %}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n2\r\n\r\n4");
+    test_template!("\n1\r\n{%- if false  %}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n\n3\r\n\r\n\r\n4");
+    test_template!("\n1\r\n{%- if true  %}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n2\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%- if false  %}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n\n3\n\n\n\n4");
+    test_template!("\n1\r\n{%- if true  %}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n2\r\n\r\n4");
+    test_template!("\n1\r\n{%- if false  %}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n\n34");
+    test_template!("\n1\r\n{%- if true  %}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n2\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%- if false  %}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n13\r\n\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%- if true  %}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n2\r\n\r\n4");
+    test_template!("\n1\r\n{%- if false  %}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n13\r\n\r\n\r\n4");
+    test_template!("\n1\r\n{%- if true  %}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n2\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%- if false  %}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n13\n\n\n\n4");
+    test_template!("\n1\r\n{%- if true  %}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n2\r\n\r\n4");
+    test_template!("\n1\r\n{%- if false  %}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n134");
+    test_template!("\n1\r\n{%- if true  %}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n2\n\n\n\n4");
+    test_template!("\n1\r\n{%- if false  %}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n\n3\r\n\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%- if true  %}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n24");
+    test_template!("\n1\r\n{%- if false  %}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n\n3\r\n\r\n\r\n4");
+    test_template!("\n1\r\n{%- if true  %}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n2\n\n\n\n4");
+    test_template!("\n1\r\n{%- if false  %}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n\n3\n\n\n\n4");
+    test_template!("\n1\r\n{%- if true  %}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n24");
+    test_template!("\n1\r\n{%- if false  %}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n\n34");
+    test_template!("\n1\r\n{%- if true  %}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n2\n\n\n\n4");
+    test_template!("\n1\r\n{%- if false  %}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n13\r\n\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%- if true  %}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n24");
+    test_template!("\n1\r\n{%- if false  %}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n13\r\n\r\n\r\n4");
+    test_template!("\n1\r\n{%- if true  %}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n2\n\n\n\n4");
+    test_template!("\n1\r\n{%- if false  %}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n13\n\n\n\n4");
+    test_template!("\n1\r\n{%- if true  %}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n24");
+    test_template!("\n1\r\n{%- if false  %}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n134");
+    test_template!("\n1\r\n{%- if true -%}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n12\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%- if false -%}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n\n3\r\n\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%- if true -%}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n12\r\n\r\n4");
+    test_template!("\n1\r\n{%- if false -%}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n\n3\r\n\r\n\r\n4");
+    test_template!("\n1\r\n{%- if true -%}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n12\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%- if false -%}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n\n3\n\n\n\n4");
+    test_template!("\n1\r\n{%- if true -%}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n12\r\n\r\n4");
+    test_template!("\n1\r\n{%- if false -%}\n\n2\r\n\r\n{%  else  %}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n\n34");
+    test_template!("\n1\r\n{%- if true -%}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n12\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%- if false -%}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n13\r\n\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%- if true -%}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n12\r\n\r\n4");
+    test_template!("\n1\r\n{%- if false -%}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n13\r\n\r\n\r\n4");
+    test_template!("\n1\r\n{%- if true -%}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n12\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%- if false -%}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n13\n\n\n\n4");
+    test_template!("\n1\r\n{%- if true -%}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n12\r\n\r\n4");
+    test_template!("\n1\r\n{%- if false -%}\n\n2\r\n\r\n{%  else -%}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n134");
+    test_template!("\n1\r\n{%- if true -%}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n12\n\n\n\n4");
+    test_template!("\n1\r\n{%- if false -%}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n\n3\r\n\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%- if true -%}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n124");
+    test_template!("\n1\r\n{%- if false -%}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n\n3\r\n\r\n\r\n4");
+    test_template!("\n1\r\n{%- if true -%}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n12\n\n\n\n4");
+    test_template!("\n1\r\n{%- if false -%}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n\n3\n\n\n\n4");
+    test_template!("\n1\r\n{%- if true -%}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n124");
+    test_template!("\n1\r\n{%- if false -%}\n\n2\r\n\r\n{%- else  %}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n1\n\n\n34");
+    test_template!("\n1\r\n{%- if true -%}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n12\n\n\n\n4");
+    test_template!("\n1\r\n{%- if false -%}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%  endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n13\r\n\r\n\r\n\n\n\n\n4");
+    test_template!("\n1\r\n{%- if true -%}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n124");
+    test_template!("\n1\r\n{%- if false -%}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%  endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n13\r\n\r\n\r\n4");
+    test_template!("\n1\r\n{%- if true -%}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n12\n\n\n\n4");
+    test_template!("\n1\r\n{%- if false -%}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%- endif  %}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n13\n\n\n\n4");
+    test_template!("\n1\r\n{%- if true -%}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n124");
+    test_template!("\n1\r\n{%- if false -%}\n\n2\r\n\r\n{%- else -%}\n\n\n3\r\n\r\n\r\n{%- endif -%}\n\n\n\n4\r\n\r\n\r\n\r\n", "\n134");
+}