diff --git a/src/lexer.rs b/src/lexer.rs index e3a90a1..e78dcae 100644 --- a/src/lexer.rs +++ b/src/lexer.rs @@ -262,33 +262,3 @@ impl Iterator for Lexer<'_> { self.next_token() } } - -#[test] -fn test_lexer() { - use TokenKind as TK; - use TokenValue as TV; - - let lex = |text| { - Lexer::new(text, Rc::new(Mutex::new(ErrorAcc::new()))) - .map(|token| (token.kind, token.value)) - .collect::>() - }; - - assert_eq!(lex("abc"), vec![(TK::Id, TV::Id(hash("abc")))]); - assert_eq!(lex("123"), vec![(TK::Int, TV::Int(123))]); - assert_eq!(lex("\"\""), vec![(TK::Str, TV::Str("".to_string()))]); - assert_eq!( - lex("\"hello\""), - vec![(TK::Str, TV::Str("hello".to_string()))] - ); - assert_eq!( - lex("\"new\\nline\""), - vec![(TK::Str, TV::Str("new\nline".to_string()))] - ); - assert_eq!( - lex("\"backslash\\\\\""), - vec![(TK::Str, TV::Str("backslash\\".to_string()))] - ); - assert_eq!(lex("->"), vec![(TK::MinusLt, TV::None)]); - assert_eq!(lex("let"), vec![(TK::Let, TV::None)]); -} diff --git a/src/parser.rs b/src/parser.rs index 464cb7b..053d5a9 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -425,227 +425,3 @@ impl<'a> Parser<'a> { self.current.as_ref().map(|t| t.kind.clone()) } } - -#[test] -fn test_parser() { - use crate::util::hash; - use assert_matches::assert_matches; - // use pretty_assertions::assert_eq; - use NodeKind::*; - - macro_rules! node { - ($kind:pat) => { - Node { kind: $kind, .. } - }; - } - - let parse = |text| Parser::new(text, Rc::new(Mutex::new(ErrorAcc::new()))).parse(); - - #[allow(non_snake_case)] - fn B(v: T) -> Box { - Box::new(v) - } - - assert_matches!(parse("abc;")[..], [node!(Id(id))] if id == hash("abc")); - assert_matches!(parse("123;")[..], [node!(Int(123))]); - assert_matches!(&parse("\"hello\";")[..], [node!(Str(v))] if *v == "hello".to_string()); - assert_matches!(parse("0;")[..], [node!(Int(0))]); - assert_matches!(parse("0;abc;")[..], [node!(Int(0)), node!(Id(id))] if id == hash("abc")); - assert_eq!( - parse("add(mul(12, 34), 56);"), - vec![Node { - kind: Call { - subject: B(Node { - kind: Id(hash("add")), - pos: Pos { - index: 0, - line: 1, - col: 1 - } - }), - args: vec![ - Node { - kind: Call { - subject: B(Node { - kind: Id(14581412793212634142), - pos: Pos { - index: 4, - line: 1, - col: 5 - } - }), - args: vec![ - Node { - kind: Int(12), - pos: Pos { - index: 8, - line: 1, - col: 9 - } - }, - Node { - kind: Int(34), - pos: Pos { - index: 12, - line: 1, - col: 13 - } - } - ] - }, - pos: Pos { - index: 4, - line: 1, - col: 5 - } - }, - Node { - kind: Int(56), - pos: Pos { - index: 17, - line: 1, - col: 18 - } - } - ] - }, - pos: Pos { - index: 0, - line: 1, - col: 1 - } - }] - ); - assert_matches!( - &parse("a = 123;")[..], - [node!(Assign { - subject, - value - })] if matches!(subject.kind, Id(id) if id == hash("a")) && matches!(value.kind, Int(123)) - ); - assert_matches!(parse("break;")[..], [node!(Break)]); - assert_matches!(parse("return;")[..], [node!(Return { value: None })]); - assert_eq!( - parse("return add(1, 2);")[..], - vec![Node { - kind: Return { - value: Some(B(Node { - kind: Call { - subject: B(Node { - kind: Id(hash("add")), - pos: Pos { - index: 7, - line: 1, - col: 8 - } - }), - args: vec![ - Node { - kind: Int(1), - pos: Pos { - index: 11, - line: 1, - col: 12 - } - }, - Node { - kind: Int(2), - pos: Pos { - index: 14, - line: 1, - col: 15 - } - } - ] - }, - pos: Pos { - index: 7, - line: 1, - col: 8 - } - })) - }, - pos: Pos { - index: 0, - line: 1, - col: 1 - } - }] - ); - assert_matches!( - &parse("a = 5;")[..], - [node!(Assign { - subject, - value - })] if matches!(subject.kind, Id(id) if id == hash("a")) && matches!(value.kind, Int(5)) - ); - assert_eq!( - parse("let a = 5;")[..], - vec![Node { - kind: Let { - subject: B(Node { - kind: Param { - subject: B(Node { - kind: Id(hash("a")), - pos: Pos { - index: 4, - line: 1, - col: 5 - } - }), - typ: None - }, - pos: Pos { - index: 4, - line: 1, - col: 5 - } - }), - value: B(Node { - kind: Int(5), - pos: Pos { - index: 8, - line: 1, - col: 9 - } - }) - }, - pos: Pos { - index: 0, - line: 1, - col: 1 - } - }] - ); - assert_matches!( - &parse("fn test() -> i32 {}")[..], - [node!(Fn { - subject, - params, - return_typ, - body - })] if subject.kind == Id(hash("test")) && *params == vec![] && return_typ.kind == Id(hash("i32")) && body.kind == Block(vec![]) - ); - assert_matches!( - &parse("if 0 {}")[..], - [node!(If { - cond, - truthy, - falsy: None - })] if matches!(cond.kind, Int(0)) && truthy.kind == Block(vec![]) - ); - assert_matches!( - &parse("if 0 {} else {}")[..], - [node!(If { - cond, - truthy, - falsy: Some(falsy), - })] if matches!(cond.kind, Int(0)) && truthy.kind == Block(vec![]) && falsy.kind == Block(vec![]) - ); - assert_matches!( - &parse("loop {}")[..], - [node!(Loop { - body, - })] if body.kind == Block(vec![]) - ); -}