-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathtestSuite.bal
319 lines (290 loc) · 11.7 KB
/
testSuite.bal
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
import ballerina/file;
import ballerina/io;
import ballerina/test;
import wso2/nballerina.bir.sexpr as bsexpr;
import wso2/nballerina.bir;
import wso2/nballerina.comm.diagnostic as d;
import wso2/nballerina.comm.err;
import wso2/nballerina.comm.sexpr;
import wso2/nballerina.front.syntax as s;
import wso2/nballerina.front;
import wso2/nballerina.nback;
import wso2/nballerina.tback;
import wso2/nballerina.types as t;
type TestSuiteCases map<[string, string]>;
@test:Config {
dataProvider: listSourcesVPO
}
function testCompileVPO(string path, string kind) returns io:Error? {
CompileError? err = testCompileFile(path);
if err is io:Error {
return err;
}
else {
string msg = "compilation error";
if err is err:Diagnostic {
string? defnName = err.detail().defnName;
if defnName != () {
msg += " :definition " + defnName;
}
}
test:assertEquals(err, (), msg);
}
}
@test:Config {
dataProvider: listSourcesTVE
}
function testSemTypes(string path, string kind) returns error? {
SubtypeTestCase res = check readSubtypeTests(path);
error? err = testSubtypes([{ lines : res[1], filename : res[0] }], res[2]);
if kind == "te" {
if err is () {
test:assertNotExactEquals(err, (), "expected an error " + path);
}
else if err is err:Diagnostic {
check checkErrorLocation(err, path);
return;
}
}
return err;
}
@test:Config {
dataProvider: listSourcesEU
}
function testCompileEU(string path, string kind) returns file:Error|io:Error? {
CompileError? err = testCompileFile(path);
if err is err:Diagnostic? {
if err is () {
test:assertNotExactEquals(err, (), "expected an error " + path);
}
else {
boolean isE = kind[0] == "e";
if isE {
if err is err:Internal {
io:println(err);
test:assertFail("invalid error should not happen" + path);
}
else if err is err:Unimplemented {
io:println(err);
test:assertFail("unimplemented error on E test" + path);
}
}
else {
test:assertFalse(err is err:Semantic, "semantic error on U test" + path);
}
if kind == "e" || kind == "ue" {
check checkErrorLocation(err, path);
}
}
}
else {
return err;
}
}
type FilenameLine [string, int];
function checkErrorLocation(err:Diagnostic err, string path) returns file:Error|io:Error? {
var [expectedFilename, expectedLineNo] = <FilenameLine> check expectedErrorLocation(err, path);
d:Location loc = err.detail().location;
string filename = loc.file.filename();
test:assertEquals(file:getAbsolutePath(filename), expectedFilename, "invalid error filename" + filename);
d:LineColumn lc = d:locationLineColumn(loc);
test:assertEquals(lc[0], expectedLineNo, "invalid error line number in " + expectedFilename);
}
function expectedErrorLocation(CompileError err, string path) returns FilenameLine|file:Error|io:Error? {
string? modulePath = check moduleDir(path);
FilenameLine? errorLocation = ();
if modulePath is string {
foreach var md in check file:readDir(modulePath) {
if md.dir {
foreach var file in check file:readDir(md.absPath) {
errorLocation = check findErrorLine(file.absPath, errorLocation);
}
}
}
}
errorLocation = check findErrorLine(path, errorLocation);
test:assertFalse(errorLocation is (), "failed to find any files with error annotation for " + path);
return errorLocation;
}
function moduleDir(string filePath) returns string|file:Error? {
string subModPath = filePath.substring(0, filePath.length()-4) + ".modules";
if check file:test(subModPath, file:IS_DIR) {
return check file:normalizePath(subModPath, file:CLEAN);
}
return ();
}
function findErrorLine(string filePath, FilenameLine? currentErrorLocation) returns FilenameLine|io:Error? {
int? fileErrorLine = check errorLine(filePath);
if fileErrorLine is int {
test:assertTrue(currentErrorLocation is (), "multiple files with error annotations found");
return [filePath, fileErrorLine];
}
else {
return currentErrorLocation;
}
}
function listSourcesVPO() returns TestSuiteCases|error => listSources("vpo");
function listSourcesEU() returns TestSuiteCases|error => listSources("eu");
function listSourcesTVE() returns TestSuiteCases|error => listSources("t");
function listSources(string initialChars) returns TestSuiteCases|io:Error|file:Error {
TestSuiteCases cases = {};
// JBUG #32615 can't use from-in-from query syntax
foreach var dir in check file:readDir("./testSuite") {
if !check file:test(dir.absPath, file:IS_DIR) {
continue;
}
string category = check file:basename(dir.absPath);
foreach var test in check file:readDir(dir.absPath) {
string name = check file:basename(test.absPath);
var [base, ext] = basenameExtension(name);
if ext != ".bal" {
continue;
}
int? dash = base.lastIndexOf("-");
test:assertTrue(dash is int, "test file name must be in <name>-<kind>.bal format");
string testKind = base.substring(1 + <int>dash);
if initialChars.includes(testKind[0]) {
cases[category + "/" + name] = [test.absPath, testKind];
}
}
}
return cases;
}
function testKind(string base) returns string:Char {
int? dash = base.lastIndexOf("-");
test:assertTrue(dash is int, "test file name must be in <name>-<kind>.bal format");
return <string:Char> base.substring(1 + <int>dash, 2 + <int>dash).toLowerAscii();
}
function includePath(string path, string initialChars) returns boolean|file:Error {
string base = check file:basename(path);
// this deals with comparing extension case-insensitively
var [_, extension] = basenameExtension(base);
return extension == SOURCE_EXTENSION && initialChars.includes(base[0].toUpperAscii());
}
function errorLine(string path) returns int|io:Error? {
string[] lines = check io:fileReadLines(path);
int? lineNo = ();
foreach var i in 0 ..< lines.length() {
if lines[i].indexOf("// @error") != () {
test:assertTrue(lineNo is (), "multiple error annotations in file " + path);
lineNo = i + 1;
}
}
return lineNo;
}
// This outputs nothing
function testCompileFile(string filename) returns CompileError? {
var [basename, _] = basenameExtension(filename);
return compileBalFile({ filename }, basename, new LlvmEmitter());
}
function testSubtypes(front:SourcePart[] sources, string[] expected) returns error? {
var [env, m] = check front:typesFromString(sources);
var tc = t:typeContext(env);
foreach var item in expected {
s:TypeTest test = check s:parseTypeTest(item);
t:SemType left = resolveTestSemtype(tc, m, test.left);
t:SemType right = resolveTestSemtype(tc, m, test.right);
string lhsStr = test.left.toString();
string rhsStr = test.right.toString();
boolean lsr = t:isSubtype(tc, left, right);
boolean rsl = t:isSubtype(tc, right, left);
boolean[2] testPair = [lsr, rsl];
match test.op {
"<" => {
test:assertEquals(testPair, [true, false], string `${lhsStr} is not a proper subtype of ${rhsStr}`);
}
"<>" => {
test:assertEquals(testPair, [false, false], string `${lhsStr} and ${rhsStr} are subtypes`);
}
"=" => {
test:assertEquals(testPair, [true, true], string `${lhsStr} is not equivalent to ${rhsStr}`);
}
}
}
}
function resolveTestSemtype(t:Context tc, map<t:SemType> m, s:Identifier|s:TypeProjection tn) returns t:SemType {
if tn is s:Identifier {
return lookupSemtype(m, tn);
}
else {
t:SemType t = lookupSemtype(m, tn.identifier);
int|s:Identifier index = tn.index;
if t:isSubtypeSimple(t, t:LIST) {
if index is int {
return testListProj(tc, t, t:intConst(index));
}
else {
t:SemType kt = lookupSemtype(m, index);
if t:isSubtypeSimple(kt, t:INT) {
return testListProj(tc, t, kt);
}
test:assertFail("index for list projection must be a subtype of int");
}
}
else if t:isSubtypeSimple(t, t:MAPPING) {
if index is s:Identifier {
t:SemType kt = lookupSemtype(m, index);
return t:mappingMemberTypeInnerVal(tc, t, kt);
}
test:assertFail("index for mapping projection must be a subtype of string");
}
else {
test:assertFail(tn.identifier + " is not a list or a mapping type");
}
}
}
function testListProj(t:Context tc, t:SemType t, t:SemType index) returns t:SemType {
t:SemType s1 = t:listProjInnerVal(tc, t, index);
t:SemType s2 = t:listMemberTypeInnerVal(tc, t, index);
if !t:isSubtype(tc, s1, s2) {
test:assertFail("listProj result is not a subtype of listMemberType");
}
testListAllMemberTypesProjection(tc, t);
return s1;
}
function lookupSemtype(map<t:SemType> m, s:Identifier id) returns t:SemType {
t:SemType? t = m[id];
if t is () {
test:assertFail(id + " is not declared");
}
else {
return t;
}
}
// Validate output of `t:listAllMemberTypes` by repeatedly testing for each range using `t:listProj` function.
function testListAllMemberTypesProjection(t:Context tc, t:SemType t) {
var [ranges, types] = t:listAllMemberTypesInner(tc, t);
foreach int i in 0 ..< ranges.length() {
t:Range r = ranges[i];
t:SemType it = types[i];
t:SemType projected = t:listMemberTypeInnerVal(tc, t, t:intConst(r.min));
if !t:isSubtype(tc, it, projected) || !t:isSubtype(tc, projected, it) {
test:assertFail(string `All projection for member index ${r.min} is not equal to individual projection`);
}
}
}
// Test bir's validity by: First lowering a given module to bir. Second, convert the module to bir text, parse it and lower it to llvm.
// Then compare first and second are textually equal.
class TestBirEmitter {
*Emitter;
function emitModule(bir:Module birMod) returns CompileError? {
string birText = check tback:toBirText(birMod);
var [firstRoundLlMod, _] = check nback:buildModule(birMod, {debugLevel: nback:DEBUG_NONE});
string firstRoundLl = firstRoundLlMod.printModuleToString();
sexpr:Data[] secondRoundSexpr = checkpanic sexpr:parse(birText); // using if and assertFail will lose the error trace
bsexpr:Module secondRoundTypeSexpr = checkpanic secondRoundSexpr.cloneWithType();
bir:Module secondRoundBirMod = bsexpr:toModule(secondRoundTypeSexpr, birMod.getId());
var [secondRoundLlMod, _] = check nback:buildModule(secondRoundBirMod, {debugLevel: nback:DEBUG_NONE});
string secondRoundLl = secondRoundLlMod.printModuleToString();
test:assertEquals(firstRoundLl, secondRoundLl);
}
function finalize(t:Env env, map<t:FunctionSignature> potentialEntryFuncs) returns CompileError? {
}
}
@test:Config {
dataProvider: listSourcesVPO
}
function testBirCompile(string filename, string kind) returns error? {
var [basename, _] = basenameExtension(filename);
check compileBalFile({ filename }, basename, new TestBirEmitter());
}