forked from meteor/meteor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathruntime-tests.js
289 lines (239 loc) · 6.46 KB
/
runtime-tests.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
Tinytest.add("ecmascript - runtime - template literals", (test) => {
function dump(pieces) {
var copy = {};
// Can't use _.extend({}, pieces) because es5-shim adds enumerable
// methods to Array.prototype, and _.extend has no own property check.
_.each(_.keys(pieces), key => copy[key] = pieces[key]);
return [copy, _.toArray(arguments).slice(1)];
};
const foo = 'B';
// uses `babelHelpers.taggedTemplateLiteralLoose`
test.equal(`\u0041${foo}C`, 'ABC');
test.equal(dump`\u0041${foo}C`,
[{0:'A', 1: 'C', raw: ['\\u0041', 'C']},
['B']]);
});
Tinytest.add("ecmascript - runtime - classes - basic", (test) => {
{
class Foo {
constructor(x) {
this.x = x;
}
}
test.throws(() => {
Foo(); // called without `new`
});
test.equal((new Foo(3)).x, 3);
}
{
class Bar {
constructor(x) {
this.x = x;
}
}
class Foo extends Bar {}
test.throws(() => {
Foo(); // called without `new`
});
test.equal((new Foo(3)).x, 3);
test.isTrue((new Foo(3)) instanceof Foo);
test.isTrue((new Foo(3)) instanceof Bar);
}
{
class Foo {
static staticMethod() {
return 'classy';
}
prototypeMethod() {
return 'prototypical';
}
}
test.equal(Foo.staticMethod(), 'classy');
test.equal((new Foo).prototypeMethod(), 'prototypical');
}
});
Tinytest.add("ecmascript - runtime - classes - use before declare", (test) => {
const x = function asdf() {};
if (typeof asdf === 'function') {
// We seem to be in IE 8, where function names leak into the enclosing
// scope, contrary to the spec. In this case, Babel does not (currently)
// throw an error if you use a class before you declare it. (Of course,
// any other browser can tell the developer they screwed up!)
test.expect_fail();
}
test.throws(() => {
new Foo(); // use before definition
class Foo {}
});
});
Tinytest.add("ecmascript - runtime - classes - inheritance", (test) => {
// uses `babelHelpers.inherits`
{
class Foo {
static static1() {
return 1;
}
}
Foo.static2 = function () {
return 2;
};
// static methods are inherited!
class Bar extends Foo {}
test.equal(Foo.static1(), 1);
test.equal(Foo.static2(), 2);
test.equal(Bar.static1(), 1);
test.equal(Bar.static2(), 2);
}
{
const buf = [];
class Foo {
constructor() {
buf.push('hi');
}
}
class Bar extends Foo {}
new Bar();
// derived class with no constructor gets a default constructor
// that calls the super constructor
test.equal(buf, ['hi']);
}
});
Tinytest.add("ecmascript - runtime - classes - computed props", (test) => {
{
const frob = "inc";
class Foo {
static [frob](n) { return n+1; }
}
test.equal(Foo.inc(3), 4);
}
});
if (Meteor.isServer) {
// getters and setters don't work in all clients, but they should work
// in classes on browsers that support them in the first place, and on
// the server. (Technically they just need a working
// Object.defineProperty, found in IE9+ and all modern environments.)
Tinytest.add("ecmascript - runtime - classes - getters/setters", (test) => {
// uses `babelHelpers.createClass`
class Foo {
get two() { return 1+1; }
static get three() { return 1+1+1; }
}
test.equal((new Foo).two, 2);
test.equal(Foo.three, 3);
});
}
Tinytest.add("ecmascript - runtime - block scope", (test) => {
{
const buf = [];
const thunks = [];
function print(x) {
buf.push(x);
};
function doLater(f) {
thunks.push(f);
};
for (let i = 0; i < 3; i++) {
print(i);
}
test.equal(buf, [0, 1, 2]);
buf.length = 0;
for (let i = 0; i < 3; i++) {
doLater(() => {
print(i);
});
}
_.each(thunks, f => f());
test.equal(buf, [0, 1, 2]);
}
});
Tinytest.add("ecmascript - runtime - classes - super", (test) => {
{
class Class1 {
foo() { return 123; }
static bar() { return 1; }
}
class Class2 extends Class1 {}
class Class3 extends Class2 {
foo() {
return super.foo() + Class3.bar();
}
}
test.equal((new Class3).foo(), 124);
}
{
class Foo {
constructor(value) { this.value = value; }
x() { return this.value; }
}
class Bar extends Foo {
constructor() { super(123); }
x() { return super.x(); }
}
test.equal((new Bar).x(), 123);
}
});
Tinytest.add("ecmascript - runtime - object rest/spread", (test) => {
const middle = {b:2, c:3};
// uses `babelHelpers._extends`
const full = {a:1, ...middle, d:4};
test.equal(full, {a:1, b:2, c:3, d:4});
});
Tinytest.add("ecmascript - runtime - spread args to new", (test) => {
const Foo = function (one, two, three) {
test.isTrue(this instanceof Foo);
test.equal(one, 1);
test.equal(two, 2);
test.equal(three, 3);
this.created = true;
};
const oneTwo = [1, 2];
// uses `babelHelpers.bind`
const foo = new Foo(...oneTwo, 3);
test.isTrue(foo.created);
});
Tinytest.add("ecmascript - runtime - destructuring", (test) => {
// uses `babelHelpers.objectWithoutProperties` and
// `babelHelpers.objectDestructuringEmpty`
const obj = {a:1, b:2};
const {a, ...rest} = obj;
test.equal(a, 1);
test.equal(rest, {b:2});
const {} = {};
test.throws(() => {
const {} = null;
}, /Cannot destructure undefined/);
});
Tinytest.addAsync("ecmascript - runtime - misc support", (test, done) => {
// Verify that the runtime was installed.
test.equal(typeof babelHelpers, "object");
class Base {
constructor(...args) {
this.sum = 0;
args.forEach(arg => this.sum += arg);
}
static inherited() {
return "inherited";
}
}
class Derived extends Base {
constructor() {
super(1, 2, 3);
}
}
// Check that static methods are inherited.
test.equal(Derived.inherited(), "inherited");
const d = new Derived();
test.equal(d.sum, 6);
const expectedError = new Error("expected");
Promise.resolve("working").then(result => {
test.equal(result, "working");
throw expectedError;
}).catch(error => {
test.equal(error, expectedError);
if (Meteor.isServer) {
const Fiber = Npm.require("fibers");
// Make sure the Promise polyfill runs callbacks in a Fiber.
test.instanceOf(Fiber.current, Fiber);
}
}).then(done, error => test.exception(error));
});