Compare commits

..

3 commits

Author SHA1 Message Date
d41cd57847 Add asting benchmark
Signed-off-by: Marcel Müller <neikos@neikos.email>
2026-03-13 07:40:21 +01:00
9aeeb4eb5b Scope tests in cases
Signed-off-by: Marcel Müller <neikos@neikos.email>
2026-03-12 18:17:22 +01:00
06816567ff Use macro per-test-file
Signed-off-by: Marcel Müller <neikos@neikos.email>
2026-03-12 18:15:20 +01:00
50 changed files with 272 additions and 271 deletions

13
Cargo.lock generated
View file

@ -442,6 +442,7 @@ dependencies = [
"insta",
"serde",
"serde_json",
"test_each_file",
"thiserror",
"winnow",
]
@ -701,6 +702,18 @@ dependencies = [
"windows-sys 0.61.2",
]
[[package]]
name = "test_each_file"
version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "44619e49a1f62888c271dc50533f036363a7936f99c7cb467ca9d45224cf83b3"
dependencies = [
"proc-macro2",
"quote",
"syn",
"unicode-ident",
]
[[package]]
name = "thiserror"
version = "2.0.18"

View file

@ -7,6 +7,10 @@ edition = "2024"
name = "parsing"
harness = false
[[bench]]
name = "asting"
harness = false
[profile.bench]
debug = true
@ -23,6 +27,7 @@ criterion = "0.8.2"
insta = { version = "1.46.3", features = ["glob", "serde"] }
serde = { version = "1.0.228", features = ["derive"] }
serde_json = "1.0.149"
test_each_file = "0.3.7"
[profile.dev.package]
insta.opt-level = 3

54
benches/asting.rs Normal file
View file

@ -0,0 +1,54 @@
use criterion::BenchmarkId;
use criterion::Criterion;
use criterion::criterion_group;
use criterion::criterion_main;
use nomo::input::NomoInput;
fn asting_benchmark(c: &mut Criterion) {
let mut parsing = c.benchmark_group("Asting");
for size in [1, 10, 200, 1000] {
let mut input = String::new();
for _ in 0..size {
input.push_str("{{ if foo }}{{= variable }}{{ end }}");
}
let input = NomoInput::from(input);
parsing.throughput(criterion::Throughput::Bytes(input.len() as u64));
parsing.bench_with_input(BenchmarkId::from_parameter(size), &input, |b, input| {
b.iter(|| {
let tokens = nomo::parser::parse(input.clone()).unwrap();
let _ast = nomo::ast::parse(tokens.tokens()).unwrap();
});
});
}
}
fn asting_nested(c: &mut Criterion) {
let mut parsing = c.benchmark_group("Asting");
for size in [1, 2, 8, 12] {
let mut input = String::new();
for _ in 0..size {
input = format!(
"{{{{ for foo in bar }}}} {input} {{{{ if foo }}}} Hi! {input} {{{{ end }}}} Yooo {{{{ end }}}}"
);
}
let input = NomoInput::from(input);
parsing.throughput(criterion::Throughput::Bytes(input.len() as u64));
parsing.bench_with_input(BenchmarkId::from_parameter(size), &input, |b, input| {
b.iter(|| {
let tokens = nomo::parser::parse(input.clone()).unwrap();
let _ast = nomo::ast::parse(tokens.tokens()).unwrap();
});
});
}
}
criterion_group!(benches, asting_benchmark, asting_nested);
criterion_main!(benches);

View file

