forked from ballerina-platform/nballerina
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtestJsonTypes.bal
71 lines (63 loc) · 2.15 KB
/
testJsonTypes.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
import ballerina/io;
import wso2/nballerina.types as t;
import wso2/nballerina.types.bdd;
import wso2/nballerina.types.'json as j;
enum Relation {
proper_subtype,
equivalent,
incomparable
}
type TestCase [Relation, json, json];
type Tests TestCase[];
public function testJsonTypes(string filename) returns error? {
json j = check io:fileReadJson(filename);
TestCase[] tests = check j.fromJsonWithType();
int testNum = 0;
int testsPassed = 0;
foreach var [rel, j1, j2] in tests {
testNum += 1;
if runTest(rel, j1, j2, testNum) {
testsPassed += 1;
}
}
io:println("Passed ", testsPassed, "/", testNum);
io:println(" created ", bdd:getCount(), " BDDs in total");
}
final var relationExpect = {
proper_subtype: [true, false],
equivalent: [true, true],
incomparable: [false, false]
};
function runTest(Relation rel, json j1, json j2, int testNum) returns boolean {
t:Env env = new;
boolean firstOk = false;
do {
boolean ok = true;
t:SemType t1 = check j:parse(env, j1);
firstOk = true;
t:SemType t2 = check j:parse(env, j2);
var expect = <[boolean,boolean]>relationExpect[rel];
return expectSubtype(testNum, j1, j2, env, t1, t2, expect, 0)
&& expectSubtype(testNum, j2, j1, env, t2, t1, expect, 1);
}
on fail j:ParseError err {
io:println("Could not parse ", firstOk ? "second" : "first", " type in test ", testNum, ": ", err.message());
return false;
}
}
function expectSubtype(int testNum, json j1, json j2, t:Env env, t:SemType t1, t:SemType t2, boolean[] expect, int i) returns boolean {
io:println("Test ", testNum, "/", i);
int tem = bdd:getCount();
var tc = t:typeCheckContext(env);
boolean b = t:isSubtype(tc, t1, t2);
if b == expect[i] {
io:println(" created ", bdd:getCount() - tem, " BDDs");
return true;
}
io:println("Fail test ", testNum, ". This type");
io:println(j1.toBalString());
io:println("was ", b ? "" : "not ", "a subtype of");
io:println(j2.toBalString());
io:println("but I expected the opposite.");
return false;
}