Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50655 views
1
'use strict';
2
3
var ES = require('../').ES6;
4
var test = require('tape');
5
6
var forEach = require('foreach');
7
var is = require('object-is');
8
var debug = require('util').format;
9
10
var hasSymbols = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol';
11
12
var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || Math.pow(2, 53) - 1;
13
14
var coercibleObject = { valueOf: function () { return 3; }, toString: function () { return 42; } };
15
var valueOfOnlyObject = { valueOf: function () { return 4; }, toString: function () { return {}; } };
16
var toStringOnlyObject = { valueOf: function () { return {}; }, toString: function () { return 7; } };
17
var uncoercibleObject = { valueOf: function () { return {}; }, toString: function () { return {}; } };
18
var objects = [{}, coercibleObject, toStringOnlyObject, valueOfOnlyObject];
19
var numbers = [0, -0, Infinity, -Infinity, 42];
20
var nonNullPrimitives = [true, false, 'foo', ''].concat(numbers);
21
var primitives = [undefined, null].concat(nonNullPrimitives);
22
23
test('ToPrimitive', function (t) {
24
t.test('primitives', function (st) {
25
var testPrimitive = function (primitive) {
26
st.ok(is(ES.ToPrimitive(primitive), primitive), primitive + ' is returned correctly');
27
};
28
forEach(primitives, testPrimitive);
29
st.end();
30
});
31
32
t.test('objects', function (st) {
33
st.equal(ES.ToPrimitive(coercibleObject), 3, 'coercibleObject with no hint coerces to valueOf');
34
st.ok(is(ES.ToPrimitive({}), '[object Object]'), '{} with no hint coerces to Object#toString');
35
st.equal(ES.ToPrimitive(coercibleObject, Number), 3, 'coercibleObject with hint Number coerces to valueOf');
36
st.ok(is(ES.ToPrimitive({}, Number), '[object Object]'), '{} with hint Number coerces to NaN');
37
st.equal(ES.ToPrimitive(coercibleObject, String), 42, 'coercibleObject with hint String coerces to nonstringified toString');
38
st.equal(ES.ToPrimitive({}, String), '[object Object]', '{} with hint String coerces to Object#toString');
39
st.equal(ES.ToPrimitive(toStringOnlyObject), 7, 'toStringOnlyObject returns non-stringified toString');
40
st.equal(ES.ToPrimitive(valueOfOnlyObject), 4, 'valueOfOnlyObject returns valueOf');
41
st.throws(function () { return ES.ToPrimitive(uncoercibleObject); }, TypeError, 'uncoercibleObject throws a TypeError');
42
st.end();
43
});
44
45
t.end();
46
});
47
48
test('ToBoolean', function (t) {
49
t.equal(false, ES.ToBoolean(undefined), 'undefined coerces to false');
50
t.equal(false, ES.ToBoolean(null), 'null coerces to false');
51
t.equal(false, ES.ToBoolean(false), 'false returns false');
52
t.equal(true, ES.ToBoolean(true), 'true returns true');
53
54
t.test('numbers', function (st) {
55
forEach([0, -0, NaN], function (falsyNumber) {
56
st.equal(false, ES.ToBoolean(falsyNumber), 'falsy number ' + falsyNumber + ' coerces to false');
57
});
58
forEach([Infinity, 42, 1, -Infinity], function (truthyNumber) {
59
st.equal(true, ES.ToBoolean(truthyNumber), 'truthy number ' + truthyNumber + ' coerces to true');
60
});
61
62
st.end();
63
});
64
65
t.equal(false, ES.ToBoolean(''), 'empty string coerces to false');
66
t.equal(true, ES.ToBoolean('foo'), 'nonempty string coerces to true');
67
68
t.test('objects', function (st) {
69
forEach(objects, function (obj) {
70
st.equal(true, ES.ToBoolean(obj), 'object coerces to true');
71
});
72
st.equal(true, ES.ToBoolean(uncoercibleObject), 'uncoercibleObject coerces to true');
73
74
st.end();
75
});
76
77
t.end();
78
});
79
80
test('ToNumber', function (t) {
81
t.ok(is(NaN, ES.ToNumber(undefined)), 'undefined coerces to NaN');
82
t.ok(is(ES.ToNumber(null), 0), 'null coerces to +0');
83
t.ok(is(ES.ToNumber(false), 0), 'false coerces to +0');
84
t.equal(1, ES.ToNumber(true), 'true coerces to 1');
85
86
t.test('numbers', function (st) {
87
st.ok(is(NaN, ES.ToNumber(NaN)), 'NaN returns itself');
88
forEach([0, -0, 42, Infinity, -Infinity], function (num) {
89
st.equal(num, ES.ToNumber(num), num + ' returns itself');
90
});
91
forEach(['foo', '0', '4a', '2.0', 'Infinity', '-Infinity'], function (numString) {
92
st.ok(is(+numString, ES.ToNumber(numString)), '"' + numString + '" coerces to ' + Number(numString));
93
});
94
st.end();
95
});
96
97
t.test('objects', function (st) {
98
forEach(objects, function (object) {
99
st.ok(is(ES.ToNumber(object), ES.ToNumber(ES.ToPrimitive(object))), 'object ' + object + ' coerces to same as ToPrimitive of object does');
100
});
101
st.throws(function () { return ES.ToNumber(uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
102
st.end();
103
});
104
105
t.test('binary literals', function (st) {
106
st.equal(ES.ToNumber('0b10'), 2, '0b10 is 2');
107
st.equal(ES.ToNumber({ toString: function () { return '0b11'; } }), 3, 'Object that toStrings to 0b11 is 3');
108
109
st.equal(true, is(ES.ToNumber('0b12'), NaN), '0b12 is NaN');
110
st.equal(true, is(ES.ToNumber({ toString: function () { return '0b112'; } }), NaN), 'Object that toStrings to 0b112 is NaN');
111
st.end();
112
});
113
114
t.test('octal literals', function (st) {
115
st.equal(ES.ToNumber('0o10'), 8, '0o10 is 8');
116
st.equal(ES.ToNumber({ toString: function () { return '0o11'; } }), 9, 'Object that toStrings to 0o11 is 9');
117
118
st.equal(true, is(ES.ToNumber('0o18'), NaN), '0o18 is NaN');
119
st.equal(true, is(ES.ToNumber({ toString: function () { return '0o118'; } }), NaN), 'Object that toStrings to 0o118 is NaN');
120
st.end();
121
});
122
123
t.test('signed hex numbers', function (st) {
124
st.equal(true, is(ES.ToNumber('-0xF'), NaN), '-0xF is NaN');
125
st.equal(true, is(ES.ToNumber(' -0xF '), NaN), 'space-padded -0xF is NaN');
126
st.equal(true, is(ES.ToNumber('+0xF'), NaN), '+0xF is NaN');
127
st.equal(true, is(ES.ToNumber(' +0xF '), NaN), 'space-padded +0xF is NaN');
128
129
st.end();
130
});
131
132
t.test('trimming of whitespace and non-whitespace characters', function (st) {
133
var whitespace = ' \t\x0b\f\xa0\ufeff\n\r\u2028\u2029\u1680\u180e\u2000\u2001\u2002\u2003\u2004\u2005\u2006\u2007\u2008\u2009\u200a\u202f\u205f\u3000';
134
st.equal(0, ES.ToNumber(whitespace + 0 + whitespace), 'whitespace is trimmed');
135
136
// Zero-width space (zws), next line character (nel), and non-character (bom) are not whitespace.
137
var nonWhitespaces = {
138
'\\u0085': '\u0085',
139
'\\u200b': '\u200b',
140
'\\ufffe': '\ufffe'
141
};
142
143
forEach(nonWhitespaces, function (desc, nonWS) {
144
st.equal(true, is(ES.ToNumber(nonWS + 0 + nonWS), NaN), 'non-whitespace ' + desc + ' not trimmed');
145
});
146
147
st.end();
148
});
149
150
t.end();
151
});
152
153
test('ToInteger', function (t) {
154
t.ok(is(0, ES.ToInteger(NaN)), 'NaN coerces to +0');
155
forEach([0, Infinity, 42], function (num) {
156
t.ok(is(num, ES.ToInteger(num)), num + ' returns itself');
157
t.ok(is(-num, ES.ToInteger(-num)), '-' + num + ' returns itself');
158
});
159
t.equal(3, ES.ToInteger(Math.PI), 'pi returns 3');
160
t.throws(function () { return ES.ToInteger(uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
161
t.end();
162
});
163
164
test('ToInt32', function (t) {
165
t.ok(is(0, ES.ToInt32(NaN)), 'NaN coerces to +0');
166
forEach([0, Infinity], function (num) {
167
t.ok(is(0, ES.ToInt32(num)), num + ' returns +0');
168
t.ok(is(0, ES.ToInt32(-num)), '-' + num + ' returns +0');
169
});
170
t.throws(function () { return ES.ToInt32(uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
171
t.ok(is(ES.ToInt32(0x100000000), 0), '2^32 returns +0');
172
t.ok(is(ES.ToInt32(0x100000000 - 1), -1), '2^32 - 1 returns -1');
173
t.ok(is(ES.ToInt32(0x80000000), -0x80000000), '2^31 returns -2^31');
174
t.ok(is(ES.ToInt32(0x80000000 - 1), 0x80000000 - 1), '2^31 - 1 returns 2^31 - 1');
175
forEach([0, Infinity, NaN, 0x100000000, 0x80000000, 0x10000, 0x42], function (num) {
176
t.ok(is(ES.ToInt32(num), ES.ToInt32(ES.ToUint32(num))), 'ToInt32(x) === ToInt32(ToUint32(x)) for 0x' + num.toString(16));
177
t.ok(is(ES.ToInt32(-num), ES.ToInt32(ES.ToUint32(-num))), 'ToInt32(x) === ToInt32(ToUint32(x)) for -0x' + num.toString(16));
178
});
179
t.end();
180
});
181
182
test('ToUint32', function (t) {
183
t.ok(is(0, ES.ToUint32(NaN)), 'NaN coerces to +0');
184
forEach([0, Infinity], function (num) {
185
t.ok(is(0, ES.ToUint32(num)), num + ' returns +0');
186
t.ok(is(0, ES.ToUint32(-num)), '-' + num + ' returns +0');
187
});
188
t.throws(function () { return ES.ToUint32(uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
189
t.ok(is(ES.ToUint32(0x100000000), 0), '2^32 returns +0');
190
t.ok(is(ES.ToUint32(0x100000000 - 1), 0x100000000 - 1), '2^32 - 1 returns 2^32 - 1');
191
t.ok(is(ES.ToUint32(0x80000000), 0x80000000), '2^31 returns 2^31');
192
t.ok(is(ES.ToUint32(0x80000000 - 1), 0x80000000 - 1), '2^31 - 1 returns 2^31 - 1');
193
forEach([0, Infinity, NaN, 0x100000000, 0x80000000, 0x10000, 0x42], function (num) {
194
t.ok(is(ES.ToUint32(num), ES.ToUint32(ES.ToInt32(num))), 'ToUint32(x) === ToUint32(ToInt32(x)) for 0x' + num.toString(16));
195
t.ok(is(ES.ToUint32(-num), ES.ToUint32(ES.ToInt32(-num))), 'ToUint32(x) === ToUint32(ToInt32(x)) for -0x' + num.toString(16));
196
});
197
t.end();
198
});
199
200
test('ToInt16', function (t) {
201
t.ok(is(0, ES.ToInt16(NaN)), 'NaN coerces to +0');
202
forEach([0, Infinity], function (num) {
203
t.ok(is(0, ES.ToInt16(num)), num + ' returns +0');
204
t.ok(is(0, ES.ToInt16(-num)), '-' + num + ' returns +0');
205
});
206
t.throws(function () { return ES.ToInt16(uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
207
t.ok(is(ES.ToInt16(0x100000000), 0), '2^32 returns +0');
208
t.ok(is(ES.ToInt16(0x100000000 - 1), -1), '2^32 - 1 returns -1');
209
t.ok(is(ES.ToInt16(0x80000000), 0), '2^31 returns +0');
210
t.ok(is(ES.ToInt16(0x80000000 - 1), -1), '2^31 - 1 returns -1');
211
t.ok(is(ES.ToInt16(0x10000), 0), '2^16 returns +0');
212
t.ok(is(ES.ToInt16(0x10000 - 1), -1), '2^16 - 1 returns -1');
213
t.end();
214
});
215
216
test('ToUint16', function (t) {
217
t.ok(is(0, ES.ToUint16(NaN)), 'NaN coerces to +0');
218
forEach([0, Infinity], function (num) {
219
t.ok(is(0, ES.ToUint16(num)), num + ' returns +0');
220
t.ok(is(0, ES.ToUint16(-num)), '-' + num + ' returns +0');
221
});
222
t.throws(function () { return ES.ToUint16(uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
223
t.ok(is(ES.ToUint16(0x100000000), 0), '2^32 returns +0');
224
t.ok(is(ES.ToUint16(0x100000000 - 1), 0x10000 - 1), '2^32 - 1 returns 2^16 - 1');
225
t.ok(is(ES.ToUint16(0x80000000), 0), '2^31 returns +0');
226
t.ok(is(ES.ToUint16(0x80000000 - 1), 0x10000 - 1), '2^31 - 1 returns 2^16 - 1');
227
t.ok(is(ES.ToUint16(0x10000), 0), '2^16 returns +0');
228
t.ok(is(ES.ToUint16(0x10000 - 1), 0x10000 - 1), '2^16 - 1 returns 2^16 - 1');
229
t.end();
230
});
231
232
test('ToInt8', function (t) {
233
t.ok(is(0, ES.ToInt8(NaN)), 'NaN coerces to +0');
234
forEach([0, Infinity], function (num) {
235
t.ok(is(0, ES.ToInt8(num)), num + ' returns +0');
236
t.ok(is(0, ES.ToInt8(-num)), '-' + num + ' returns +0');
237
});
238
t.throws(function () { return ES.ToInt8(uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
239
t.ok(is(ES.ToInt8(0x100000000), 0), '2^32 returns +0');
240
t.ok(is(ES.ToInt8(0x100000000 - 1), -1), '2^32 - 1 returns -1');
241
t.ok(is(ES.ToInt8(0x80000000), 0), '2^31 returns +0');
242
t.ok(is(ES.ToInt8(0x80000000 - 1), -1), '2^31 - 1 returns -1');
243
t.ok(is(ES.ToInt8(0x10000), 0), '2^16 returns +0');
244
t.ok(is(ES.ToInt8(0x10000 - 1), -1), '2^16 - 1 returns -1');
245
t.ok(is(ES.ToInt8(0x100), 0), '2^8 returns +0');
246
t.ok(is(ES.ToInt8(0x100 - 1), -1), '2^8 - 1 returns -1');
247
t.ok(is(ES.ToInt8(0x10), 0x10), '2^4 returns 2^4');
248
t.end();
249
});
250
251
test('ToUint8', function (t) {
252
t.ok(is(0, ES.ToUint8(NaN)), 'NaN coerces to +0');
253
forEach([0, Infinity], function (num) {
254
t.ok(is(0, ES.ToUint8(num)), num + ' returns +0');
255
t.ok(is(0, ES.ToUint8(-num)), '-' + num + ' returns +0');
256
});
257
t.throws(function () { return ES.ToUint8(uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
258
t.ok(is(ES.ToUint8(0x100000000), 0), '2^32 returns +0');
259
t.ok(is(ES.ToUint8(0x100000000 - 1), 0x100 - 1), '2^32 - 1 returns 2^8 - 1');
260
t.ok(is(ES.ToUint8(0x80000000), 0), '2^31 returns +0');
261
t.ok(is(ES.ToUint8(0x80000000 - 1), 0x100 - 1), '2^31 - 1 returns 2^8 - 1');
262
t.ok(is(ES.ToUint8(0x10000), 0), '2^16 returns +0');
263
t.ok(is(ES.ToUint8(0x10000 - 1), 0x100 - 1), '2^16 - 1 returns 2^8 - 1');
264
t.ok(is(ES.ToUint8(0x100), 0), '2^8 returns +0');
265
t.ok(is(ES.ToUint8(0x100 - 1), 0x100 - 1), '2^8 - 1 returns 2^16 - 1');
266
t.ok(is(ES.ToUint8(0x10), 0x10), '2^4 returns 2^4');
267
t.ok(is(ES.ToUint8(0x10 - 1), 0x10 - 1), '2^4 - 1 returns 2^4 - 1');
268
t.end();
269
});
270
271
test('ToUint8Clamp', function (t) {
272
t.ok(is(0, ES.ToUint8Clamp(NaN)), 'NaN coerces to +0');
273
t.ok(is(0, ES.ToUint8Clamp(0)), '+0 returns +0');
274
t.ok(is(0, ES.ToUint8Clamp(-0)), '-0 returns +0');
275
t.ok(is(0, ES.ToUint8Clamp(-Infinity)), '-Infinity returns +0');
276
t.throws(function () { return ES.ToUint8Clamp(uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
277
forEach([255, 256, 0x100000, Infinity], function (number) {
278
t.ok(is(255, ES.ToUint8Clamp(number)), number + ' coerces to 255');
279
});
280
t.equal(1, ES.ToUint8Clamp(1.49), '1.49 coerces to 1');
281
t.equal(2, ES.ToUint8Clamp(1.5), '1.5 coerces to 2, because 2 is even');
282
t.equal(2, ES.ToUint8Clamp(1.51), '1.51 coerces to 2');
283
284
t.equal(2, ES.ToUint8Clamp(2.49), '2.49 coerces to 2');
285
t.equal(2, ES.ToUint8Clamp(2.5), '2.5 coerces to 2, because 2 is even');
286
t.equal(3, ES.ToUint8Clamp(2.51), '2.51 coerces to 3');
287
t.end();
288
});
289
290
test('ToString', function (t) {
291
forEach(objects.concat(primitives), function (item) {
292
t.equal(ES.ToString(item), String(item), 'ES.ToString(' + debug(item) + ') ToStrings to String(' + debug(item) + ')');
293
});
294
t.throws(function () { return ES.ToString(uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
295
if (hasSymbols) {
296
t.throws(function () { return ES.ToString(Symbol.iterator); }, TypeError, debug(Symbol.iterator) + ' throws');
297
}
298
t.end();
299
});
300
301
test('ToObject', function (t) {
302
t.throws(function () { return ES.ToObject(undefined); }, TypeError, 'undefined throws');
303
t.throws(function () { return ES.ToObject(null); }, TypeError, 'null throws');
304
forEach(numbers, function (number) {
305
var obj = ES.ToObject(number);
306
t.equal(typeof obj, 'object', 'number ' + number + ' coerces to object');
307
t.equal(true, obj instanceof Number, 'object of ' + number + ' is Number object');
308
t.ok(is(obj.valueOf(), number), 'object of ' + number + ' coerces to ' + number);
309
});
310
t.end();
311
});
312
313
test('RequireObjectCoercible', function (t) {
314
t.equal(false, 'CheckObjectCoercible' in ES, 'CheckObjectCoercible -> RequireObjectCoercible in ES6');
315
t.throws(function () { return ES.RequireObjectCoercible(undefined); }, TypeError, 'undefined throws');
316
t.throws(function () { return ES.RequireObjectCoercible(null); }, TypeError, 'null throws');
317
var doesNotThrow = function (value) {
318
t.doesNotThrow(function () { return ES.RequireObjectCoercible(value); }, '"' + value + '" does not throw');
319
};
320
forEach(objects.concat(nonNullPrimitives), doesNotThrow);
321
t.end();
322
});
323
324
test('IsCallable', function (t) {
325
t.equal(true, ES.IsCallable(function () {}), 'function is callable');
326
var nonCallables = [/a/g, {}, Object.prototype, NaN].concat(primitives);
327
forEach(nonCallables, function (nonCallable) {
328
t.equal(false, ES.IsCallable(nonCallable), nonCallable + ' is not callable');
329
});
330
t.end();
331
});
332
333
test('SameValue', function (t) {
334
t.equal(true, ES.SameValue(NaN, NaN), 'NaN is SameValue as NaN');
335
t.equal(false, ES.SameValue(0, -0), '+0 is not SameValue as -0');
336
forEach(objects.concat(primitives), function (val) {
337
t.equal(val === val, ES.SameValue(val, val), '"' + val + '" is SameValue to itself');
338
});
339
t.end();
340
});
341
342
test('SameValueZero', function (t) {
343
t.equal(true, ES.SameValueZero(NaN, NaN), 'NaN is SameValueZero as NaN');
344
t.equal(true, ES.SameValueZero(0, -0), '+0 is SameValueZero as -0');
345
forEach(objects.concat(primitives), function (val) {
346
t.equal(val === val, ES.SameValueZero(val, val), '"' + val + '" is SameValueZero to itself');
347
});
348
t.end();
349
});
350
351
test('ToPropertyKey', function (t) {
352
forEach(objects.concat(primitives), function (value) {
353
t.equal(ES.ToPropertyKey(value), String(value), 'ToPropertyKey(value) === String(value) for non-Symbols');
354
});
355
if (hasSymbols) {
356
t.equal(ES.ToPropertyKey(Symbol.iterator), 'Symbol(Symbol.iterator)', 'ToPropertyKey(Symbol.iterator) === "Symbol(Symbol.iterator)"');
357
}
358
t.end();
359
});
360
361
test('ToLength', function (t) {
362
t.throws(function () { return ES.ToLength(uncoercibleObject); }, TypeError, 'uncoercibleObject throws a TypeError');
363
t.equal(3, ES.ToLength(coercibleObject), 'coercibleObject coerces to 3');
364
t.equal(42, ES.ToLength('42.5'), '"42.5" coerces to 42');
365
t.equal(7, ES.ToLength(7.3), '7.3 coerces to 7');
366
forEach([-0, -1, -42, -Infinity], function (negative) {
367
t.ok(is(0, ES.ToLength(negative)), negative + ' coerces to +0');
368
});
369
t.equal(MAX_SAFE_INTEGER, ES.ToLength(MAX_SAFE_INTEGER + 1), '2^53 coerces to 2^53 - 1');
370
t.equal(MAX_SAFE_INTEGER, ES.ToLength(MAX_SAFE_INTEGER + 3), '2^53 + 2 coerces to 2^53 - 1');
371
t.end();
372
});
373
374
test('IsArray', function (t) {
375
t.equal(true, ES.IsArray([]), '[] is array');
376
t.equal(false, ES.IsArray({}), '{} is not array');
377
t.equal(false, ES.IsArray({ length: 1, 0: true }), 'arraylike object is not array');
378
forEach(objects.concat(primitives), function (value) {
379
t.equal(false, ES.IsArray(value), value + ' is not array');
380
});
381
t.end();
382
});
383
384
test('IsRegExp', function (t) {
385
forEach([/a/g, new RegExp('a', 'g')], function (regex) {
386
t.equal(true, ES.IsRegExp(regex), regex + ' is regex');
387
});
388
forEach(objects.concat(primitives), function (nonRegex) {
389
t.equal(false, ES.IsRegExp(nonRegex), nonRegex + ' is not regex');
390
});
391
t.test('Symbol.match', { skip: !hasSymbols || !Symbol.match }, function (st) {
392
var obj = {};
393
obj[Symbol.match] = true;
394
st.equal(true, ES.IsRegExp(obj), 'object with truthy Symbol.match is regex');
395
396
var regex = /a/;
397
regex[Symbol.match] = false;
398
st.equal(false, ES.IsRegExp(regex), 'regex with falsy Symbol.match is not regex');
399
400
st.end();
401
});
402
t.end();
403
});
404
405
test('IsPropertyKey', function (t) {
406
forEach(numbers.concat(objects), function (notKey) {
407
t.equal(false, ES.IsPropertyKey(notKey), notKey + ' is not property key');
408
});
409
t.equal(true, ES.IsPropertyKey('foo'), 'string is property key');
410
if (hasSymbols) {
411
t.equal(true, ES.IsPropertyKey(Symbol.iterator), 'Symbol.iterator is property key');
412
}
413
t.end();
414
});
415
416
test('IsInteger', function (t) {
417
for (var i = -100; i < 100; i += 10) {
418
t.equal(true, ES.IsInteger(i), i + ' is integer');
419
t.equal(false, ES.IsInteger(i + 0.2), (i + 0.2) + ' is not integer');
420
}
421
t.equal(true, ES.IsInteger(-0), '-0 is integer');
422
var notInts = objects.concat([Infinity, -Infinity, NaN, true, false, null, undefined, [], new Date()]);
423
if (hasSymbols) { notInts.push(Symbol.iterator); }
424
forEach(notInts, function (notInt) {
425
t.equal(false, ES.IsInteger(notInt), ES.ToPropertyKey(notInt) + ' is not integer');
426
});
427
t.equal(false, ES.IsInteger(uncoercibleObject), 'uncoercibleObject is not integer');
428
t.end();
429
});
430
431
test('IsExtensible', function (t) {
432
forEach(objects, function (object) {
433
t.equal(true, ES.IsExtensible(object), object + ' object is extensible');
434
});
435
forEach(primitives, function (primitive) {
436
t.equal(false, ES.IsExtensible(primitive), primitive + ' is not extensible');
437
});
438
if (Object.preventExtensions) {
439
t.equal(false, ES.IsExtensible(Object.preventExtensions({})), 'object with extensions prevented is not extensible');
440
}
441
t.end();
442
});
443
444
test('CanonicalNumericIndexString', function (t) {
445
var throwsOnNonString = function (notString) {
446
t.throws(function () { return ES.CanonicalNumericIndexString(notString); }, TypeError, notString + ' is not a string');
447
};
448
forEach(objects.concat(numbers), throwsOnNonString);
449
t.ok(is(-0, ES.CanonicalNumericIndexString('-0')), '"-0" returns -0');
450
for (var i = -50; i < 50; i += 10) {
451
t.equal(i, ES.CanonicalNumericIndexString(String(i)), '"' + i + '" returns ' + i);
452
t.equal(undefined, ES.CanonicalNumericIndexString(String(i) + 'a'), '"' + i + 'a" returns undefined');
453
}
454
t.end();
455
});
456
457
test('IsConstructor', function (t) {
458
t.equal(true, ES.IsConstructor(function () {}), 'function is constructor');
459
t.equal(false, ES.IsConstructor(/a/g), 'regex is not constructor');
460
forEach(objects, function (object) {
461
t.equal(false, ES.IsConstructor(object), object + ' object is not constructor');
462
});
463
464
try {
465
var foo = Function('return class Foo {}')(); // eslint-disable-line no-new-func
466
t.equal(ES.IsConstructor(foo), true, 'class is constructor');
467
} catch (e) {
468
t.comment('SKIP: class syntax not supported.');
469
}
470
t.end();
471
});
472
473
test('Call', function (t) {
474
var receiver = {};
475
var notFuncs = objects.concat(primitives).concat([/a/g, new RegExp('a', 'g')]);
476
t.plan(notFuncs.length + 4);
477
var throwsOnCall = function (notFunc) {
478
t.throws(function () { return ES.Call(notFunc, receiver); }, TypeError, notFunc + ' (' + typeof notFunc + ') is not callable');
479
};
480
forEach(notFuncs, throwsOnCall);
481
ES.Call(function (a, b) {
482
t.equal(this, receiver, 'context matches expected');
483
t.deepEqual([a, b], [1, 2], 'named args are correct');
484
t.equal(arguments.length, 3, 'extra argument was passed');
485
t.equal(arguments[2], 3, 'extra argument was correct');
486
}, receiver, [1, 2, 3]);
487
t.end();
488
});
489
490
test('GetV', function (t) {
491
t.throws(function () { return ES.GetV({ 7: 7 }, 7); }, TypeError, 'Throws a TypeError if `P` is not a property key');
492
var obj = { a: function () {} };
493
t.equal(ES.GetV(obj, 'a'), obj.a, 'returns property if it exists');
494
t.equal(ES.GetV(obj, 'b'), undefined, 'returns undefiend if property does not exist');
495
t.end();
496
});
497
498
test('GetMethod', function (t) {
499
t.throws(function () { return ES.GetMethod({ 7: 7 }, 7); }, TypeError, 'Throws a TypeError if `P` is not a property key');
500
t.equal(ES.GetMethod({}, 'a'), undefined, 'returns undefined in property is undefined');
501
t.equal(ES.GetMethod({ a: null }, 'a'), undefined, 'returns undefined if property is null');
502
t.equal(ES.GetMethod({ a: undefined }, 'a'), undefined, 'returns undefined if property is undefined');
503
var obj = { a: function () {} };
504
t.throws(function () { ES.GetMethod({ a: 'b' }, 'a'); }, TypeError, 'throws TypeError if property exists and is not callable');
505
t.equal(ES.GetMethod(obj, 'a'), obj.a, 'returns property if it is callable');
506
t.end();
507
});
508
509
test('Get', function (t) {
510
t.throws(function () { return ES.Get('a', 'a'); }, TypeError, 'Throws a TypeError if `O` is not an Object');
511
t.throws(function () { return ES.Get({ 7: 7 }, 7); }, TypeError, 'Throws a TypeError if `P` is not a property key');
512
513
var value = {};
514
t.test('Symbols', { skip: !hasSymbols }, function (st) {
515
var sym = Symbol('sym');
516
var obj = {};
517
obj[sym] = value;
518
st.equal(ES.Get(obj, sym), value, 'returns property `P` if it exists on object `O`');
519
st.end();
520
});
521
t.equal(ES.Get({ a: value }, 'a'), value, 'returns property `P` if it exists on object `O`');
522
t.end();
523
});
524
525
test('Type', { skip: !hasSymbols }, function (t) {
526
t.equal(ES.Type(Symbol.iterator), 'Symbol', 'Type(Symbol.iterator) is Symbol');
527
t.end();
528
});
529
530
test('SpeciesConstructor', function (t) {
531
t.throws(function () { ES.SpeciesConstructor(null); }, TypeError);
532
t.throws(function () { ES.SpeciesConstructor(undefined); }, TypeError);
533
534
var defaultConstructor = function Foo() {};
535
536
t.equal(
537
ES.SpeciesConstructor({ constructor: undefined }, defaultConstructor),
538
defaultConstructor,
539
'undefined constructor returns defaultConstructor'
540
);
541
542
t.throws(
543
function () { return ES.SpeciesConstructor({ constructor: null }, defaultConstructor); },
544
TypeError,
545
'non-undefined non-object constructor throws'
546
);
547
548
var Bar = function Bar() {};
549
var hasSpecies = hasSymbols && Symbol.species;
550
if (hasSpecies) {
551
Bar[Symbol.species] = null;
552
}
553
t.equal(
554
ES.SpeciesConstructor(new Bar(), defaultConstructor),
555
defaultConstructor,
556
'undefined/null Symbol.species returns default constructor'
557
);
558
559
t.test('with Symbol.species', { skip: !hasSpecies }, function (st) {
560
var Baz = function Baz() {};
561
Baz[Symbol.species] = Bar;
562
st.equal(
563
ES.SpeciesConstructor(new Baz(), defaultConstructor),
564
Bar,
565
'returns Symbol.species constructor value'
566
);
567
568
Baz[Symbol.species] = {};
569
st.throws(
570
function () { ES.SpeciesConstructor(new Baz(), defaultConstructor); },
571
TypeError,
572
'throws when non-constructor non-null non-undefined species value found'
573
);
574
575
st.end();
576
});
577
t.end();
578
});
579
580