From a42415a90a55447da90e8b533e0a7df773f75972 Mon Sep 17 00:00:00 2001 From: Natapat Samutpong Date: Sun, 6 Mar 2022 23:02:54 +0700 Subject: [PATCH] some test --- crates/lexer/src/lib.rs | 21 +++++++++++++++++++++ crates/parser/src/lib.rs | 20 ++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/crates/lexer/src/lib.rs b/crates/lexer/src/lib.rs index b4684cd..bad50cd 100644 --- a/crates/lexer/src/lib.rs +++ b/crates/lexer/src/lib.rs @@ -129,4 +129,25 @@ pub fn lexer() -> impl Parser, Error = Simple> { pub fn lex(src: String) -> (Option)>>, Vec>) { let (tokens, lex_error) = lexer().parse_recovery(src.as_str()); return (tokens, lex_error); +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn lex_let_simple() { + let (tokens, err) = lex("let x: Int = 1;".to_string()); + + assert_eq!(tokens, Some(vec![ + (Token::KwLet, 0..3), + (Token::Identifier("x".to_string()), 4..5), + (Token::Colon, 5..6), + (Token::Identifier("Int".to_string()), 7..10), + (Token::Assign, 11..12), + (Token::Int(1), 13..14), + (Token::SemiColon, 14..15), + ])); + assert_eq!(err, vec![]); + } } \ No newline at end of file diff --git a/crates/parser/src/lib.rs b/crates/parser/src/lib.rs index 287e85c..e87f08d 100644 --- a/crates/parser/src/lib.rs +++ b/crates/parser/src/lib.rs @@ -228,4 +228,24 @@ pub fn parse(tokens: Vec<(Token, std::ops::Range)>, len: usize) -> (Optio )); return (ast, parse_error) +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parse_simple() { + let (_, err) = parse(vec![ + (Token::KwLet, 0..3), + (Token::Identifier("x".to_string()), 4..5), + (Token::Colon, 5..6), + (Token::Identifier("Int".to_string()), 7..10), + (Token::Assign, 11..12), + (Token::Int(1), 13..14), + (Token::SemiColon, 14..15), + ], 15); + + assert_eq!(err, vec![]); + } } \ No newline at end of file