diff --git a/src/lexer.rs b/src/lexer.rs index 5e56219..f1b6b1f 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -73,3 +73,62 @@ impl<'a> Display for Token<'a> { } } } + +#[cfg(test)] +mod tests { + use super::*; + use Token::*; + + fn assert_lex(src: &str, expected: &[Token]) { + assert_eq!(Token::lexer(src).collect::>(), expected) + } + #[test] + fn gibberish() { + assert_lex( + "(][)}'{\"ABLE\"corp :wisp ยจ", + &[ + LeftParen, + RightBracket, + LeftBracket, + RightParen, + RightCurly, + Quote, + LeftCurly, + String("ABLE"), + Symbol("corp"), + Keyword("wisp"), + Error, + ], + ) + } + + #[test] + fn function() { + assert_lex( + "(defun print-add [x y] (println (add x y))) (print-add 42 12)", + &[ + LeftParen, + Symbol("defun"), + Symbol("print-add"), + LeftBracket, + Symbol("x"), + Symbol("y"), + RightBracket, + LeftParen, + Symbol("println"), + LeftParen, + Symbol("add"), + Symbol("x"), + Symbol("y"), + RightParen, + RightParen, + RightParen, + LeftParen, + Symbol("print-add"), + Number(42.0.into()), + Number(12.0.into()), + RightParen, + ], + ) + } +} diff --git a/src/parser.rs b/src/parser.rs index 626426e..07b8088 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -61,3 +61,28 @@ fn stream_of_lexer<'a>( let len = lexer.source().len(); Stream::from_iter(len..len + 1, lexer.spanned()) } + +#[cfg(test)] +mod tests { + use super::*; + + fn assert_parse(src: &str, expected: &[Value]) { + assert_eq!(read(src).unwrap(), expected) + } + + #[test] + fn simple_fn() { + assert_parse( + "(defn test [] (println \"Hello, world!\"))", + &[Value::List(Box::new(List::from_vec(vec![ + Value::Symbol("defn".into()), + Value::Symbol("test".into()), + Value::Vector(vec![]), + Value::List(Box::new(List::from_vec(vec![ + Value::Symbol("println".into()), + Value::String("Hello, world!".into()), + ]))), + ])))], + ) + } +}