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
2
fn detects_undefined_variable() {
6
2
    let source = "find x: int(1..10)\nsuch that x = y";
7
    // y is undefined
8
2
    let diagnostics = get_diagnostics(source);
9

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

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

            
18
2
    check_diagnostic(diag, 1, 14, 1, 15, "Undefined variable: 'y'");
19
2
}
20

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

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

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

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

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

            
48
2
    check_diagnostic(diag, 1, 14, 1, 27, "Undefined variable: 'undefined_var'");
49
2
}
50

            
51
// not enforced in conjure
52
#[ignore]
53
#[test]
54
1
fn domain_start_greater_than_end() {
55
    let source = "find x: int(10..1)";
56
1
    let diagnostics = get_diagnostics(source);
57

            
58
    assert_eq!(
59
        diagnostics.len(),
60
        1,
61
        "Expected exactly one diagnostic for undefined variable"
62
    );
63

            
64
    let diag = &diagnostics[0];
65

            
66
    check_diagnostic(
67
        diag,
68
        0,
69
        12,
70
        0,
71
        17,
72
        "Start value greater than end value in 'domain'",
73
    );
74
}
75

            
76
#[ignore]
77
#[test]
78
fn incorrect_type_for_equation() {
79
    let source = "
80
    letting y be false\n
81
    find x: int(5..10)\n
82
    such that 5 + y = 6";
83
    let diagnostics = get_diagnostics(source);
84

            
85
    assert_eq!(
86
        diagnostics.len(),
87
        1,
88
        "Expected exactly one diagnostic for undefined variable"
89
    );
90

            
91
    let diag = &diagnostics[0];
92

            
93
    check_diagnostic(
94
        diag,
95
        2,
96
        14,
97
        2,
98
        15,
99
        "Incorrect type 'bool' for variable 'y', expected 'int'",
100
    );
101
}
102

            
103
#[ignore]
104
#[test]
105
fn dividing_over_zero() {
106
    let source = "find x: int(5..10)\nsuch that x/0 = 3";
107
    let diagnostics = get_diagnostics(source);
108

            
109
    assert_eq!(
110
        diagnostics.len(),
111
        1,
112
        "Expected exactly one diagnostic for undefined variable"
113
    );
114

            
115
    let diag = &diagnostics[0];
116

            
117
    check_diagnostic(diag, 1, 10, 1, 13, "Unsafe division attempted");
118
}
119

            
120
#[ignore]
121
#[test]
122
fn invalid_index() {
123
    let source = "letting s be (0,1,1,0)
124
                \nletting t be (0,0,0,1)
125
                \nfind a : bool such that a = (s[5] = t[1])";
126
    let diagnostics = get_diagnostics(source);
127

            
128
    assert_eq!(
129
        diagnostics.len(),
130
        1,
131
        "Expected exactly one diagnostic for undefined variable"
132
    );
133

            
134
    let diag = &diagnostics[0];
135

            
136
    check_diagnostic(diag, 2, 31, 2, 32, "Index out of bounds");
137
}
138

            
139
#[ignore]
140
#[test]
141
fn duplicate_declaration_of_variable() {
142
    let source = "find x: int(1..10)\nfind x: int(2..3)";
143
    let diagnostics = get_diagnostics(source);
144

            
145
    assert_eq!(
146
        diagnostics.len(),
147
        1,
148
        "Expected exactly one diagnostic for undefined variable"
149
    );
150

            
151
    let diag = &diagnostics[0];
152

            
153
    check_diagnostic(
154
        diag,
155
        1,
156
        5,
157
        1,
158
        6,
159
        "Redeclaration of variable 'x' which was previously defined",
160
    );
161
}
162

            
163
#[ignore]
164
#[test]
165
fn extra_comma_in_variable_list() {
166
    let source = "find x,: int(1..10)";
167
    let diagnostics = get_diagnostics(source);
168

            
169
    assert_eq!(
170
        diagnostics.len(),
171
        1,
172
        "Expected exactly one diagnostic for undefined variable"
173
    );
174

            
175
    let diag = &diagnostics[0];
176

            
177
    check_diagnostic(
178
        diag,
179
        0,
180
        6,
181
        0,
182
        7,
183
        "Semantic Error: Extra ',' at the end of 'variable_list'",
184
    );
185
}