1#[cfg(test)]
2mod tests {
3 use crate::parse;
4 use insta::*;
5
6 pub fn test_ast_tree(name: &str, input: &str) {
7 let ast = match parse(input) {
8 Ok(node) => match serde_json::to_string_pretty(&node) {
9 Ok(x) => x,
10 Err(e) => {
11 println!("{:?}", node);
12 panic!("serde_json error: {} for input {}", e, input)
13 }
14 },
15 Err(e) => panic!("parse error: {} for input {}", e[0], input),
16 };
17 assert_snapshot!(name, ast, input);
18 }
19
20 #[test]
22 fn test_let() {
23 let input = "let a = 3";
24 test_ast_tree("test_let", input)
25 }
26
27 #[test]
29 fn test_string() {
30 let input = r#""jw""#;
31 test_ast_tree("test_string", input)
32 }
33
34 #[test]
36 fn test_array() {
37 let input = "[1, true]";
38 test_ast_tree("test_array", input)
39 }
40
41 #[test]
43 fn test_hash() {
44 let input = r#"{"a": 1}"#;
45 test_ast_tree("test_hash", input)
46 }
47
48 #[test]
50 fn test_return() {
51 let input = "return 3";
52 test_ast_tree("test_return", input)
53 }
54
55 #[test]
57 fn test_unary() {
58 let input = "-3";
59 test_ast_tree("test_unary", input)
60 }
61
62 #[test]
64 fn test_binary() {
65 let input = "1 + 2 * 3";
66 test_ast_tree("test_binary", input)
67 }
68
69 #[test]
71 fn test_binary_nested() {
72 let input = "1+2+3";
73 test_ast_tree("test_binary_nested", input)
74 }
75
76 #[test]
78 fn test_if() {
79 let input = "if (x < y) { x } else { y }";
80 test_ast_tree("test_if", input)
81 }
82
83 #[test]
85 fn test_func_declaration() {
86 let input = "fn(x) { x };";
87 test_ast_tree("test_func_declaration", input)
88 }
89
90 #[test]
92 fn test_func_call() {
93 let input = "add(1, 2)";
94 test_ast_tree("test_func_call", input)
95 }
96
97 #[test]
99 fn test_index() {
100 let input = "a[1]";
101 test_ast_tree("test_index", input)
102 }
103
104 #[test]
105 fn test_func_with_name() {
106 let input = "let my_func = fn(x) { x };";
107 test_ast_tree("test_func_with_name", input)
108 }
109}