1
use conjure_cp_essence_parser::diagnostics::diagnostics_api::get_diagnostics;
2
use conjure_cp_essence_parser::diagnostics::error_detection::collect_errors::check_diagnostic;
3
use conjure_cp_essence_parser::util::get_tree;
4

            
5
#[test]
6
1
fn invalid_top_level_statement_expression() {
7
1
    let source = " a,a,b: int(1..3)";
8
1
    let (cst, _) = get_tree(&source).unwrap();
9

            
10
1
    let diags = get_diagnostics(&source, &cst);
11

            
12
1
    assert_eq!(
13
1
        diags.len(),
14
        1,
15
        "Expected a valid top-level statement, but got 'a,a,b: int(1..3)'"
16
    );
17
1
    let diag = &diags[0];
18

            
19
1
    check_diagnostic(
20
1
        diag,
21
        0,
22
        0,
23
        0,
24
        17,
25
1
        "Expected a valid top-level statement, but got 'a,a,b: int(1..3)'",
26
    );
27
1
}
28

            
29
#[test]
30
1
fn malformed_find_2() {
31
1
    let source = "find >=lex,b,c: int(1..3)";
32
1
    let (cst, _) = get_tree(&source).unwrap();
33

            
34
    // using >=lex operator instead of identifier
35
1
    let diagnostics = get_diagnostics(&source, &cst);
36
1
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
37

            
38
1
    let diag = &diagnostics[0];
39
1
    check_diagnostic(
40
1
        diag,
41
        0,
42
        0,
43
        0,
44
        25,
45
1
        "Expected a find declaration statement, but got 'find >=lex,b,c: int(1..3)'",
46
    );
47
1
}
48

            
49
#[test]
50
1
fn malformed_find_3() {
51
1
    let source = "find +,a,b: int(1..3)";
52
1
    let (cst, _) = get_tree(&source).unwrap();
53

            
54
    // println!("{}", cst.root_node().to_sexp());
55

            
56
1
    let diags = get_diagnostics(&source, &cst);
57
    // debug print the diagnostics for easier debugging
58
    // for diag in &diags {
59
    //     println!("Diagnostic: {:?}", diag);
60
    // }
61
1
    assert_eq!(diags.len(), 1, "Expected exactly one diagnostic");
62
1
    let diag = &diags[0];
63
1
    check_diagnostic(
64
1
        diag,
65
        0,
66
        0,
67
        0,
68
        21,
69
1
        "Expected a find declaration statement, but got 'find +,a,b: int(1..3)'",
70
    );
71
1
}
72

            
73
#[test]
74
1
fn unexpected_colon_used_as_identifier() {
75
1
    let source = "find :,b,c: int(1..3)";
76
1
    let (cst, _) = get_tree(&source).unwrap();
77

            
78
1
    let diagnostics = get_diagnostics(&source, &cst);
79

            
80
    // Should be exactly one diagnostic
81
1
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
82
1
    let diag = &diagnostics[0];
83

            
84
1
    check_diagnostic(
85
1
        diag,
86
        0,
87
        0,
88
        0,
89
        21,
90
1
        "Expected a find declaration statement, but got 'find :,b,c: int(1..3)'",
91
    );
92
1
}
93

            
94
#[test]
95
1
fn missing_colon_domain_in_find_statement_1st_line() {
96
1
    let source = "find x";
97
1
    let (cst, _) = get_tree(&source).unwrap();
98

            
99
1
    let diagnostics = get_diagnostics(&source, &cst);
100

            
101
    // Should be exactly one diagnostic
102
1
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
103

            
104
1
    let diag = &diagnostics[0];
105

            
106
1
    check_diagnostic(
107
1
        diag,
108
        0,
109
        0,
110
        0,
111
        6,
112
1
        "Expected a find declaration statement, but got 'find x'",
113
    );
114
1
}
115

            
116
#[test]
117
1
fn missing_colon_domain_in_find_statement_2nd_line() {
118
1
    let source = "find x: int(1..3)\nfind x";
119
1
    let (cst, _) = get_tree(&source).unwrap();
120

            
121
1
    let diagnostics = get_diagnostics(&source, &cst);
122

            
123
    // Should be exactly one diagnostic
124
1
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
125

            
126
1
    let diag = &diagnostics[0];
127
1
    check_diagnostic(
128
1
        diag,
129
        1,
130
        0,
131
        1,
132
        6,
133
1
        "Expected a find declaration statement, but got 'find x'",
134
    );
135
1
}
136

            
137
#[test]
138
1
fn unexpected_print_2nd_line() {
139
1
    let source = "find a,b,c: int(1..3)\nprint a";
140
1
    let (cst, _) = get_tree(&source).unwrap();
141

            
142
1
    let diagnostics = get_diagnostics(&source, &cst);
143

            
144
    // Should be exactly one diagnostic
145
1
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
146

            
147
1
    let diag = &diagnostics[0];
148
1
    check_diagnostic(
149
1
        diag,
150
        1,
151
        0,
152
        1,
153
        7,
154
1
        "Expected a valid top-level statement, but got 'print a'",
155
    );
156
1
}