Add tests for for loop

Signed-off-by: Marcel Müller <neikos@neikos.email>
This commit is contained in:
Marcel Müller 2026-03-11 18:15:14 +01:00
parent 42e0056374
commit dc8281036c
7 changed files with 178 additions and 2 deletions

View file

@ -151,6 +151,7 @@ pub fn execute(vm: &VMInstructions, global_context: &Context) -> Result<String,
}; };
ip = *new_ip; ip = *new_ip;
continue;
} }
} }
Instruction::GetIteratorEmptyOrJump { Instruction::GetIteratorEmptyOrJump {
@ -166,6 +167,7 @@ pub fn execute(vm: &VMInstructions, global_context: &Context) -> Result<String,
}; };
ip = *new_ip; ip = *new_ip;
continue;
} }
} }
Instruction::PopScope => scopes.pop_scope(), Instruction::PopScope => scopes.pop_scope(),

View file

@ -2,7 +2,9 @@
source: tests/file_tests.rs source: tests/file_tests.rs
expression: parsed expression: parsed
info: info:
input: "{{ for value in values -}}\n {{-= value }}\n{{- end }}\n{{ for value in no_values -}}\n {{-= value }}\n{{ else -}}\nNo Values >:C\n{{- end }}"
context: context:
no_values: []
values: values:
- one - one
- two - two
@ -37,5 +39,42 @@ ParsedTemplate {
[End]"end" (49..52), [End]"end" (49..52),
[Whitespace]" " (52..53), [Whitespace]" " (52..53),
[RightDelim]"}}" (53..55), [RightDelim]"}}" (53..55),
[Whitespace]"\n" (55..56),
[LeftDelim]"{{" (56..58),
[Whitespace]" " (58..59),
[For]"for" (59..62),
[Whitespace]" " (62..63),
[Ident]"value" (63..68),
[Whitespace]" " (68..69),
[In]"in" (69..71),
[Whitespace]" " (71..72),
[Ident]"no_values" (72..81),
[Whitespace]" " (81..82),
[TrimWhitespace]"-" (82..83),
[RightDelim]"}}" (83..85),
[Whitespace]"\n " (85..90),
[LeftDelim]"{{" (90..92),
[TrimWhitespace]"-" (92..93),
[WantsOutput]"=" (93..94),
[Whitespace]" " (94..95),
[Ident]"value" (95..100),
[Whitespace]" " (100..101),
[RightDelim]"}}" (101..103),
[Whitespace]"\n" (103..104),
[LeftDelim]"{{" (104..106),
[Whitespace]" " (106..107),
[ConditionalElse]"else" (107..111),
[Whitespace]" " (111..112),
[TrimWhitespace]"-" (112..113),
[RightDelim]"}}" (113..115),
[Whitespace]"\n" (115..116),
[Content]"No Values >:C" (116..129),
[Whitespace]"\n" (129..130),
[LeftDelim]"{{" (130..132),
[TrimWhitespace]"-" (132..133),
[Whitespace]" " (133..134),
[End]"end" (134..137),
[Whitespace]" " (137..138),
[RightDelim]"}}" (138..140),
], ],
} }

View file

