~starkingdoms/starkingdoms

ref: d024fde6beb37c38cb2f0c7088a828aaa6b2a09d starkingdoms/crates/kabel/src/test.rs -rw-r--r-- 1.8 KiB
d024fde6 — ghostly_zsh oh shut up cargo.lock 8 months ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
use test_each_file::test_each_file;

use crate::{debug::{debug_ast, debug_token_array}, run_lexer, run_parser};

test_each_file! { for ["kab", "out"] in "./crates/kabel/test/runtime/" => test }
test_each_file! { for ["kab", "out"] in "./crates/kabel/test/lexer/" => test_lexer }
test_each_file! { for ["kab", "out"] in "./crates/kabel/test/syntax/" => test_parser }

fn test_lexer([program, out]: [&str; 2]) {
    let mut output = "".to_string();

    let lexer = run_lexer(program.to_string());

    for error in lexer.errors.clone() {
        output += &error.to_string();
        output += "\n";
    }
    if lexer.errors.len() != 0 || lexer.output.len() == 0 {
        assert_eq!(output, out);
        return;
    }
    output += &debug_token_array(lexer.output.clone());
    assert_eq!(output.trim(), out.trim());
}

fn test_parser([program, out]: [&str; 2]) {
    let mut output = "".to_string();

    let lexer = run_lexer(program.to_string());

    for error in lexer.errors.clone() {
        output += &error.to_string();
        output += "\n";
    }
    if lexer.errors.len() != 0 || lexer.output.len() == 0 {
        panic!("lexer error");
    }

    let (ast, parser) = run_parser(program.to_string(), lexer.output);

    for error in parser.errors.clone() {
        output += &error.to_string();
        output += "\n";
    }
    if parser.errors.len() != 0 {
        assert_eq!(output, out);
        return;
    }
    output += &debug_ast(ast, 0);
    /*let analyzer = run_semantic_analysis(program.to_string(), ast);
    for error in analyzer.errors.clone() {
        output += &error.to_string();
        output += "\n";
    }
    assert_eq!(kabel::compile(program.to_string()), output);*/
    assert_eq!(output.trim(), out.trim());
}
fn test([program, out]: [&str; 2]) {
    assert_eq!(crate::compile(program.to_string()).trim(), out.trim());
}