From ef02e94591c89ba7d0d5a59df8573ccf8f221d7b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marcel=20M=C3=BCller?= Date: Mon, 9 Mar 2026 11:40:12 +0100 Subject: [PATCH] Add tests for if/else if/else/if MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Marcel Müller --- src/ast/mod.rs | 54 ++++++++++++++++- .../nomo__ast__tests__check_if_else.snap | 55 +++++++++++++++++ .../nomo__ast__tests__check_if_else_if.snap | 59 +++++++++++++++++++ 3 files changed, 166 insertions(+), 2 deletions(-) create mode 100644 src/ast/snapshots/nomo__ast__tests__check_if_else.snap create mode 100644 src/ast/snapshots/nomo__ast__tests__check_if_else_if.snap diff --git a/src/ast/mod.rs b/src/ast/mod.rs index 8e2a1a7..ba8122f 100644 --- a/src/ast/mod.rs +++ b/src/ast/mod.rs @@ -675,11 +675,61 @@ mod tests { } #[test] - fn check_empty_output() { + fn check_empty_if_output() { let input = "{{ if foo }}{{ end }}"; let parsed = crate::parser::parse(input.into()).unwrap(); - panic_pretty(input, parse(parsed.tokens())); + let ast = panic_pretty(input, parse(parsed.tokens())); + + insta::assert_debug_snapshot!(ast, @r#" + TemplateAst { + root: [ + ConditionalChain { + chain: [ + IfConditional { + if_block: Block { + prev_whitespace_content: None, + expression: VariableAccess( + [Ident]"foo" (6..9), + ), + post_whitespace_content: None, + }, + }, + ConditionalContent { + content: [], + }, + Block { + prev_whitespace_content: None, + expression: EndBlock, + post_whitespace_content: None, + }, + ], + }, + ], + } + "#); + } + + #[test] + fn check_if_else() { + let input = "{{ if foo }} foo {{ else }} bar {{ end }}"; + + let parsed = crate::parser::parse(input.into()).unwrap(); + + let ast = panic_pretty(input, parse(parsed.tokens())); + + insta::assert_debug_snapshot!(ast); + } + + #[test] + fn check_if_else_if() { + let input = "{{ if foo }} foo {{ else if bar }} bar {{ end }}"; + + let parsed = crate::parser::parse(input.into()).unwrap(); + + let ast = panic_pretty(input, parse(parsed.tokens())); + + insta::assert_debug_snapshot!(ast); } } diff --git a/src/ast/snapshots/nomo__ast__tests__check_if_else.snap b/src/ast/snapshots/nomo__ast__tests__check_if_else.snap new file mode 100644 index 0000000..07e4955 --- /dev/null +++ b/src/ast/snapshots/nomo__ast__tests__check_if_else.snap @@ -0,0 +1,55 @@ +--- +source: src/ast/mod.rs +expression: ast +--- +TemplateAst { + root: [ + ConditionalChain { + chain: [ + IfConditional { + if_block: Block { + prev_whitespace_content: None, + expression: VariableAccess( + [Ident]"foo" (6..9), + ), + post_whitespace_content: Some( + [Whitespace]" " (12..13), + ), + }, + }, + ConditionalContent { + content: [ + StaticContent( + [Content]"foo" (13..16), + ), + ], + }, + Block { + prev_whitespace_content: Some( + [Whitespace]" " (16..17), + ), + expression: ElseConditional { + expression: None, + }, + post_whitespace_content: Some( + [Whitespace]" " (27..28), + ), + }, + ConditionalContent { + content: [ + StaticContent( + [Content]"bar" (28..31), + ), + ], + }, + Block { + prev_whitespace_content: Some( + [Whitespace]" " (31..32), + ), + expression: EndBlock, + post_whitespace_content: None, + }, + ], + }, + ], +} diff --git a/src/ast/snapshots/nomo__ast__tests__check_if_else_if.snap b/src/ast/snapshots/nomo__ast__tests__check_if_else_if.snap new file mode 100644 index 0000000..2690597 --- /dev/null +++ b/src/ast/snapshots/nomo__ast__tests__check_if_else_if.snap @@ -0,0 +1,59 @@ +--- +source: src/ast/mod.rs +expression: ast +--- +TemplateAst { + root: [ + ConditionalChain { + chain: [ + IfConditional { + if_block: Block { + prev_whitespace_content: None, + expression: VariableAccess( + [Ident]"foo" (6..9), + ), + post_whitespace_content: Some( + [Whitespace]" " (12..13), + ), + }, + }, + ConditionalContent { + content: [ + StaticContent( + [Content]"foo" (13..16), + ), + ], + }, + Block { + prev_whitespace_content: Some( + [Whitespace]" " (16..17), + ), + expression: ElseConditional { + expression: Some( + VariableAccess( + [Ident]"bar" (28..31), + ), + ), + }, + post_whitespace_content: Some( + [Whitespace]" " (34..35), + ), + }, + ConditionalContent { + content: [ + StaticContent( + [Content]"bar" (35..38), + ), + ], + }, + Block { + prev_whitespace_content: Some( + [Whitespace]" " (38..39), + ), + expression: EndBlock, + post_whitespace_content: None, + }, + ], + }, + ], +}