-
Notifications
You must be signed in to change notification settings - Fork 4
/
shape-test.js
606 lines (479 loc) · 15.6 KB
/
shape-test.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
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
var QUnit = require('steal-qunit');
var canSymbol = require('can-symbol');
var shapeReflections = require("./shape");
var getSetReflections = require("../get-set/get-set");
var testHelpers = require('../../can-reflect-test_helpers');
require("./schema/schema-test");
QUnit.module('can-reflect: shape reflections: own+enumerable');
function testModifiedMap(callback, symbolToMethod){
symbolToMethod = symbolToMethod || {
getOwnEnumerableKeys: "keys",
hasOwnKey: "has",
getKeyValue: "get"
};
if(testHelpers.mapSupported) {
shapeReflections.eachKey(symbolToMethod, function(method, symbol){
getSetReflections.setKeyValue(Map.prototype,canSymbol.for("can."+symbol),function(){
return this[method].apply(this, arguments);
});
});
callback();
shapeReflections.eachKey(symbolToMethod, function(symbol){
delete Map.prototype[canSymbol.for("can."+symbol)];
});
}
}
QUnit.test("getOwnEnumerableKeys (aka: keys)", function(){
QUnit.deepEqual( shapeReflections.keys( {foo: 1, bar: 2}), ["foo","bar"], "POJO" );
QUnit.deepEqual( shapeReflections.keys( ["0", "1"] ), Object.keys([1,2]), "Array" );
// Can we decorate a Map
testModifiedMap(function(){
var map = new Map(),
obj = {};
map.set("foo",1);
map.set(obj, 2);
QUnit.deepEqual( shapeReflections.toArray(shapeReflections.keys(map)),
["foo",{}], "Decorated Map with can.getOwnEnumerableKeys" );
});
// Can we do the long form w/o the fast path
var proto = {};
getSetReflections.setKeyValue(proto,canSymbol.for("can.getOwnKeys"),function(){
return ["a","b","c"];
});
getSetReflections.setKeyValue(proto,canSymbol.for("can.getOwnKeyDescriptor"),function(key){
return ({
a: {enumerable: false},
b: {enumerable: true },
c: {enumerable: true }
})[key];
});
var defineMapLike = Object.create(proto,{});
QUnit.deepEqual( shapeReflections.toArray(shapeReflections.keys(defineMapLike)),
["b","c"], "Decorated Object with can.getOwnKeys and can.getOwnKeyDescriptor");
/*var map = new Map(),
obj = {};
map.set("foo",1);
map.set(obj, 2);
QUnit.deepEqual( shapeReflections.toArray(shapeReflections.keys(map)),
["foo",{}], "un-decorated Map" );*/
});
QUnit.test("eachIndex", function(){
// Iterators work
var Ctr = function(){};
var arr = ["a", "b"];
getSetReflections.setKeyValue(Ctr.prototype,canSymbol.iterator,function(){
return {
i: 0,
next: function(){
if(this.i === 1) {
return { value: undefined, done: true };
}
this.i++;
return { value: arr, done: false };
}
};
});
var obj = new Ctr();
shapeReflections.eachIndex(obj, function(value, index){
QUnit.equal(index, 0);
QUnit.equal(value,arr);
});
shapeReflections.eachIndex(["a"], function(value, index){
QUnit.equal(index, 0);
QUnit.equal(value, "a");
});
function ArrayLike() {}
ArrayLike.prototype = [];
ArrayLike.prototype[canSymbol.iterator] = null;
var noniterator = new ArrayLike();
noniterator.push("a");
shapeReflections.eachIndex(noniterator, function(value, index){
QUnit.equal(index, 0);
QUnit.equal(value,"a");
});
});
QUnit.test("eachKey", function(){
var index;
var answers, map;
// Defined on something
testModifiedMap(function(){
var o1 = {}, o2 = {};
map = new Map([[o1, "1"], [o2, 2]]);
index = 0;
answers = [[o1, "1"], [o2, 2]];
shapeReflections.eachKey(map, function(value, key){
var answer = answers[index++];
QUnit.equal(value, answer[1], "map value");
QUnit.equal(key, answer[0], "map key");
});
});
var obj = {a: "1", b: "2"};
index = 0;
answers = [["a", "1"], ["b", "2"]];
shapeReflections.eachKey(obj, function(value, key){
var answer = answers[index++];
QUnit.equal(value, answer[1], "object value");
QUnit.equal(key, answer[0], "object key");
});
/*
map = new Map([[o1, "1"], [o2, 2]]);
index = 0;
answers = [[o1, "1"], [o2, 2]];
shapeReflections.eachKey(map, function(value, key){
var answer = answers[index++];
QUnit.equal(value, answer[1], "plain map value");
QUnit.equal(key, answer[0], "plain map key");
});*/
});
QUnit.test("each", function(){
shapeReflections.each({foo: "bar"}, function(value, key){
QUnit.equal(key, "foo");
QUnit.equal(value, "bar");
});
shapeReflections.each(["bar"], function(value, index){
QUnit.equal(index, 0);
QUnit.equal(value, "bar");
});
});
QUnit.test("toArray", function(){
if(typeof document !== "undefined") {
var ul = document.createElement("ul");
ul.innerHTML = "<li/><li/>";
var arr = shapeReflections.toArray(ul.childNodes);
QUnit.equal(arr.length, 2, "childNodes");
QUnit.equal(arr[0].nodeName.toLowerCase(), "li", "childNodes");
}
});
QUnit.module('can-reflect: shape reflections: own');
QUnit.test("hasOwnKey", function(){
var map;
// Defined on something
testModifiedMap(function(){
var o1 = {};
map = new Map();
map.set(o1, "1");
QUnit.ok( shapeReflections.hasOwnKey(map, o1) , "Map" );
});
var obj = {foo: "bar"};
QUnit.ok( shapeReflections.hasOwnKey(obj, "foo") , "obj" );
QUnit.ok( !shapeReflections.hasOwnKey(obj, "bar") , "obj" );
});
QUnit.test("getOwnKeys", function(){
var obj = Object.create(null,{
foo: {value: "1", enumerable: true},
bar: {value: "2", enumerable: false},
});
QUnit.deepEqual( shapeReflections.getOwnKeys(obj), ["foo","bar"] , "obj" );
});
QUnit.test("getOwnKeyDescriptor", function(){
var obj = {foo: "bar"};
QUnit.deepEqual(
shapeReflections.getOwnKeyDescriptor(obj,"foo"),
Object.getOwnPropertyDescriptor(obj, "foo") , "POJO" );
var obj2 = {};
getSetReflections.setKeyValue(obj2,canSymbol.for("can.getOwnKeyDescriptor"),function(key){
return ({foo:{enumerable: true, type: "thing"}})[key];
});
QUnit.deepEqual(
shapeReflections.getOwnKeyDescriptor(obj2,"foo"),
{enumerable: true, type: "thing"}, "w/ symbol" );
});
QUnit.test("unwrap basics", function(){
// tests something like
// compute(
// new Map({
// a: "A",
// list: new List([0,2])
// })
// )
var list = {};
getSetReflections.setKeyValue(list,canSymbol.iterator,function(){
return {
i: 0,
next: function(){
if(this.i === 3) {
return { value: undefined, done: true };
}
this.i++;
return { value: (this.i-1)*2, done: false };
}
};
});
getSetReflections.setKeyValue(list, canSymbol.for("can.isMoreListLikeThanMapLike"), true);
var compute = {};
getSetReflections.setKeyValue(compute,canSymbol.for("can.getValue"),function(){
var map = {};
getSetReflections.setKeyValue(map, canSymbol.for("can.getOwnEnumerableKeys"), function(){
return ["a","b","c","list"];
});
getSetReflections.setKeyValue(map, canSymbol.for("can.getKeyValue"), function(key){
return key === "list" ? list : key.toUpperCase();
});
return map;
});
var plain = shapeReflections.unwrap(compute);
QUnit.deepEqual( plain, {
a: "A",
b: "B",
c: "C",
list: [0,2,4]
});
});
QUnit.test("unwrap handles POJOs", function(){
var a = {foo: "bar"};
var plain = shapeReflections.unwrap(a);
QUnit.deepEqual( plain, a);
QUnit.ok( a !== plain , "returns copy");
});
if(typeof Map !== "undefined") {
QUnit.test("handles cycles", function(){
var a = {},
b = {};
a.b = b;
b.a = a;
var plain = shapeReflections.unwrap(a, Map);
QUnit.equal(plain.b.a, plain, "cycle intact");
QUnit.ok( a !== plain , "returns copy");
});
}
QUnit.test("isBuiltIn is only called after decorators are checked in shouldSerialize", function() {
var arr = [];
QUnit.ok(!shapeReflections.isSerialized(arr), "array is not isSerialized");
QUnit.ok(!shapeReflections.isSerialized({}), "obj is not isSerialized");
arr[canSymbol.for('can.setKeyValue')] = function() {};
QUnit.ok(!shapeReflections.isSerialized(arr));
if (testHelpers.setSupported) {
var set = new Set([{}, {}, {}]);
QUnit.ok(shapeReflections.isSerialized(set));
set[canSymbol.for("can.setKeyValue")] = function() {};
QUnit.ok(!shapeReflections.isSerialized(set));
}
});
QUnit.test(".serialize handles recursion with .unwrap", function(){
// tests something like
// compute(
// new Map({
// a: "A",
// list: new List([0,2])
// })
// )
var list = {};
getSetReflections.setKeyValue(list,canSymbol.iterator,function(){
return {
i: 0,
next: function(){
if(this.i === 3) {
return { value: undefined, done: true };
}
this.i++;
return { value: (this.i-1)*2, done: false };
}
};
});
getSetReflections.setKeyValue(list, canSymbol.for("can.isMoreListLikeThanMapLike"), true);
var compute = {};
getSetReflections.setKeyValue(compute,canSymbol.for("can.getValue"),function(){
var map = {};
getSetReflections.setKeyValue(map, canSymbol.for("can.getOwnEnumerableKeys"), function(){
return ["a","b","c","list"];
});
getSetReflections.setKeyValue(map, canSymbol.for("can.getKeyValue"), function(key){
return key === "list" ? list : key.toUpperCase();
});
return map;
});
var plain = shapeReflections.unwrap(compute);
QUnit.deepEqual( plain, {
a: "A",
b: "B",
c: "C",
list: [0,2,4]
});
});
QUnit.test(".serialize with recursive data structures", function(){
var obj = {};
obj.prop = obj;
var s = shapeReflections.serialize(obj);
QUnit.equal(s.prop, s, "Object points to itself");
});
QUnit.test("updateDeep basics", function(){
var obj = {
name: "Justin",
hobbies: [{id: 1, name: "js"},{id: 2, name: "foosball"}]
};
var hobbies = obj.hobbies;
var js = obj.hobbies[0];
shapeReflections.updateDeep(obj, {
age: 34,
hobbies: [{id: 1, name: "JS", fun: true}]
});
QUnit.deepEqual(obj, {
age: 34,
hobbies: [{id: 1, name: "JS", fun: true}]
});
QUnit.equal(obj.hobbies, hobbies, "merged hobbies");
QUnit.equal(obj.hobbies[0], js, "merged js");
shapeReflections.updateDeep(obj, {
age: 34,
hobbies: [{id: 1, name: "JS", fun: true},{id: 2, name: "foosball"}]
});
QUnit.deepEqual(obj, {
age: 34,
hobbies: [{id: 1, name: "JS", fun: true},{id: 2, name: "foosball"}]
}, "added foosball");
QUnit.equal(obj.hobbies, hobbies, "merged hobbies");
QUnit.equal(obj.hobbies[0], js, "merged js");
});
QUnit.test("updateDeep", function(){
var a = [];
shapeReflections.updateDeep(a, ["a","b"]);
QUnit.deepEqual(a, ["a","b"]);
});
QUnit.test("can assign undefined values", function(){
var obj = shapeReflections.assignMap({}, {foo: undefined});
QUnit.ok(obj.hasOwnProperty("foo"), "has an undefined foo");
});
QUnit.test("assignMap", function(){
var target = shapeReflections.assignSymbols({},{
"can.setKeyValue": function(key, value){
this[key] = value * 2;
},
"can.getKeyValue": function(key) {
return this[key] !== undefined ? this[key] / 2 : undefined;
}
});
target.a = 22;
var source = shapeReflections.assignSymbols({},{
"can.setKeyValue": function(key, value){
this[key] = value * 3;
},
"can.getKeyValue": function(key) {
return this[key] !== undefined ? this[key] / 3 : undefined;
}
});
shapeReflections.assignMap(source,{
a: 1,
b: 2
});
QUnit.deepEqual(source,{
a: 3,
b: 6
}, "set values on source");
shapeReflections.assignMap(target, source);
QUnit.deepEqual(target,{
a: 2,
b: 4
}, "set values on target");
});
QUnit.test("getOwnEnumerableKeys with primitives", function(){
QUnit.deepEqual(shapeReflections.getOwnEnumerableKeys(1),[],"works with primitive");
});
if(typeof Symbol !== "undefined") {
QUnit.test("assignSymbols can set Symbol.iterator", function(){
var fn = function(){ };
var obj = shapeReflections.assignSymbols({},{
"iterator": fn
});
QUnit.equal(obj[Symbol.iterator], fn, "works");
});
}
QUnit.test("defineInstanceKey with symbol on prototype", function() {
var testKey = "foo";
var testDef = { value: "bar" };
function Foo() {}
Foo.prototype[canSymbol.for("can.defineInstanceKey")] = function(key, definition) {
QUnit.equal(key, testKey);
QUnit.deepEqual(definition, testDef);
};
shapeReflections.defineInstanceKey(Foo, testKey, testDef);
});
QUnit.test("defineInstanceKey with no symbol on prototype", function() {
var testKey = "foo";
var testDef = { value: "bar" };
var def;
function Foo() {}
shapeReflections.defineInstanceKey(Foo, testKey, testDef);
QUnit.ok(def = Object.getOwnPropertyDescriptor(Foo.prototype, testKey), "Has descriptor");
QUnit.equal(def.value, testDef.value, "Value is correctly set");
QUnit.equal(def.configurable, true, "value is configurable");
QUnit.equal(def.writable, true, "value is writable");
});
QUnit.test("updateDeep recurses correctly (#73)", function(){
var source = {
name: 'juan',
hobbies: ['games', 'photography', 'building']
},
sourceArray = source.hobbies;
shapeReflections.updateDeep(source, {hobbies: ['headdesk']});
QUnit.deepEqual(source, {hobbies: ['headdesk']}, "source looks right");
QUnit.equal(sourceArray, source.hobbies, "array updated");
});
QUnit.module('can-reflect: shape reflections: proto chain');
QUnit.test("hasKey", function() {
/*var objHasKey = {};
Object.defineProperty(objHasKey, "_keys", {
value: { foo: true }
});
getSetReflections.setKeyValue(objHasKey, canSymbol.for("can.hasKey"), function(key) {
return key in this._keys;
});
QUnit.ok(shapeReflections.hasKey(objHasKey, "foo") , "returns true when hasKey Symbol returns true");
QUnit.ok(!shapeReflections.hasKey(objHasKey, "bar") , "returns false when hasKey Symbol returns false");
var objHasOwnKey = {};
Object.defineProperty(objHasOwnKey, "_keys", {
value: { foo: true }
});
getSetReflections.setKeyValue(objHasOwnKey, canSymbol.for("can.hasOwnKey"), function(key) {
return key in this._keys;
});
QUnit.ok(shapeReflections.hasKey(objHasOwnKey, "foo") , "returns true when hasOwnKey Symbol returns true");
QUnit.ok(!shapeReflections.hasKey(objHasOwnKey, "bar") , "returns false when hasOwnKey Symbol returns false");
objHasOwnKey.bar = "baz";
QUnit.ok(shapeReflections.hasKey(objHasOwnKey, "bar") , "returns true when hasOwnKey Symbol returns false but `in` returns true");*/
debugger;
QUnit.ok(shapeReflections.hasKey(55, "toFixed") , "works on primitives");
QUnit.ok(shapeReflections.hasKey(true, "valueOf") , "works on primitives");
QUnit.ok(shapeReflections.hasKey('foo', "length") , "works on primitives");
QUnit.notOk(shapeReflections.hasKey(null, "length") , "works on null");
QUnit.notOk(shapeReflections.hasKey(undefined, "length") , "works on undefined");
});
QUnit.test("serialize clones", function(){
var obj = {foo: {bar: "zed"}};
var res = shapeReflections.serialize(obj);
QUnit.deepEqual(res, obj, "look equal");
QUnit.notOk(res === obj);
QUnit.notOk(res.foo === obj.foo);
});
QUnit.test("serialize clones arrays", function(){
var obj = {foo: [{zed: "ted"}]};
var obj2 = shapeReflections.serialize(obj);
QUnit.deepEqual(obj2, obj, "deep equal");
QUnit.notOk(obj === obj2, "ret not the same");
QUnit.notOk(obj.foo === obj2.foo, "foo not the same");
QUnit.notOk(obj.foo[0] === obj2.foo[0]);
});
QUnit.test(".size", function(){
QUnit.equal( shapeReflections.size([1]), 1, "array");
QUnit.equal( shapeReflections.size([]), 0, "array");
QUnit.equal( shapeReflections.size("a"), 1, "string");
QUnit.equal( shapeReflections.size(""), 0, "array");
QUnit.equal( shapeReflections.size({}), 0, "empty object");
QUnit.equal( shapeReflections.size({foo:"bar"}), 1, "object");
QUnit.equal( shapeReflections.size(null), 0, "null");
QUnit.equal( shapeReflections.size(undefined), 0, "undefined");
});
QUnit.test("size works with out hasOwnProperty (#109)", function(){
var obj = Object.create(null);
QUnit.equal( shapeReflections.size(obj), 0, "empty object");
obj.foo = "bar";
QUnit.equal( shapeReflections.size(obj), 1, "has value");
});
QUnit.test("each loops without needing `this`", function(){
var each = shapeReflections.each;
each({}, function(){});
QUnit.ok(true, "no error");
});
/*QUnit.test("getAllEnumerableKeys", function(){
});
QUnit.test("getAllKeys", function(){
});*/