add some parser tests

This commit is contained in:
mehbark 2026-01-02 22:26:24 -05:00
parent a039303d8c
commit 6f71d9f5b3
3 changed files with 46 additions and 0 deletions

View file

@ -8,6 +8,9 @@ mod parser;
mod symbol; mod symbol;
mod val; mod val;
#[cfg(test)]
mod tests;
fn main() { fn main() {
let src = &env::args().nth(1).expect("give me an argument now"); let src = &env::args().nth(1).expect("give me an argument now");
let res = parse(src); let res = parse(src);

1
src/tests/mod.rs Normal file
View file

@ -0,0 +1 @@
mod parser;

42
src/tests/parser.rs Normal file
View file

@ -0,0 +1,42 @@
use crate::parser::*;
fn test(src: &str, expected: &str) {
assert_eq!(
parse(src)
.into_iter()
.map(|stmt| format!("{stmt}"))
.collect::<Vec<_>>()
.join(";\n"),
expected.trim(),
);
}
#[test]
fn double() {
test(
"
fn double([x]) (
^x = x * 2;
)
",
"(set! double (fn ([x] [x]) (^x = (x * 2))))",
);
}
#[test]
fn make_counter() {
test(
"
fn make_counter([] [inc]) (
i = 0;
^inc = fn([] [i]) (
i = i + 1;
^i = i;
);
# or fn ^inc(...)
)
",
"(set! make_counter (fn ([] [inc]) (i = 0) (^inc = (fn ([] [i]) (i = (i + 1)) (^i = i)))))",
);
}