1#[cfg(test)]
2mod tests {
3 use crate::token::{Token, TokenKind};
4 use crate::Lexer;
5 use insta::*;
6
7 fn test_token_set(l: &mut Lexer) -> Vec<Token> {
8 let mut token_vs: Vec<Token> = vec![];
9 loop {
10 let t = l.next_token();
11 if t.kind == TokenKind::EOF {
12 token_vs.push(t);
13 break;
14 } else {
15 token_vs.push(t);
16 }
17 }
18 token_vs
19 }
20
21 pub fn test_lexer_common(name: &str, input: &str) {
22 let mut l = Lexer::new(input);
23 let token_vs = test_token_set(&mut l);
24
25 assert_snapshot!(name, serde_json::to_string_pretty(&token_vs).unwrap(), input);
26 }
27
28 #[test]
29 fn test_lexer_simple() {
30 test_lexer_common("simple", "=+(){},:;");
31 }
32
33 #[test]
34 fn test_lexer_let() {
35 test_lexer_common("let", "let x=5");
36 }
37
38 #[test]
39 fn test_comments() {
40 test_lexer_common("comments", "// I am comments");
41 }
42
43 #[test]
44 fn test_lexer_let_with_space() {
45 test_lexer_common("let_with_space", "let x = 5");
46 }
47
48 #[test]
49 fn test_lexer_string() {
50 test_lexer_common("string", r#""a""#);
51 }
52
53 #[test]
54 fn test_lexer_array() {
55 test_lexer_common("array", "[3]");
56 }
57
58 #[test]
59 fn test_lexer_hash() {
60 test_lexer_common("hash", r#"{"one": 1, "two": 2, "three": 3}"#);
61 }
62
63 #[test]
64 fn test_lexer_bool() {
65 test_lexer_common("bool", "let y=true");
66 }
67
68 #[test]
69 fn test_lexer_complex() {
70 test_lexer_common(
71 "complex",
72 "
73// welcome to monkeylang
74let five = 5;
75let ten = 10;
76
77let add = fn(x, y) {
78 x + y;
79};
80
81let result = add(five, ten);
82!-/*5;
835 < 10 > 5;
84
85if (5 < 10) {
86 return true;
87} else {
88 return false;
89}
90
9110 == 10;
9210 != 9;",
93 );
94 }
95}