1'use strict'; 2 3var test = require('tape'); 4 5var forEach = require('foreach'); 6var is = require('object-is'); 7var debug = require('object-inspect'); 8var assign = require('object.assign'); 9 10var v = require('./helpers/values'); 11var diffOps = require('./diffOps'); 12 13var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || Math.pow(2, 53) - 1; 14 15var getArraySubclassWithSpeciesConstructor = function getArraySubclass(speciesConstructor) { 16 var Bar = function Bar() { 17 var inst = []; 18 Object.setPrototypeOf(inst, Bar.prototype); 19 Object.defineProperty(inst, 'constructor', { value: Bar }); 20 return inst; 21 }; 22 Bar.prototype = Object.create(Array.prototype); 23 Object.setPrototypeOf(Bar, Array); 24 Object.defineProperty(Bar, Symbol.species, { value: speciesConstructor }); 25 26 return Bar; 27}; 28 29var hasSpecies = v.hasSymbols && Symbol.species; 30 31var hasGroups = 'groups' in (/a/).exec('a'); 32var groups = function groups(matchObject) { 33 return hasGroups ? assign(matchObject, { groups: matchObject.groups }) : matchObject; 34}; 35 36var es2015 = function ES2015(ES, ops, expectedMissing) { 37 test('has expected operations', function (t) { 38 var diff = diffOps(ES, ops, expectedMissing); 39 40 t.deepEqual(diff.extra, [], 'no extra ops'); 41 42 t.deepEqual(diff.missing, [], 'no unexpected missing ops'); 43 44 t.end(); 45 }); 46 47 test('ToPrimitive', function (t) { 48 t.test('primitives', function (st) { 49 var testPrimitive = function (primitive) { 50 st.ok(is(ES.ToPrimitive(primitive), primitive), debug(primitive) + ' is returned correctly'); 51 }; 52 forEach(v.primitives, testPrimitive); 53 st.end(); 54 }); 55 56 t.test('objects', function (st) { 57 st.equal(ES.ToPrimitive(v.coercibleObject), 3, 'coercibleObject with no hint coerces to valueOf'); 58 st.ok(is(ES.ToPrimitive({}), '[object Object]'), '{} with no hint coerces to Object#toString'); 59 st.equal(ES.ToPrimitive(v.coercibleObject, Number), 3, 'coercibleObject with hint Number coerces to valueOf'); 60 st.ok(is(ES.ToPrimitive({}, Number), '[object Object]'), '{} with hint Number coerces to NaN'); 61 st.equal(ES.ToPrimitive(v.coercibleObject, String), 42, 'coercibleObject with hint String coerces to nonstringified toString'); 62 st.equal(ES.ToPrimitive({}, String), '[object Object]', '{} with hint String coerces to Object#toString'); 63 st.equal(ES.ToPrimitive(v.toStringOnlyObject), 7, 'toStringOnlyObject returns non-stringified toString'); 64 st.equal(ES.ToPrimitive(v.valueOfOnlyObject), 4, 'valueOfOnlyObject returns valueOf'); 65 st['throws'](function () { return ES.ToPrimitive(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws a TypeError'); 66 st.end(); 67 }); 68 69 t.test('dates', function (st) { 70 var invalid = new Date(NaN); 71 st.equal(ES.ToPrimitive(invalid), Date.prototype.toString.call(invalid), 'invalid Date coerces to Date#toString'); 72 var now = new Date(); 73 st.equal(ES.ToPrimitive(now), Date.prototype.toString.call(now), 'Date coerces to Date#toString'); 74 st.end(); 75 }); 76 77 t.end(); 78 }); 79 80 test('ToBoolean', function (t) { 81 t.equal(false, ES.ToBoolean(undefined), 'undefined coerces to false'); 82 t.equal(false, ES.ToBoolean(null), 'null coerces to false'); 83 t.equal(false, ES.ToBoolean(false), 'false returns false'); 84 t.equal(true, ES.ToBoolean(true), 'true returns true'); 85 86 t.test('numbers', function (st) { 87 forEach([0, -0, NaN], function (falsyNumber) { 88 st.equal(false, ES.ToBoolean(falsyNumber), 'falsy number ' + falsyNumber + ' coerces to false'); 89 }); 90 forEach([Infinity, 42, 1, -Infinity], function (truthyNumber) { 91 st.equal(true, ES.ToBoolean(truthyNumber), 'truthy number ' + truthyNumber + ' coerces to true'); 92 }); 93 94 st.end(); 95 }); 96 97 t.equal(false, ES.ToBoolean(''), 'empty string coerces to false'); 98 t.equal(true, ES.ToBoolean('foo'), 'nonempty string coerces to true'); 99 100 t.test('objects', function (st) { 101 forEach(v.objects, function (obj) { 102 st.equal(true, ES.ToBoolean(obj), 'object coerces to true'); 103 }); 104 st.equal(true, ES.ToBoolean(v.uncoercibleObject), 'uncoercibleObject coerces to true'); 105 106 st.end(); 107 }); 108 109 t.end(); 110 }); 111 112 test('ToNumber', function (t) { 113 t.ok(is(NaN, ES.ToNumber(undefined)), 'undefined coerces to NaN'); 114 t.ok(is(ES.ToNumber(null), 0), 'null coerces to +0'); 115 t.ok(is(ES.ToNumber(false), 0), 'false coerces to +0'); 116 t.equal(1, ES.ToNumber(true), 'true coerces to 1'); 117 118 t.test('numbers', function (st) { 119 st.ok(is(NaN, ES.ToNumber(NaN)), 'NaN returns itself'); 120 forEach([0, -0, 42, Infinity, -Infinity], function (num) { 121 st.equal(num, ES.ToNumber(num), num + ' returns itself'); 122 }); 123 forEach(['foo', '0', '4a', '2.0', 'Infinity', '-Infinity'], function (numString) { 124 st.ok(is(+numString, ES.ToNumber(numString)), '"' + numString + '" coerces to ' + Number(numString)); 125 }); 126 st.end(); 127 }); 128 129 t.test('objects', function (st) { 130 forEach(v.objects, function (object) { 131 st.ok(is(ES.ToNumber(object), ES.ToNumber(ES.ToPrimitive(object))), 'object ' + object + ' coerces to same as ToPrimitive of object does'); 132 }); 133 st['throws'](function () { return ES.ToNumber(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws'); 134 st.end(); 135 }); 136 137 t.test('binary literals', function (st) { 138 st.equal(ES.ToNumber('0b10'), 2, '0b10 is 2'); 139 st.equal(ES.ToNumber({ toString: function () { return '0b11'; } }), 3, 'Object that toStrings to 0b11 is 3'); 140 141 st.equal(true, is(ES.ToNumber('0b12'), NaN), '0b12 is NaN'); 142 st.equal(true, is(ES.ToNumber({ toString: function () { return '0b112'; } }), NaN), 'Object that toStrings to 0b112 is NaN'); 143 st.end(); 144 }); 145 146 t.test('octal literals', function (st) { 147 st.equal(ES.ToNumber('0o10'), 8, '0o10 is 8'); 148 st.equal(ES.ToNumber({ toString: function () { return '0o11'; } }), 9, 'Object that toStrings to 0o11 is 9'); 149 150 st.equal(true, is(ES.ToNumber('0o18'), NaN), '0o18 is NaN'); 151 st.equal(true, is(ES.ToNumber({ toString: function () { return '0o118'; } }), NaN), 'Object that toStrings to 0o118 is NaN'); 152 st.end(); 153 }); 154 155 t.test('signed hex numbers', function (st) { 156 st.equal(true, is(ES.ToNumber('-0xF'), NaN), '-0xF is NaN'); 157 st.equal(true, is(ES.ToNumber(' -0xF '), NaN), 'space-padded -0xF is NaN'); 158 st.equal(true, is(ES.ToNumber('+0xF'), NaN), '+0xF is NaN'); 159 st.equal(true, is(ES.ToNumber(' +0xF '), NaN), 'space-padded +0xF is NaN'); 160 161 st.end(); 162 }); 163 164 t.test('trimming of whitespace and non-whitespace characters', function (st) { 165 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'; 166 st.equal(0, ES.ToNumber(whitespace + 0 + whitespace), 'whitespace is trimmed'); 167 168 // Zero-width space (zws), next line character (nel), and non-character (bom) are not whitespace. 169 var nonWhitespaces = { 170 '\\u0085': '\u0085', 171 '\\u200b': '\u200b', 172 '\\ufffe': '\ufffe' 173 }; 174 175 forEach(nonWhitespaces, function (desc, nonWS) { 176 st.equal(true, is(ES.ToNumber(nonWS + 0 + nonWS), NaN), 'non-whitespace ' + desc + ' not trimmed'); 177 }); 178 179 st.end(); 180 }); 181 182 forEach(v.symbols, function (symbol) { 183 t['throws']( 184 function () { ES.ToNumber(symbol); }, 185 TypeError, 186 'Symbols can’t be converted to a Number: ' + debug(symbol) 187 ); 188 }); 189 190 t.test('dates', function (st) { 191 var invalid = new Date(NaN); 192 st.ok(is(ES.ToNumber(invalid), NaN), 'invalid Date coerces to NaN'); 193 var now = Date.now(); 194 st.equal(ES.ToNumber(new Date(now)), now, 'Date coerces to timestamp'); 195 st.end(); 196 }); 197 198 t.end(); 199 }); 200 201 test('ToInteger', function (t) { 202 t.ok(is(0, ES.ToInteger(NaN)), 'NaN coerces to +0'); 203 forEach([0, Infinity, 42], function (num) { 204 t.ok(is(num, ES.ToInteger(num)), num + ' returns itself'); 205 t.ok(is(-num, ES.ToInteger(-num)), '-' + num + ' returns itself'); 206 }); 207 t.equal(3, ES.ToInteger(Math.PI), 'pi returns 3'); 208 t['throws'](function () { return ES.ToInteger(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws'); 209 t.end(); 210 }); 211 212 test('ToInt32', function (t) { 213 t.ok(is(0, ES.ToInt32(NaN)), 'NaN coerces to +0'); 214 forEach([0, Infinity], function (num) { 215 t.ok(is(0, ES.ToInt32(num)), num + ' returns +0'); 216 t.ok(is(0, ES.ToInt32(-num)), '-' + num + ' returns +0'); 217 }); 218 t['throws'](function () { return ES.ToInt32(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws'); 219 t.ok(is(ES.ToInt32(0x100000000), 0), '2^32 returns +0'); 220 t.ok(is(ES.ToInt32(0x100000000 - 1), -1), '2^32 - 1 returns -1'); 221 t.ok(is(ES.ToInt32(0x80000000), -0x80000000), '2^31 returns -2^31'); 222 t.ok(is(ES.ToInt32(0x80000000 - 1), 0x80000000 - 1), '2^31 - 1 returns 2^31 - 1'); 223 forEach([0, Infinity, NaN, 0x100000000, 0x80000000, 0x10000, 0x42], function (num) { 224 t.ok(is(ES.ToInt32(num), ES.ToInt32(ES.ToUint32(num))), 'ToInt32(x) === ToInt32(ToUint32(x)) for 0x' + num.toString(16)); 225 t.ok(is(ES.ToInt32(-num), ES.ToInt32(ES.ToUint32(-num))), 'ToInt32(x) === ToInt32(ToUint32(x)) for -0x' + num.toString(16)); 226 }); 227 t.end(); 228 }); 229 230 test('ToUint32', function (t) { 231 t.ok(is(0, ES.ToUint32(NaN)), 'NaN coerces to +0'); 232 forEach([0, Infinity], function (num) { 233 t.ok(is(0, ES.ToUint32(num)), num + ' returns +0'); 234 t.ok(is(0, ES.ToUint32(-num)), '-' + num + ' returns +0'); 235 }); 236 t['throws'](function () { return ES.ToUint32(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws'); 237 t.ok(is(ES.ToUint32(0x100000000), 0), '2^32 returns +0'); 238 t.ok(is(ES.ToUint32(0x100000000 - 1), 0x100000000 - 1), '2^32 - 1 returns 2^32 - 1'); 239 t.ok(is(ES.ToUint32(0x80000000), 0x80000000), '2^31 returns 2^31'); 240 t.ok(is(ES.ToUint32(0x80000000 - 1), 0x80000000 - 1), '2^31 - 1 returns 2^31 - 1'); 241 forEach([0, Infinity, NaN, 0x100000000, 0x80000000, 0x10000, 0x42], function (num) { 242 t.ok(is(ES.ToUint32(num), ES.ToUint32(ES.ToInt32(num))), 'ToUint32(x) === ToUint32(ToInt32(x)) for 0x' + num.toString(16)); 243 t.ok(is(ES.ToUint32(-num), ES.ToUint32(ES.ToInt32(-num))), 'ToUint32(x) === ToUint32(ToInt32(x)) for -0x' + num.toString(16)); 244 }); 245 t.end(); 246 }); 247 248 test('ToInt16', function (t) { 249 t.ok(is(0, ES.ToInt16(NaN)), 'NaN coerces to +0'); 250 forEach([0, Infinity], function (num) { 251 t.ok(is(0, ES.ToInt16(num)), num + ' returns +0'); 252 t.ok(is(0, ES.ToInt16(-num)), '-' + num + ' returns +0'); 253 }); 254 t['throws'](function () { return ES.ToInt16(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws'); 255 t.ok(is(ES.ToInt16(0x100000000), 0), '2^32 returns +0'); 256 t.ok(is(ES.ToInt16(0x100000000 - 1), -1), '2^32 - 1 returns -1'); 257 t.ok(is(ES.ToInt16(0x80000000), 0), '2^31 returns +0'); 258 t.ok(is(ES.ToInt16(0x80000000 - 1), -1), '2^31 - 1 returns -1'); 259 t.ok(is(ES.ToInt16(0x10000), 0), '2^16 returns +0'); 260 t.ok(is(ES.ToInt16(0x10000 - 1), -1), '2^16 - 1 returns -1'); 261 t.end(); 262 }); 263 264 test('ToUint16', function (t) { 265 t.ok(is(0, ES.ToUint16(NaN)), 'NaN coerces to +0'); 266 forEach([0, Infinity], function (num) { 267 t.ok(is(0, ES.ToUint16(num)), num + ' returns +0'); 268 t.ok(is(0, ES.ToUint16(-num)), '-' + num + ' returns +0'); 269 }); 270 t['throws'](function () { return ES.ToUint16(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws'); 271 t.ok(is(ES.ToUint16(0x100000000), 0), '2^32 returns +0'); 272 t.ok(is(ES.ToUint16(0x100000000 - 1), 0x10000 - 1), '2^32 - 1 returns 2^16 - 1'); 273 t.ok(is(ES.ToUint16(0x80000000), 0), '2^31 returns +0'); 274 t.ok(is(ES.ToUint16(0x80000000 - 1), 0x10000 - 1), '2^31 - 1 returns 2^16 - 1'); 275 t.ok(is(ES.ToUint16(0x10000), 0), '2^16 returns +0'); 276 t.ok(is(ES.ToUint16(0x10000 - 1), 0x10000 - 1), '2^16 - 1 returns 2^16 - 1'); 277 t.end(); 278 }); 279 280 test('ToInt8', function (t) { 281 t.ok(is(0, ES.ToInt8(NaN)), 'NaN coerces to +0'); 282 forEach([0, Infinity], function (num) { 283 t.ok(is(0, ES.ToInt8(num)), num + ' returns +0'); 284 t.ok(is(0, ES.ToInt8(-num)), '-' + num + ' returns +0'); 285 }); 286 t['throws'](function () { return ES.ToInt8(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws'); 287 t.ok(is(ES.ToInt8(0x100000000), 0), '2^32 returns +0'); 288 t.ok(is(ES.ToInt8(0x100000000 - 1), -1), '2^32 - 1 returns -1'); 289 t.ok(is(ES.ToInt8(0x80000000), 0), '2^31 returns +0'); 290 t.ok(is(ES.ToInt8(0x80000000 - 1), -1), '2^31 - 1 returns -1'); 291 t.ok(is(ES.ToInt8(0x10000), 0), '2^16 returns +0'); 292 t.ok(is(ES.ToInt8(0x10000 - 1), -1), '2^16 - 1 returns -1'); 293 t.ok(is(ES.ToInt8(0x100), 0), '2^8 returns +0'); 294 t.ok(is(ES.ToInt8(0x100 - 1), -1), '2^8 - 1 returns -1'); 295 t.ok(is(ES.ToInt8(0x10), 0x10), '2^4 returns 2^4'); 296 t.end(); 297 }); 298 299 test('ToUint8', function (t) { 300 t.ok(is(0, ES.ToUint8(NaN)), 'NaN coerces to +0'); 301 forEach([0, Infinity], function (num) { 302 t.ok(is(0, ES.ToUint8(num)), num + ' returns +0'); 303 t.ok(is(0, ES.ToUint8(-num)), '-' + num + ' returns +0'); 304 }); 305 t['throws'](function () { return ES.ToUint8(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws'); 306 t.ok(is(ES.ToUint8(0x100000000), 0), '2^32 returns +0'); 307 t.ok(is(ES.ToUint8(0x100000000 - 1), 0x100 - 1), '2^32 - 1 returns 2^8 - 1'); 308 t.ok(is(ES.ToUint8(0x80000000), 0), '2^31 returns +0'); 309 t.ok(is(ES.ToUint8(0x80000000 - 1), 0x100 - 1), '2^31 - 1 returns 2^8 - 1'); 310 t.ok(is(ES.ToUint8(0x10000), 0), '2^16 returns +0'); 311 t.ok(is(ES.ToUint8(0x10000 - 1), 0x100 - 1), '2^16 - 1 returns 2^8 - 1'); 312 t.ok(is(ES.ToUint8(0x100), 0), '2^8 returns +0'); 313 t.ok(is(ES.ToUint8(0x100 - 1), 0x100 - 1), '2^8 - 1 returns 2^16 - 1'); 314 t.ok(is(ES.ToUint8(0x10), 0x10), '2^4 returns 2^4'); 315 t.ok(is(ES.ToUint8(0x10 - 1), 0x10 - 1), '2^4 - 1 returns 2^4 - 1'); 316 t.end(); 317 }); 318 319 test('ToUint8Clamp', function (t) { 320 t.ok(is(0, ES.ToUint8Clamp(NaN)), 'NaN coerces to +0'); 321 t.ok(is(0, ES.ToUint8Clamp(0)), '+0 returns +0'); 322 t.ok(is(0, ES.ToUint8Clamp(-0)), '-0 returns +0'); 323 t.ok(is(0, ES.ToUint8Clamp(-Infinity)), '-Infinity returns +0'); 324 t['throws'](function () { return ES.ToUint8Clamp(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws'); 325 forEach([255, 256, 0x100000, Infinity], function (number) { 326 t.ok(is(255, ES.ToUint8Clamp(number)), number + ' coerces to 255'); 327 }); 328 t.equal(1, ES.ToUint8Clamp(1.49), '1.49 coerces to 1'); 329 t.equal(2, ES.ToUint8Clamp(1.5), '1.5 coerces to 2, because 2 is even'); 330 t.equal(2, ES.ToUint8Clamp(1.51), '1.51 coerces to 2'); 331 332 t.equal(2, ES.ToUint8Clamp(2.49), '2.49 coerces to 2'); 333 t.equal(2, ES.ToUint8Clamp(2.5), '2.5 coerces to 2, because 2 is even'); 334 t.equal(3, ES.ToUint8Clamp(2.51), '2.51 coerces to 3'); 335 t.end(); 336 }); 337 338 test('ToString', function (t) { 339 forEach(v.objects.concat(v.nonSymbolPrimitives), function (item) { 340 t.equal(ES.ToString(item), String(item), 'ES.ToString(' + debug(item) + ') ToStrings to String(' + debug(item) + ')'); 341 }); 342 343 t['throws'](function () { return ES.ToString(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws'); 344 345 forEach(v.symbols, function (symbol) { 346 t['throws'](function () { return ES.ToString(symbol); }, TypeError, debug(symbol) + ' throws'); 347 }); 348 t.end(); 349 }); 350 351 test('ToObject', function (t) { 352 t['throws'](function () { return ES.ToObject(undefined); }, TypeError, 'undefined throws'); 353 t['throws'](function () { return ES.ToObject(null); }, TypeError, 'null throws'); 354 forEach(v.numbers, function (number) { 355 var obj = ES.ToObject(number); 356 t.equal(typeof obj, 'object', 'number ' + number + ' coerces to object'); 357 t.equal(true, obj instanceof Number, 'object of ' + number + ' is Number object'); 358 t.ok(is(obj.valueOf(), number), 'object of ' + number + ' coerces to ' + number); 359 }); 360 t.end(); 361 }); 362 363 test('RequireObjectCoercible', function (t) { 364 t.equal(false, 'CheckObjectCoercible' in ES, 'CheckObjectCoercible -> RequireObjectCoercible in ES6'); 365 t['throws'](function () { return ES.RequireObjectCoercible(undefined); }, TypeError, 'undefined throws'); 366 t['throws'](function () { return ES.RequireObjectCoercible(null); }, TypeError, 'null throws'); 367 var isCoercible = function (value) { 368 t.doesNotThrow(function () { return ES.RequireObjectCoercible(value); }, debug(value) + ' does not throw'); 369 }; 370 forEach(v.objects.concat(v.nonNullPrimitives), isCoercible); 371 t.end(); 372 }); 373 374 test('IsCallable', function (t) { 375 t.equal(true, ES.IsCallable(function () {}), 'function is callable'); 376 var nonCallables = [/a/g, {}, Object.prototype, NaN].concat(v.primitives); 377 forEach(nonCallables, function (nonCallable) { 378 t.equal(false, ES.IsCallable(nonCallable), debug(nonCallable) + ' is not callable'); 379 }); 380 t.end(); 381 }); 382 383 test('SameValue', function (t) { 384 t.equal(true, ES.SameValue(NaN, NaN), 'NaN is SameValue as NaN'); 385 t.equal(false, ES.SameValue(0, -0), '+0 is not SameValue as -0'); 386 forEach(v.objects.concat(v.primitives), function (val) { 387 t.equal(val === val, ES.SameValue(val, val), debug(val) + ' is SameValue to itself'); 388 }); 389 t.end(); 390 }); 391 392 test('SameValueZero', function (t) { 393 t.equal(true, ES.SameValueZero(NaN, NaN), 'NaN is SameValueZero as NaN'); 394 t.equal(true, ES.SameValueZero(0, -0), '+0 is SameValueZero as -0'); 395 forEach(v.objects.concat(v.primitives), function (val) { 396 t.equal(val === val, ES.SameValueZero(val, val), debug(val) + ' is SameValueZero to itself'); 397 }); 398 t.end(); 399 }); 400 401 test('ToPropertyKey', function (t) { 402 forEach(v.objects.concat(v.nonSymbolPrimitives), function (value) { 403 t.equal(ES.ToPropertyKey(value), String(value), 'ToPropertyKey(value) === String(value) for non-Symbols'); 404 }); 405 406 forEach(v.symbols, function (symbol) { 407 t.equal( 408 ES.ToPropertyKey(symbol), 409 symbol, 410 'ToPropertyKey(' + debug(symbol) + ') === ' + debug(symbol) 411 ); 412 t.equal( 413 ES.ToPropertyKey(Object(symbol)), 414 symbol, 415 'ToPropertyKey(' + debug(Object(symbol)) + ') === ' + debug(symbol) 416 ); 417 }); 418 419 t.end(); 420 }); 421 422 test('ToLength', function (t) { 423 t['throws'](function () { return ES.ToLength(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws a TypeError'); 424 t.equal(3, ES.ToLength(v.coercibleObject), 'coercibleObject coerces to 3'); 425 t.equal(42, ES.ToLength('42.5'), '"42.5" coerces to 42'); 426 t.equal(7, ES.ToLength(7.3), '7.3 coerces to 7'); 427 forEach([-0, -1, -42, -Infinity], function (negative) { 428 t.ok(is(0, ES.ToLength(negative)), negative + ' coerces to +0'); 429 }); 430 t.equal(MAX_SAFE_INTEGER, ES.ToLength(MAX_SAFE_INTEGER + 1), '2^53 coerces to 2^53 - 1'); 431 t.equal(MAX_SAFE_INTEGER, ES.ToLength(MAX_SAFE_INTEGER + 3), '2^53 + 2 coerces to 2^53 - 1'); 432 t.end(); 433 }); 434 435 test('IsArray', function (t) { 436 t.equal(true, ES.IsArray([]), '[] is array'); 437 t.equal(false, ES.IsArray({}), '{} is not array'); 438 t.equal(false, ES.IsArray({ length: 1, 0: true }), 'arraylike object is not array'); 439 forEach(v.objects.concat(v.primitives), function (value) { 440 t.equal(false, ES.IsArray(value), debug(value) + ' is not array'); 441 }); 442 t.end(); 443 }); 444 445 test('IsRegExp', function (t) { 446 forEach([/a/g, new RegExp('a', 'g')], function (regex) { 447 t.equal(true, ES.IsRegExp(regex), regex + ' is regex'); 448 }); 449 450 forEach(v.objects.concat(v.primitives), function (nonRegex) { 451 t.equal(false, ES.IsRegExp(nonRegex), debug(nonRegex) + ' is not regex'); 452 }); 453 454 t.test('Symbol.match', { skip: !v.hasSymbols || !Symbol.match }, function (st) { 455 var obj = {}; 456 obj[Symbol.match] = true; 457 st.equal(true, ES.IsRegExp(obj), 'object with truthy Symbol.match is regex'); 458 459 var regex = /a/; 460 regex[Symbol.match] = false; 461 st.equal(false, ES.IsRegExp(regex), 'regex with falsy Symbol.match is not regex'); 462 463 st.end(); 464 }); 465 466 t.end(); 467 }); 468 469 test('IsPropertyKey', function (t) { 470 forEach(v.numbers.concat(v.objects), function (notKey) { 471 t.equal(false, ES.IsPropertyKey(notKey), debug(notKey) + ' is not property key'); 472 }); 473 474 t.equal(true, ES.IsPropertyKey('foo'), 'string is property key'); 475 476 forEach(v.symbols, function (symbol) { 477 t.equal(true, ES.IsPropertyKey(symbol), debug(symbol) + ' is property key'); 478 }); 479 t.end(); 480 }); 481 482 test('IsInteger', function (t) { 483 for (var i = -100; i < 100; i += 10) { 484 t.equal(true, ES.IsInteger(i), i + ' is integer'); 485 t.equal(false, ES.IsInteger(i + 0.2), (i + 0.2) + ' is not integer'); 486 } 487 t.equal(true, ES.IsInteger(-0), '-0 is integer'); 488 var notInts = v.nonNumbers.concat(v.nonIntegerNumbers, [Infinity, -Infinity, NaN, [], new Date()]); 489 forEach(notInts, function (notInt) { 490 t.equal(false, ES.IsInteger(notInt), debug(notInt) + ' is not integer'); 491 }); 492 t.equal(false, ES.IsInteger(v.uncoercibleObject), 'uncoercibleObject is not integer'); 493 t.end(); 494 }); 495 496 test('IsExtensible', function (t) { 497 forEach(v.objects, function (object) { 498 t.equal(true, ES.IsExtensible(object), debug(object) + ' object is extensible'); 499 }); 500 forEach(v.primitives, function (primitive) { 501 t.equal(false, ES.IsExtensible(primitive), debug(primitive) + ' is not extensible'); 502 }); 503 if (Object.preventExtensions) { 504 t.equal(false, ES.IsExtensible(Object.preventExtensions({})), 'object with extensions prevented is not extensible'); 505 } 506 t.end(); 507 }); 508 509 test('CanonicalNumericIndexString', function (t) { 510 var throwsOnNonString = function (notString) { 511 t['throws']( 512 function () { return ES.CanonicalNumericIndexString(notString); }, 513 TypeError, 514 debug(notString) + ' is not a string' 515 ); 516 }; 517 forEach(v.objects.concat(v.numbers), throwsOnNonString); 518 t.ok(is(-0, ES.CanonicalNumericIndexString('-0')), '"-0" returns -0'); 519 for (var i = -50; i < 50; i += 10) { 520 t.equal(i, ES.CanonicalNumericIndexString(String(i)), '"' + i + '" returns ' + i); 521 t.equal(undefined, ES.CanonicalNumericIndexString(String(i) + 'a'), '"' + i + 'a" returns undefined'); 522 } 523 t.end(); 524 }); 525 526 test('IsConstructor', function (t) { 527 t.equal(true, ES.IsConstructor(function () {}), 'function is constructor'); 528 t.equal(false, ES.IsConstructor(/a/g), 'regex is not constructor'); 529 forEach(v.objects, function (object) { 530 t.equal(false, ES.IsConstructor(object), object + ' object is not constructor'); 531 }); 532 533 try { 534 var foo = Function('return class Foo {}')(); // eslint-disable-line no-new-func 535 t.equal(ES.IsConstructor(foo), true, 'class is constructor'); 536 } catch (e) { 537 t.comment('SKIP: class syntax not supported.'); 538 } 539 t.end(); 540 }); 541 542 test('Call', function (t) { 543 var receiver = {}; 544 var notFuncs = v.objects.concat(v.primitives).concat([/a/g, new RegExp('a', 'g')]); 545 t.plan(notFuncs.length + 4); 546 var throwsIfNotCallable = function (notFunc) { 547 t['throws']( 548 function () { return ES.Call(notFunc, receiver); }, 549 TypeError, 550 debug(notFunc) + ' (' + typeof notFunc + ') is not callable' 551 ); 552 }; 553 forEach(notFuncs, throwsIfNotCallable); 554 ES.Call(function (a, b) { 555 t.equal(this, receiver, 'context matches expected'); 556 t.deepEqual([a, b], [1, 2], 'named args are correct'); 557 t.equal(arguments.length, 3, 'extra argument was passed'); 558 t.equal(arguments[2], 3, 'extra argument was correct'); 559 }, receiver, [1, 2, 3]); 560 t.end(); 561 }); 562 563 test('GetV', function (t) { 564 t['throws'](function () { return ES.GetV({ 7: 7 }, 7); }, TypeError, 'Throws a TypeError if `P` is not a property key'); 565 var obj = { a: function () {} }; 566 t.equal(ES.GetV(obj, 'a'), obj.a, 'returns property if it exists'); 567 t.equal(ES.GetV(obj, 'b'), undefined, 'returns undefiend if property does not exist'); 568 t.end(); 569 }); 570 571 test('GetMethod', function (t) { 572 t['throws'](function () { return ES.GetMethod({ 7: 7 }, 7); }, TypeError, 'Throws a TypeError if `P` is not a property key'); 573 t.equal(ES.GetMethod({}, 'a'), undefined, 'returns undefined in property is undefined'); 574 t.equal(ES.GetMethod({ a: null }, 'a'), undefined, 'returns undefined if property is null'); 575 t.equal(ES.GetMethod({ a: undefined }, 'a'), undefined, 'returns undefined if property is undefined'); 576 var obj = { a: function () {} }; 577 t['throws'](function () { ES.GetMethod({ a: 'b' }, 'a'); }, TypeError, 'throws TypeError if property exists and is not callable'); 578 t.equal(ES.GetMethod(obj, 'a'), obj.a, 'returns property if it is callable'); 579 t.end(); 580 }); 581 582 test('Get', function (t) { 583 t['throws'](function () { return ES.Get('a', 'a'); }, TypeError, 'Throws a TypeError if `O` is not an Object'); 584 t['throws'](function () { return ES.Get({ 7: 7 }, 7); }, TypeError, 'Throws a TypeError if `P` is not a property key'); 585 586 var value = {}; 587 t.test('Symbols', { skip: !v.hasSymbols }, function (st) { 588 var sym = Symbol('sym'); 589 var obj = {}; 590 obj[sym] = value; 591 st.equal(ES.Get(obj, sym), value, 'returns property `P` if it exists on object `O`'); 592 st.end(); 593 }); 594 t.equal(ES.Get({ a: value }, 'a'), value, 'returns property `P` if it exists on object `O`'); 595 t.end(); 596 }); 597 598 test('Type', { skip: !v.hasSymbols }, function (t) { 599 t.equal(ES.Type(Symbol.iterator), 'Symbol', 'Type(Symbol.iterator) is Symbol'); 600 t.end(); 601 }); 602 603 test('SpeciesConstructor', function (t) { 604 t['throws'](function () { ES.SpeciesConstructor(null); }, TypeError); 605 t['throws'](function () { ES.SpeciesConstructor(undefined); }, TypeError); 606 607 var defaultConstructor = function Foo() {}; 608 609 t.equal( 610 ES.SpeciesConstructor({ constructor: undefined }, defaultConstructor), 611 defaultConstructor, 612 'undefined constructor returns defaultConstructor' 613 ); 614 615 t['throws']( 616 function () { return ES.SpeciesConstructor({ constructor: null }, defaultConstructor); }, 617 TypeError, 618 'non-undefined non-object constructor throws' 619 ); 620 621 t.test('with Symbol.species', { skip: !hasSpecies }, function (st) { 622 var Bar = function Bar() {}; 623 Bar[Symbol.species] = null; 624 625 st.equal( 626 ES.SpeciesConstructor(new Bar(), defaultConstructor), 627 defaultConstructor, 628 'undefined/null Symbol.species returns default constructor' 629 ); 630 631 var Baz = function Baz() {}; 632 Baz[Symbol.species] = Bar; 633 st.equal( 634 ES.SpeciesConstructor(new Baz(), defaultConstructor), 635 Bar, 636 'returns Symbol.species constructor value' 637 ); 638 639 Baz[Symbol.species] = {}; 640 st['throws']( 641 function () { ES.SpeciesConstructor(new Baz(), defaultConstructor); }, 642 TypeError, 643 'throws when non-constructor non-null non-undefined species value found' 644 ); 645 646 st.end(); 647 }); 648 649 t.end(); 650 }); 651 652 var bothDescriptor = function () { 653 return { '[[Get]]': function () {}, '[[Value]]': true }; 654 }; 655 var accessorDescriptor = function () { 656 return { 657 '[[Get]]': function () {}, 658 '[[Enumerable]]': true, 659 '[[Configurable]]': true 660 }; 661 }; 662 var mutatorDescriptor = function () { 663 return { 664 '[[Set]]': function () {}, 665 '[[Enumerable]]': true, 666 '[[Configurable]]': true 667 }; 668 }; 669 var dataDescriptor = function () { 670 return { 671 '[[Value]]': 42, 672 '[[Writable]]': false 673 }; 674 }; 675 var genericDescriptor = function () { 676 return { 677 '[[Configurable]]': true, 678 '[[Enumerable]]': false 679 }; 680 }; 681 682 test('IsPropertyDescriptor', function (t) { 683 forEach(v.nonUndefinedPrimitives, function (primitive) { 684 t.equal( 685 ES.IsPropertyDescriptor(primitive), 686 false, 687 debug(primitive) + ' is not a Property Descriptor' 688 ); 689 }); 690 691 t.equal(ES.IsPropertyDescriptor({ invalid: true }), false, 'invalid keys not allowed on a Property Descriptor'); 692 693 t.equal(ES.IsPropertyDescriptor({}), true, 'empty object is an incomplete Property Descriptor'); 694 695 t.equal(ES.IsPropertyDescriptor(accessorDescriptor()), true, 'accessor descriptor is a Property Descriptor'); 696 t.equal(ES.IsPropertyDescriptor(mutatorDescriptor()), true, 'mutator descriptor is a Property Descriptor'); 697 t.equal(ES.IsPropertyDescriptor(dataDescriptor()), true, 'data descriptor is a Property Descriptor'); 698 t.equal(ES.IsPropertyDescriptor(genericDescriptor()), true, 'generic descriptor is a Property Descriptor'); 699 700 t['throws'](function () { 701 ES.IsPropertyDescriptor(bothDescriptor()); 702 }, TypeError, 'a Property Descriptor can not be both a Data and an Accessor Descriptor'); 703 704 t.end(); 705 }); 706 707 test('IsAccessorDescriptor', function (t) { 708 forEach(v.nonUndefinedPrimitives, function (primitive) { 709 t['throws']( 710 function () { ES.IsAccessorDescriptor(primitive); }, 711 TypeError, 712 debug(primitive) + ' is not a Property Descriptor' 713 ); 714 }); 715 716 t.equal(ES.IsAccessorDescriptor(), false, 'no value is not an Accessor Descriptor'); 717 t.equal(ES.IsAccessorDescriptor(undefined), false, 'undefined value is not an Accessor Descriptor'); 718 719 t.equal(ES.IsAccessorDescriptor(accessorDescriptor()), true, 'accessor descriptor is an Accessor Descriptor'); 720 t.equal(ES.IsAccessorDescriptor(mutatorDescriptor()), true, 'mutator descriptor is an Accessor Descriptor'); 721 t.equal(ES.IsAccessorDescriptor(dataDescriptor()), false, 'data descriptor is not an Accessor Descriptor'); 722 t.equal(ES.IsAccessorDescriptor(genericDescriptor()), false, 'generic descriptor is not an Accessor Descriptor'); 723 724 t.end(); 725 }); 726 727 test('IsDataDescriptor', function (t) { 728 forEach(v.nonUndefinedPrimitives, function (primitive) { 729 t['throws']( 730 function () { ES.IsDataDescriptor(primitive); }, 731 TypeError, 732 debug(primitive) + ' is not a Property Descriptor' 733 ); 734 }); 735 736 t.equal(ES.IsDataDescriptor(), false, 'no value is not a Data Descriptor'); 737 t.equal(ES.IsDataDescriptor(undefined), false, 'undefined value is not a Data Descriptor'); 738 739 t.equal(ES.IsDataDescriptor(accessorDescriptor()), false, 'accessor descriptor is not a Data Descriptor'); 740 t.equal(ES.IsDataDescriptor(mutatorDescriptor()), false, 'mutator descriptor is not a Data Descriptor'); 741 t.equal(ES.IsDataDescriptor(dataDescriptor()), true, 'data descriptor is a Data Descriptor'); 742 t.equal(ES.IsDataDescriptor(genericDescriptor()), false, 'generic descriptor is not a Data Descriptor'); 743 744 t.end(); 745 }); 746 747 test('IsGenericDescriptor', function (t) { 748 forEach(v.nonUndefinedPrimitives, function (primitive) { 749 t['throws']( 750 function () { ES.IsGenericDescriptor(primitive); }, 751 TypeError, 752 debug(primitive) + ' is not a Property Descriptor' 753 ); 754 }); 755 756 t.equal(ES.IsGenericDescriptor(), false, 'no value is not a Data Descriptor'); 757 t.equal(ES.IsGenericDescriptor(undefined), false, 'undefined value is not a Data Descriptor'); 758 759 t.equal(ES.IsGenericDescriptor(accessorDescriptor()), false, 'accessor descriptor is not a generic Descriptor'); 760 t.equal(ES.IsGenericDescriptor(mutatorDescriptor()), false, 'mutator descriptor is not a generic Descriptor'); 761 t.equal(ES.IsGenericDescriptor(dataDescriptor()), false, 'data descriptor is not a generic Descriptor'); 762 763 t.equal(ES.IsGenericDescriptor(genericDescriptor()), true, 'generic descriptor is a generic Descriptor'); 764 765 t.end(); 766 }); 767 768 test('FromPropertyDescriptor', function (t) { 769 t.equal(ES.FromPropertyDescriptor(), undefined, 'no value begets undefined'); 770 t.equal(ES.FromPropertyDescriptor(undefined), undefined, 'undefined value begets undefined'); 771 772 forEach(v.nonUndefinedPrimitives, function (primitive) { 773 t['throws']( 774 function () { ES.FromPropertyDescriptor(primitive); }, 775 TypeError, 776 debug(primitive) + ' is not a Property Descriptor' 777 ); 778 }); 779 780 var accessor = accessorDescriptor(); 781 t.deepEqual(ES.FromPropertyDescriptor(accessor), { 782 get: accessor['[[Get]]'], 783 set: accessor['[[Set]]'], 784 enumerable: !!accessor['[[Enumerable]]'], 785 configurable: !!accessor['[[Configurable]]'] 786 }); 787 788 var mutator = mutatorDescriptor(); 789 t.deepEqual(ES.FromPropertyDescriptor(mutator), { 790 get: mutator['[[Get]]'], 791 set: mutator['[[Set]]'], 792 enumerable: !!mutator['[[Enumerable]]'], 793 configurable: !!mutator['[[Configurable]]'] 794 }); 795 var data = dataDescriptor(); 796 t.deepEqual(ES.FromPropertyDescriptor(data), { 797 value: data['[[Value]]'], 798 writable: data['[[Writable]]'], 799 enumerable: !!data['[[Enumerable]]'], 800 configurable: !!data['[[Configurable]]'] 801 }); 802 803 t['throws']( 804 function () { ES.FromPropertyDescriptor(genericDescriptor()); }, 805 TypeError, 806 'a complete Property Descriptor is required' 807 ); 808 809 t.end(); 810 }); 811 812 test('ToPropertyDescriptor', function (t) { 813 forEach(v.nonUndefinedPrimitives, function (primitive) { 814 t['throws']( 815 function () { ES.ToPropertyDescriptor(primitive); }, 816 TypeError, 817 debug(primitive) + ' is not an Object' 818 ); 819 }); 820 821 var accessor = accessorDescriptor(); 822 t.deepEqual(ES.ToPropertyDescriptor({ 823 get: accessor['[[Get]]'], 824 enumerable: !!accessor['[[Enumerable]]'], 825 configurable: !!accessor['[[Configurable]]'] 826 }), accessor); 827 828 var mutator = mutatorDescriptor(); 829 t.deepEqual(ES.ToPropertyDescriptor({ 830 set: mutator['[[Set]]'], 831 enumerable: !!mutator['[[Enumerable]]'], 832 configurable: !!mutator['[[Configurable]]'] 833 }), mutator); 834 835 var data = dataDescriptor(); 836 t.deepEqual(ES.ToPropertyDescriptor({ 837 value: data['[[Value]]'], 838 writable: data['[[Writable]]'], 839 configurable: !!data['[[Configurable]]'] 840 }), assign(data, { '[[Configurable]]': false })); 841 842 var both = bothDescriptor(); 843 t['throws']( 844 function () { 845 ES.FromPropertyDescriptor({ get: both['[[Get]]'], value: both['[[Value]]'] }); 846 }, 847 TypeError, 848 'data and accessor descriptors are mutually exclusive' 849 ); 850 851 t.end(); 852 }); 853 854 test('CompletePropertyDescriptor', function (t) { 855 forEach(v.nonUndefinedPrimitives, function (primitive) { 856 t['throws']( 857 function () { ES.CompletePropertyDescriptor(primitive); }, 858 TypeError, 859 debug(primitive) + ' is not a Property Descriptor' 860 ); 861 }); 862 863 var generic = genericDescriptor(); 864 t.deepEqual(ES.CompletePropertyDescriptor(generic), { 865 '[[Configurable]]': !!generic['[[Configurable]]'], 866 '[[Enumerable]]': !!generic['[[Enumerable]]'], 867 '[[Value]]': undefined, 868 '[[Writable]]': false 869 }, 'completes a Generic Descriptor'); 870 871 var data = dataDescriptor(); 872 t.deepEqual(ES.CompletePropertyDescriptor(data), { 873 '[[Configurable]]': !!data['[[Configurable]]'], 874 '[[Enumerable]]': false, 875 '[[Value]]': data['[[Value]]'], 876 '[[Writable]]': !!data['[[Writable]]'] 877 }, 'completes a Data Descriptor'); 878 879 var accessor = accessorDescriptor(); 880 t.deepEqual(ES.CompletePropertyDescriptor(accessor), { 881 '[[Get]]': accessor['[[Get]]'], 882 '[[Enumerable]]': !!accessor['[[Enumerable]]'], 883 '[[Configurable]]': !!accessor['[[Configurable]]'], 884 '[[Set]]': undefined 885 }, 'completes an Accessor Descriptor'); 886 887 var mutator = mutatorDescriptor(); 888 t.deepEqual(ES.CompletePropertyDescriptor(mutator), { 889 '[[Set]]': mutator['[[Set]]'], 890 '[[Enumerable]]': !!mutator['[[Enumerable]]'], 891 '[[Configurable]]': !!mutator['[[Configurable]]'], 892 '[[Get]]': undefined 893 }, 'completes a mutator Descriptor'); 894 895 t['throws']( 896 function () { ES.CompletePropertyDescriptor(bothDescriptor()); }, 897 TypeError, 898 'data and accessor descriptors are mutually exclusive' 899 ); 900 901 t.end(); 902 }); 903 904 test('Set', function (t) { 905 forEach(v.primitives, function (primitive) { 906 t['throws']( 907 function () { ES.Set(primitive, '', null, false); }, 908 TypeError, 909 debug(primitive) + ' is not an Object' 910 ); 911 }); 912 913 forEach(v.nonPropertyKeys, function (nonKey) { 914 t['throws']( 915 function () { ES.Set({}, nonKey, null, false); }, 916 TypeError, 917 debug(nonKey) + ' is not a Property Key' 918 ); 919 }); 920 921 forEach(v.nonBooleans, function (nonBoolean) { 922 t['throws']( 923 function () { ES.Set({}, '', null, nonBoolean); }, 924 TypeError, 925 debug(nonBoolean) + ' is not a Boolean' 926 ); 927 }); 928 929 var o = {}; 930 var value = {}; 931 ES.Set(o, 'key', value, true); 932 t.deepEqual(o, { key: value }, 'key is set'); 933 934 t.test('nonwritable', { skip: !Object.defineProperty }, function (st) { 935 var obj = { a: value }; 936 Object.defineProperty(obj, 'a', { writable: false }); 937 938 st['throws']( 939 function () { ES.Set(obj, 'a', value, true); }, 940 TypeError, 941 'can not Set nonwritable property' 942 ); 943 944 st.doesNotThrow( 945 function () { ES.Set(obj, 'a', value, false); }, 946 'setting Throw to false prevents an exception' 947 ); 948 949 st.end(); 950 }); 951 952 t.test('nonconfigurable', { skip: !Object.defineProperty }, function (st) { 953 var obj = { a: value }; 954 Object.defineProperty(obj, 'a', { configurable: false }); 955 956 ES.Set(obj, 'a', value, true); 957 st.deepEqual(obj, { a: value }, 'key is set'); 958 959 st.end(); 960 }); 961 962 t.end(); 963 }); 964 965 test('HasOwnProperty', function (t) { 966 forEach(v.primitives, function (primitive) { 967 t['throws']( 968 function () { ES.HasOwnProperty(primitive, 'key'); }, 969 TypeError, 970 debug(primitive) + ' is not an Object' 971 ); 972 }); 973 974 forEach(v.nonPropertyKeys, function (nonKey) { 975 t['throws']( 976 function () { ES.HasOwnProperty({}, nonKey); }, 977 TypeError, 978 debug(nonKey) + ' is not a Property Key' 979 ); 980 }); 981 982 t.equal(ES.HasOwnProperty({}, 'toString'), false, 'inherited properties are not own'); 983 t.equal( 984 ES.HasOwnProperty({ toString: 1 }, 'toString'), 985 true, 986 'shadowed inherited own properties are own' 987 ); 988 t.equal(ES.HasOwnProperty({ a: 1 }, 'a'), true, 'own properties are own'); 989 990 t.end(); 991 }); 992 993 test('HasProperty', function (t) { 994 forEach(v.primitives, function (primitive) { 995 t['throws']( 996 function () { ES.HasProperty(primitive, 'key'); }, 997 TypeError, 998 debug(primitive) + ' is not an Object' 999 ); 1000 }); 1001 1002 forEach(v.nonPropertyKeys, function (nonKey) { 1003 t['throws']( 1004 function () { ES.HasProperty({}, nonKey); }, 1005 TypeError, 1006 debug(nonKey) + ' is not a Property Key' 1007 ); 1008 }); 1009 1010 t.equal(ES.HasProperty({}, 'nope'), false, 'object does not have nonexistent properties'); 1011 t.equal(ES.HasProperty({}, 'toString'), true, 'object has inherited properties'); 1012 t.equal( 1013 ES.HasProperty({ toString: 1 }, 'toString'), 1014 true, 1015 'object has shadowed inherited own properties' 1016 ); 1017 t.equal(ES.HasProperty({ a: 1 }, 'a'), true, 'object has own properties'); 1018 1019 t.end(); 1020 }); 1021 1022 test('IsConcatSpreadable', function (t) { 1023 forEach(v.primitives, function (primitive) { 1024 t.equal(ES.IsConcatSpreadable(primitive), false, debug(primitive) + ' is not an Object'); 1025 }); 1026 1027 var hasSymbolConcatSpreadable = v.hasSymbols && Symbol.isConcatSpreadable; 1028 t.test('Symbol.isConcatSpreadable', { skip: !hasSymbolConcatSpreadable }, function (st) { 1029 forEach(v.falsies, function (falsy) { 1030 var obj = {}; 1031 obj[Symbol.isConcatSpreadable] = falsy; 1032 st.equal( 1033 ES.IsConcatSpreadable(obj), 1034 false, 1035 'an object with ' + debug(falsy) + ' as Symbol.isConcatSpreadable is not concat spreadable' 1036 ); 1037 }); 1038 1039 forEach(v.truthies, function (truthy) { 1040 var obj = {}; 1041 obj[Symbol.isConcatSpreadable] = truthy; 1042 st.equal( 1043 ES.IsConcatSpreadable(obj), 1044 true, 1045 'an object with ' + debug(truthy) + ' as Symbol.isConcatSpreadable is concat spreadable' 1046 ); 1047 }); 1048 1049 st.end(); 1050 }); 1051 1052 forEach(v.objects, function (object) { 1053 t.equal( 1054 ES.IsConcatSpreadable(object), 1055 false, 1056 'non-array without Symbol.isConcatSpreadable is not concat spreadable' 1057 ); 1058 }); 1059 1060 t.equal(ES.IsConcatSpreadable([]), true, 'arrays are concat spreadable'); 1061 1062 t.end(); 1063 }); 1064 1065 test('Invoke', function (t) { 1066 forEach(v.nonPropertyKeys, function (nonKey) { 1067 t['throws']( 1068 function () { ES.Invoke({}, nonKey); }, 1069 TypeError, 1070 debug(nonKey) + ' is not a Property Key' 1071 ); 1072 }); 1073 1074 t['throws'](function () { ES.Invoke({ o: false }, 'o'); }, TypeError, 'fails on a non-function'); 1075 1076 t.test('invoked callback', function (st) { 1077 var aValue = {}; 1078 var bValue = {}; 1079 var obj = { 1080 f: function (a) { 1081 st.equal(arguments.length, 2, '2 args passed'); 1082 st.equal(a, aValue, 'first arg is correct'); 1083 st.equal(arguments[1], bValue, 'second arg is correct'); 1084 } 1085 }; 1086 st.plan(3); 1087 ES.Invoke(obj, 'f', aValue, bValue); 1088 }); 1089 1090 t.end(); 1091 }); 1092 1093 test('GetIterator', { skip: true }); 1094 1095 test('IteratorNext', { skip: true }); 1096 1097 test('IteratorComplete', { skip: true }); 1098 1099 test('IteratorValue', { skip: true }); 1100 1101 test('IteratorStep', { skip: true }); 1102 1103 test('IteratorClose', { skip: true }); 1104 1105 test('CreateIterResultObject', function (t) { 1106 forEach(v.nonBooleans, function (nonBoolean) { 1107 t['throws']( 1108 function () { ES.CreateIterResultObject({}, nonBoolean); }, 1109 TypeError, 1110 '"done" argument must be a boolean; ' + debug(nonBoolean) + ' is not' 1111 ); 1112 }); 1113 1114 var value = {}; 1115 t.deepEqual(ES.CreateIterResultObject(value, true), { 1116 value: value, 1117 done: true 1118 }, 'creates a "done" iteration result'); 1119 t.deepEqual(ES.CreateIterResultObject(value, false), { 1120 value: value, 1121 done: false 1122 }, 'creates a "not done" iteration result'); 1123 1124 t.end(); 1125 }); 1126 1127 test('RegExpExec', function (t) { 1128 forEach(v.primitives, function (primitive) { 1129 t['throws']( 1130 function () { ES.RegExpExec(primitive); }, 1131 TypeError, 1132 '"R" argument must be an object; ' + debug(primitive) + ' is not' 1133 ); 1134 }); 1135 1136 forEach(v.nonStrings, function (nonString) { 1137 t['throws']( 1138 function () { ES.RegExpExec({}, nonString); }, 1139 TypeError, 1140 '"S" argument must be a String; ' + debug(nonString) + ' is not' 1141 ); 1142 }); 1143 1144 t.test('gets and calls a callable "exec"', function (st) { 1145 var str = '123'; 1146 var o = { 1147 exec: function (S) { 1148 st.equal(this, o, '"exec" receiver is R'); 1149 st.equal(S, str, '"exec" argument is S'); 1150 1151 return null; 1152 } 1153 }; 1154 st.plan(2); 1155 ES.RegExpExec(o, str); 1156 st.end(); 1157 }); 1158 1159 t.test('throws if a callable "exec" returns a non-null non-object', function (st) { 1160 var str = '123'; 1161 st.plan(v.nonNullPrimitives.length); 1162 forEach(v.nonNullPrimitives, function (nonNullPrimitive) { 1163 st['throws']( 1164 function () { ES.RegExpExec({ exec: function () { return nonNullPrimitive; } }, str); }, 1165 TypeError, 1166 '"exec" method must return `null` or an Object; ' + debug(nonNullPrimitive) + ' is not' 1167 ); 1168 }); 1169 st.end(); 1170 }); 1171 1172 t.test('actual regex that should match against a string', function (st) { 1173 var S = 'aabc'; 1174 var R = /a/g; 1175 var match1 = ES.RegExpExec(R, S); 1176 var match2 = ES.RegExpExec(R, S); 1177 var match3 = ES.RegExpExec(R, S); 1178 st.deepEqual(match1, assign(['a'], groups({ index: 0, input: S })), 'match object 1 is as expected'); 1179 st.deepEqual(match2, assign(['a'], groups({ index: 1, input: S })), 'match object 2 is as expected'); 1180 st.equal(match3, null, 'match 3 is null as expected'); 1181 st.end(); 1182 }); 1183 1184 t.test('actual regex that should match against a string, with shadowed "exec"', function (st) { 1185 var S = 'aabc'; 1186 var R = /a/g; 1187 R.exec = undefined; 1188 var match1 = ES.RegExpExec(R, S); 1189 var match2 = ES.RegExpExec(R, S); 1190 var match3 = ES.RegExpExec(R, S); 1191 st.deepEqual(match1, assign(['a'], groups({ index: 0, input: S })), 'match object 1 is as expected'); 1192 st.deepEqual(match2, assign(['a'], groups({ index: 1, input: S })), 'match object 2 is as expected'); 1193 st.equal(match3, null, 'match 3 is null as expected'); 1194 st.end(); 1195 }); 1196 t.end(); 1197 }); 1198 1199 test('ArraySpeciesCreate', function (t) { 1200 t.test('errors', function (st) { 1201 var testNonNumber = function (nonNumber) { 1202 st['throws']( 1203 function () { ES.ArraySpeciesCreate([], nonNumber); }, 1204 TypeError, 1205 debug(nonNumber) + ' is not a number' 1206 ); 1207 }; 1208 forEach(v.nonNumbers, testNonNumber); 1209 1210 st['throws']( 1211 function () { ES.ArraySpeciesCreate([], -1); }, 1212 TypeError, 1213 '-1 is not >= 0' 1214 ); 1215 st['throws']( 1216 function () { ES.ArraySpeciesCreate([], -Infinity); }, 1217 TypeError, 1218 '-Infinity is not >= 0' 1219 ); 1220 1221 var testNonIntegers = function (nonInteger) { 1222 st['throws']( 1223 function () { ES.ArraySpeciesCreate([], nonInteger); }, 1224 TypeError, 1225 debug(nonInteger) + ' is not an integer' 1226 ); 1227 }; 1228 forEach(v.nonIntegerNumbers, testNonIntegers); 1229 1230 st.end(); 1231 }); 1232 1233 t.test('works with a non-array', function (st) { 1234 forEach(v.objects.concat(v.primitives), function (nonArray) { 1235 var arr = ES.ArraySpeciesCreate(nonArray, 0); 1236 st.ok(ES.IsArray(arr), 'is an array'); 1237 st.equal(arr.length, 0, 'length is correct'); 1238 st.equal(arr.constructor, Array, 'constructor is correct'); 1239 }); 1240 1241 st.end(); 1242 }); 1243 1244 t.test('works with a normal array', function (st) { 1245 var len = 2; 1246 var orig = [1, 2, 3]; 1247 var arr = ES.ArraySpeciesCreate(orig, len); 1248 1249 st.ok(ES.IsArray(arr), 'is an array'); 1250 st.equal(arr.length, len, 'length is correct'); 1251 st.equal(arr.constructor, orig.constructor, 'constructor is correct'); 1252 1253 st.end(); 1254 }); 1255 1256 t.test('-0 length produces +0 length', function (st) { 1257 var len = -0; 1258 st.ok(is(len, -0), '-0 is negative zero'); 1259 st.notOk(is(len, 0), '-0 is not positive zero'); 1260 1261 var orig = [1, 2, 3]; 1262 var arr = ES.ArraySpeciesCreate(orig, len); 1263 1264 st.equal(ES.IsArray(arr), true); 1265 st.ok(is(arr.length, 0)); 1266 st.equal(arr.constructor, orig.constructor); 1267 1268 st.end(); 1269 }); 1270 1271 t.test('works with species construtor', { skip: !hasSpecies }, function (st) { 1272 var sentinel = {}; 1273 var Foo = function Foo(len) { 1274 this.length = len; 1275 this.sentinel = sentinel; 1276 }; 1277 var Bar = getArraySubclassWithSpeciesConstructor(Foo); 1278 var bar = new Bar(); 1279 1280 t.equal(ES.IsArray(bar), true, 'Bar instance is an array'); 1281 1282 var arr = ES.ArraySpeciesCreate(bar, 3); 1283 st.equal(arr.constructor, Foo, 'result used species constructor'); 1284 st.equal(arr.length, 3, 'length property is correct'); 1285 st.equal(arr.sentinel, sentinel, 'Foo constructor was exercised'); 1286 1287 st.end(); 1288 }); 1289 1290 t.test('works with null species constructor', { skip: !hasSpecies }, function (st) { 1291 var Bar = getArraySubclassWithSpeciesConstructor(null); 1292 var bar = new Bar(); 1293 1294 t.equal(ES.IsArray(bar), true, 'Bar instance is an array'); 1295 1296 var arr = ES.ArraySpeciesCreate(bar, 3); 1297 st.equal(arr.constructor, Array, 'result used default constructor'); 1298 st.equal(arr.length, 3, 'length property is correct'); 1299 1300 st.end(); 1301 }); 1302 1303 t.test('works with undefined species constructor', { skip: !hasSpecies }, function (st) { 1304 var Bar = getArraySubclassWithSpeciesConstructor(); 1305 var bar = new Bar(); 1306 1307 t.equal(ES.IsArray(bar), true, 'Bar instance is an array'); 1308 1309 var arr = ES.ArraySpeciesCreate(bar, 3); 1310 st.equal(arr.constructor, Array, 'result used default constructor'); 1311 st.equal(arr.length, 3, 'length property is correct'); 1312 1313 st.end(); 1314 }); 1315 1316 t.test('throws with object non-construtor species constructor', { skip: !hasSpecies }, function (st) { 1317 forEach(v.objects, function (obj) { 1318 var Bar = getArraySubclassWithSpeciesConstructor(obj); 1319 var bar = new Bar(); 1320 1321 st.equal(ES.IsArray(bar), true, 'Bar instance is an array'); 1322 1323 st['throws']( 1324 function () { ES.ArraySpeciesCreate(bar, 3); }, 1325 TypeError, 1326 debug(obj) + ' is not a constructor' 1327 ); 1328 }); 1329 1330 st.end(); 1331 }); 1332 1333 t.end(); 1334 }); 1335 1336 test('CreateDataProperty', function (t) { 1337 forEach(v.primitives, function (primitive) { 1338 t['throws']( 1339 function () { ES.CreateDataProperty(primitive); }, 1340 TypeError, 1341 debug(primitive) + ' is not an object' 1342 ); 1343 }); 1344 1345 forEach(v.nonPropertyKeys, function (nonPropertyKey) { 1346 t['throws']( 1347 function () { ES.CreateDataProperty({}, nonPropertyKey); }, 1348 TypeError, 1349 debug(nonPropertyKey) + ' is not a property key' 1350 ); 1351 }); 1352 1353 var sentinel = {}; 1354 forEach(v.propertyKeys, function (propertyKey) { 1355 var obj = {}; 1356 var status = ES.CreateDataProperty(obj, propertyKey, sentinel); 1357 t.equal(status, true, 'status is true'); 1358 t.equal( 1359 obj[propertyKey], 1360 sentinel, 1361 debug(sentinel) + ' is installed on "' + debug(propertyKey) + '" on the object' 1362 ); 1363 1364 if (typeof Object.defineProperty === 'function') { 1365 var nonWritable = Object.defineProperty({}, propertyKey, { configurable: true, writable: false }); 1366 1367 var nonWritableStatus = ES.CreateDataProperty(nonWritable, propertyKey, sentinel); 1368 t.equal(nonWritableStatus, false, 'create data property failed'); 1369 t.notEqual( 1370 nonWritable[propertyKey], 1371 sentinel, 1372 debug(sentinel) + ' is not installed on "' + debug(propertyKey) + '" on the object when key is nonwritable' 1373 ); 1374 1375 var nonConfigurable = Object.defineProperty({}, propertyKey, { configurable: false, writable: true }); 1376 1377 var nonConfigurableStatus = ES.CreateDataProperty(nonConfigurable, propertyKey, sentinel); 1378 t.equal(nonConfigurableStatus, false, 'create data property failed'); 1379 t.notEqual( 1380 nonConfigurable[propertyKey], 1381 sentinel, 1382 debug(sentinel) + ' is not installed on "' + debug(propertyKey) + '" on the object when key is nonconfigurable' 1383 ); 1384 } 1385 }); 1386 1387 t.end(); 1388 }); 1389 1390 test('CreateDataPropertyOrThrow', function (t) { 1391 forEach(v.primitives, function (primitive) { 1392 t['throws']( 1393 function () { ES.CreateDataPropertyOrThrow(primitive); }, 1394 TypeError, 1395 debug(primitive) + ' is not an object' 1396 ); 1397 }); 1398 1399 forEach(v.nonPropertyKeys, function (nonPropertyKey) { 1400 t['throws']( 1401 function () { ES.CreateDataPropertyOrThrow({}, nonPropertyKey); }, 1402 TypeError, 1403 debug(nonPropertyKey) + ' is not a property key' 1404 ); 1405 }); 1406 1407 var sentinel = {}; 1408 forEach(v.propertyKeys, function (propertyKey) { 1409 var obj = {}; 1410 var status = ES.CreateDataPropertyOrThrow(obj, propertyKey, sentinel); 1411 t.equal(status, true, 'status is true'); 1412 t.equal( 1413 obj[propertyKey], 1414 sentinel, 1415 debug(sentinel) + ' is installed on "' + debug(propertyKey) + '" on the object' 1416 ); 1417 1418 if (typeof Object.preventExtensions === 'function') { 1419 var notExtensible = {}; 1420 Object.preventExtensions(notExtensible); 1421 1422 t['throws']( 1423 function () { ES.CreateDataPropertyOrThrow(notExtensible, propertyKey, sentinel); }, 1424 TypeError, 1425 'can not install ' + debug(propertyKey) + ' on non-extensible object' 1426 ); 1427 t.notEqual( 1428 notExtensible[propertyKey], 1429 sentinel, 1430 debug(sentinel) + ' is not installed on "' + debug(propertyKey) + '" on the object' 1431 ); 1432 } 1433 }); 1434 1435 t.end(); 1436 }); 1437 1438 test('ObjectCreate', function (t) { 1439 forEach(v.nonNullPrimitives, function (value) { 1440 t['throws']( 1441 function () { ES.ObjectCreate(value); }, 1442 TypeError, 1443 debug(value) + ' is not null, or an object' 1444 ); 1445 }); 1446 1447 t.test('proto arg', function (st) { 1448 var Parent = function Parent() {}; 1449 Parent.prototype.foo = {}; 1450 var child = ES.ObjectCreate(Parent.prototype); 1451 st.equal(child instanceof Parent, true, 'child is instanceof Parent'); 1452 st.equal(child.foo, Parent.prototype.foo, 'child inherits properties from Parent.prototype'); 1453 1454 st.end(); 1455 }); 1456 1457 t.test('internal slots arg', function (st) { 1458 st.doesNotThrow(function () { ES.ObjectCreate(null, []); }, 'an empty slot list is valid'); 1459 1460 st['throws']( 1461 function () { ES.ObjectCreate(null, ['a']); }, 1462 SyntaxError, 1463 'internal slots are not supported' 1464 ); 1465 1466 st.end(); 1467 }); 1468 1469 t.test('null proto', { skip: !Object.create }, function (st) { 1470 st.equal('toString' in ({}), true, 'normal objects have toString'); 1471 st.equal('toString' in ES.ObjectCreate(null), false, 'makes a null object'); 1472 1473 st.end(); 1474 }); 1475 1476 t.test('null proto when no native Object.create', { skip: Object.create }, function (st) { 1477 st['throws']( 1478 function () { ES.ObjectCreate(null); }, 1479 SyntaxError, 1480 'without a native Object.create, can not create null objects' 1481 ); 1482 1483 st.end(); 1484 }); 1485 1486 t.end(); 1487 }); 1488 1489 test('AdvanceStringIndex', function (t) { 1490 forEach(v.nonStrings, function (nonString) { 1491 t['throws']( 1492 function () { ES.AdvanceStringIndex(nonString); }, 1493 TypeError, 1494 '"S" argument must be a String; ' + debug(nonString) + ' is not' 1495 ); 1496 }); 1497 1498 var notInts = v.nonNumbers.concat( 1499 v.nonIntegerNumbers, 1500 [Infinity, -Infinity, NaN, [], new Date(), Math.pow(2, 53), -1] 1501 ); 1502 forEach(notInts, function (nonInt) { 1503 t['throws']( 1504 function () { ES.AdvanceStringIndex('abc', nonInt); }, 1505 TypeError, 1506 '"index" argument must be an integer, ' + debug(nonInt) + ' is not.' 1507 ); 1508 }); 1509 1510 forEach(v.nonBooleans, function (nonBoolean) { 1511 t['throws']( 1512 function () { ES.AdvanceStringIndex('abc', 0, nonBoolean); }, 1513 TypeError, 1514 debug(nonBoolean) + ' is not a Boolean' 1515 ); 1516 }); 1517 1518 var str = 'a\uD83D\uDCA9c'; 1519 1520 t.test('non-unicode mode', function (st) { 1521 for (var i = 0; i < str.length + 2; i += 1) { 1522 st.equal(ES.AdvanceStringIndex(str, i, false), i + 1, i + ' advances to ' + (i + 1)); 1523 } 1524 1525 st.end(); 1526 }); 1527 1528 t.test('unicode mode', function (st) { 1529 st.equal(ES.AdvanceStringIndex(str, 0, true), 1, '0 advances to 1'); 1530 st.equal(ES.AdvanceStringIndex(str, 1, true), 3, '1 advances to 3'); 1531 st.equal(ES.AdvanceStringIndex(str, 2, true), 3, '2 advances to 3'); 1532 st.equal(ES.AdvanceStringIndex(str, 3, true), 4, '3 advances to 4'); 1533 st.equal(ES.AdvanceStringIndex(str, 4, true), 5, '4 advances to 5'); 1534 1535 st.end(); 1536 }); 1537 1538 t.test('lone surrogates', function (st) { 1539 var halfPoo = 'a\uD83Dc'; 1540 1541 st.equal(ES.AdvanceStringIndex(halfPoo, 0, true), 1, '0 advances to 1'); 1542 st.equal(ES.AdvanceStringIndex(halfPoo, 1, true), 2, '1 advances to 2'); 1543 st.equal(ES.AdvanceStringIndex(halfPoo, 2, true), 3, '2 advances to 3'); 1544 st.equal(ES.AdvanceStringIndex(halfPoo, 3, true), 4, '3 advances to 4'); 1545 1546 st.end(); 1547 }); 1548 1549 t.test('surrogate pairs', function (st) { 1550 var lowestPair = String.fromCharCode('0xD800') + String.fromCharCode('0xDC00'); 1551 var highestPair = String.fromCharCode('0xDBFF') + String.fromCharCode('0xDFFF'); 1552 var poop = String.fromCharCode('0xD83D') + String.fromCharCode('0xDCA9'); 1553 1554 st.equal(ES.AdvanceStringIndex(lowestPair, 0, true), 2, 'lowest surrogate pair, 0 -> 2'); 1555 st.equal(ES.AdvanceStringIndex(highestPair, 0, true), 2, 'highest surrogate pair, 0 -> 2'); 1556 st.equal(ES.AdvanceStringIndex(poop, 0, true), 2, 'poop, 0 -> 2'); 1557 1558 st.end(); 1559 }); 1560 1561 t.end(); 1562 }); 1563}; 1564 1565var es2016 = function ES2016(ES, ops, expectedMissing) { 1566 es2015(ES, ops, expectedMissing); 1567 1568 test('SameValueNonNumber', function (t) { 1569 var willThrow = [ 1570 [3, 4], 1571 [NaN, 4], 1572 [4, ''], 1573 ['abc', true], 1574 [{}, false] 1575 ]; 1576 forEach(willThrow, function (nums) { 1577 t['throws'](function () { return ES.SameValueNonNumber.apply(ES, nums); }, TypeError, 'value must be same type and non-number'); 1578 }); 1579 1580 forEach(v.objects.concat(v.nonNumberPrimitives), function (val) { 1581 t.equal(val === val, ES.SameValueNonNumber(val, val), debug(val) + ' is SameValueNonNumber to itself'); 1582 }); 1583 1584 t.end(); 1585 }); 1586}; 1587 1588var es2017 = function E2017(ES, ops, expectedMissing) { 1589 es2016(ES, ops, expectedMissing); 1590 1591 test('ToIndex', function (t) { 1592 t.ok(is(ES.ToIndex(), 0), 'no value gives 0'); 1593 t.ok(is(ES.ToIndex(undefined), 0), 'undefined value gives 0'); 1594 1595 t['throws'](function () { ES.ToIndex(-1); }, RangeError, 'negative numbers throw'); 1596 1597 t['throws'](function () { ES.ToIndex(MAX_SAFE_INTEGER + 1); }, RangeError, 'too large numbers throw'); 1598 1599 t.equal(ES.ToIndex(3), 3, 'numbers work'); 1600 t.equal(ES.ToIndex(v.valueOfOnlyObject), 4, 'coercible objects are coerced'); 1601 1602 t.end(); 1603 }); 1604}; 1605 1606module.exports = { 1607 es2015: es2015, 1608 es2016: es2016, 1609 es2017: es2017 1610}; 1611