-
Notifications
You must be signed in to change notification settings - Fork 176
/
suite.js
452 lines (385 loc) · 12.3 KB
/
suite.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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
/*
* Copyright 2016 Google Inc. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
module.exports = () => function(scope) {
'use strict';
/**
* @return Test object useful for the polyfill, as objects are sealed after creation.
*/
function buildObject() {
return {
name: 'I\'m a test',
value: 123,
sub: {name: 'sub'},
};
}
function suiteFor(impl, name) {
suite(name, function() {
test('get', function() {
var testObj = buildObject();
var gets = [];
var p = new impl(testObj, {get: function(obj, prop) {
gets.push(prop);
return obj[prop];
}});
p.name;
p.sub;
p.sub.name;
var s = p.sub;
s.name; // not a get
assert.deepEqual(gets, 'name sub sub sub'.split(/\s+/));
});
test('set', function() {
var testObj = buildObject();
var sets = [];
var p = new impl(testObj, {set: function(obj, prop) {
sets.push(prop);
return true;
}});
p.value += 1;
p.sub = 45;
assert.isNotNumber(p.sub, 'setter should not actually set');
assert.deepEqual(sets, 'value sub'.split(/\s+/));
});
test('proxy chain', function() {
var object = {value: 123};
var p = new impl(object, {});
var pp = new impl(p, {get: function(obj, prop) {
return obj[prop];
}});
var ppp = new impl(pp, {});
assert.equal(ppp.value, 123);
})
test('callable', function() {
var calls = 0;
var callable = function() {
return ++calls;
};
var p = new impl(callable, {});
assert.equal(1, p());
assert.equal(2, callable());
assert.equal(3, p());
});
// test wrapping a constructor without proxying it
test('wrap constructor', function() {
var fn = function(y) {
this.x = 1;
};
fn.prototype.sentinel = true;
var p = new impl(fn, {});
var obj = new p();
assert(obj.sentinel, 'prototype not configured correctly');
});
test('construct/apply assertions', function() {
var pc = new impl({}, {construct: function(target, argumentsList) {
assert(false, 'should not get here');
}});
assert.throws(function() {
pc();
}, TypeError);
var pa = new impl({}, {apply: function(target, argumentsList) {
assert(false, 'should not get here');
}});
assert.throws(function() {
new pa();
}, TypeError);
});
test('construct', function() {
var fn = function(y) {
var me = this || {}; // this won't be set in strict mode, fake it
me.x = (y || 0);
return me;
};
fn.prototype.sentinel = true;
var p = new impl(fn, {construct: function(target, argumentsList) {
return new target((argumentsList[0] || 0) + 10);
}});
var obj = new p(5);
assert.equal(obj.x, 15);
assert(obj.sentinel);
var funcObj = p(5);
assert.equal(funcObj.x, 5);
assert(!funcObj.sentinel, 'apply use should not contain sentinel');
});
test('proxy without construct handler passes arguments', function() {
var cls = function(x, y) {
assert(this instanceof cls, 'cls prototype is not set correctly');
this.x = x;
this.y = y;
};
var p = new impl(cls, {});
var x = new p(1, 2);
assert.equal(x.x, 1);
assert.equal(x.y, 2);
});
test('apply on non-function', function() {
var object = {};
var dummy = new impl(object, {});
assert.isNotFunction(dummy, 'stock proxy is not function');
assert.throws(function() {
dummy();
}, TypeError);
var p = new impl(object, {apply: function() {
// doesn't matter
}});
assert.throws(function() {
p();
});
});
test('apply', function() {
var fauxThis = {};
var fn = function() {
assert(false, 'should not get here');
};
var called = 0;
var p = new impl(fn, {apply: function(target, thisArg, argumentsList) {
assert.equal(fn, target);
assert.equal(fauxThis, thisArg);
assert.equal(argumentsList.length, 2);
assert.equal(argumentsList[0], 'a');
assert.equal(argumentsList[1], 2);
++called;
}});
p.call(fauxThis, 'a', 2);
assert.equal(called, 1);
});
test('traps function', function() {
var real = function(x, y) {
return 1;
};
var p = new impl(real, function(x, y) {
assert.equal(this, real);
assert.equal(this(), 1);
assert.equal(x, undefined); // arguments aren't passed
assert.equal(y, undefined);
return 2;
});
assert.equal(p(3, 4), 2);
});
test('revocable proxy', function() {
var p = impl.revocable({a: 1}, {});
p.proxy.a = 2;
p.revoke();
assert.throws(function() {
p.proxy.a = 3;
}, TypeError);
var calls = 0;
p = impl.revocable({b: 2}, {get: function(obj, prop) {
++calls;
return obj[prop];
}});
p.proxy.b;
p.proxy.b;
p.revoke();
assert.throws(function() {
p.proxy.b;
}, TypeError);
assert.equal(calls, 2);
var fn = function() {
assert(false, 'should never get here');
};
p = impl.revocable(fn, {apply: function() {
// doesn't matter
}});
p.revoke();
assert.throws(function() {
p.proxy();
}, TypeError);
});
test('proxy instance of class', function() {
var cls = function() {
this.y = 1;
};
cls.prototype.x = function() {
++this.y;
};
var inst = new cls();
var p = new impl(inst, {});
assert('x' in inst, 'inst should have function');
assert('x' in p, 'proxy should have function');
p.x();
assert.equal(p.y, 2);
});
test('proxy class with inline props', function() {
class Foo {
foo = "bar";
}
var called = [];
var inst = new Foo();
var p = new impl(inst, {
set(target, property, value) {
target[property] = value;
called.push(property);
return true;
},
});
assert('foo' in inst, 'inst should have prop');
assert('foo' in p, 'proxy should have prop');
p.foo = "que";
assert.equal(inst.foo, "que");
assert.sameMembers(called, ['foo'], 'should be called with "foo"');
});
test('proxy class with class props', function() {
class Foo {
_foo = 'bar';
get foo() { return this._foo };
set foo(v) { this._foo = v };
}
var called = [];
var inst = new Foo();
var p = new impl(inst, {
set(target, property, value) {
target[property] = value;
called.push(property);
return true;
},
});
assert('foo' in inst, 'inst should have prop');
assert('foo' in p, 'proxy should have prop');
p.foo = "que";
assert.equal(inst.foo, "que");
assert.sameMembers(called, ['foo'], 'should be called with "foo"');
});
test('proxy class with super props', function() {
class SuperClass {
_foo = 'bar';
get foo() { return this._foo };
set foo(v) { this._foo = v };
}
class Foo extends SuperClass {
}
var called = [];
var inst = new Foo();
var p = new impl(inst, {
set(target, property, value) {
target[property] = value;
called.push(property);
return true;
},
});
assert('foo' in inst, 'inst should have prop');
assert('foo' in p, 'proxy should have prop');
p.foo = "que";
assert.equal(inst.foo, "que");
assert.sameMembers(called, ['foo'], 'should be called with "foo"');
});
test('trap instance methods', function() {
var cls = function() {
this.y = 1;
};
cls.prototype.x = function() {};
var inst = new cls();
var found;
var p = new impl(inst, {get: function(obj, prop) {
assert.equal(obj, inst);
found = prop;
return obj[prop];
}});
p.x();
assert.equal(found, 'x', 'expected get of function');
// Confirm set method behavior, proxy vs polyfill.
var custom = function() { this.y = 2; };
var supportSet = true;
try {
inst.x = custom;
} catch (e) {
supportSet = false;
}
p.x();
if (supportSet) { // native
assert.equal(inst.x, custom);
assert.equal(p.y, 2);
} else { // polyfill
assert.equal(p.y, 1);
}
});
test('subclass of array', function() {
class Foo extends Array {}
var f = new Foo(2);
f[0] = 'first';
f[1] = 'second';
var p = new impl(f, {get: function(obj, prop) {
return obj[prop];
}});
assert.equal(p[0], 'first');
assert.equal(p.length, 2);
assert.isTrue(p instanceof Array, 'should be instanceof Array');
if (impl === scope.NativeProxy) {
// nb. this fails in NativeProxy
assert.throws(function() {
p instanceof impl;
}, 'Function has non-object prototype', 'cannot instanceof native proxy');
} else {
assert.isFalse(p instanceof impl, 'should not be instanceof Proxy');
}
assert.equal(Object.getPrototypeOf(p), Object.getPrototypeOf(f));
});
});
}
suiteFor(scope.Proxy, 'polyfill');
if (scope.NativeProxy) {
suiteFor(scope.NativeProxy, 'native');
}
suite('general polyfill', function() {
test('prototypal inheritance', function() {
function Foo() {
}
var f = new Foo();
var p = new Proxy(f, {});
assert.isTrue(p instanceof Foo, 'proxy looks like proxied object');
assert.isFalse(p instanceof Proxy, 'proxy does not look like Proxy')
});
test('seals object', function() {
var testObj = buildObject();
assert.isNotSealed(testObj);
var p = new Proxy(testObj, {});
assert.isSealed(testObj);
assert.isSealed(p, 'proxy should also be sealed');
new Proxy(testObj, {});
assert.isSealed(testObj);
var pp = new Proxy(p, {});
assert.isSealed(p);
assert.throws(function() {
pp.newProperty = true;
}, TypeError);
});
test('seals array', function() {
var testArray = [7,8,9];
assert.isNotSealed(testArray);
var p = new Proxy(testArray, {});
assert.isSealed(testArray);
assert.isSealed(p, 'proxy should also be sealed');
assert.throws(function() {
p.push(1);
}, TypeError);
// slice is a copy of array
var slice = testArray.slice(0, 1);
slice.push(2);
assert.equal(slice.length, 2);
});
// nb. Trying to resolve issue #12
test('array as property', function() {
var testObj = {arr: [1,2,3]};
var p = new Proxy(testObj, {get: function(obj, prop) {
return obj[prop]; // zero get handler
}});
assert.equal(p.arr.length, 3);
p.arr.push(4);
assert.equal(p.arr.length, 4);
p.arr.splice(0,2);
assert.equal(p.arr.length, 2);
});
});
}(typeof process !== 'undefined' && {}.toString.call(process) === '[object process]' ? global : self);