forked from as-pect/visitor-as
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtests.spec.js
170 lines (139 loc) · 3.57 KB
/
tests.spec.js
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
import { compileAndInit, compileExample } from "./setup.js";
import { ASTBuilder, SimpleParser } from "../dist/index.js";
import mocha from "mocha"
const { describe, it } = mocha
import { expect } from "chai"
const FOO = `
@list
class Foo {
a: u8;
b: bool;
i: i32;
c: Animal.Cat;
}
`;
const VEC = `
@list
class Vec3 {
constructor(public x: f64 = 0, public y: i64 = 0, public z: u32 = 0) {}
}
`;
const GENERIC = `
@list
class GenericMethods {
nonGeneric(): void {}
foo<T>(t: T): void {}
faa<A,B>(): string { return "hello"; }
orNull(): string | null { return null; }
orNullMap(): Map<string, string | null> | null {
return null;
}
}
`
describe("List", () => {
it("should handle simple struct", async () => {
expect(await compileExample(FOO, "./dist/examples/list.js")).to.deep.equal([
"a: u8",
"b: bool",
"i: i32",
"c: Animal.Cat",
]);
});
it("should list fields defined in constructor", async () => {
expect(await compileExample(VEC, "./dist/examples/list.js")).to.deep.equal([
"x: f64",
"y: i64",
"z: u32",
]);
});
it("should list methods", async () => {
expect(await compileExample(GENERIC, "./dist/examples/list.js")).to.deep.equal([
"nonGeneric: () => void",
"foo<T>: (t: T) => void",
"faa<A, B>: () => string",
"orNull: () => string | null",
"orNullMap: () => Map<string, string | null> | null",
]);
});
});
const HelloWorldYay = `
@capitalize
const hello = \`hello\`;
@capitalize
const world = "world";
@capitalize
const yay = 'yay';
`;
describe("Capitilize", () => {
it("should handle simple struct", async () => {
expect(
await compileExample(HelloWorldYay, "./dist/examples/capitalize.js")
).to.deep.equal(["hello -> HELLO", "world -> WORLD", "yay -> YAY"]);
});
});
const EXPORT_AS = `
@exportAs("new")
export function main(): u32 {
return 42;
}
`
describe("exportAs", () => {
it("should rename exported function", async () => {
let res = await compileAndInit(EXPORT_AS, "./dist/examples/exportAs.js");
expect(res.exports["new"]()).to.equal(42);
})
})
describe('hello world transform', () => {
it("should not throw", async () => {
await compileAndInit("assert(foo() == 'hello world', 'should equal')", "./dist/examples/functionCallTransform.js")
});
it("should handle \`'s", async () => {
await compileAndInit("assert(foo() == `hello world`, 'should equal')", "./dist/examples/functionCallTransform.js")
});
});
function expr(s) {
expect(ASTBuilder.build(SimpleParser.parseExpression(s)))
.to.equal(s)
}
function stmt(s) {
expect(ASTBuilder.build(SimpleParser.parseStatement(s)))
.to.equal(s)
}
const foo = `
class Foo {
f: i32 = 0;
}
`
describe("Parser", () => {
describe("Expressions", () => {
it("binary", () => {
expr("1 + 1");
});
it("call", () => {
expr("callFunction()");
});
});
describe("Statements", () => {
it("assignment", () => {
stmt("let x = 1");
});
});
describe("top level", () => {
let _class;
beforeEach(() => {
_class = SimpleParser.parseTopLevelStatement(foo);
});
it("should have a field", () => {
expect(_class.members.length).to.equal(1);
expect(_class.members[0].name.range.toString()).to.equal("f");
});
it("should be able to add member", () => {
let method = `
getF(): i32 { return this.f; }
`
let methodAST = SimpleParser.parseClassMember(method, _class);
_class.members.push(methodAST);
expect(_class.members[1].name.text).to.equal("getF");
})
})
});