-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidating.test.ts
More file actions
357 lines (295 loc) · 13.1 KB
/
Copy pathvalidating.test.ts
File metadata and controls
357 lines (295 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
import { describe, expect, test } from "vitest";
import { createBcsEngineeringServices } from "../../src/language/bcs-engineering-module.js";
import { extractDocuments } from "../../src/cli/cli-util.js";
import * as path from "node:path";
import { NodeFileSystem } from "langium/node";
// Utility to exclude hints
function getDiagnosticsWithoutHints(allDocs: any[]) {
return allDocs
.flatMap((doc) => doc.diagnostics ?? [])
.filter((d) => d.severity !== 4); // 4 = Hint
}
describe("BCS Control Validation Tests", () => {
test("No errors in valid control/hardware files", async () => {
const services = createBcsEngineeringServices(NodeFileSystem);
const [mainDoc, allDocs] = await extractDocuments(
path.join(__dirname, "files", "valid", "valid_control.bcsctrl"),
services.bcsControl,
false
);
const allDiagnostics = getDiagnosticsWithoutHints(allDocs);
expect(allDiagnostics.length).toBe(0);
});
test("Detect Assignment type mismatch", async () => {
const services = createBcsEngineeringServices(NodeFileSystem);
const [mainDoc, allDocs] = await extractDocuments(
path.join(__dirname, "files", "invalid", "invalid_control.bcsctrl"),
services.bcsControl,
false
);
const allDiagnostics = getDiagnosticsWithoutHints(allDocs);
const diagString = allDiagnostics.map((d) => d.message).join("\n");
expect(allDiagnostics.length).toBe(3);
expect(diagString).toMatch(/Cannot assign "REAL" to "BOOL"/);
});
test("Detect Enum type mismatch", async () => {
const services = createBcsEngineeringServices(NodeFileSystem);
const [mainDoc, allDocs] = await extractDocuments(
path.join(__dirname, "files", "invalid_enum", "control_enum.bcsctrl"),
services.bcsControl,
false
);
const allDiagnostics = getDiagnosticsWithoutHints(allDocs);
const diagString = allDiagnostics.map((d) => d.message).join("\n");
expect(allDiagnostics.length).toBe(1);
expect(diagString).toMatch(
'Type mismatch: Cannot assign "ENUM:Status" to "ENUM:Mode".'
);
});
test("Detect VarDecl type mismatch", async () => {
const services = createBcsEngineeringServices(NodeFileSystem);
const [mainDoc, allDocs] = await extractDocuments(
path.join(
__dirname,
"files",
"invalid_vardecl",
"control_vardecl.bcsctrl"
),
services.bcsControl,
false
);
const allDiagnostics = getDiagnosticsWithoutHints(allDocs);
const diagString = allDiagnostics.map((d) => d.message).join("\n");
expect(allDiagnostics.length).toBe(9);
const expectedMessages = [
'Type mismatch: Cannot assign "BOOL" to "INT".',
'Type mismatch: Cannot assign "INT" to "BOOL".',
'Type mismatch: Cannot assign "INT" to "STRING".',
'Type mismatch: Cannot assign "TOD" to "TIME".',
'Type mismatch: Cannot assign "TIME" to "TOD".',
'Type mismatch: Cannot assign "ENUM:Status" to "ENUM:Mode".',
'Type mismatch: Cannot assign "ENUM:Mode" to "ENUM:Status".',
'Type mismatch: Cannot assign "ENUM:Status" to "INT".',
];
expectedMessages.forEach((msg) => {
expect(diagString).toContain(msg);
});
});
test("Test UseStmt errors", async () => {
const services = createBcsEngineeringServices(NodeFileSystem);
const [mainDoc, allDocs] = await extractDocuments(
path.join(
__dirname,
"files",
"invalid_usestmt",
"control_usestmt.bcsctrl"
),
services.bcsControl,
false
);
const allDiagnostics = getDiagnosticsWithoutHints(allDocs);
const diagString = allDiagnostics.map((d) => d.message).join("\n");
expect(allDiagnostics.length).toBe(14);
const expectedMessages = [
// 1. Too many inputs (duplicate key)
"Duplicate mapping for input 'iWindow' in use of function block 'HeatingLogicFB'.",
// 2. Missing input (iMode)
"Function block 'HeatingLogicFB' expects 3 input arguments, but got 2.",
// 3. Wrong input types
"Type mismatch for input 'iWindow': expected 'BOOL', got 'REAL'.",
"Type mismatch for input 'iMode': expected 'ENUM:Mode', got 'ENUM:Status'.",
"Logical operator '||' requires both operands to be BOOL, but got 'INT' and 'INT'.",
"Type mismatch for input 'iTemp': expected 'REAL', got 'BOOL'.",
// 4. Output count mismatch for single assignment
"Function block 'LightLogicFB' has 2 outputs, cannot use direct assignment. Use mapping instead.",
// 5. Mapping output count mismatch
"Function block 'LightLogicFB' expects 2 outputs, but got 1.",
"Function block 'LightLogicFB' expects 2 outputs, but got 3.",
// 6. Output type mismatch (single)
"Type mismatch for output 'oHeating': cannot assign to 'isHeating_should_fail_type_matching' of type 'INT', expected 'BOOL'.",
// 7. Output type mismatch (mapping)
"Type mismatch: cannot assign output 'oHeating' to 'isHeating_should_fail_type_matching'. Expected assignable type for 'BOOL', but got 'INT'.",
// 8. Output mapping could not resolve
"Could not resolve reference to VarDecl named 'oExtra'.",
"Could not resolve reference to VarDecl named 'oWrong'.",
// 9. Input mapping could not resolve
"Could not resolve reference to VarDecl named 'iWrong'.",
];
expectedMessages.forEach((msg) => {
expect(diagString).toContain(msg);
});
});
test("Detect duplicates", async () => {
const services = createBcsEngineeringServices(NodeFileSystem);
const [mainDoc, allDocs] = await extractDocuments(
path.join(
__dirname,
"files",
"invalid_duplicate",
"control_duplicate.bcsctrl"
),
services.bcsControl,
false
);
const allDiagnostics = getDiagnosticsWithoutHints(allDocs);
const diagString = allDiagnostics.map((d) => d.message).join("\n");
expect(allDiagnostics.length).toBe(17);
const expectedMessages = [
"Duplicate enum 'DuplicateMode'.",
"Duplicate struct 'DuplicateStruct'.",
"Duplicate function block 'DuplicateFB'.",
"Duplicate global variable 'duplicateVar'.",
"Duplicate control unit 'DuplicateUnit'.",
"Duplicate variable name 'iDuplicate' in function block 'DuplicateFB'.",
"Duplicate variable name 'oDuplicate' in function block 'DuplicateFB'.",
"Duplicate variable name 'lDuplicate' in function block 'DuplicateFB'.",
"Duplicate variable name 'duplicateFBVar' in function block 'DuplicateFB'.",
"Only one 'inputs' block allowed in function block 'DuplicateFB', found 2.",
"Only one 'outputs' block allowed in function block 'DuplicateFB', found 2.",
"Only one 'locals' block allowed in function block 'DuplicateFB', found 2.",
"Duplicate variable 'duplicateUnitVar' in unit 'DuplicateUnit'.",
"Variable 'globalVar' conflicts with global variable.",
"Duplicate component name 'windowContact_duplicate' in this controller.",
"Duplicate component name 'motor' in this controller.",
"Link \"Channel 2^Input\" on module 'IOModule_DI1' is already used by 'windowContact_duplicate.value'.",
];
expectedMessages.forEach((msg) => {
expect(diagString).toContain(msg);
});
});
test("Test when clause", async () => {
const services = createBcsEngineeringServices(NodeFileSystem);
const [mainDoc, allDocs] = await extractDocuments(
path.join(__dirname, "files", "invalid_when", "control_when.bcsctrl"),
services.bcsControl,
false
);
const allDiagnostics = getDiagnosticsWithoutHints(allDocs);
const diagString = allDiagnostics.map((d) => d.message).join("\n");
expect(allDiagnostics.length).toBe(1);
const expectedMessages = [
"Condition in 'when (...)' of unit 'Test' must be of type BOOL, but got 'INT'.",
];
expectedMessages.forEach((msg) => {
expect(diagString).toContain(msg);
});
});
test("Test switch-case stmt", async () => {
const services = createBcsEngineeringServices(NodeFileSystem);
const [mainDoc, allDocs] = await extractDocuments(
path.join(__dirname, "files", "invalid_switch", "control_switch.bcsctrl"),
services.bcsControl,
false
);
const allDiagnostics = getDiagnosticsWithoutHints(allDocs);
const diagString = allDiagnostics.map((d) => d.message).join("\n");
expect(allDiagnostics.length).toBe(3);
const expectedMessages = [
"Case literal 'Status.OFF' is of type 'ENUM:Status', but switch expression is 'ENUM:Mode'.",
"Duplicate case literal 'Mode.ECO'.",
"Case literal 'true' is of type 'BOOL', but switch expression is 'ENUM:Mode'.",
];
expectedMessages.forEach((msg) => {
expect(diagString).toContain(msg);
});
});
test("Detect Invalid Arrays", async () => {
const services = createBcsEngineeringServices(NodeFileSystem);
const [mainDoc, allDocs] = await extractDocuments(
path.join(__dirname, "files", "invalid_array", "control_array.bcsctrl"),
services.bcsControl,
false
);
const allDiagnostics = getDiagnosticsWithoutHints(allDocs);
const diagString = allDiagnostics.map((d) => d.message).join("\n");
expect(allDiagnostics.length).toBe(28);
const expectedErrors = [
'Type mismatch: Cannot assign "INT" to "BOOL".',
"Array size mismatch: expected 3 elements, but got 2.",
'Type mismatch: Cannot assign "ARRAY<BOOL>[2]" to "ARRAY<BOOL>[3]".',
'Type mismatch: Cannot assign "ARRAY<INT>[5][5][5]" to "ARRAY<BOOL>[5][5][5]".',
"Array index [-1] out of bounds: allowed range is 0 to 2.",
"Array index [2] out of bounds: allowed range is 0 to 0.",
"Array index [3] out of bounds: allowed range is 0 to 1.",
"Array index [4] out of bounds: allowed range is 0 to 2.",
'Type mismatch: Cannot assign "ARRAY<BOOL>[5]" to "ARRAY<BOOL>[5][5][5]".',
"Expected nested array with 3 dimensions.",
'Type mismatch: Cannot assign "ARRAY<INT>[5]" to "ARRAY<INT>[5][5][5]".',
'Type mismatch: Cannot assign "ARRAY<REAL>[5]" to "ARRAY<REAL>[5][5][5]".',
"Expected nested array with 2 dimensions.",
'Type mismatch: Cannot assign "ARRAY<mixed>[5]" to "ARRAY<BOOL>[5][5]".',
'Type mismatch: Cannot assign "ARRAY<ENUM:Mode>[5]" to "ARRAY<ENUM:Mode>[5][5][5]".',
'Type mismatch: Cannot assign "ARRAY<INT>[2]" to "ARRAY<BOOL>[2]".',
"Array size mismatch: expected 3 elements, but got 1.",
'Type mismatch: Cannot assign "ARRAY<BOOL>[1][3]" to "ARRAY<BOOL>[3][3]".',
"Cannot infer type for variable initialization: invalidIndex = x",
"Cannot infer type for variable initialization: invalidAccess = invalidScalarIndex",
'Array index must be of type INT, but got "STRING".',
'Type mismatch: Cannot assign "ARRAY<mixed>[2]" to "ARRAY<BOOL>[2]".',
'Type mismatch: Cannot assign "ARRAY<mixed>[5]" to "ARRAY<BOOL>[5][5]".',
];
for (const expected of expectedErrors) {
expect(diagString).toMatch(expected);
}
});
test("Detect Invalid Structs", async () => {
const services = createBcsEngineeringServices(NodeFileSystem);
const [mainDoc, allDocs] = await extractDocuments(
path.join(__dirname, "files", "invalid_struct", "control_struct.bcsctrl"),
services.bcsControl,
false
);
const allDiagnostics = getDiagnosticsWithoutHints(allDocs);
const diagString = allDiagnostics.map((d) => d.message).join("\n");
expect(allDiagnostics.length).toBe(12);
const expectedErrors = [
"Unexpected field 'f' in struct literal for 'Rectangle'.",
"Missing field 'x' in struct literal for 'Rectangle'.",
"Duplicate field 'y' in struct literal for 'Rectangle'.",
"Missing field 'y' in struct literal for 'Rectangle'.",
'Type mismatch: Cannot assign "STRUCT:Point" to "STRUCT:Rectangle".',
"Unexpected field 'z' in struct literal for 'Rectangle'.",
];
for (const expected of expectedErrors) {
expect(diagString).toMatch(expected);
}
});
test("Detect Invalid Edge Detection", async () => {
const services = createBcsEngineeringServices(NodeFileSystem);
const [mainDoc, allDocs] = await extractDocuments(
path.join(
__dirname,
"files",
"invalid_edge_detection",
"control_edge_detection.bcsctrl"
),
services.bcsControl,
false
);
const allDiagnostics = getDiagnosticsWithoutHints(allDocs);
const diagString = allDiagnostics.map((d) => d.message).join("\n");
expect(allDiagnostics.length).toBe(2);
const expectedErrors = [
"Signal in 'on_rising' must be of type BOOL, but got 'INT'.",
"Signal in 'on_falling' must be of type BOOL, but got 'INT'.",
];
for (const expected of expectedErrors) {
expect(diagString).toMatch(expected);
}
});
test("No errors in valid library call", async () => {
const services = createBcsEngineeringServices(NodeFileSystem);
const [mainDoc, allDocs] = await extractDocuments(
path.join(
__dirname,
"files",
"valid_library_call",
"valid_library_call_control.bcsctrl"
),
services.bcsControl,
false
);
const allDiagnostics = getDiagnosticsWithoutHints(allDocs);
expect(allDiagnostics.length).toBe(0);
});
});