diff options
author | Dominick Allen <dominick.allen1989@gmail.com> | 2020-06-20 13:38:03 -0500 |
---|---|---|
committer | Dominick Allen <dominick.allen1989@gmail.com> | 2020-06-20 13:38:03 -0500 |
commit | 36e1bf722a3d366cea20ab7315d63d588d23dc48 (patch) | |
tree | 84ef440e398978bfd4467a99d375d7710f016031 /src/tests |
Working on a simple LISP/scheme interpreter.
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/mod.rs | 1 | ||||
-rw-r--r-- | src/tests/parse_test.rs | 67 |
2 files changed, 68 insertions, 0 deletions
diff --git a/src/tests/mod.rs b/src/tests/mod.rs new file mode 100644 index 0000000..0b03ef2 --- /dev/null +++ b/src/tests/mod.rs @@ -0,0 +1 @@ +pub mod parse_test; diff --git a/src/tests/parse_test.rs b/src/tests/parse_test.rs new file mode 100644 index 0000000..b9e98b4 --- /dev/null +++ b/src/tests/parse_test.rs @@ -0,0 +1,67 @@ +use super::super::lib::types::Op::*; +use super::super::lib::types::Type::*; +use super::super::lib::types::Number; +use super::super::lib::parse::{is_paren, is_op, is_bool, is_var, is_number}; +use super::super::lib::parse::Token::*; +use super::super::lib::parse::TokenStream; + +#[test] +pub fn token_test() { + let add_string = "(+ 1 1)".to_string(); + let add_tokens = vec!(LParen, Value(Operator(Add)), Value(Number(Number::Int(1))), + Value(Number(Number::Int(1))), RParen); + + let tokenstream = TokenStream::new(add_string, + vec!(is_paren, is_op, is_bool, is_var, is_number)); + + let mut tokens = Vec::new(); + for token in tokenstream { + match token { + Ok(x) => tokens.push(x), + Err(f) => panic!(f) + } + } + assert!(tokens.len() == add_tokens.len()); + for (token1, token2) in tokens.iter().zip(add_tokens) { + assert!(token1 == &token2); + } +} + +#[test] +pub fn types_test() { + let foo = "(= (> 3.0 0) true)".to_string(); + + let tokenstream = TokenStream::new(foo, + vec!(is_paren, is_op, is_bool, is_var, is_number)); + + let correct_tokens = vec!(LParen, Value(Operator(Equals)), LParen, + Value(Operator(Greater)), + Value(Number(Number::Float(3.0))), + Value(Number(Number::Int(0))), + RParen, Value(Bool(true)), RParen); + + for (foo_token, correct_token) in tokenstream.zip(correct_tokens.into_iter()) { + match foo_token { + Ok(x) => assert!(x == correct_token), + Err(f) => panic!(f) + } + } +} + +#[test] +pub fn string_test() { + let bar = "(define foo \t \n \t \"Bar \\\" Baz\")"; + let tokenstream = TokenStream::default(bar); + let correct_tokens = vec!(LParen, Value(Operator(Define)), Value(Symbol("foo".to_string())), + Value(Str("Bar \\\" Baz".to_string())), RParen); + for (bar_token, correct_token) in tokenstream.zip(correct_tokens.into_iter()) { + match bar_token { + Ok(x) => { + if x != correct_token { + panic!(format!("{:?} != {:?}", x, correct_token)) + } + }, + Err(f) => panic!(f) + } + } +} |