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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

            
195
3
    let diagnostics = get_diagnostics(source);
196

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

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