parser/
ast_tree_test.rs

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    // https://astexplorer.net/#/gist/3a8ce5192e08ab973d255db5295671b1/831267b339e5562244c88f88587744153fbcfb6b
21    #[test]
22    fn test_let() {
23        let input = "let a = 3";
24        test_ast_tree("test_let", input)
25    }
26
27    // https://astexplorer.net/#/gist/0911a07ddb31d261074d1d59f6291a7c/f462e81e4940309f2c6a694a3bdedd283e7b035d
28    #[test]
29    fn test_string() {
30        let input = r#""jw""#;
31        test_ast_tree("test_string", input)
32    }
33
34    // https://astexplorer.net/#/gist/0911a07ddb31d261074d1d59f6291a7c/e74ab41b095abe19f2dd0c0398ffae94674d0a8c
35    #[test]
36    fn test_array() {
37        let input = "[1, true]";
38        test_ast_tree("test_array", input)
39    }
40
41    // https://astexplorer.net/#/gist/0911a07ddb31d261074d1d59f6291a7c/9263c8a45953e56d209597e90299547a733622a9
42    #[test]
43    fn test_hash() {
44        let input = r#"{"a": 1}"#;
45        test_ast_tree("test_hash", input)
46    }
47
48    // https://astexplorer.net/#/gist/0911a07ddb31d261074d1d59f6291a7c/ebbaf31807ccdcec6605aaa2d3a222258cac7f28
49    #[test]
50    fn test_return() {
51        let input = "return 3";
52        test_ast_tree("test_return", input)
53    }
54
55    // https://astexplorer.net/#/gist/0911a07ddb31d261074d1d59f6291a7c/5bf612e5f406bc59076f54371671af05022a74d0
56    #[test]
57    fn test_unary() {
58        let input = "-3";
59        test_ast_tree("test_unary", input)
60    }
61
62    // https://astexplorer.net/#/gist/0911a07ddb31d261074d1d59f6291a7c/7a2707a45fffc15d322807ffa2738b16b2690a67
63    #[test]
64    fn test_binary() {
65        let input = "1 + 2 * 3";
66        test_ast_tree("test_binary", input)
67    }
68
69    // https://astexplorer.net/#/gist/0911a07ddb31d261074d1d59f6291a7c/ab013f6feb719e8bece669b375c8b2d43f2231d0
70    #[test]
71    fn test_binary_nested() {
72        let input = "1+2+3";
73        test_ast_tree("test_binary_nested", input)
74    }
75
76    // https://astexplorer.net/#/gist/0911a07ddb31d261074d1d59f6291a7c/25ae0e103e732ace3d443a4bf0c620a44bedcd55
77    #[test]
78    fn test_if() {
79        let input = "if (x < y) { x } else { y }";
80        test_ast_tree("test_if", input)
81    }
82
83    // https://astexplorer.net/#/gist/0911a07ddb31d261074d1d59f6291a7c/9f5b73a95afba5c5300f67ac69f837048da77750
84    #[test]
85    fn test_func_declaration() {
86        let input = "fn(x) { x };";
87        test_ast_tree("test_func_declaration", input)
88    }
89
90    // https://astexplorer.net/#/gist/0911a07ddb31d261074d1d59f6291a7c/3213bab3987673584c55b2f5ff4d723369aecd8d
91    #[test]
92    fn test_func_call() {
93        let input = "add(1, 2)";
94        test_ast_tree("test_func_call", input)
95    }
96
97    // https://astexplorer.net/#/gist/0911a07ddb31d261074d1d59f6291a7c/3d5d8bfe7ec5b192674f46211408a07d88d14c65
98    #[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}