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

            
4
#[test]
5
3
fn detects_undefined_variable() {
6
3
    let source = "find x: int(1..10)\nsuch that x = y";
7
    // y is undefined
8
3
    let diagnostics = get_diagnostics(source);
9

            
10
3
    assert_eq!(
11
3
        diagnostics.len(),
12
        1,
13
        "Expected exactly one diagnostic for undefined variable"
14
    );
15

            
16
3
    let diag = &diagnostics[0];
17

            
18
3
    check_diagnostic(diag, 1, 14, 1, 15, "The identifier 'y' is not defined");
19
3
}
20

            
21
#[test]
22
3
fn no_errors_for_valid_code() {
23
3
    let source = "find x, y: int(1..10)\nsuch that x + y = 10";
24
3
    let diagnostics = get_diagnostics(source);
25

            
26
    // should have no diagnostics
27
3
    assert_eq!(
28
3
        diagnostics.len(),
29
        0,
30
        "Expected no diagnostics for valid code, got: {:?}",
31
        diagnostics
32
    );
33
3
}
34

            
35
#[test]
36
3
fn range_points_to_error_location() {
37
3
    let source = "find x: int(1..10)\nsuch that x = undefined_var";
38
3
    let diagnostics = get_diagnostics(source);
39

            
40
3
    assert_eq!(
41
3
        diagnostics.len(),
42
        1,
43
        "Expected exactly one diagnostic for undefined variable"
44
    );
45

            
46
3
    let diag = &diagnostics[0];
47

            
48
3
    check_diagnostic(
49
3
        diag,
50
        1,
51
        14,
52
        1,
53
        27,
54
3
        "The identifier 'undefined_var' is not defined",
55
    );
56
3
}
57

            
58
// not enforced in conjure
59
#[ignore]
60
#[test]
61
fn domain_start_greater_than_end() {
62
    let source = "find x: int(10..1)";
63
    let diagnostics = get_diagnostics(source);
64

            
65
    assert_eq!(
66
        diagnostics.len(),
67
        1,
68
        "Expected exactly one diagnostic for undefined variable"
69
    );
70

            
71
    let diag = &diagnostics[0];
72

            
73
    check_diagnostic(
74
        diag,
75
        0,
76
        12,
77
        0,
78
        17,
79
        "Start value greater than end value in 'domain'",
80
    );
81
}
82

            
83
#[ignore]
84
#[test]
85
fn incorrect_type_for_equation() {
86
    let source = "
87
    letting y be false\n
88
    find x: int(5..10)\n
89
    such that 5 + y = 6";
90
    let diagnostics = get_diagnostics(source);
91

            
92
    assert_eq!(
93
        diagnostics.len(),
94
        1,
95
        "Expected exactly one diagnostic for undefined variable"
96
    );
97

            
98
    let diag = &diagnostics[0];
99

            
100
    check_diagnostic(
101
        diag,
102
        2,
103
        14,
104
        2,
105
        15,
106
        "Incorrect type 'bool' for variable 'y', expected 'int'",
107
    );
108
}
109

            
110
#[ignore]
111
#[test]
112
fn dividing_over_zero() {
113
    let source = "find x: int(5..10)\nsuch that x/0 = 3";
114
    let diagnostics = get_diagnostics(source);
115

            
116
    assert_eq!(
117
        diagnostics.len(),
118
        1,
119
        "Expected exactly one diagnostic for undefined variable"
120
    );
121

            
122
    let diag = &diagnostics[0];
123

            
124
    check_diagnostic(diag, 1, 10, 1, 13, "Unsafe division attempted");
125
}
126

            
127
#[ignore]
128
#[test]
129
fn invalid_index() {
130
    let source = "letting s be (0,1,1,0)
131
                \nletting t be (0,0,0,1)
132
                \nfind a : bool such that a = (s[5] = t[1])";
133
    let diagnostics = get_diagnostics(source);
134

            
135
    assert_eq!(
136
        diagnostics.len(),
137
        1,
138
        "Expected exactly one diagnostic for undefined variable"
139
    );
140

            
141
    let diag = &diagnostics[0];
142

            
143
    check_diagnostic(diag, 2, 31, 2, 32, "Index out of bounds");
144
}
145

            
146
#[ignore]
147
#[test]
148
fn duplicate_declaration_of_variable() {
149
    let source = "find x: int(1..10)\nfind x: int(2..3)";
150
    let diagnostics = get_diagnostics(source);
151

            
152
    assert_eq!(
153
        diagnostics.len(),
154
        1,
155
        "Expected exactly one diagnostic for undefined variable"
156
    );
157

            
158
    let diag = &diagnostics[0];
159

            
160
    check_diagnostic(
161
        diag,
162
        1,
163
        5,
164
        1,
165
        6,
166
        "Redeclaration of variable 'x' which was previously defined",
167
    );
168
}
169

            
170
#[ignore]
171
#[test]
172
fn extra_comma_in_variable_list() {
173
    let source = "find x,: int(1..10)";
174
    let diagnostics = get_diagnostics(source);
175

            
176
    assert_eq!(
177
        diagnostics.len(),
178
        1,
179
        "Expected exactly one diagnostic for undefined variable"
180
    );
181

            
182
    let diag = &diagnostics[0];
183

            
184
    check_diagnostic(
185
        diag,
186
        0,
187
        6,
188
        0,
189
        7,
190
        "Semantic Error: Extra ',' at the end of 'variable_list'",
191
    );
192
}