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 unexpected_closing_paren() {
7
1
    let source = "find x: int(1..3))";
8
1
    let (cst, _) = get_tree(&source).unwrap();
9

            
10
1
    let diagnostics = get_diagnostics(&source, &cst);
11
1
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
12
1
    let diag = &diagnostics[0];
13
1
    check_diagnostic(diag, 0, 17, 0, 18, "Unexpected )");
14
1
}
15

            
16
#[test]
17
1
fn unexpected_identifier_in_range() {
18
1
    let source = "find x: int(1..3x)";
19
1
    let (cst, _) = get_tree(&source).unwrap();
20

            
21
1
    let diagnostics = get_diagnostics(&source, &cst);
22
1
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
23
1
    let diag = &diagnostics[0];
24
1
    check_diagnostic(diag, 0, 16, 0, 17, "Unexpected x inside an Integer Domain");
25
1
}
26

            
27
#[test]
28
1
fn unexpected_semicolon() {
29
1
    let source = "\
30
1
find x: int(1..3)
31
1
such that x = 6;
32
1
        ";
33
1
    let (cst, _) = get_tree(&source).unwrap();
34

            
35
1
    let diagnostics = get_diagnostics(&source, &cst);
36
1
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
37
1
    let diag = &diagnostics[0];
38
1
    check_diagnostic(diag, 1, 15, 1, 16, "Unexpected ;");
39
1
}
40

            
41
#[test]
42
1
fn unexpected_extra_comma_in_find() {
43
1
    let source = "find x,, y: int(1..3)";
44
1
    let (cst, _) = get_tree(&source).unwrap();
45

            
46
1
    let diagnostics = get_diagnostics(&source, &cst);
47
1
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
48
1
    let diag = &diagnostics[0];
49
1
    check_diagnostic(diag, 0, 6, 0, 7, "Unexpected , inside a Variable List");
50
1
}
51

            
52
#[test]
53
1
fn unexpected_token_in_implication() {
54
1
    let source = "\
55
1
find x: int(1..3)
56
1
such that x -> %9
57
1
";
58
1
    let (cst, _) = get_tree(&source).unwrap();
59

            
60
1
    let diagnostics = get_diagnostics(&source, &cst);
61
1
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
62
1
    let diag = &diagnostics[0];
63
1
    check_diagnostic(diag, 1, 15, 1, 16, "Unexpected % inside an Implication");
64
1
}
65

            
66
#[test]
67
1
fn unexpected_token_in_matrix_domain() {
68
1
    let source = "find x: matrix indexed by [int, &] of int";
69
1
    let (cst, _) = get_tree(&source).unwrap();
70

            
71
1
    let diagnostics = get_diagnostics(&source, &cst);
72
1
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
73
1
    let diag = &diagnostics[0];
74
1
    check_diagnostic(diag, 0, 32, 0, 33, "Unexpected & inside a Matrix Domain");
75
1
}
76

            
77
#[test]
78
1
fn unexpected_token_in_set_literal() {
79
1
    let source = "find x: set of int\nsuch that x = {1, 2, @}";
80
1
    let (cst, _) = get_tree(&source).unwrap();
81

            
82
1
    let diagnostics = get_diagnostics(&source, &cst);
83
1
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
84
1
    let diag = &diagnostics[0];
85
1
    check_diagnostic(diag, 1, 21, 1, 22, "Unexpected @ inside a Set");
86
1
}
87

            
88
// Multiple unexpected tokens
89
// One at the end of a find statement, one inside a set
90
#[test]
91
1
fn multiple_unexpected_tokens() {
92
1
    let source = "\
93
1
find x: set of int;
94
1
such that x = {1, 2, @}";
95
1
    let (cst, _) = get_tree(&source).unwrap();
96

            
97
1
    let diagnostics = get_diagnostics(&source, &cst);
98
1
    assert_eq!(diagnostics.len(), 2, "Expected exactly two diagnostics");
99

            
100
    // First unexpected token: ';' at the end of domain
101
1
    let diag1 = &diagnostics[0];
102
1
    check_diagnostic(diag1, 0, 18, 0, 19, "Unexpected ;");
103

            
104
    // Second unexpected token: '@' in set literal
105
1
    let diag2 = &diagnostics[1];
106
1
    check_diagnostic(diag2, 1, 21, 1, 22, "Unexpected @ inside a Set");
107
1
}
108

            
109
#[test]
110
1
fn unexpected_x_in_all_diff() {
111
1
    let source = "\
112
1
find a : bool
113
1
such that a = allDiff([1,2,4,1]x)";
114
1
    let (cst, _) = get_tree(&source).unwrap();
115

            
116
1
    let diagnostics = get_diagnostics(&source, &cst);
117
1
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
118
1
    let diag = &diagnostics[0];
119

            
120
1
    check_diagnostic(
121
1
        diag,
122
        1,
123
        31,
124
        1,
125
        32,
126
1
        "Unexpected x inside an All Diff Comparison",
127
    );
128
1
}
129

            
130
#[test]
131
1
fn unexpected_int_at_the_end() {
132
1
    let source = "\
133
1
find a : bool
134
1
such that a = allDiff([1,2,4,1])8";
135
1
    let (cst, _) = get_tree(&source).unwrap();
136

            
137
1
    let diagnostics = get_diagnostics(&source, &cst);
138
1
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
139
1
    let diag = &diagnostics[0];
140
1
    check_diagnostic(diag, 1, 32, 1, 33, "Unexpected 8");
141
1
}
142

            
143
#[test]
144
1
fn unexpected_operand_at_end() {
145
1
    let source = "\
146
1
find x, a, b: int(1..3)+
147
1
";
148
1
    let (cst, _) = get_tree(&source).unwrap();
149

            
150
1
    let diagnostics = get_diagnostics(&source, &cst);
151
1
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
152
1
    let diag = &diagnostics[0];
153
1
    check_diagnostic(diag, 0, 23, 0, 24, "Unexpected +");
154
1
}
155

            
156
#[test]
157
1
fn unexpected_operand_middle_no_comma() {
158
1
    let source = "\
159
1
find x-, b: int(1..3)
160
1
";
161
1
    let (cst, _) = get_tree(&source).unwrap();
162

            
163
1
    let diagnostics = get_diagnostics(&source, &cst);
164
1
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
165
1
    let diag = &diagnostics[0];
166
1
    check_diagnostic(diag, 0, 6, 0, 7, "Unexpected - inside a Variable List");
167
1
}
168

            
169
#[test]
170
1
fn unexpected_operand_middle_comma() {
171
1
    let source = "\
172
1
find x,-, b: int(1..3)
173
1
";
174
1
    let (cst, _) = get_tree(&source).unwrap();
175

            
176
1
    let diagnostics = get_diagnostics(&source, &cst);
177
1
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
178
1
    let diag = &diagnostics[0];
179
1
    check_diagnostic(diag, 0, 6, 0, 8, "Unexpected ,- inside a Variable List");
180
1
}
181

            
182
#[test]
183
1
fn unexpected_token_in_identifier() {
184
1
    let source = "find v@lue: int(1..3)";
185
1
    let (cst, _) = get_tree(&source).unwrap();
186

            
187
1
    let diagnostics = get_diagnostics(&source, &cst);
188
1
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
189
1
    let diag = &diagnostics[0];
190
1
    check_diagnostic(
191
1
        diag,
192
        0,
193
        6,
194
        0,
195
        10,
196
1
        "Unexpected @lue inside a Variable Declaration",
197
    );
198
1
}
199

            
200
// Temporary before better logic is developed
201
#[test]
202
1
fn missing_right_operand_in_and_expr() {
203
1
    let source = "\
204
1
find x: int
205
1
such that x /\\
206
1
";
207
1
    let (cst, _) = get_tree(&source).unwrap();
208

            
209
1
    let diagnostics = get_diagnostics(&source, &cst);
210
1
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
211
1
    let diag = &diagnostics[0];
212
1
    check_diagnostic(diag, 1, 12, 1, 14, "Unexpected /\\");
213
1
}
214

            
215
// Temporary before better logic is developed
216
#[test]
217
1
fn unexpected_token_in_comparison() {
218
1
    let source = "\
219
1
find x: int
220
1
such that 5 =
221
1
    ";
222
1
    let (cst, _) = get_tree(&source).unwrap();
223

            
224
1
    let diagnostics = get_diagnostics(&source, &cst);
225
1
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
226
1
    let diag = &diagnostics[0];
227
1
    check_diagnostic(diag, 1, 12, 1, 13, "Unexpected =");
228
1
}
229

            
230
#[test]
231
1
fn unexpected_token_in_domain() {
232
    // not indented because have to avoid leading spaces for accurate character count
233
1
    let source = "find a: int(1.3)";
234

            
235
1
    let (cst, _) = get_tree(&source).unwrap();
236

            
237
1
    let diagnostics = get_diagnostics(&source, &cst);
238

            
239
    // Should be exactly one diagnostic
240
1
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
241
1
    let diag = &diagnostics[0];
242

            
243
1
    check_diagnostic(diag, 0, 13, 0, 15, "Unexpected .3 inside an Integer Domain");
244
1
}