forked from emberjs/ember.js
/
array.js
279 lines (234 loc) · 6.08 KB
/
array.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
import ArrayProxy from '../../lib/system/array_proxy';
import EmberArray, { A as emberA, MutableArray } from '../../lib/mixins/array';
import { generateGuid, guidFor } from '@ember/-internals/utils';
import {
get,
computed,
addArrayObserver,
removeArrayObserver,
arrayContentWillChange,
arrayContentDidChange,
} from '@ember/-internals/metal';
import EmberObject from '../../lib/system/object';
import { moduleFor } from 'internal-test-helpers';
export function newFixture(cnt) {
let ret = [];
while (--cnt >= 0) {
ret.push(generateGuid());
}
return ret;
}
export function newObjectsFixture(cnt) {
let ret = [];
let item;
while (--cnt >= 0) {
item = {};
guidFor(item);
ret.push(item);
}
return ret;
}
const ArrayTestsObserverClass = EmberObject.extend({
init() {
this._super(...arguments);
this.isEnabled = true;
this.reset();
},
reset() {
this._keys = {};
this._values = {};
this._before = null;
this._after = null;
return this;
},
observe(obj, ...keys) {
if (obj.addObserver) {
let loc = keys.length;
while (--loc >= 0) {
obj.addObserver(keys[loc], this, 'propertyDidChange');
}
} else {
this.isEnabled = false;
}
return this;
},
observeArray(obj) {
addArrayObserver(obj, this);
return this;
},
stopObserveArray(obj) {
removeArrayObserver(obj, this);
return this;
},
propertyDidChange(target, key, value) {
if (this._keys[key] === undefined) {
this._keys[key] = 0;
}
this._keys[key]++;
this._values[key] = value;
},
arrayWillChange() {
this.assert.equal(this._before, null, 'should only call once');
this._before = Array.prototype.slice.call(arguments);
},
arrayDidChange() {
this.assert.equal(this._after, null, 'should only call once');
this._after = Array.prototype.slice.call(arguments);
},
validate(key, value) {
if (!this.isEnabled) {
return true;
}
if (!this._keys[key]) {
return false;
}
if (arguments.length > 1) {
return this._values[key] === value;
} else {
return true;
}
},
timesCalled(key) {
return this._keys[key] || 0;
},
});
class AbstractArrayHelper {
beforeEach(assert) {
this.assert = assert;
}
newObject(ary) {
return ary ? ary.slice() : newFixture(3);
}
toArray(obj) {
return obj.slice();
}
newObserver() {
let ret = ArrayTestsObserverClass.create({
assert: this.assert,
});
if (arguments.length > 0) {
ret.observe.apply(ret, arguments);
}
return ret;
}
}
class NativeArrayHelpers extends AbstractArrayHelper {
newObject(ary) {
return emberA(super.newObject(ary));
}
mutate(obj) {
obj.pushObject(obj.length + 1);
}
}
class ArrayProxyHelpers extends AbstractArrayHelper {
newObject(ary) {
return ArrayProxy.create({ content: emberA(super.newObject(ary)) });
}
mutate(obj) {
obj.pushObject(get(obj, 'length') + 1);
}
toArray(obj) {
return obj.toArray ? obj.toArray() : obj.slice();
}
}
/*
Implement a basic fake mutable array. This validates that any non-native
enumerable can impl this API.
*/
const TestArray = EmberObject.extend(EmberArray, {
_content: null,
init() {
this._content = this._content || [];
},
// some methods to modify the array so we can test changes. Note that
// arrays can be modified even if they don't implement MutableArray. The
// MutableArray is just a standard API for mutation but not required.
addObject(obj) {
let idx = this._content.length;
arrayContentWillChange(this, idx, 0, 1);
this._content.push(obj);
arrayContentDidChange(this, idx, 0, 1);
},
removeFirst() {
arrayContentWillChange(this, 0, 1, 0);
this._content.shift();
arrayContentDidChange(this, 0, 1, 0);
},
objectAt(idx) {
return this._content[idx];
},
length: computed(function () {
return this._content.length;
}),
});
/*
Implement a basic fake mutable array. This validates that any non-native
enumerable can impl this API.
*/
const TestMutableArray = EmberObject.extend(MutableArray, {
_content: null,
init(ary = []) {
this._content = emberA(ary);
},
replace(idx, amt, objects) {
let args = objects ? objects.slice() : [];
let removeAmt = amt;
let addAmt = args.length;
arrayContentWillChange(this, idx, removeAmt, addAmt);
args.unshift(amt);
args.unshift(idx);
this._content.splice.apply(this._content, args);
arrayContentDidChange(this, idx, removeAmt, addAmt);
return this;
},
objectAt(idx) {
return this._content[idx];
},
length: computed(function () {
return this._content.length;
}),
slice() {
return this._content.slice();
},
});
class MutableArrayHelpers extends NativeArrayHelpers {
newObject(ary) {
return TestMutableArray.create(super.newObject(ary));
}
// allows for testing of the basic enumerable after an internal mutation
mutate(obj) {
obj.addObject(this.getFixture(1)[0]);
}
}
class EmberArrayHelpers extends MutableArrayHelpers {
newObject(ary) {
return TestArray.create(super.newObject(ary));
}
}
export function runArrayTests(name, Tests, ...types) {
if (types.length > 0) {
types.forEach((type) => {
switch (type) {
case 'ArrayProxy':
moduleFor(`ArrayProxy: ${name}`, Tests, ArrayProxyHelpers);
break;
case 'EmberArray':
moduleFor(`EmberArray: ${name}`, Tests, EmberArrayHelpers);
break;
case 'MutableArray':
moduleFor(`MutableArray: ${name}`, Tests, MutableArrayHelpers);
break;
case 'NativeArray':
moduleFor(`NativeArray: ${name}`, Tests, NativeArrayHelpers);
break;
default:
throw new Error(`runArrayTests passed unexpected type ${type}`);
}
});
} else {
moduleFor(`ArrayProxy: ${name}`, Tests, ArrayProxyHelpers);
moduleFor(`EmberArray: ${name}`, Tests, EmberArrayHelpers);
moduleFor(`MutableArray: ${name}`, Tests, MutableArrayHelpers);
moduleFor(`NativeArray: ${name}`, Tests, NativeArrayHelpers);
}
}