diff options
| -rw-r--r-- | src/lib.rs | 31 | ||||
| -rw-r--r-- | src/typer.rs | 23 |
2 files changed, 48 insertions, 6 deletions
@@ -32,15 +32,15 @@ pub fn run(tokens: TokenStream, path: &Path, print: bool) -> Result<(), Vec<Erro #[cfg(test)] mod tests { use super::{run_file, run_string}; - use crate::error::{Error, ErrorKind}; use std::path::Path; + #[macro_export] macro_rules! assert_errs { ($result:expr, [ $( $kind:pat ),* ]) => { println!("{} => {:?}", stringify!($result), $result); assert!(matches!( $result.unwrap_err().as_slice(), - &[$(Error { + &[$($crate::error::Error { kind: $kind, file: _, line: _, @@ -51,21 +51,40 @@ mod tests { }; } - #[test] - fn unreachable_token() { - assert_errs!(run_string("<!>\n", true), [ErrorKind::Unreachable]); + #[macro_export] + macro_rules! test_string { + ($fn:ident, $prog:literal) => { + #[test] + fn $fn() { + $crate::run_string($prog, true).unwrap(); + } + }; + ($fn:ident, $prog:literal, $errs:tt) => { + #[test] + fn $fn() { + $crate::assert_errs!($crate::run_string($prog, true), $errs); + } + } } + #[macro_export] macro_rules! test_file { ($fn:ident, $path:literal) => { #[test] fn $fn() { let file = Path::new($path); - assert!(run_file(&file, true).is_ok()); + run_file(&file, true).unwrap(); } }; } + use crate::error::ErrorKind; + + #[test] + fn unreachable_token() { + assert_errs!(run_string("<!>\n", true), [ErrorKind::Unreachable]); + } + test_file!(order_of_operations, "tests/order-of-operations.tdy"); test_file!(variables, "tests/variables.tdy"); test_file!(scoping, "tests/scoping.tdy"); diff --git a/src/typer.rs b/src/typer.rs index d6692d8..c7d0d92 100644 --- a/src/typer.rs +++ b/src/typer.rs @@ -332,3 +332,26 @@ impl VM { } } } + +#[cfg(test)] +mod tests { + use crate::test_string; + use crate::error::ErrorKind; + + test_string!(uncallable_type, " + f := fn i: int { + i() + } + ", + [ErrorKind::TypeError(_, _)]); + + test_string!(wrong_params, " + f : fn -> int = fn a: int -> int {} + ", + [ErrorKind::TypeError(_, _)]); + + test_string!(wrong_ret, " + f : fn -> int = fn {} + ", + [ErrorKind::TypeError(_, _)]); +} |
