added a bit of tests

This commit is contained in:
Erin 2022-07-21 17:27:37 +02:00 committed by ondra05
parent 9e45589810
commit c49b36a1e9
2 changed files with 84 additions and 0 deletions

View file

@ -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::<Vec<_>>(), 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,
],
)
}
}

View file

@ -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()),
]))),
])))],
)
}
}