forked from codecombat/codecombat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcs_spec.js
315 lines (290 loc) · 10.2 KB
/
cs_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
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
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/main/docs/suggestions.md
*/
const Aether = require('../aether');
describe("CS test Suite!", function() {
describe("CS compilation", function() {
const aether = new Aether({language: "coffeescript"});
return it("Should compile functions", function() {
const code = `\
return 1000\
`;
aether.transpile(code);
expect(aether.run()).toEqual(1000);
return expect(aether.problems.errors).toEqual([]);
});
});
describe("CS compilation with lang set after contruction", function() {
const aether = new Aether();
return it("Should compile functions", function() {
const code = `\
return 2000 if false
return 1000\
`;
aether.setLanguage("coffeescript");
aether.transpile(code);
expect(aether.canTranspile(code)).toEqual(true);
return expect(aether.problems.errors).toEqual([]);
});
});
describe("CS Test Spec #1", function() {
const aether = new Aether({language: "coffeescript"});
return it("mathmetics order", function() {
const code = `\
return (2*2 + 2/2 - 2*2/2)\
`;
aether.transpile(code);
expect(aether.run()).toEqual(3);
return expect(aether.problems.errors).toEqual([]);
});
});
describe("CS Test Spec #2", function() {
const aether = new Aether({language: "coffeescript"});
return it("function call", function() {
const code = `\
fib = (n) ->
(if n < 2 then n else fib(n - 1) + fib(n - 2))
chupacabra = fib(6)\
`;
aether.transpile(code);
const fn = aether.createFunction();
expect(aether.canTranspile(code)).toEqual(true);
expect(aether.run()).toEqual(8); // fail
return expect(aether.problems.errors).toEqual([]);
});
});
describe("Basics", function() {
const aether = new Aether({language: "coffeescript"});
it("Simple For", function() {
const code = `\
count = 0
count++ for num in [1..10]
return count\
`;
aether.transpile(code);
expect(aether.canTranspile(code)).toEqual(true);
expect(aether.run()).toEqual(10);
return expect(aether.problems.errors).toEqual([]);
});
it("Simple While", function() {
const code = `\
count = 0
count++ until count is 100
return count\
`;
aether.transpile(code);
expect(aether.canTranspile(code)).toEqual(true);
expect(aether.run()).toEqual(100);
return expect(aether.problems.errors).toEqual([]);
});
it("Should Map", function() {
// See: https://github.com/codecombat/aether/issues/97
const code = "return (num for num in [10..1])";
aether.transpile(code);
expect(aether.canTranspile(code)).toEqual(true);
expect(aether.run()).toEqual([10, 9, 8, 7, 6, 5, 4, 3, 2, 1]);
return expect(aether.problems.errors).toEqual([]);
});
it("Should Map properties", function() {
const code = `\
yearsOld = max: 10, ida: 9, tim: 11
ages = for child, age of yearsOld
"#{child} is #{age}"
return ages\
`;
aether.transpile(code);
expect(aether.canTranspile(code)).toEqual(true);
expect(aether.run()).toEqual(["max is 10", "ida is 9", "tim is 11"]);
return expect(aether.problems.errors).toEqual([]);
});
it("Should compile empty function", function() {
const code = `\
func = () ->
return typeof func\
`;
aether.transpile(code);
expect(aether.canTranspile(code)).toEqual(true);
expect(aether.run()).toEqual('function');
return expect(aether.problems.errors).toEqual([]);
});
it("Should compile objects", function() {
const code = `\
singers = {Jagger: 'Rock', Elvis: 'Roll'}
return singers\
`;
aether.transpile(code);
expect(aether.canTranspile(code)).toEqual(true);
expect(aether.run()).toEqual(({Jagger: 'Rock', Elvis: 'Roll'}));
return expect(aether.problems.errors).toEqual([]);
});
it("Should compile classes", function() {
const code = `\
class MyClass
test: ->
return 1000
myClass = new MyClass()
return myClass.test()\
`;
aether.transpile(code);
expect(aether.canTranspile(code)).toEqual(true);
expect(aether.run()).toEqual(1000);
return expect(aether.problems.errors).toEqual([]);
});
xit("Should compile super", function() {
// super is not supported in CSR yet: https://github.com/michaelficarra/CoffeeScriptRedux/search?q=super&ref=cmdform&type=Issues
const code = `\
class Animal
constructor: (@name) ->
move: (meters) ->
@name + " moved " + meters + "m."
class Snake extends Animal
move: ->
super 5
sam = new Snake "Sammy the Python"
sam.move()\
`;
aether.transpile(code);
expect(aether.run()).toEqual("Sammy the Python moved 5m.");
return expect(aether.problems.errors).toEqual([]);
});
it("Should compile string interpolation", function() {
const code = `\
meters = 5
"Sammy the Python moved #{meters}m."\
`;
aether.transpile(code);
expect(aether.run()).toEqual("Sammy the Python moved 5m.");
return expect(aether.problems.errors).toEqual([]);
});
return it("Should implicitly return the last statement", function() {
aether.transpile('"hi"');
expect(aether.run()).toEqual('hi');
return expect(aether.problems.errors).toEqual([]);
});
});
return describe("Errors", function() {
const aether = new Aether({language: "coffeescript"});
it("Bad indent", function() {
const code = `\
fn = ->
x = 45
x += 5
return x
return fn()\
`;
aether.transpile(code);
expect(aether.problems.errors.length).toEqual(1);
// TODO: No range information for this error
// https://github.com/codecombat/aether/issues/114
return expect(aether.problems.errors[0].message.indexOf("Syntax error on line 5, column 10: unexpected '+'")).toBe(0);
});
it("Transpile error, missing )", function() {
const code = `\
fn = ->
return 45
x = fn(
return x\
`;
aether.transpile(code);
expect(aether.problems.errors.length).toEqual(1);
// TODO: No range information for this error
// https://github.com/codecombat/aether/issues/114
return expect(aether.problems.errors[0].message.indexOf("Unexpected DEDENT")).toBe(0);
});
xit("Missing @: x() row 0", function() {
// TODO: error ranges incorrect
// https://github.com/codecombat/aether/issues/153
const code = "x()";
aether.transpile(code);
expect(aether.problems.errors.length).toEqual(1);
expect(aether.problems.errors[0].message).toEqual("Missing `@` keyword; should be `@x`.");
return expect(aether.problems.errors[0].range).toEqual([ { ofs: 0, row: 0, col: 0 }, { ofs: 3, row: 0, col: 3 } ]);
});
it("Missing @: x() row 1", function() {
const code = `\
y = 5
x()\
`;
aether.transpile(code);
expect(aether.problems.errors.length).toEqual(1);
return expect(aether.problems.errors[0].message).toEqual("Missing `@` keyword; should be `@x`.");
});
// https://github.com/codecombat/aether/issues/115
// expect(aether.problems.errors[0].range).toEqual([ { ofs: 6, row: 1, col: 0 }, { ofs: 9, row: 1, col: 3 } ])
it("Missing @: x() row 3", function() {
const code = `\
y = 5
s = 'some other stuff'
if y is 5
x()\
`;
aether.transpile(code);
expect(aether.problems.errors.length).toEqual(1);
return expect(aether.problems.errors[0].message).toEqual("Missing `@` keyword; should be `@x`.");
});
// https://github.com/codecombat/aether/issues/115
// expect(aether.problems.errors[0].range).toEqual([ { ofs: 42, row: 3, col: 2 }, { ofs: 45, row: 3, col: 5 } ])
xit("@getItems missing parentheses", function() {
// https://github.com/codecombat/aether/issues/111
const history = [];
const getItems = () => [{'pos':1}, {'pos':4}, {'pos':3}, {'pos':5}];
const move = i => history.push(i);
const thisValue = {getItems, move};
const code = `\
@getItems\
`;
aether.transpile(code);
const method = aether.createMethod(thisValue);
aether.run(method);
expect(aether.problems.errors.length).toEqual(1);
expect(aether.problems.errors[0].message).toEqual('@getItems has no effect.');
expect(aether.problems.errors[0].hint).toEqual('Is it a method? Those need parentheses: @getItems()');
return expect(aether.problems.errors[0].range).toEqual([ { ofs : 0, row : 0, col : 0 }, { ofs : 10, row : 0, col : 10 } ]);
});
xit("@getItems missing parentheses row 1", function() {
// https://github.com/codecombat/aether/issues/110
const history = [];
const getItems = () => [{'pos':1}, {'pos':4}, {'pos':3}, {'pos':5}];
const move = i => history.push(i);
const thisValue = {getItems, move};
const code = `\
x = 5
@getItems
y = 6\
`;
aether.transpile(code);
const method = aether.createMethod(thisValue);
aether.run(method);
expect(aether.problems.errors.length).toEqual(1);
expect(aether.problems.errors[0].message).toEqual('@getItems has no effect.');
expect(aether.problems.errors[0].hint).toEqual('Is it a method? Those need parentheses: @getItems()');
return expect(aether.problems.errors[0].range).toEqual([ { ofs : 7, row : 1, col : 0 }, { ofs : 16, row : 1, col : 9 } ]);
});
it("Incomplete string", function() {
const code = `\
s = 'hi
return s\
`;
aether.transpile(code);
expect(aether.problems.errors.length).toEqual(1);
return expect(aether.problems.errors[0].message).toEqual("Unclosed \"'\" at EOF");
});
// https://github.com/codecombat/aether/issues/114
// expect(aether.problems.errors[0].range).toEqual([ { ofs : 4, row : 0, col : 4 }, { ofs : 7, row : 0, col : 7 } ])
return xit("Runtime ReferenceError", function() {
// TODO: error ranges incorrect
// https://github.com/codecombat/aether/issues/153
const code = `\
x = 5
y = x + z\
`;
aether.transpile(code);
aether.run();
expect(aether.problems.errors.length).toEqual(1);
expect(/ReferenceError/.test(aether.problems.errors[0].message)).toBe(true);
return expect(aether.problems.errors[0].range).toEqual([ { ofs : 14, row : 1, col : 8 }, { ofs : 15, row : 1, col : 9 } ]);
});
});
});