From d086b7c44cb7d466236042b862148a3e4863a4ed Mon Sep 17 00:00:00 2001 From: Empa Date: Mon, 29 Apr 2024 15:03:24 +0200 Subject: [PATCH] Test some more --- crates/saft-bytecode/src/vm.rs | 11 ++++- crates/saft-macro/src/discover_tests.rs | 2 + .../res/tests/items/functions/assignment.saf | 9 ++++ tests/res/tests/items/functions/order.saf | 8 ++++ tests/src/lib.rs | 41 +++++++++---------- 5 files changed, 49 insertions(+), 22 deletions(-) create mode 100644 tests/res/tests/items/functions/assignment.saf create mode 100644 tests/res/tests/items/functions/order.saf diff --git a/crates/saft-bytecode/src/vm.rs b/crates/saft-bytecode/src/vm.rs index ff80f47..1e188fd 100644 --- a/crates/saft-bytecode/src/vm.rs +++ b/crates/saft-bytecode/src/vm.rs @@ -120,7 +120,16 @@ impl Vm { let op = call_frame.chunk.get_op(call_frame.i).unwrap().clone(); let s = call_frame.chunk.get_span(call_frame.i).unwrap().clone(); - self.exec_op(&op, &s, constants)?; + match self.exec_op(&op, &s, constants) { + Ok(_) => {} + Err(err) => { + // Pop all call stacks but the global one + while self.call_stack.len() > 1 { + self.call_stack.pop(); + } + return Err(err); + } + }; } Ok(()) diff --git a/crates/saft-macro/src/discover_tests.rs b/crates/saft-macro/src/discover_tests.rs index b461102..b956989 100644 --- a/crates/saft-macro/src/discover_tests.rs +++ b/crates/saft-macro/src/discover_tests.rs @@ -110,6 +110,8 @@ pub fn expand_discover_tests( let node_quote = create_tests(node, test_fn_ident); toks.push(quote! { mod #id { + use super::#test_fn_ident; + #node_quote } diff --git a/tests/res/tests/items/functions/assignment.saf b/tests/res/tests/items/functions/assignment.saf new file mode 100644 index 0000000..7057f2c --- /dev/null +++ b/tests/res/tests/items/functions/assignment.saf @@ -0,0 +1,9 @@ +fn f(x) { + print("Okay boomer " + x) +} + +x := f; +x(1); + +# output: +# Okay boomer 1 diff --git a/tests/res/tests/items/functions/order.saf b/tests/res/tests/items/functions/order.saf new file mode 100644 index 0000000..f257a5a --- /dev/null +++ b/tests/res/tests/items/functions/order.saf @@ -0,0 +1,8 @@ +printadd(1, 2); + +fn printadd(a, b) { + print(a + b); +} + +# output: +# 3 diff --git a/tests/src/lib.rs b/tests/src/lib.rs index 3b10347..e0b1188 100644 --- a/tests/src/lib.rs +++ b/tests/src/lib.rs @@ -1,29 +1,28 @@ -#[cfg(test)] -mod test { - use assert_cmd::Command; - use pretty_assertions::assert_eq; - use saft_macro::discover_tests; +#![cfg(test)] - #[discover_tests(root = "./tests/res/tests", glob = "**/[!_]*.saf")] - fn test(file_name: &str) { - let mut cmd = Command::cargo_bin("saft").unwrap(); - cmd.arg(file_name); +use assert_cmd::Command; +use pretty_assertions::assert_eq; +use saft_macro::discover_tests; - let got = String::from_utf8(cmd.unwrap().stdout).unwrap(); +#[discover_tests(root = "./tests/res/tests", glob = "**/[!_]*.saf")] +fn test_file(file_name: &str) { + let mut cmd = Command::cargo_bin("saft").unwrap(); + cmd.arg(file_name); - let mut expected = Vec::new(); + let got = String::from_utf8(cmd.unwrap().stdout).unwrap(); - let mut got_output = false; - for line in std::fs::read_to_string(file_name).unwrap().lines() { - if let Some(comment) = line.strip_prefix("# ") { - if got_output { - expected.push(comment.to_string()); - } else if comment == "output:" { - got_output = true; - } + let mut expected = Vec::new(); + + let mut got_output = false; + for line in std::fs::read_to_string(file_name).unwrap().lines() { + if let Some(comment) = line.strip_prefix("# ") { + if got_output { + expected.push(comment.to_string()); + } else if comment == "output:" { + got_output = true; } } - - assert_eq!(got.trim(), expected.join("\n").trim()); } + + assert_eq!(got.trim(), expected.join("\n").trim()); }