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 unexpected_closing_paren() {
6
2
    let source = "find x: int(1..3))";
7
2
    let diagnostics = get_diagnostics(source);
8
2
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
9
2
    let diag = &diagnostics[0];
10
2
    check_diagnostic(diag, 0, 17, 0, 18, "Unexpected )");
11
2
}
12

            
13
#[test]
14
2
fn unexpected_identifier_in_range() {
15
2
    let source = "find x: int(1..3x)";
16
2
    let diagnostics = get_diagnostics(source);
17
2
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
18
2
    let diag = &diagnostics[0];
19
2
    check_diagnostic(diag, 0, 16, 0, 17, "Unexpected x inside an Integer Domain");
20
2
}
21

            
22
#[test]
23
2
fn unexpected_semicolon() {
24
2
    let source = "\
25
2
find x: int(1..3)
26
2
such that x = 6;
27
2
        ";
28
2
    let diagnostics = get_diagnostics(source);
29
2
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
30
2
    let diag = &diagnostics[0];
31
2
    check_diagnostic(diag, 1, 15, 1, 16, "Unexpected ;");
32
2
}
33

            
34
#[test]
35
2
fn unexpected_extra_comma_in_find() {
36
2
    let source = "find x,, y: int(1..3)";
37
2
    let diagnostics = get_diagnostics(source);
38
2
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
39
2
    let diag = &diagnostics[0];
40
2
    check_diagnostic(diag, 0, 6, 0, 7, "Unexpected , inside a Variable List");
41
2
}
42

            
43
#[test]
44
2
fn unexpected_token_in_implication() {
45
2
    let source = "\
46
2
find x: int(1..3)
47
2
such that x -> %9
48
2
";
49
2
    let diagnostics = get_diagnostics(source);
50
2
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
51
2
    let diag = &diagnostics[0];
52
2
    check_diagnostic(diag, 1, 15, 1, 16, "Unexpected % inside an Implication");
53
2
}
54

            
55
#[test]
56
2
fn unexpected_token_in_matrix_domain() {
57
2
    let source = "find x: matrix indexed by [int, &] of int";
58
2
    let diagnostics = get_diagnostics(source);
59
2
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
60
2
    let diag = &diagnostics[0];
61
2
    check_diagnostic(diag, 0, 32, 0, 33, "Unexpected & inside a Matrix Domain");
62
2
}
63

            
64
#[test]
65
2
fn unexpected_token_in_set_literal() {
66
2
    let source = "find x: set of int\nsuch that x = {1, 2, @}";
67
2
    let diagnostics = get_diagnostics(source);
68
2
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
69
2
    let diag = &diagnostics[0];
70
2
    check_diagnostic(diag, 1, 21, 1, 22, "Unexpected @ inside a Set");
71
2
}
72

            
73
// Multiple unexpected tokens
74
// One at the end of a find statement, one inside a set
75
#[test]
76
2
fn multiple_unexpected_tokens() {
77
2
    let source = "\
78
2
find x: set of int;
79
2
such that x = {1, 2, @}";
80
2
    let diagnostics = get_diagnostics(source);
81
2
    assert_eq!(diagnostics.len(), 2, "Expected exactly two diagnostics");
82

            
83
    // First unexpected token: ';' at the end of domain
84
2
    let diag1 = &diagnostics[0];
85
2
    check_diagnostic(diag1, 0, 18, 0, 19, "Unexpected ;");
86

            
87
    // Second unexpected token: '@' in set literal
88
2
    let diag2 = &diagnostics[1];
89
2
    check_diagnostic(diag2, 1, 21, 1, 22, "Unexpected @ inside a Set");
90
2
}
91

            
92
#[test]
93
2
fn unexpected_x_in_all_diff() {
94
2
    let source = "\
95
2
find a : bool 
96
2
such that a = allDiff([1,2,4,1]x)";
97
2
    let diagnostics = get_diagnostics(source);
98
2
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
99
2
    let diag = &diagnostics[0];
100

            
101
2
    check_diagnostic(
102
2
        diag,
103
        1,
104
        31,
105
        1,
106
        32,
107
2
        "Unexpected x inside a List Combining Expression Bool",
108
    );
109
2
}
110

            
111
#[test]
112
2
fn unexpected_int_at_the_end() {
113
2
    let source = "\
114
2
find a : bool 
115
2
such that a = allDiff([1,2,4,1])8";
116
2
    let diagnostics = get_diagnostics(source);
117
2
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
118
2
    let diag = &diagnostics[0];
119
2
    check_diagnostic(diag, 1, 32, 1, 33, "Unexpected 8");
120
2
}
121

            
122
#[test]
123
2
fn unexpected_operand_at_end() {
124
2
    let source = "\
125
2
find x, a, b: int(1..3)+
126
2
";
127
2
    let diags = get_diagnostics(source);
128
2
    assert_eq!(diags.len(), 1, "Expected exactly one diagnostic");
129
2
    let diag = &diags[0];
130
2
    check_diagnostic(diag, 0, 23, 0, 24, "Unexpected +");
131
2
}
132

            
133
#[test]
134
2
fn unexpected_operand_middle_no_comma() {
135
2
    let source = "\
136
2
find x-, b: int(1..3)
137
2
";
138
2
    let diags = get_diagnostics(source);
139
2
    assert_eq!(diags.len(), 1, "Expected exactly one diagnostic");
140
2
    let diag = &diags[0];
141
2
    check_diagnostic(diag, 0, 6, 0, 7, "Unexpected - inside a Variable List");
142
2
}
143

            
144
#[test]
145
2
fn unexpected_operand_middle_comma() {
146
2
    let source = "\
147
2
find x,-, b: int(1..3)
148
2
";
149
2
    let diags = get_diagnostics(source);
150
2
    assert_eq!(diags.len(), 1, "Expected exactly one diagnostic");
151
2
    let diag = &diags[0];
152
2
    check_diagnostic(diag, 0, 6, 0, 8, "Unexpected ,- inside a Variable List");
153
2
}
154

            
155
#[test]
156
2
fn unexpected_token_in_identifier() {
157
2
    let source = "find v@lue: int(1..3)";
158
2
    let diagnostics = get_diagnostics(source);
159
2
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
160
2
    let diag = &diagnostics[0];
161
2
    check_diagnostic(diag, 0, 6, 0, 10, "Unexpected @lue inside a Find Statement");
162
2
}
163

            
164
// Temporary before better logic is developed
165
#[test]
166
2
fn missing_right_operand_in_and_expr() {
167
2
    let source = "\
168
2
find x: int
169
2
such that x /\\
170
2
";
171
2
    let diagnostics = get_diagnostics(source);
172
2
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
173
2
    let diag = &diagnostics[0];
174
2
    check_diagnostic(diag, 1, 12, 1, 14, "Unexpected /\\");
175
2
}
176

            
177
// Temporary before better logic is developed
178
#[test]
179
2
fn unexpected_token_in_comparison() {
180
2
    let source = "\
181
2
find x: int
182
2
such that 5 =
183
2
    ";
184
2
    let diagnostics = get_diagnostics(source);
185
2
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
186
2
    let diag = &diagnostics[0];
187
2
    check_diagnostic(diag, 1, 12, 1, 13, "Unexpected =");
188
2
}
189

            
190
#[test]
191
2
fn unexpected_token_in_domain() {
192
    // not indented because have to avoid leading spaces for accurate character count
193
2
    let source = "find a: int(1.3)";
194

            
195
2
    let diagnostics = get_diagnostics(source);
196

            
197
    // Should be exactly one diagnostic
198
2
    assert_eq!(diagnostics.len(), 1, "Expected exactly one diagnostic");
199
2
    let diag = &diagnostics[0];
200

            
201
2
    check_diagnostic(diag, 0, 13, 0, 15, "Unexpected .3 inside an Integer Domain");
202
2
}