@ -1,152 +0,0 @@
---
source: src/ast/mod.rs
expression: parsed
---
ParsedTemplate {
tokens: [
TemplateToken {
kind: LeftDelim,
source: "{{" (0..2),
},
TemplateToken {
kind: Whitespace,
source: " " (2..3),
},
TemplateToken {
kind: ConditionalIf,
source: "if" (3..5),
},
TemplateToken {
kind: Whitespace,
source: " " (5..6),
},
TemplateToken {
kind: Ident,
source: "foo" (6..9),
},
TemplateToken {
kind: Whitespace,
source: " " (9..10),
},
TemplateToken {
kind: RightDelim,
source: "}}" (10..12),
},
TemplateToken {
kind: Whitespace,
source: "\n " (12..25),
},
TemplateToken {
kind: LeftDelim,
source: "{{" (25..27),
},
TemplateToken {
kind: Whitespace,
source: " " (27..28),
},
TemplateToken {
kind: ConditionalIf,
source: "if" (28..30),
},
TemplateToken {
kind: Whitespace,
source: " " (30..31),
},
TemplateToken {
kind: Ident,
source: "bar" (31..34),
},
TemplateToken {
kind: Whitespace,
source: " " (34..35),
},
TemplateToken {
kind: RightDelim,
source: "}}" (35..37),
},
TemplateToken {
kind: Whitespace,
source: "\n " (37..54),
},
TemplateToken {
kind: Content,
source: "Hiii" (54..58),
},
TemplateToken {
kind: Whitespace,
source: "\n " (58..71),
},
TemplateToken {
kind: LeftDelim,
source: "{{" (71..73),
},
TemplateToken {
kind: Whitespace,
source: " " (73..74),
},
TemplateToken {
kind: End,
source: "end" (74..77),
},
TemplateToken {
kind: Whitespace,
source: " " (77..78),
},
TemplateToken {
kind: RightDelim,
source: "}}" (78..80),
},
TemplateToken {
kind: Whitespace,
source: "\n " (80..89),
},
TemplateToken {
kind: LeftDelim,
source: "{{" (89..91),
},
TemplateToken {
kind: Whitespace,
source: " " (91..92),
},
TemplateToken {
kind: End,
source: "end" (92..95),
},
TemplateToken {
kind: Whitespace,
source: " " (95..96),
},
TemplateToken {
kind: RightDelim,
source: "}}" (96..98),
},
TemplateToken {
kind: Whitespace,
source: "\n\n " (98..108),
},
TemplateToken {
kind: LeftDelim,
source: "{{" (108..110),
},
TemplateToken {
kind: Whitespace,
source: " " (110..111),
},
TemplateToken {
kind: Ident,
source: "value" (111..116),
},
TemplateToken {
kind: Whitespace,
source: " " (116..117),
},
TemplateToken {
kind: RightDelim,
source: "}}" (117..119),
},
TemplateToken {
kind: Whitespace,
source: "\n " (119..128),
},
],
}

View file

@ -1,6 +0,0 @@
---
source: tests/file_tests.rs
expression: output
input_file: tests/cases/condition.nomo
---
"\n Hello World!\n\n\nmore"

View file

@ -1,6 +0,0 @@
---
source: tests/file_tests.rs
expression: output
input_file: tests/cases/identifiers.nomo
---
"Foo\nFoo\nFoo\nFoo\nFoo\nFoo"

View file

@ -1,6 +0,0 @@
---
source: tests/file_tests.rs
expression: output
input_file: tests/cases/if_else_if.nomo
---
"\n Hello World!\n"

View file

@ -1,6 +0,0 @@
---
source: tests/file_tests.rs
expression: output
input_file: tests/cases/multiple.nomo
---
"Hi there! My name is Hemera Green"

View file

@ -1,6 +0,0 @@
---
source: tests/file_tests.rs
expression: output
input_file: tests/cases/trim_whitespace.nomo
---
"Hello Hemera"

View file

