From adce02662016193481ff1d486cc681fe55c0019e Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 7 Apr 2022 18:42:54 +0200 Subject: Add test for suppress_whitespace option --- testing/tests/gen_ws_tests.py | 58 +++++++++++++++++++++++++++++-------------- 1 file changed, 39 insertions(+), 19 deletions(-) (limited to 'testing/tests/gen_ws_tests.py') diff --git a/testing/tests/gen_ws_tests.py b/testing/tests/gen_ws_tests.py index 5d2a332..bf59a09 100644 --- a/testing/tests/gen_ws_tests.py +++ b/testing/tests/gen_ws_tests.py @@ -10,7 +10,10 @@ BRANCHES = 2 # branches IF, ELSE_IF, ELSE, END_IF = 0, 1, 2, 3 NL = "\\n" -dash = lambda ws: [" ", "-"][ws] + + +def handle_ws(ws, trim=True): + return [" ", "-" if trim else "+"][ws] def trim(s, ws): @@ -40,8 +43,9 @@ def pairwise(iterable): return zip(a, b) -def write_cond(conds, active_branch): +def write_cond(conds, active_branch, sign, invert): n = len(conds) - 1 + trim = sign == "-" lits = [] for i in range(1, n + 2 + 1): @@ -54,7 +58,7 @@ def write_cond(conds, active_branch): 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)}%}}" + cond = f"{{%{handle_ws(pws, trim)} {cond} {handle_ws(nws, trim)}%}}" conds[i] = cond it = map("".join, lits) @@ -64,8 +68,15 @@ def write_cond(conds, active_branch): 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("-%}") + pws = cond.startswith(f"{{%{sign}") + nws = cond.endswith(f"{sign}%}}") + + if sign == "+": + pws = not pws + nws = not nws + elif invert: + pws = True + nws = True cond = i == active_branch prev_cond = i == (active_branch + 1) @@ -87,13 +98,13 @@ def write_match(contents, arms, match_ws): code = before pws, nws = match_ws[0] - code += f"{{%{dash(pws)} match {expr} {dash(nws)}%}}" + code += f"{{%{handle_ws(pws)} match {expr} {handle_ws(nws)}%}}" for (arm, expr), (pws, nws) in zip(arms, match_ws[1:-1]): - code += f"{{%{dash(pws)} when {arm} {dash(nws)}%}}{expr}" + code += f"{{%{handle_ws(pws)} when {arm} {handle_ws(nws)}%}}{expr}" pws, nws = match_ws[-1] - code += f"{{%{dash(pws)} endmatch {dash(nws)}%}}" + code += f"{{%{handle_ws(pws)} endmatch {handle_ws(nws)}%}}" code += after return code @@ -109,22 +120,25 @@ def write_match_result(active_arm, contents, arms, match_ws): return expected -def write_cond_tests(f): +def write_cond_tests(f, name_extra, config): f.write(""" -macro_rules! test_template { - ($source:literal, $rendered:expr) => {{ +macro_rules! test_template{name_extra} {{ + ($source:literal, $rendered:expr) => {{{{ #[derive(Template)] - #[template(source = $source, ext = "txt")] + #[template(source = $source, ext = "txt"{config})] struct CondWs; assert_eq!(CondWs.render().unwrap(), $rendered); - }}; -} + }}}}; +}} #[rustfmt::skip] #[test] -fn test_cond_ws() { -""") +fn test_cond_ws{name_extra}() {{ +""".format(name_extra = name_extra, config = config)) + + invert = len(name_extra) > 0 + sign = "+" if invert else "-" for branches in range(1, BRANCHES + 1): for x in product([False, True], repeat=(branches+1)*2): @@ -133,8 +147,13 @@ fn test_cond_ws() { 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') + code, expected = write_cond(conds, i, sign, invert) + f.write(f' test_template{name_extra}!("{code}", "{expected}");\n') + if invert and ("{%+" in code or "+%}" in code): + # We also check that the minus sign is trimming with suppress_whitespace + # option enabled. + code, expected = write_cond(conds, i, "-", invert) + f.write(f' test_template{name_extra}!("{code}", "{expected}");\n') if branches != BRANCHES: f.write("\n") @@ -182,5 +201,6 @@ if __name__ == "__main__": with open("ws.rs", "w") as f: f.write("// This file is auto generated by gen_ws_tests.py\n\n") f.write("use askama::Template;\n") - write_cond_tests(f) + write_cond_tests(f, "", "") + write_cond_tests(f, "_inverted", ', config = "test_trim.toml"') write_match_tests(f) -- cgit