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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
195
1
    let diagnostics = get_diagnostics(source);
196

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

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