From 1b78fbff825d2d9a0944483bbb841c1ece446ee4 Mon Sep 17 00:00:00 2001 From: greg Date: Tue, 14 Aug 2018 21:39:33 -0700 Subject: [PATCH] Tests for basic pattern matching --- schala-lang/src/eval.rs | 40 ++++++++++++++++++++++++++++++++-------- 1 file changed, 32 insertions(+), 8 deletions(-) diff --git a/schala-lang/src/eval.rs b/schala-lang/src/eval.rs index ad27aec..89e7c26 100644 --- a/schala-lang/src/eval.rs +++ b/schala-lang/src/eval.rs @@ -389,16 +389,27 @@ mod eval_tests { use parsing::parse; use eval::State; + macro_rules! all_output { + ($string:expr) => { + { + let symbol_table = Rc::new(RefCell::new(SymbolTable::new())); + let mut state = State::new(symbol_table); + let ast = parse(tokenize($string)).0.unwrap(); + state.symbol_table_handle.borrow_mut().add_top_level_symbols(&ast); + let reduced = ast.reduce(&state.symbol_table_handle.borrow()); + let all_output = state.evaluate(reduced, true); + all_output + } + } + } + macro_rules! fresh_env { ($string:expr, $correct:expr) => { - let symbol_table = Rc::new(RefCell::new(SymbolTable::new())); - let mut state = State::new(symbol_table); - let ast = parse(tokenize($string)).0.unwrap(); - state.symbol_table_handle.borrow_mut().add_top_level_symbols(&ast); - let reduced = ast.reduce(&state.symbol_table_handle.borrow()); - let all_output = state.evaluate(reduced, true); - let ref output = all_output.last().unwrap(); - assert_eq!(**output, Ok($correct.to_string())); + { + let all_output = all_output!($string); + let ref output = all_output.last().unwrap(); + assert_eq!(**output, Ok($correct.to_string())); + } } } @@ -437,4 +448,17 @@ mod eval_tests { "#; fresh_env!(scope_ok, "20"); } + + #[test] + fn basic_patterns() { + let source = r#" +type Option = Some(T) | None +let x = Some(9); if x is Some(q) then { q } else { 0 }"#; + fresh_env!(source, "9"); + + let source = r#" +type Option = Some(T) | None +let x = None; if x is Some(q) then { q } else { 0 }"#; + fresh_env!(source, "0"); + } }