@ -1,7 +1,11 @@
---
source: tests/file_tests.rs
expression: parsed
input_file: tests/cases/condition.nomo
info:
input: "{{ if test }}\n Hello World!\n{{ end }}\n\n{{= stuff }}"
context:
stuff: more
test: true
---
ParsedTemplate {
tokens: [

View file

@ -1,7 +1,11 @@
---
source: tests/file_tests.rs
expression: ast
input_file: tests/cases/condition.nomo
info:
input: "{{ if test }}\n Hello World!\n{{ end }}\n\n{{= stuff }}"
context:
stuff: more
test: true
---
TemplateAst {
root: [

View file

@ -1,7 +1,11 @@
---
source: tests/file_tests.rs
expression: emit
input_file: tests/cases/condition.nomo
info:
input: "{{ if test }}\n Hello World!\n{{ end }}\n\n{{= stuff }}"
context:
stuff: more
test: true
---
VMInstructions {
labels: {

View file

@ -0,0 +1,10 @@
---
source: tests/file_tests.rs
expression: output
info:
input: "{{ if test }}\n Hello World!\n{{ end }}\n\n{{= stuff }}"
context:
stuff: more
test: true
---
"\n Hello World!\n\n\nmore"

View file

@ -4,13 +4,12 @@ expression: parsed
info:
input: "{{= _name }}\n{{= a_name }}\n{{= name }}\n{{= _name1 }}\n{{= _namE }}\n{{= name1 }}"
context:
_namE: Foo
name1: Foo
name: Foo
_name: Foo
_namE: Foo
a_name: Foo
name: Foo
_name1: Foo
input_file: tests/cases/identifiers.nomo
---
ParsedTemplate {
tokens: [

View file

@ -4,13 +4,12 @@ expression: ast
info:
input: "{{= _name }}\n{{= a_name }}\n{{= name }}\n{{= _name1 }}\n{{= _namE }}\n{{= name1 }}"
context:
_namE: Foo
name1: Foo
name: Foo
_name: Foo
_namE: Foo
a_name: Foo
name: Foo
_name1: Foo
input_file: tests/cases/identifiers.nomo
---
TemplateAst {
root: [

View file

@ -4,13 +4,12 @@ expression: emit
info:
input: "{{= _name }}\n{{= a_name }}\n{{= name }}\n{{= _name1 }}\n{{= _namE }}\n{{= name1 }}"
context:
_namE: Foo
name1: Foo
name: Foo
_name: Foo
_namE: Foo
a_name: Foo
name: Foo
_name1: Foo
input_file: tests/cases/identifiers.nomo
---
VMInstructions {
labels: {},

View file

@ -0,0 +1,14 @@
---
source: tests/file_tests.rs
expression: output
info:
input: "{{= _name }}\n{{= a_name }}\n{{= name }}\n{{= _name1 }}\n{{= _namE }}\n{{= name1 }}"
context:
name1: Foo
_name: Foo
_namE: Foo
a_name: Foo
name: Foo
_name1: Foo
---
"Foo\nFoo\nFoo\nFoo\nFoo\nFoo"

View file

@ -1,7 +1,12 @@
---
source: tests/file_tests.rs
expression: parsed
input_file: tests/cases/if_else_if.nomo
info:
input: "{{ if test }}\n Not Hello World! :C\n{{ else if another_test }}\n Hello World!\n{{ end }}"
context:
test: false
stuff: more
another_test: true
---
ParsedTemplate {
tokens: [

View file

@ -1,7 +1,12 @@
---
source: tests/file_tests.rs
expression: ast
input_file: tests/cases/if_else_if.nomo
info:
input: "{{ if test }}\n Not Hello World! :C\n{{ else if another_test }}\n Hello World!\n{{ end }}"
context:
test: false
stuff: more
another_test: true
---
TemplateAst {
root: [

View file

@ -1,7 +1,12 @@
---
source: tests/file_tests.rs
expression: emit
input_file: tests/cases/if_else_if.nomo
info:
input: "{{ if test }}\n Not Hello World! :C\n{{ else if another_test }}\n Hello World!\n{{ end }}"
context:
test: false
stuff: more
another_test: true
---
VMInstructions {
labels: {

View file

@ -0,0 +1,11 @@
---
source: tests/file_tests.rs
expression: output
info:
input: "{{ if test }}\n Not Hello World! :C\n{{ else if another_test }}\n Hello World!\n{{ end }}"
context:
test: false
stuff: more
another_test: true
---
"\n Hello World!\n"

View file

@ -1,7 +1,10 @@
---
source: tests/file_tests.rs
expression: parsed
input_file: tests/cases/interpolation.nomo
info:
input: "Hello! I'm {{= name }}"
context:
name: Hemera
---
ParsedTemplate {
tokens: [

View file

@ -1,7 +1,10 @@
---
source: tests/file_tests.rs
expression: ast
input_file: tests/cases/interpolation.nomo
info:
input: "Hello! I'm {{= name }}"
context:
name: Hemera
---
TemplateAst {
root: [

View file

@ -1,7 +1,10 @@
---
source: tests/file_tests.rs
expression: emit
input_file: tests/cases/interpolation.nomo
info:
input: "Hello! I'm {{= name }}"
context:
name: Hemera
---
VMInstructions {
labels: {},

View file

@ -1,6 +1,9 @@
---
source: tests/file_tests.rs
expression: output
input_file: tests/cases/interpolation.nomo
info:
input: "Hello! I'm {{= name }}"
context:
name: Hemera
---
"Hello! I'm Hemera"

View file

@ -4,7 +4,6 @@ expression: parsed
info:
input: "{{ if true }}\n Hello World!\n{{ end }}"
context: {}
input_file: tests/cases/literals.nomo
---
ParsedTemplate {
tokens: [

View file

@ -4,7 +4,6 @@ expression: ast
info:
input: "{{ if true }}\n Hello World!\n{{ end }}"
context: {}
input_file: tests/cases/literals.nomo
---
TemplateAst {
root: [

View file

@ -4,7 +4,6 @@ expression: emit
info:
input: "{{ if true }}\n Hello World!\n{{ end }}"
context: {}
input_file: tests/cases/literals.nomo
---
VMInstructions {
labels: {

View file

@ -4,6 +4,5 @@ expression: output
info:
input: "{{ if true }}\n Hello World!\n{{ end }}"
context: {}
input_file: tests/cases/literals.nomo
---
"\n Hello World!\n"

View file

@ -4,7 +4,6 @@ expression: parsed
info:
input: "{{= 5 * 3 }}\n{{= 2 * 3 + 4 * 3 }}\n{{= 3 / 3 + 3 }}"
context: {}
input_file: tests/cases/maths.nomo
---
ParsedTemplate {
tokens: [

View file

@ -4,7 +4,6 @@ expression: ast
info:
input: "{{= 5 * 3 }}\n{{= 2 * 3 + 4 * 3 }}\n{{= 3 / 3 + 3 }}"
context: {}
input_file: tests/cases/maths.nomo
---
TemplateAst {
root: [

View file

@ -4,7 +4,6 @@ expression: emit
info:
input: "{{= 5 * 3 }}\n{{= 2 * 3 + 4 * 3 }}\n{{= 3 / 3 + 3 }}"
context: {}
input_file: tests/cases/maths.nomo
---
VMInstructions {
labels: {},

View file

@ -4,6 +4,5 @@ expression: output
info:
input: "{{= 5 * 3 }}\n{{= 2 * 3 + 4 * 3 }}\n{{= 3 / 3 + 3 }}"
context: {}
input_file: tests/cases/maths.nomo
---
"15\n18\n4"

View file

@ -1,7 +1,11 @@
---
source: tests/file_tests.rs
expression: parsed
input_file: tests/cases/multiple.nomo
info:
input: "Hi there! My name is {{= name }} {{= lastname }}"
context:
name: Hemera
lastname: Green
---
ParsedTemplate {
tokens: [

View file

@ -1,7 +1,11 @@
---
source: tests/file_tests.rs
expression: ast
input_file: tests/cases/multiple.nomo
info:
input: "Hi there! My name is {{= name }} {{= lastname }}"
context:
name: Hemera
lastname: Green
---
TemplateAst {
root: [

View file

@ -1,7 +1,11 @@
---
source: tests/file_tests.rs
expression: emit
input_file: tests/cases/multiple.nomo
info:
input: "Hi there! My name is {{= name }} {{= lastname }}"
context:
name: Hemera
lastname: Green
---
VMInstructions {
labels: {},

View file

@ -0,0 +1,10 @@
---
source: tests/file_tests.rs
expression: output
info:
input: "Hi there! My name is {{= name }} {{= lastname }}"
context:
name: Hemera
lastname: Green
---
"Hi there! My name is Hemera Green"

View file

@ -1,7 +1,9 @@
---
source: tests/file_tests.rs
expression: parsed
input_file: tests/cases/simple.nomo
info:
input: Hello World!
context: {}
---
ParsedTemplate {
tokens: [

View file

@ -1,7 +1,9 @@
---
source: tests/file_tests.rs
expression: ast
input_file: tests/cases/simple.nomo
info:
input: Hello World!
context: {}
---
TemplateAst {
root: [

View file

@ -1,7 +1,9 @@
---
source: tests/file_tests.rs
expression: emit
input_file: tests/cases/simple.nomo
info:
input: Hello World!
context: {}
---
VMInstructions {
labels: {},

View file

@ -1,6 +1,8 @@
---
source: tests/file_tests.rs
expression: output
input_file: tests/cases/simple.nomo
info:
input: Hello World!
context: {}
---
"Hello World!"

View file

@ -8,7 +8,6 @@ info:
values:
- one
- two
input_file: tests/cases/simple_for.nomo
---
ParsedTemplate {
tokens: [

View file

@ -8,7 +8,6 @@ info:
values:
- one
- two
input_file: tests/cases/simple_for.nomo
---
TemplateAst {
root: [

View file

@ -8,7 +8,6 @@ info:
values:
- one
- two
input_file: tests/cases/simple_for.nomo
---
VMInstructions {
labels: {

View file

@ -8,6 +8,5 @@ info:
values:
- one
- two
input_file: tests/cases/simple_for.nomo
---
"one\ntwo\n\nNo Values >:C"

View file

@ -1,7 +1,11 @@
---
source: tests/file_tests.rs
expression: parsed
input_file: tests/cases/trim_whitespace.nomo
info:
input: "{{ if test -}}\n Hello {{= stuff -}}\n{{- end }}"
context:
stuff: Hemera
test: true
---
ParsedTemplate {
tokens: [

View file

@ -1,7 +1,11 @@
---
source: tests/file_tests.rs
expression: ast
input_file: tests/cases/trim_whitespace.nomo
info:
input: "{{ if test -}}\n Hello {{= stuff -}}\n{{- end }}"
context:
stuff: Hemera
test: true
---
TemplateAst {
root: [

View file

@ -1,7 +1,11 @@
---
source: tests/file_tests.rs
expression: emit
input_file: tests/cases/trim_whitespace.nomo
info:
input: "{{ if test -}}\n Hello {{= stuff -}}\n{{- end }}"
context:
stuff: Hemera
test: true
---
VMInstructions {
labels: {

View file

@ -0,0 +1,10 @@
---
source: tests/file_tests.rs
expression: output
info:
input: "{{ if test -}}\n Hello {{= stuff -}}\n{{- end }}"
context:
stuff: Hemera
test: true
---
"Hello Hemera"

View file

@ -1,64 +1,64 @@
use std::collections::HashMap;
use std::path::Path;
use nomo::Context;
test_each_file::test_each_path! { for ["nomo"] in "./tests/cases/" as cases => check_for_input }
#[derive(serde::Serialize)]
struct Info {
input: String,
context: HashMap<String, serde_json::Value>,
}
#[test]
fn check_cases() {
insta::glob!("cases/*.nomo", |path| {
let mut settings = insta::Settings::clone_current();
settings.set_snapshot_path("cases");
settings.set_snapshot_suffix(path.file_stem().unwrap().display().to_string());
settings.set_prepend_module_to_snapshot(false);
fn check_for_input([path]: [&Path; 1]) {
let mut settings = insta::Settings::clone_current();
settings.set_snapshot_path("cases");
settings.set_prepend_module_to_snapshot(false);
let input = std::fs::read_to_string(path).unwrap();
let basename = path.file_stem().unwrap().to_string_lossy();
let input = std::fs::read_to_string(path).unwrap();
let (context, input) = input.split_once("\n---\n").unwrap_or_else(|| ("", &input));
let (context, input) = input.split_once("\n---\n").unwrap_or_else(|| ("", &input));
let map = if !context.is_empty() {
serde_json::from_str::<HashMap<String, serde_json::Value>>(context).unwrap()
} else {
HashMap::new()
};
let map = if !context.is_empty() {
serde_json::from_str::<HashMap<String, serde_json::Value>>(context).unwrap()
} else {
HashMap::new()
};
settings.set_info(&Info {
input: input.to_string(),
context: map.clone(),
});
let mut context = Context::new();
for (k, v) in map {
context.try_insert(k, v).unwrap();
}
let parsed = nomo::parser::parse(input.into()).unwrap();
let _guard = settings.bind_to_scope();
insta::assert_debug_snapshot!("1-parsed", parsed);
let ast = match nomo::ast::parse(parsed.tokens()) {
Ok(ast) => ast,
Err(err) => {
eprintln!("{}", err.to_report(input));
panic!("Could not evaluate ast");
}
};
insta::assert_debug_snapshot!("2-ast", ast);
let emit = nomo::emit::emit_machine(ast);
insta::assert_debug_snapshot!("3-instructions", emit);
let output = nomo::eval::execute(&emit, &context).unwrap();
insta::assert_debug_snapshot!("4-output", output);
settings.set_info(&Info {
input: input.to_string(),
context: map.clone(),
});
let mut context = Context::new();
for (k, v) in map {
context.try_insert(k, v).unwrap();
}
let parsed = nomo::parser::parse(input.into()).unwrap();
let _guard = settings.bind_to_scope();
insta::assert_debug_snapshot!(format!("{basename}.1-parsed"), parsed);
let ast = match nomo::ast::parse(parsed.tokens()) {
Ok(ast) => ast,
Err(err) => {
eprintln!("{}", err.to_report(input));
panic!("Could not evaluate ast");
}
};
insta::assert_debug_snapshot!(format!("{basename}.2-ast"), ast);
let emit = nomo::emit::emit_machine(ast);
insta::assert_debug_snapshot!(format!("{basename}.3-instructions"), emit);
let output = nomo::eval::execute(&emit, &context).unwrap();
insta::assert_debug_snapshot!(format!("{basename}.4-output"), output);
}