64 lines
1.2 KiB
Rust
64 lines
1.2 KiB
Rust
#![cfg(test)]
|
|
|
|
use crate::symbol_table::SymbolTable;
|
|
use crate::reduced_ast::reduce;
|
|
use crate::eval::State;
|
|
|
|
fn evaluate_all_outputs(input: &str) -> Vec<Result<String, String>> {
|
|
let ast = crate::util::quick_ast(input);
|
|
|
|
let mut symbol_table = SymbolTable::new();
|
|
symbol_table.process_ast(&ast).unwrap();
|
|
|
|
let reduced = reduce(&ast, &symbol_table);
|
|
|
|
let mut state = State::new();
|
|
|
|
state.evaluate(reduced, true)
|
|
}
|
|
|
|
macro_rules! test_in_fresh_env {
|
|
($string:expr, $correct:expr) => {
|
|
{
|
|
let all_output = evaluate_all_outputs($string);
|
|
let output = &all_output.last().unwrap();
|
|
assert_eq!(**output, Ok($correct.to_string()));
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
fn prim_obj_pattern() {
|
|
let source = r#"
|
|
type Stuff = Mulch(Nat) | Jugs(Nat, String) | Mardok
|
|
let a = Stuff::Mulch(20)
|
|
let b = Stuff::Jugs(1, "haha")
|
|
let c = Stuff::Mardok
|
|
|
|
let x = if a {
|
|
is Stuff::Mulch(20) then "x",
|
|
is _ then "ERR"
|
|
}
|
|
|
|
let y = if b {
|
|
is Stuff::Mulch(n) then "ERR",
|
|
is Stuff::Jugs(2, _) then "ERR",
|
|
is Stuff::Jugs(1, s) then s,
|
|
is _ then "ERR",
|
|
}
|
|
|
|
let z = if c {
|
|
is Stuff::Jugs(_, _) then "ERR",
|
|
is Stuff::Mardok then "NIGH",
|
|
is _ then "ERR",
|
|
}
|
|
|
|
(x, y, z)
|
|
"#;
|
|
test_in_fresh_env!(source, r#"("x", "haha", "NIGH")"#);
|
|
}
|
|
|
|
|