@ -2,7 +2,9 @@
source: tests/file_tests.rs source: tests/file_tests.rs
expression: ast expression: ast
info: info:
input: "{{ for value in values -}}\n {{-= value }}\n{{- end }}\n{{ for value in no_values -}}\n {{-= value }}\n{{ else -}}\nNo Values >:C\n{{- end }}"
context: context:
no_values: []
values: values:
- one - one
- two - two
@ -34,6 +36,50 @@ TemplateAst {
], ],
else_block: None, else_block: None,
else_content: None, else_content: None,
end_block: Block {
prev_whitespace_content: None,
expression: EndBlock,
post_whitespace_content: Some(
[Whitespace]"\n" (55..56),
),
},
},
ForChain {
for_block: Block {
prev_whitespace_content: None,
expression: For {
value_ident: [Ident]"value" (63..68),
value_expression: VariableAccess(
[Ident]"no_values" (72..81),
),
},
post_whitespace_content: None,
},
content: [
Interpolation {
prev_whitespace_content: None,
expression: VariableAccess(
[Ident]"value" (95..100),
),
post_whitespace_content: Some(
[Whitespace]"\n" (103..104),
),
},
],
else_block: Some(
Block {
prev_whitespace_content: None,
expression: ForElse,
post_whitespace_content: None,
},
),
else_content: Some(
[
StaticContent(
[Content]"No Values >:C" (116..129),
),
],
),
end_block: Block { end_block: Block {
prev_whitespace_content: None, prev_whitespace_content: None,
expression: EndBlock, expression: EndBlock,

View file

@ -2,7 +2,9 @@
source: tests/file_tests.rs source: tests/file_tests.rs
expression: emit expression: emit
info: info:
input: "{{ for value in values -}}\n {{-= value }}\n{{- end }}\n{{ for value in no_values -}}\n {{-= value }}\n{{ else -}}\nNo Values >:C\n{{- end }}"
context: context:
no_values: []
values: values:
- one - one
- two - two
@ -19,6 +21,15 @@ VMInstructions {
LabelSlot { LabelSlot {
index: 2, index: 2,
}: 4, }: 4,
LabelSlot {
index: 7,
}: 22,
LabelSlot {
index: 8,
}: 23,
LabelSlot {
index: 9,
}: 16,
}, },
instructions: [ instructions: [
PushScope { PushScope {
@ -83,5 +94,73 @@ VMInstructions {
}, },
}, },
PopScope, PopScope,
AppendContent {
content: "\n" (55..56),
},
PushScope {
inherit_parent: true,
},
LoadFromContextToSlot {
name: "no_values" (72..81),
slot: VariableSlot {
index: 11,
},
},
CreateIteratorFromSlotToSlot {
iterator_slot: VariableSlot {
index: 12,
},
iterator_source_slot: VariableSlot {
index: 11,
},
},
GetIteratorEmptyOrJump {
iterator_slot: VariableSlot {
index: 12,
},
jump: LabelSlot {
index: 7,
},
},
AdvanceIteratorOrJump {
iterator_slot: VariableSlot {
index: 12,
},
value_slot: VariableSlot {
index: 10,
},
jump: LabelSlot {
index: 8,
},
},
LoadFromSlotToContext {
value_ident: "value" (63..68),
value_slot: VariableSlot {
index: 10,
},
},
LoadFromContextToSlot {
name: "value" (95..100),
slot: VariableSlot {
index: 13,
},
},
EmitFromSlot {
slot: VariableSlot {
index: 13,
},
},
AppendContent {
content: "\n" (103..104),
},
Jump {
jump: LabelSlot {
index: 9,
},
},
AppendContent {
content: "No Values >:C" (116..129),
},
PopScope,
], ],
} }

View file

@ -2,10 +2,12 @@
source: tests/file_tests.rs source: tests/file_tests.rs
expression: output expression: output
info: info:
input: "{{ for value in values -}}\n {{-= value }}\n{{- end }}\n{{ for value in no_values -}}\n {{-= value }}\n{{ else -}}\nNo Values >:C\n{{- end }}"
context: context:
no_values: []
values: values:
- one - one
- two - two
input_file: tests/cases/simple_for.nomo input_file: tests/cases/simple_for.nomo
--- ---
"one\ntwo\n" "one\ntwo\n\nNo Values >:C"

View file

@ -1,7 +1,13 @@
{ {
"values": [ "one", "two" ] "values": [ "one", "two" ],
"no_values": []
} }
--- ---
{{ for value in values -}} {{ for value in values -}}
{{-= value }} {{-= value }}
{{- end }}
{{ for value in no_values -}}
{{-= value }}
{{ else -}}
No Values >:C
{{- end }} {{- end }}

View file

@ -4,6 +4,7 @@ use nomo::Context;
#[derive(serde::Serialize)] #[derive(serde::Serialize)]
struct Info { struct Info {
input: String,
context: HashMap<String, serde_json::Value>, context: HashMap<String, serde_json::Value>,
} }
@ -26,6 +27,7 @@ fn check_cases() {
}; };
settings.set_info(&Info { settings.set_info(&Info {
input: input.to_string(),
context: map.clone(), context: map.clone(),
}); });