You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4074 lines
129 KiB

4 years ago
  1. 'use strict';
  2. var test = require('tape');
  3. var forEach = require('foreach');
  4. var is = require('object-is');
  5. var debug = require('object-inspect');
  6. var assign = require('object.assign');
  7. var keys = require('object-keys');
  8. var has = require('has');
  9. var arrowFns = require('make-arrow-function').list();
  10. var getInferredName = require('../helpers/getInferredName');
  11. var assertRecordTests = require('./helpers/assertRecord');
  12. var v = require('./helpers/values');
  13. var diffOps = require('./diffOps');
  14. var MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER || Math.pow(2, 53) - 1;
  15. var 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. return Bar;
  26. };
  27. var testIterator = function (t, iterator, expected) {
  28. var resultCount = 0;
  29. var result;
  30. while (result = iterator.next(), !result.done) { // eslint-disable-line no-sequences
  31. t.deepEqual(result, { done: false, value: expected[resultCount] }, 'result ' + resultCount);
  32. resultCount += 1;
  33. }
  34. t.equal(resultCount, expected.length, 'expected ' + expected.length + ', got ' + resultCount);
  35. };
  36. var hasSpecies = v.hasSymbols && Symbol.species;
  37. var hasGroups = 'groups' in (/a/).exec('a');
  38. var groups = function groups(matchObject) {
  39. return hasGroups ? assign(matchObject, { groups: matchObject.groups }) : matchObject;
  40. };
  41. var testEnumerableOwnNames = function (t, enumerableOwnNames) {
  42. forEach(v.primitives, function (nonObject) {
  43. t['throws'](
  44. function () { enumerableOwnNames(nonObject); },
  45. debug(nonObject) + ' is not an Object'
  46. );
  47. });
  48. var Child = function Child() {
  49. this.own = {};
  50. };
  51. Child.prototype = {
  52. inherited: {}
  53. };
  54. var obj = new Child();
  55. t.equal('own' in obj, true, 'has "own"');
  56. t.equal(has(obj, 'own'), true, 'has own "own"');
  57. t.equal(Object.prototype.propertyIsEnumerable.call(obj, 'own'), true, 'has enumerable "own"');
  58. t.equal('inherited' in obj, true, 'has "inherited"');
  59. t.equal(has(obj, 'inherited'), false, 'has non-own "inherited"');
  60. t.equal(has(Child.prototype, 'inherited'), true, 'Child.prototype has own "inherited"');
  61. t.equal(Child.prototype.inherited, obj.inherited, 'Child.prototype.inherited === obj.inherited');
  62. t.equal(Object.prototype.propertyIsEnumerable.call(Child.prototype, 'inherited'), true, 'has enumerable "inherited"');
  63. t.equal('toString' in obj, true, 'has "toString"');
  64. t.equal(has(obj, 'toString'), false, 'has non-own "toString"');
  65. t.equal(has(Object.prototype, 'toString'), true, 'Object.prototype has own "toString"');
  66. t.equal(Object.prototype.toString, obj.toString, 'Object.prototype.toString === obj.toString');
  67. // eslint-disable-next-line no-useless-call
  68. t.equal(Object.prototype.propertyIsEnumerable.call(Object.prototype, 'toString'), false, 'has non-enumerable "toString"');
  69. return obj;
  70. };
  71. var es2015 = function ES2015(ES, ops, expectedMissing, skips) {
  72. test('has expected operations', function (t) {
  73. var diff = diffOps(ES, ops, expectedMissing);
  74. t.deepEqual(diff.extra, [], 'no extra ops');
  75. t.deepEqual(diff.missing, [], 'no unexpected missing ops');
  76. t.end();
  77. });
  78. test('ToPrimitive', function (t) {
  79. t.test('primitives', function (st) {
  80. var testPrimitive = function (primitive) {
  81. st.ok(is(ES.ToPrimitive(primitive), primitive), debug(primitive) + ' is returned correctly');
  82. };
  83. forEach(v.primitives, testPrimitive);
  84. st.end();
  85. });
  86. t.test('objects', function (st) {
  87. st.equal(ES.ToPrimitive(v.coercibleObject), 3, 'coercibleObject with no hint coerces to valueOf');
  88. st.ok(is(ES.ToPrimitive({}), '[object Object]'), '{} with no hint coerces to Object#toString');
  89. st.equal(ES.ToPrimitive(v.coercibleObject, Number), 3, 'coercibleObject with hint Number coerces to valueOf');
  90. st.ok(is(ES.ToPrimitive({}, Number), '[object Object]'), '{} with hint Number coerces to NaN');
  91. st.equal(ES.ToPrimitive(v.coercibleObject, String), 42, 'coercibleObject with hint String coerces to nonstringified toString');
  92. st.equal(ES.ToPrimitive({}, String), '[object Object]', '{} with hint String coerces to Object#toString');
  93. st.equal(ES.ToPrimitive(v.toStringOnlyObject), 7, 'toStringOnlyObject returns non-stringified toString');
  94. st.equal(ES.ToPrimitive(v.valueOfOnlyObject), 4, 'valueOfOnlyObject returns valueOf');
  95. st['throws'](function () { return ES.ToPrimitive(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws a TypeError');
  96. st.end();
  97. });
  98. t.test('dates', function (st) {
  99. var invalid = new Date(NaN);
  100. st.equal(ES.ToPrimitive(invalid), Date.prototype.toString.call(invalid), 'invalid Date coerces to Date#toString');
  101. var now = new Date();
  102. st.equal(ES.ToPrimitive(now), Date.prototype.toString.call(now), 'Date coerces to Date#toString');
  103. st.end();
  104. });
  105. t.end();
  106. });
  107. test('ToBoolean', function (t) {
  108. t.equal(false, ES.ToBoolean(undefined), 'undefined coerces to false');
  109. t.equal(false, ES.ToBoolean(null), 'null coerces to false');
  110. t.equal(false, ES.ToBoolean(false), 'false returns false');
  111. t.equal(true, ES.ToBoolean(true), 'true returns true');
  112. t.test('numbers', function (st) {
  113. forEach(v.zeroes.concat(NaN), function (falsyNumber) {
  114. st.equal(false, ES.ToBoolean(falsyNumber), 'falsy number ' + falsyNumber + ' coerces to false');
  115. });
  116. forEach(v.infinities.concat([42, 1]), function (truthyNumber) {
  117. st.equal(true, ES.ToBoolean(truthyNumber), 'truthy number ' + truthyNumber + ' coerces to true');
  118. });
  119. st.end();
  120. });
  121. t.equal(false, ES.ToBoolean(''), 'empty string coerces to false');
  122. t.equal(true, ES.ToBoolean('foo'), 'nonempty string coerces to true');
  123. t.test('objects', function (st) {
  124. forEach(v.objects, function (obj) {
  125. st.equal(true, ES.ToBoolean(obj), 'object coerces to true');
  126. });
  127. st.equal(true, ES.ToBoolean(v.uncoercibleObject), 'uncoercibleObject coerces to true');
  128. st.end();
  129. });
  130. t.end();
  131. });
  132. test('ToNumber', function (t) {
  133. t.ok(is(NaN, ES.ToNumber(undefined)), 'undefined coerces to NaN');
  134. t.ok(is(ES.ToNumber(null), 0), 'null coerces to +0');
  135. t.ok(is(ES.ToNumber(false), 0), 'false coerces to +0');
  136. t.equal(1, ES.ToNumber(true), 'true coerces to 1');
  137. t.test('numbers', function (st) {
  138. st.ok(is(NaN, ES.ToNumber(NaN)), 'NaN returns itself');
  139. forEach(v.zeroes.concat(v.infinities, 42), function (num) {
  140. st.equal(num, ES.ToNumber(num), num + ' returns itself');
  141. });
  142. forEach(['foo', '0', '4a', '2.0', 'Infinity', '-Infinity'], function (numString) {
  143. st.ok(is(+numString, ES.ToNumber(numString)), '"' + numString + '" coerces to ' + Number(numString));
  144. });
  145. st.end();
  146. });
  147. t.test('objects', function (st) {
  148. forEach(v.objects, function (object) {
  149. st.ok(is(ES.ToNumber(object), ES.ToNumber(ES.ToPrimitive(object))), 'object ' + object + ' coerces to same as ToPrimitive of object does');
  150. });
  151. st['throws'](function () { return ES.ToNumber(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
  152. st.end();
  153. });
  154. t.test('binary literals', function (st) {
  155. st.equal(ES.ToNumber('0b10'), 2, '0b10 is 2');
  156. st.equal(ES.ToNumber({ toString: function () { return '0b11'; } }), 3, 'Object that toStrings to 0b11 is 3');
  157. st.equal(true, is(ES.ToNumber('0b12'), NaN), '0b12 is NaN');
  158. st.equal(true, is(ES.ToNumber({ toString: function () { return '0b112'; } }), NaN), 'Object that toStrings to 0b112 is NaN');
  159. st.end();
  160. });
  161. t.test('octal literals', function (st) {
  162. st.equal(ES.ToNumber('0o10'), 8, '0o10 is 8');
  163. st.equal(ES.ToNumber({ toString: function () { return '0o11'; } }), 9, 'Object that toStrings to 0o11 is 9');
  164. st.equal(true, is(ES.ToNumber('0o18'), NaN), '0o18 is NaN');
  165. st.equal(true, is(ES.ToNumber({ toString: function () { return '0o118'; } }), NaN), 'Object that toStrings to 0o118 is NaN');
  166. st.end();
  167. });
  168. t.test('signed hex numbers', function (st) {
  169. st.equal(true, is(ES.ToNumber('-0xF'), NaN), '-0xF is NaN');
  170. st.equal(true, is(ES.ToNumber(' -0xF '), NaN), 'space-padded -0xF is NaN');
  171. st.equal(true, is(ES.ToNumber('+0xF'), NaN), '+0xF is NaN');
  172. st.equal(true, is(ES.ToNumber(' +0xF '), NaN), 'space-padded +0xF is NaN');
  173. st.end();
  174. });
  175. t.test('trimming of whitespace and non-whitespace characters', function (st) {
  176. 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';
  177. st.equal(0, ES.ToNumber(whitespace + 0 + whitespace), 'whitespace is trimmed');
  178. // Zero-width space (zws), next line character (nel), and non-character (bom) are not whitespace.
  179. var nonWhitespaces = {
  180. '\\u0085': '\u0085',
  181. '\\u200b': '\u200b',
  182. '\\ufffe': '\ufffe'
  183. };
  184. forEach(nonWhitespaces, function (desc, nonWS) {
  185. st.equal(true, is(ES.ToNumber(nonWS + 0 + nonWS), NaN), 'non-whitespace ' + desc + ' not trimmed');
  186. });
  187. st.end();
  188. });
  189. forEach(v.symbols, function (symbol) {
  190. t['throws'](
  191. function () { ES.ToNumber(symbol); },
  192. TypeError,
  193. 'Symbols can’t be converted to a Number: ' + debug(symbol)
  194. );
  195. });
  196. t.test('dates', function (st) {
  197. var invalid = new Date(NaN);
  198. st.ok(is(ES.ToNumber(invalid), NaN), 'invalid Date coerces to NaN');
  199. var now = Date.now();
  200. st.equal(ES.ToNumber(new Date(now)), now, 'Date coerces to timestamp');
  201. st.end();
  202. });
  203. t.end();
  204. });
  205. test('ToInteger', function (t) {
  206. t.ok(is(0, ES.ToInteger(NaN)), 'NaN coerces to +0');
  207. forEach([0, Infinity, 42], function (num) {
  208. t.ok(is(num, ES.ToInteger(num)), num + ' returns itself');
  209. t.ok(is(-num, ES.ToInteger(-num)), '-' + num + ' returns itself');
  210. });
  211. t.equal(3, ES.ToInteger(Math.PI), 'pi returns 3');
  212. t['throws'](function () { return ES.ToInteger(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
  213. t.end();
  214. });
  215. test('ToInt32', function (t) {
  216. t.ok(is(0, ES.ToInt32(NaN)), 'NaN coerces to +0');
  217. forEach([0, Infinity], function (num) {
  218. t.ok(is(0, ES.ToInt32(num)), num + ' returns +0');
  219. t.ok(is(0, ES.ToInt32(-num)), '-' + num + ' returns +0');
  220. });
  221. t['throws'](function () { return ES.ToInt32(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
  222. t.ok(is(ES.ToInt32(0x100000000), 0), '2^32 returns +0');
  223. t.ok(is(ES.ToInt32(0x100000000 - 1), -1), '2^32 - 1 returns -1');
  224. t.ok(is(ES.ToInt32(0x80000000), -0x80000000), '2^31 returns -2^31');
  225. t.ok(is(ES.ToInt32(0x80000000 - 1), 0x80000000 - 1), '2^31 - 1 returns 2^31 - 1');
  226. forEach([0, Infinity, NaN, 0x100000000, 0x80000000, 0x10000, 0x42], function (num) {
  227. t.ok(is(ES.ToInt32(num), ES.ToInt32(ES.ToUint32(num))), 'ToInt32(x) === ToInt32(ToUint32(x)) for 0x' + num.toString(16));
  228. t.ok(is(ES.ToInt32(-num), ES.ToInt32(ES.ToUint32(-num))), 'ToInt32(x) === ToInt32(ToUint32(x)) for -0x' + num.toString(16));
  229. });
  230. t.end();
  231. });
  232. test('ToUint32', function (t) {
  233. t.ok(is(0, ES.ToUint32(NaN)), 'NaN coerces to +0');
  234. forEach([0, Infinity], function (num) {
  235. t.ok(is(0, ES.ToUint32(num)), num + ' returns +0');
  236. t.ok(is(0, ES.ToUint32(-num)), '-' + num + ' returns +0');
  237. });
  238. t['throws'](function () { return ES.ToUint32(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
  239. t.ok(is(ES.ToUint32(0x100000000), 0), '2^32 returns +0');
  240. t.ok(is(ES.ToUint32(0x100000000 - 1), 0x100000000 - 1), '2^32 - 1 returns 2^32 - 1');
  241. t.ok(is(ES.ToUint32(0x80000000), 0x80000000), '2^31 returns 2^31');
  242. t.ok(is(ES.ToUint32(0x80000000 - 1), 0x80000000 - 1), '2^31 - 1 returns 2^31 - 1');
  243. forEach([0, Infinity, NaN, 0x100000000, 0x80000000, 0x10000, 0x42], function (num) {
  244. t.ok(is(ES.ToUint32(num), ES.ToUint32(ES.ToInt32(num))), 'ToUint32(x) === ToUint32(ToInt32(x)) for 0x' + num.toString(16));
  245. t.ok(is(ES.ToUint32(-num), ES.ToUint32(ES.ToInt32(-num))), 'ToUint32(x) === ToUint32(ToInt32(x)) for -0x' + num.toString(16));
  246. });
  247. t.end();
  248. });
  249. test('ToInt16', function (t) {
  250. t.ok(is(0, ES.ToInt16(NaN)), 'NaN coerces to +0');
  251. forEach([0, Infinity], function (num) {
  252. t.ok(is(0, ES.ToInt16(num)), num + ' returns +0');
  253. t.ok(is(0, ES.ToInt16(-num)), '-' + num + ' returns +0');
  254. });
  255. t['throws'](function () { return ES.ToInt16(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
  256. t.ok(is(ES.ToInt16(0x100000000), 0), '2^32 returns +0');
  257. t.ok(is(ES.ToInt16(0x100000000 - 1), -1), '2^32 - 1 returns -1');
  258. t.ok(is(ES.ToInt16(0x80000000), 0), '2^31 returns +0');
  259. t.ok(is(ES.ToInt16(0x80000000 - 1), -1), '2^31 - 1 returns -1');
  260. t.ok(is(ES.ToInt16(0x10000), 0), '2^16 returns +0');
  261. t.ok(is(ES.ToInt16(0x10000 - 1), -1), '2^16 - 1 returns -1');
  262. t.end();
  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. test('ToInt8', function (t) {
  280. t.ok(is(0, ES.ToInt8(NaN)), 'NaN coerces to +0');
  281. forEach([0, Infinity], function (num) {
  282. t.ok(is(0, ES.ToInt8(num)), num + ' returns +0');
  283. t.ok(is(0, ES.ToInt8(-num)), '-' + num + ' returns +0');
  284. });
  285. t['throws'](function () { return ES.ToInt8(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
  286. t.ok(is(ES.ToInt8(0x100000000), 0), '2^32 returns +0');
  287. t.ok(is(ES.ToInt8(0x100000000 - 1), -1), '2^32 - 1 returns -1');
  288. t.ok(is(ES.ToInt8(0x80000000), 0), '2^31 returns +0');
  289. t.ok(is(ES.ToInt8(0x80000000 - 1), -1), '2^31 - 1 returns -1');
  290. t.ok(is(ES.ToInt8(0x10000), 0), '2^16 returns +0');
  291. t.ok(is(ES.ToInt8(0x10000 - 1), -1), '2^16 - 1 returns -1');
  292. t.ok(is(ES.ToInt8(0x100), 0), '2^8 returns +0');
  293. t.ok(is(ES.ToInt8(0x100 - 1), -1), '2^8 - 1 returns -1');
  294. t.ok(is(ES.ToInt8(0x10), 0x10), '2^4 returns 2^4');
  295. t.end();
  296. });
  297. test('ToUint8', function (t) {
  298. t.ok(is(0, ES.ToUint8(NaN)), 'NaN coerces to +0');
  299. forEach([0, Infinity], function (num) {
  300. t.ok(is(0, ES.ToUint8(num)), num + ' returns +0');
  301. t.ok(is(0, ES.ToUint8(-num)), '-' + num + ' returns +0');
  302. });
  303. t['throws'](function () { return ES.ToUint8(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
  304. t.ok(is(ES.ToUint8(0x100000000), 0), '2^32 returns +0');
  305. t.ok(is(ES.ToUint8(0x100000000 - 1), 0x100 - 1), '2^32 - 1 returns 2^8 - 1');
  306. t.ok(is(ES.ToUint8(0x80000000), 0), '2^31 returns +0');
  307. t.ok(is(ES.ToUint8(0x80000000 - 1), 0x100 - 1), '2^31 - 1 returns 2^8 - 1');
  308. t.ok(is(ES.ToUint8(0x10000), 0), '2^16 returns +0');
  309. t.ok(is(ES.ToUint8(0x10000 - 1), 0x100 - 1), '2^16 - 1 returns 2^8 - 1');
  310. t.ok(is(ES.ToUint8(0x100), 0), '2^8 returns +0');
  311. t.ok(is(ES.ToUint8(0x100 - 1), 0x100 - 1), '2^8 - 1 returns 2^16 - 1');
  312. t.ok(is(ES.ToUint8(0x10), 0x10), '2^4 returns 2^4');
  313. t.ok(is(ES.ToUint8(0x10 - 1), 0x10 - 1), '2^4 - 1 returns 2^4 - 1');
  314. t.end();
  315. });
  316. test('ToUint8Clamp', function (t) {
  317. t.ok(is(0, ES.ToUint8Clamp(NaN)), 'NaN coerces to +0');
  318. t.ok(is(0, ES.ToUint8Clamp(0)), '+0 returns +0');
  319. t.ok(is(0, ES.ToUint8Clamp(-0)), '-0 returns +0');
  320. t.ok(is(0, ES.ToUint8Clamp(-Infinity)), '-Infinity returns +0');
  321. t['throws'](function () { return ES.ToUint8Clamp(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
  322. forEach([255, 256, 0x100000, Infinity], function (number) {
  323. t.ok(is(255, ES.ToUint8Clamp(number)), number + ' coerces to 255');
  324. });
  325. t.equal(1, ES.ToUint8Clamp(1.49), '1.49 coerces to 1');
  326. t.equal(2, ES.ToUint8Clamp(1.5), '1.5 coerces to 2, because 2 is even');
  327. t.equal(2, ES.ToUint8Clamp(1.51), '1.51 coerces to 2');
  328. t.equal(2, ES.ToUint8Clamp(2.49), '2.49 coerces to 2');
  329. t.equal(2, ES.ToUint8Clamp(2.5), '2.5 coerces to 2, because 2 is even');
  330. t.equal(3, ES.ToUint8Clamp(2.51), '2.51 coerces to 3');
  331. t.end();
  332. });
  333. test('ToString', function (t) {
  334. forEach(v.objects.concat(v.nonSymbolPrimitives), function (item) {
  335. t.equal(ES.ToString(item), String(item), 'ES.ToString(' + debug(item) + ') ToStrings to String(' + debug(item) + ')');
  336. });
  337. t['throws'](function () { return ES.ToString(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
  338. forEach(v.symbols, function (symbol) {
  339. t['throws'](function () { return ES.ToString(symbol); }, TypeError, debug(symbol) + ' throws');
  340. });
  341. t.end();
  342. });
  343. test('ToObject', function (t) {
  344. t['throws'](function () { return ES.ToObject(undefined); }, TypeError, 'undefined throws');
  345. t['throws'](function () { return ES.ToObject(null); }, TypeError, 'null throws');
  346. forEach(v.numbers, function (number) {
  347. var obj = ES.ToObject(number);
  348. t.equal(typeof obj, 'object', 'number ' + number + ' coerces to object');
  349. t.equal(true, obj instanceof Number, 'object of ' + number + ' is Number object');
  350. t.ok(is(obj.valueOf(), number), 'object of ' + number + ' coerces to ' + number);
  351. });
  352. t.end();
  353. });
  354. test('RequireObjectCoercible', function (t) {
  355. t.equal(false, 'CheckObjectCoercible' in ES, 'CheckObjectCoercible -> RequireObjectCoercible in ES6');
  356. t['throws'](function () { return ES.RequireObjectCoercible(undefined); }, TypeError, 'undefined throws');
  357. t['throws'](function () { return ES.RequireObjectCoercible(null); }, TypeError, 'null throws');
  358. var isCoercible = function (value) {
  359. t.doesNotThrow(function () { return ES.RequireObjectCoercible(value); }, debug(value) + ' does not throw');
  360. };
  361. forEach(v.objects.concat(v.nonNullPrimitives), isCoercible);
  362. t.end();
  363. });
  364. test('IsCallable', function (t) {
  365. t.equal(true, ES.IsCallable(function () {}), 'function is callable');
  366. var nonCallables = [/a/g, {}, Object.prototype, NaN].concat(v.nonFunctions);
  367. forEach(nonCallables, function (nonCallable) {
  368. t.equal(false, ES.IsCallable(nonCallable), debug(nonCallable) + ' is not callable');
  369. });
  370. t.end();
  371. });
  372. test('SameValue', function (t) {
  373. t.equal(true, ES.SameValue(NaN, NaN), 'NaN is SameValue as NaN');
  374. t.equal(false, ES.SameValue(0, -0), '+0 is not SameValue as -0');
  375. forEach(v.objects.concat(v.primitives), function (val) {
  376. t.equal(val === val, ES.SameValue(val, val), debug(val) + ' is SameValue to itself');
  377. });
  378. t.end();
  379. });
  380. test('SameValueZero', function (t) {
  381. t.equal(true, ES.SameValueZero(NaN, NaN), 'NaN is SameValueZero as NaN');
  382. t.equal(true, ES.SameValueZero(0, -0), '+0 is SameValueZero as -0');
  383. forEach(v.objects.concat(v.primitives), function (val) {
  384. t.equal(val === val, ES.SameValueZero(val, val), debug(val) + ' is SameValueZero to itself');
  385. });
  386. t.end();
  387. });
  388. test('ToPropertyKey', function (t) {
  389. forEach(v.objects.concat(v.nonSymbolPrimitives), function (value) {
  390. t.equal(ES.ToPropertyKey(value), String(value), 'ToPropertyKey(value) === String(value) for non-Symbols');
  391. });
  392. forEach(v.symbols, function (symbol) {
  393. t.equal(
  394. ES.ToPropertyKey(symbol),
  395. symbol,
  396. 'ToPropertyKey(' + debug(symbol) + ') === ' + debug(symbol)
  397. );
  398. t.equal(
  399. ES.ToPropertyKey(Object(symbol)),
  400. symbol,
  401. 'ToPropertyKey(' + debug(Object(symbol)) + ') === ' + debug(symbol)
  402. );
  403. });
  404. t.end();
  405. });
  406. test('ToLength', function (t) {
  407. t['throws'](function () { return ES.ToLength(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws a TypeError');
  408. t.equal(3, ES.ToLength(v.coercibleObject), 'coercibleObject coerces to 3');
  409. t.equal(42, ES.ToLength('42.5'), '"42.5" coerces to 42');
  410. t.equal(7, ES.ToLength(7.3), '7.3 coerces to 7');
  411. forEach([-0, -1, -42, -Infinity], function (negative) {
  412. t.ok(is(0, ES.ToLength(negative)), negative + ' coerces to +0');
  413. });
  414. t.equal(MAX_SAFE_INTEGER, ES.ToLength(MAX_SAFE_INTEGER + 1), '2^53 coerces to 2^53 - 1');
  415. t.equal(MAX_SAFE_INTEGER, ES.ToLength(MAX_SAFE_INTEGER + 3), '2^53 + 2 coerces to 2^53 - 1');
  416. t.end();
  417. });
  418. test('IsArray', function (t) {
  419. t.equal(true, ES.IsArray([]), '[] is array');
  420. t.equal(false, ES.IsArray({}), '{} is not array');
  421. t.equal(false, ES.IsArray({ length: 1, 0: true }), 'arraylike object is not array');
  422. forEach(v.objects.concat(v.primitives), function (value) {
  423. t.equal(false, ES.IsArray(value), debug(value) + ' is not array');
  424. });
  425. t.end();
  426. });
  427. test('IsRegExp', function (t) {
  428. forEach([/a/g, new RegExp('a', 'g')], function (regex) {
  429. t.equal(true, ES.IsRegExp(regex), regex + ' is regex');
  430. });
  431. forEach(v.objects.concat(v.primitives), function (nonRegex) {
  432. t.equal(false, ES.IsRegExp(nonRegex), debug(nonRegex) + ' is not regex');
  433. });
  434. t.test('Symbol.match', { skip: !v.hasSymbols || !Symbol.match }, function (st) {
  435. var obj = {};
  436. obj[Symbol.match] = true;
  437. st.equal(true, ES.IsRegExp(obj), 'object with truthy Symbol.match is regex');
  438. var regex = /a/;
  439. regex[Symbol.match] = false;
  440. st.equal(false, ES.IsRegExp(regex), 'regex with falsy Symbol.match is not regex');
  441. st.end();
  442. });
  443. t.end();
  444. });
  445. test('IsPropertyKey', function (t) {
  446. forEach(v.numbers.concat(v.objects), function (notKey) {
  447. t.equal(false, ES.IsPropertyKey(notKey), debug(notKey) + ' is not property key');
  448. });
  449. t.equal(true, ES.IsPropertyKey('foo'), 'string is property key');
  450. forEach(v.symbols, function (symbol) {
  451. t.equal(true, ES.IsPropertyKey(symbol), debug(symbol) + ' is property key');
  452. });
  453. t.end();
  454. });
  455. test('IsInteger', function (t) {
  456. for (var i = -100; i < 100; i += 10) {
  457. t.equal(true, ES.IsInteger(i), i + ' is integer');
  458. t.equal(false, ES.IsInteger(i + 0.2), (i + 0.2) + ' is not integer');
  459. }
  460. t.equal(true, ES.IsInteger(-0), '-0 is integer');
  461. var notInts = v.nonNumbers.concat(v.nonIntegerNumbers, v.infinities, [NaN, [], new Date()]);
  462. forEach(notInts, function (notInt) {
  463. t.equal(false, ES.IsInteger(notInt), debug(notInt) + ' is not integer');
  464. });
  465. t.equal(false, ES.IsInteger(v.uncoercibleObject), 'uncoercibleObject is not integer');
  466. t.end();
  467. });
  468. test('IsExtensible', function (t) {
  469. forEach(v.objects, function (object) {
  470. t.equal(true, ES.IsExtensible(object), debug(object) + ' object is extensible');
  471. });
  472. forEach(v.primitives, function (primitive) {
  473. t.equal(false, ES.IsExtensible(primitive), debug(primitive) + ' is not extensible');
  474. });
  475. if (Object.preventExtensions) {
  476. t.equal(false, ES.IsExtensible(Object.preventExtensions({})), 'object with extensions prevented is not extensible');
  477. }
  478. t.end();
  479. });
  480. test('CanonicalNumericIndexString', function (t) {
  481. var throwsOnNonString = function (notString) {
  482. t['throws'](
  483. function () { return ES.CanonicalNumericIndexString(notString); },
  484. TypeError,
  485. debug(notString) + ' is not a string'
  486. );
  487. };
  488. forEach(v.objects.concat(v.numbers), throwsOnNonString);
  489. t.ok(is(-0, ES.CanonicalNumericIndexString('-0')), '"-0" returns -0');
  490. for (var i = -50; i < 50; i += 10) {
  491. t.equal(i, ES.CanonicalNumericIndexString(String(i)), '"' + i + '" returns ' + i);
  492. t.equal(undefined, ES.CanonicalNumericIndexString(String(i) + 'a'), '"' + i + 'a" returns undefined');
  493. }
  494. t.end();
  495. });
  496. test('IsConstructor', function (t) {
  497. t.equal(true, ES.IsConstructor(function () {}), 'function is constructor');
  498. t.equal(false, ES.IsConstructor(/a/g), 'regex is not constructor');
  499. forEach(v.objects, function (object) {
  500. t.equal(false, ES.IsConstructor(object), object + ' object is not constructor');
  501. });
  502. try {
  503. var foo = Function('return class Foo {}')(); // eslint-disable-line no-new-func
  504. t.equal(ES.IsConstructor(foo), true, 'class is constructor');
  505. } catch (e) {
  506. t.comment('SKIP: class syntax not supported.');
  507. }
  508. t.end();
  509. });
  510. test('Call', function (t) {
  511. var receiver = {};
  512. var notFuncs = v.nonFunctions.concat([/a/g, new RegExp('a', 'g')]);
  513. t.plan(notFuncs.length + 4);
  514. var throwsIfNotCallable = function (notFunc) {
  515. t['throws'](
  516. function () { return ES.Call(notFunc, receiver); },
  517. TypeError,
  518. debug(notFunc) + ' (' + typeof notFunc + ') is not callable'
  519. );
  520. };
  521. forEach(notFuncs, throwsIfNotCallable);
  522. ES.Call(
  523. function (a, b) {
  524. t.equal(this, receiver, 'context matches expected');
  525. t.deepEqual([a, b], [1, 2], 'named args are correct');
  526. t.equal(arguments.length, 3, 'extra argument was passed');
  527. t.equal(arguments[2], 3, 'extra argument was correct');
  528. },
  529. receiver,
  530. [1, 2, 3]
  531. );
  532. t.end();
  533. });
  534. test('GetV', function (t) {
  535. t['throws'](function () { return ES.GetV({ 7: 7 }, 7); }, TypeError, 'Throws a TypeError if `P` is not a property key');
  536. var obj = { a: function () {} };
  537. t.equal(ES.GetV(obj, 'a'), obj.a, 'returns property if it exists');
  538. t.equal(ES.GetV(obj, 'b'), undefined, 'returns undefiend if property does not exist');
  539. t.end();
  540. });
  541. test('GetMethod', function (t) {
  542. t['throws'](function () { return ES.GetMethod({ 7: 7 }, 7); }, TypeError, 'Throws a TypeError if `P` is not a property key');
  543. t.equal(ES.GetMethod({}, 'a'), undefined, 'returns undefined in property is undefined');
  544. t.equal(ES.GetMethod({ a: null }, 'a'), undefined, 'returns undefined if property is null');
  545. t.equal(ES.GetMethod({ a: undefined }, 'a'), undefined, 'returns undefined if property is undefined');
  546. var obj = { a: function () {} };
  547. t['throws'](function () { ES.GetMethod({ a: 'b' }, 'a'); }, TypeError, 'throws TypeError if property exists and is not callable');
  548. t.equal(ES.GetMethod(obj, 'a'), obj.a, 'returns property if it is callable');
  549. t.end();
  550. });
  551. test('Get', function (t) {
  552. t['throws'](function () { return ES.Get('a', 'a'); }, TypeError, 'Throws a TypeError if `O` is not an Object');
  553. t['throws'](function () { return ES.Get({ 7: 7 }, 7); }, TypeError, 'Throws a TypeError if `P` is not a property key');
  554. var value = {};
  555. t.test('Symbols', { skip: !v.hasSymbols }, function (st) {
  556. var sym = Symbol('sym');
  557. var obj = {};
  558. obj[sym] = value;
  559. st.equal(ES.Get(obj, sym), value, 'returns property `P` if it exists on object `O`');
  560. st.end();
  561. });
  562. t.equal(ES.Get({ a: value }, 'a'), value, 'returns property `P` if it exists on object `O`');
  563. t.end();
  564. });
  565. test('Type', { skip: !v.hasSymbols }, function (t) {
  566. t.equal(ES.Type(Symbol.iterator), 'Symbol', 'Type(Symbol.iterator) is Symbol');
  567. t.end();
  568. });
  569. test('SpeciesConstructor', function (t) {
  570. t['throws'](function () { ES.SpeciesConstructor(null); }, TypeError);
  571. t['throws'](function () { ES.SpeciesConstructor(undefined); }, TypeError);
  572. var defaultConstructor = function Foo() {};
  573. t.equal(
  574. ES.SpeciesConstructor({ constructor: undefined }, defaultConstructor),
  575. defaultConstructor,
  576. 'undefined constructor returns defaultConstructor'
  577. );
  578. t['throws'](
  579. function () { return ES.SpeciesConstructor({ constructor: null }, defaultConstructor); },
  580. TypeError,
  581. 'non-undefined non-object constructor throws'
  582. );
  583. t.test('with Symbol.species', { skip: !hasSpecies }, function (st) {
  584. var Bar = function Bar() {};
  585. Bar[Symbol.species] = null;
  586. st.equal(
  587. ES.SpeciesConstructor(new Bar(), defaultConstructor),
  588. defaultConstructor,
  589. 'undefined/null Symbol.species returns default constructor'
  590. );
  591. var Baz = function Baz() {};
  592. Baz[Symbol.species] = Bar;
  593. st.equal(
  594. ES.SpeciesConstructor(new Baz(), defaultConstructor),
  595. Bar,
  596. 'returns Symbol.species constructor value'
  597. );
  598. Baz[Symbol.species] = {};
  599. st['throws'](
  600. function () { ES.SpeciesConstructor(new Baz(), defaultConstructor); },
  601. TypeError,
  602. 'throws when non-constructor non-null non-undefined species value found'
  603. );
  604. st.end();
  605. });
  606. t.end();
  607. });
  608. test('IsPropertyDescriptor', { skip: skips && skips.IsPropertyDescriptor }, function (t) {
  609. forEach(v.nonUndefinedPrimitives, function (primitive) {
  610. t.equal(
  611. ES.IsPropertyDescriptor(primitive),
  612. false,
  613. debug(primitive) + ' is not a Property Descriptor'
  614. );
  615. });
  616. t.equal(ES.IsPropertyDescriptor({ invalid: true }), false, 'invalid keys not allowed on a Property Descriptor');
  617. t.equal(ES.IsPropertyDescriptor({}), true, 'empty object is an incomplete Property Descriptor');
  618. t.equal(ES.IsPropertyDescriptor(v.accessorDescriptor()), true, 'accessor descriptor is a Property Descriptor');
  619. t.equal(ES.IsPropertyDescriptor(v.mutatorDescriptor()), true, 'mutator descriptor is a Property Descriptor');
  620. t.equal(ES.IsPropertyDescriptor(v.dataDescriptor()), true, 'data descriptor is a Property Descriptor');
  621. t.equal(ES.IsPropertyDescriptor(v.genericDescriptor()), true, 'generic descriptor is a Property Descriptor');
  622. t['throws'](
  623. function () { ES.IsPropertyDescriptor(v.bothDescriptor()); },
  624. TypeError,
  625. 'a Property Descriptor can not be both a Data and an Accessor Descriptor'
  626. );
  627. t.end();
  628. });
  629. assertRecordTests(ES, test);
  630. test('IsAccessorDescriptor', function (t) {
  631. forEach(v.nonUndefinedPrimitives, function (primitive) {
  632. t['throws'](
  633. function () { ES.IsAccessorDescriptor(primitive); },
  634. TypeError,
  635. debug(primitive) + ' is not a Property Descriptor'
  636. );
  637. });
  638. t.equal(ES.IsAccessorDescriptor(), false, 'no value is not an Accessor Descriptor');
  639. t.equal(ES.IsAccessorDescriptor(undefined), false, 'undefined value is not an Accessor Descriptor');
  640. t.equal(ES.IsAccessorDescriptor(v.accessorDescriptor()), true, 'accessor descriptor is an Accessor Descriptor');
  641. t.equal(ES.IsAccessorDescriptor(v.mutatorDescriptor()), true, 'mutator descriptor is an Accessor Descriptor');
  642. t.equal(ES.IsAccessorDescriptor(v.dataDescriptor()), false, 'data descriptor is not an Accessor Descriptor');
  643. t.equal(ES.IsAccessorDescriptor(v.genericDescriptor()), false, 'generic descriptor is not an Accessor Descriptor');
  644. t.end();
  645. });
  646. test('IsDataDescriptor', function (t) {
  647. forEach(v.nonUndefinedPrimitives, function (primitive) {
  648. t['throws'](
  649. function () { ES.IsDataDescriptor(primitive); },
  650. TypeError,
  651. debug(primitive) + ' is not a Property Descriptor'
  652. );
  653. });
  654. t.equal(ES.IsDataDescriptor(), false, 'no value is not a Data Descriptor');
  655. t.equal(ES.IsDataDescriptor(undefined), false, 'undefined value is not a Data Descriptor');
  656. t.equal(ES.IsDataDescriptor(v.accessorDescriptor()), false, 'accessor descriptor is not a Data Descriptor');
  657. t.equal(ES.IsDataDescriptor(v.mutatorDescriptor()), false, 'mutator descriptor is not a Data Descriptor');
  658. t.equal(ES.IsDataDescriptor(v.dataDescriptor()), true, 'data descriptor is a Data Descriptor');
  659. t.equal(ES.IsDataDescriptor(v.genericDescriptor()), false, 'generic descriptor is not a Data Descriptor');
  660. t.end();
  661. });
  662. test('IsGenericDescriptor', function (t) {
  663. forEach(v.nonUndefinedPrimitives, function (primitive) {
  664. t['throws'](
  665. function () { ES.IsGenericDescriptor(primitive); },
  666. TypeError,
  667. debug(primitive) + ' is not a Property Descriptor'
  668. );
  669. });
  670. t.equal(ES.IsGenericDescriptor(), false, 'no value is not a Data Descriptor');
  671. t.equal(ES.IsGenericDescriptor(undefined), false, 'undefined value is not a Data Descriptor');
  672. t.equal(ES.IsGenericDescriptor(v.accessorDescriptor()), false, 'accessor descriptor is not a generic Descriptor');
  673. t.equal(ES.IsGenericDescriptor(v.mutatorDescriptor()), false, 'mutator descriptor is not a generic Descriptor');
  674. t.equal(ES.IsGenericDescriptor(v.dataDescriptor()), false, 'data descriptor is not a generic Descriptor');
  675. t.equal(ES.IsGenericDescriptor(v.genericDescriptor()), true, 'generic descriptor is a generic Descriptor');
  676. t.end();
  677. });
  678. test('FromPropertyDescriptor', function (t) {
  679. t.equal(ES.FromPropertyDescriptor(), undefined, 'no value begets undefined');
  680. t.equal(ES.FromPropertyDescriptor(undefined), undefined, 'undefined value begets undefined');
  681. forEach(v.nonUndefinedPrimitives, function (primitive) {
  682. t['throws'](
  683. function () { ES.FromPropertyDescriptor(primitive); },
  684. TypeError,
  685. debug(primitive) + ' is not a Property Descriptor'
  686. );
  687. });
  688. var accessor = v.accessorDescriptor();
  689. t.deepEqual(ES.FromPropertyDescriptor(accessor), {
  690. get: accessor['[[Get]]'],
  691. enumerable: !!accessor['[[Enumerable]]'],
  692. configurable: !!accessor['[[Configurable]]']
  693. });
  694. var mutator = v.mutatorDescriptor();
  695. t.deepEqual(ES.FromPropertyDescriptor(mutator), {
  696. set: mutator['[[Set]]'],
  697. enumerable: !!mutator['[[Enumerable]]'],
  698. configurable: !!mutator['[[Configurable]]']
  699. });
  700. var data = v.dataDescriptor();
  701. t.deepEqual(ES.FromPropertyDescriptor(data), {
  702. value: data['[[Value]]'],
  703. writable: data['[[Writable]]']
  704. });
  705. t.deepEqual(ES.FromPropertyDescriptor(v.genericDescriptor()), {
  706. enumerable: false,
  707. configurable: true
  708. });
  709. t.end();
  710. });
  711. test('ToPropertyDescriptor', function (t) {
  712. forEach(v.nonUndefinedPrimitives, function (primitive) {
  713. t['throws'](
  714. function () { ES.ToPropertyDescriptor(primitive); },
  715. TypeError,
  716. debug(primitive) + ' is not an Object'
  717. );
  718. });
  719. var accessor = v.accessorDescriptor();
  720. t.deepEqual(ES.ToPropertyDescriptor({
  721. get: accessor['[[Get]]'],
  722. enumerable: !!accessor['[[Enumerable]]'],
  723. configurable: !!accessor['[[Configurable]]']
  724. }), accessor);
  725. var mutator = v.mutatorDescriptor();
  726. t.deepEqual(ES.ToPropertyDescriptor({
  727. set: mutator['[[Set]]'],
  728. enumerable: !!mutator['[[Enumerable]]'],
  729. configurable: !!mutator['[[Configurable]]']
  730. }), mutator);
  731. var data = v.dataDescriptor();
  732. t.deepEqual(ES.ToPropertyDescriptor({
  733. value: data['[[Value]]'],
  734. writable: data['[[Writable]]'],
  735. configurable: !!data['[[Configurable]]']
  736. }), assign(data, { '[[Configurable]]': false }));
  737. var both = v.bothDescriptor();
  738. t['throws'](
  739. function () {
  740. ES.FromPropertyDescriptor({ get: both['[[Get]]'], value: both['[[Value]]'] });
  741. },
  742. TypeError,
  743. 'data and accessor descriptors are mutually exclusive'
  744. );
  745. t.end();
  746. });
  747. test('CompletePropertyDescriptor', function (t) {
  748. forEach(v.nonUndefinedPrimitives, function (primitive) {
  749. t['throws'](
  750. function () { ES.CompletePropertyDescriptor(primitive); },
  751. TypeError,
  752. debug(primitive) + ' is not a Property Descriptor'
  753. );
  754. });
  755. var generic = v.genericDescriptor();
  756. t.deepEqual(
  757. ES.CompletePropertyDescriptor(generic),
  758. {
  759. '[[Configurable]]': !!generic['[[Configurable]]'],
  760. '[[Enumerable]]': !!generic['[[Enumerable]]'],
  761. '[[Value]]': undefined,
  762. '[[Writable]]': false
  763. },
  764. 'completes a Generic Descriptor'
  765. );
  766. var data = v.dataDescriptor();
  767. t.deepEqual(
  768. ES.CompletePropertyDescriptor(data),
  769. {
  770. '[[Configurable]]': !!data['[[Configurable]]'],
  771. '[[Enumerable]]': false,
  772. '[[Value]]': data['[[Value]]'],
  773. '[[Writable]]': !!data['[[Writable]]']
  774. },
  775. 'completes a Data Descriptor'
  776. );
  777. var accessor = v.accessorDescriptor();
  778. t.deepEqual(
  779. ES.CompletePropertyDescriptor(accessor),
  780. {
  781. '[[Get]]': accessor['[[Get]]'],
  782. '[[Enumerable]]': !!accessor['[[Enumerable]]'],
  783. '[[Configurable]]': !!accessor['[[Configurable]]'],
  784. '[[Set]]': undefined
  785. },
  786. 'completes an Accessor Descriptor'
  787. );
  788. var mutator = v.mutatorDescriptor();
  789. t.deepEqual(
  790. ES.CompletePropertyDescriptor(mutator),
  791. {
  792. '[[Set]]': mutator['[[Set]]'],
  793. '[[Enumerable]]': !!mutator['[[Enumerable]]'],
  794. '[[Configurable]]': !!mutator['[[Configurable]]'],
  795. '[[Get]]': undefined
  796. },
  797. 'completes a mutator Descriptor'
  798. );
  799. t['throws'](
  800. function () { ES.CompletePropertyDescriptor(v.bothDescriptor()); },
  801. TypeError,
  802. 'data and accessor descriptors are mutually exclusive'
  803. );
  804. t.end();
  805. });
  806. test('Set', function (t) {
  807. forEach(v.primitives, function (primitive) {
  808. t['throws'](
  809. function () { ES.Set(primitive, '', null, false); },
  810. TypeError,
  811. debug(primitive) + ' is not an Object'
  812. );
  813. });
  814. forEach(v.nonPropertyKeys, function (nonKey) {
  815. t['throws'](
  816. function () { ES.Set({}, nonKey, null, false); },
  817. TypeError,
  818. debug(nonKey) + ' is not a Property Key'
  819. );
  820. });
  821. forEach(v.nonBooleans, function (nonBoolean) {
  822. t['throws'](
  823. function () { ES.Set({}, '', null, nonBoolean); },
  824. TypeError,
  825. debug(nonBoolean) + ' is not a Boolean'
  826. );
  827. });
  828. var o = {};
  829. var value = {};
  830. ES.Set(o, 'key', value, true);
  831. t.deepEqual(o, { key: value }, 'key is set');
  832. t.test('nonwritable', { skip: !Object.defineProperty }, function (st) {
  833. var obj = { a: value };
  834. Object.defineProperty(obj, 'a', { writable: false });
  835. st['throws'](
  836. function () { ES.Set(obj, 'a', value, true); },
  837. TypeError,
  838. 'can not Set nonwritable property'
  839. );
  840. st.doesNotThrow(
  841. function () { ES.Set(obj, 'a', value, false); },
  842. 'setting Throw to false prevents an exception'
  843. );
  844. st.end();
  845. });
  846. t.test('nonconfigurable', { skip: !Object.defineProperty }, function (st) {
  847. var obj = { a: value };
  848. Object.defineProperty(obj, 'a', { configurable: false });
  849. ES.Set(obj, 'a', value, true);
  850. st.deepEqual(obj, { a: value }, 'key is set');
  851. st.end();
  852. });
  853. t.end();
  854. });
  855. test('HasOwnProperty', function (t) {
  856. forEach(v.primitives, function (primitive) {
  857. t['throws'](
  858. function () { ES.HasOwnProperty(primitive, 'key'); },
  859. TypeError,
  860. debug(primitive) + ' is not an Object'
  861. );
  862. });
  863. forEach(v.nonPropertyKeys, function (nonKey) {
  864. t['throws'](
  865. function () { ES.HasOwnProperty({}, nonKey); },
  866. TypeError,
  867. debug(nonKey) + ' is not a Property Key'
  868. );
  869. });
  870. t.equal(ES.HasOwnProperty({}, 'toString'), false, 'inherited properties are not own');
  871. t.equal(
  872. ES.HasOwnProperty({ toString: 1 }, 'toString'),
  873. true,
  874. 'shadowed inherited own properties are own'
  875. );
  876. t.equal(ES.HasOwnProperty({ a: 1 }, 'a'), true, 'own properties are own');
  877. t.end();
  878. });
  879. test('HasProperty', function (t) {
  880. forEach(v.primitives, function (primitive) {
  881. t['throws'](
  882. function () { ES.HasProperty(primitive, 'key'); },
  883. TypeError,
  884. debug(primitive) + ' is not an Object'
  885. );
  886. });
  887. forEach(v.nonPropertyKeys, function (nonKey) {
  888. t['throws'](
  889. function () { ES.HasProperty({}, nonKey); },
  890. TypeError,
  891. debug(nonKey) + ' is not a Property Key'
  892. );
  893. });
  894. t.equal(ES.HasProperty({}, 'nope'), false, 'object does not have nonexistent properties');
  895. t.equal(ES.HasProperty({}, 'toString'), true, 'object has inherited properties');
  896. t.equal(
  897. ES.HasProperty({ toString: 1 }, 'toString'),
  898. true,
  899. 'object has shadowed inherited own properties'
  900. );
  901. t.equal(ES.HasProperty({ a: 1 }, 'a'), true, 'object has own properties');
  902. t.end();
  903. });
  904. test('IsConcatSpreadable', function (t) {
  905. forEach(v.primitives, function (primitive) {
  906. t.equal(ES.IsConcatSpreadable(primitive), false, debug(primitive) + ' is not an Object');
  907. });
  908. var hasSymbolConcatSpreadable = v.hasSymbols && Symbol.isConcatSpreadable;
  909. t.test('Symbol.isConcatSpreadable', { skip: !hasSymbolConcatSpreadable }, function (st) {
  910. forEach(v.falsies, function (falsy) {
  911. var obj = {};
  912. obj[Symbol.isConcatSpreadable] = falsy;
  913. st.equal(
  914. ES.IsConcatSpreadable(obj),
  915. false,
  916. 'an object with ' + debug(falsy) + ' as Symbol.isConcatSpreadable is not concat spreadable'
  917. );
  918. });
  919. forEach(v.truthies, function (truthy) {
  920. var obj = {};
  921. obj[Symbol.isConcatSpreadable] = truthy;
  922. st.equal(
  923. ES.IsConcatSpreadable(obj),
  924. true,
  925. 'an object with ' + debug(truthy) + ' as Symbol.isConcatSpreadable is concat spreadable'
  926. );
  927. });
  928. st.end();
  929. });
  930. forEach(v.objects, function (object) {
  931. t.equal(
  932. ES.IsConcatSpreadable(object),
  933. false,
  934. 'non-array without Symbol.isConcatSpreadable is not concat spreadable'
  935. );
  936. });
  937. t.equal(ES.IsConcatSpreadable([]), true, 'arrays are concat spreadable');
  938. t.end();
  939. });
  940. test('Invoke', function (t) {
  941. forEach(v.nonPropertyKeys, function (nonKey) {
  942. t['throws'](
  943. function () { ES.Invoke({}, nonKey); },
  944. TypeError,
  945. debug(nonKey) + ' is not a Property Key'
  946. );
  947. });
  948. t['throws'](function () { ES.Invoke({ o: false }, 'o'); }, TypeError, 'fails on a non-function');
  949. t.test('invoked callback', function (st) {
  950. var aValue = {};
  951. var bValue = {};
  952. var obj = {
  953. f: function (a) {
  954. st.equal(arguments.length, 2, '2 args passed');
  955. st.equal(a, aValue, 'first arg is correct');
  956. st.equal(arguments[1], bValue, 'second arg is correct');
  957. }
  958. };
  959. st.plan(3);
  960. ES.Invoke(obj, 'f', aValue, bValue);
  961. });
  962. t.end();
  963. });
  964. test('GetIterator', function (t) {
  965. var arr = [1, 2];
  966. testIterator(t, ES.GetIterator(arr), arr);
  967. testIterator(t, ES.GetIterator('abc'), 'abc'.split(''));
  968. t.test('Symbol.iterator', { skip: !v.hasSymbols }, function (st) {
  969. var m = new Map();
  970. m.set(1, 'a');
  971. m.set(2, 'b');
  972. testIterator(st, ES.GetIterator(m), [[1, 'a'], [2, 'b']]);
  973. st.end();
  974. });
  975. t.end();
  976. });
  977. test('IteratorNext', { skip: true });
  978. test('IteratorComplete', { skip: true });
  979. test('IteratorValue', { skip: true });
  980. test('IteratorStep', { skip: true });
  981. test('IteratorClose', { skip: true });
  982. test('CreateIterResultObject', function (t) {
  983. forEach(v.nonBooleans, function (nonBoolean) {
  984. t['throws'](
  985. function () { ES.CreateIterResultObject({}, nonBoolean); },
  986. TypeError,
  987. '"done" argument must be a boolean; ' + debug(nonBoolean) + ' is not'
  988. );
  989. });
  990. var value = {};
  991. t.deepEqual(
  992. ES.CreateIterResultObject(value, true),
  993. { value: value, done: true },
  994. 'creates a "done" iteration result'
  995. );
  996. t.deepEqual(
  997. ES.CreateIterResultObject(value, false),
  998. { value: value, done: false },
  999. 'creates a "not done" iteration result'
  1000. );
  1001. t.end();
  1002. });
  1003. test('RegExpExec', function (t) {
  1004. forEach(v.primitives, function (primitive) {
  1005. t['throws'](
  1006. function () { ES.RegExpExec(primitive); },
  1007. TypeError,
  1008. '"R" argument must be an object; ' + debug(primitive) + ' is not'
  1009. );
  1010. });
  1011. forEach(v.nonStrings, function (nonString) {
  1012. t['throws'](
  1013. function () { ES.RegExpExec({}, nonString); },
  1014. TypeError,
  1015. '"S" argument must be a String; ' + debug(nonString) + ' is not'
  1016. );
  1017. });
  1018. t.test('gets and calls a callable "exec"', function (st) {
  1019. var str = '123';
  1020. var o = {
  1021. exec: function (S) {
  1022. st.equal(this, o, '"exec" receiver is R');
  1023. st.equal(S, str, '"exec" argument is S');
  1024. return null;
  1025. }
  1026. };
  1027. st.plan(2);
  1028. ES.RegExpExec(o, str);
  1029. st.end();
  1030. });
  1031. t.test('throws if a callable "exec" returns a non-null non-object', function (st) {
  1032. var str = '123';
  1033. st.plan(v.nonNullPrimitives.length);
  1034. forEach(v.nonNullPrimitives, function (nonNullPrimitive) {
  1035. st['throws'](
  1036. function () { ES.RegExpExec({ exec: function () { return nonNullPrimitive; } }, str); },
  1037. TypeError,
  1038. '"exec" method must return `null` or an Object; ' + debug(nonNullPrimitive) + ' is not'
  1039. );
  1040. });
  1041. st.end();
  1042. });
  1043. t.test('actual regex that should match against a string', function (st) {
  1044. var S = 'aabc';
  1045. var R = /a/g;
  1046. var match1 = ES.RegExpExec(R, S);
  1047. var match2 = ES.RegExpExec(R, S);
  1048. var match3 = ES.RegExpExec(R, S);
  1049. st.deepEqual(match1, assign(['a'], groups({ index: 0, input: S })), 'match object 1 is as expected');
  1050. st.deepEqual(match2, assign(['a'], groups({ index: 1, input: S })), 'match object 2 is as expected');
  1051. st.equal(match3, null, 'match 3 is null as expected');
  1052. st.end();
  1053. });
  1054. t.test('actual regex that should match against a string, with shadowed "exec"', function (st) {
  1055. var S = 'aabc';
  1056. var R = /a/g;
  1057. R.exec = undefined;
  1058. var match1 = ES.RegExpExec(R, S);
  1059. var match2 = ES.RegExpExec(R, S);
  1060. var match3 = ES.RegExpExec(R, S);
  1061. st.deepEqual(match1, assign(['a'], groups({ index: 0, input: S })), 'match object 1 is as expected');
  1062. st.deepEqual(match2, assign(['a'], groups({ index: 1, input: S })), 'match object 2 is as expected');
  1063. st.equal(match3, null, 'match 3 is null as expected');
  1064. st.end();
  1065. });
  1066. t.end();
  1067. });
  1068. test('ArraySpeciesCreate', function (t) {
  1069. t.test('errors', function (st) {
  1070. var testNonNumber = function (nonNumber) {
  1071. st['throws'](
  1072. function () { ES.ArraySpeciesCreate([], nonNumber); },
  1073. TypeError,
  1074. debug(nonNumber) + ' is not a number'
  1075. );
  1076. };
  1077. forEach(v.nonNumbers, testNonNumber);
  1078. st['throws'](
  1079. function () { ES.ArraySpeciesCreate([], -1); },
  1080. TypeError,
  1081. '-1 is not >= 0'
  1082. );
  1083. st['throws'](
  1084. function () { ES.ArraySpeciesCreate([], -Infinity); },
  1085. TypeError,
  1086. '-Infinity is not >= 0'
  1087. );
  1088. var testNonIntegers = function (nonInteger) {
  1089. st['throws'](
  1090. function () { ES.ArraySpeciesCreate([], nonInteger); },
  1091. TypeError,
  1092. debug(nonInteger) + ' is not an integer'
  1093. );
  1094. };
  1095. forEach(v.nonIntegerNumbers, testNonIntegers);
  1096. st.end();
  1097. });
  1098. t.test('works with a non-array', function (st) {
  1099. forEach(v.objects.concat(v.primitives), function (nonArray) {
  1100. var arr = ES.ArraySpeciesCreate(nonArray, 0);
  1101. st.ok(ES.IsArray(arr), 'is an array');
  1102. st.equal(arr.length, 0, 'length is correct');
  1103. st.equal(arr.constructor, Array, 'constructor is correct');
  1104. });
  1105. st.end();
  1106. });
  1107. t.test('works with a normal array', function (st) {
  1108. var len = 2;
  1109. var orig = [1, 2, 3];
  1110. var arr = ES.ArraySpeciesCreate(orig, len);
  1111. st.ok(ES.IsArray(arr), 'is an array');
  1112. st.equal(arr.length, len, 'length is correct');
  1113. st.equal(arr.constructor, orig.constructor, 'constructor is correct');
  1114. st.end();
  1115. });
  1116. t.test('-0 length produces +0 length', function (st) {
  1117. var len = -0;
  1118. st.ok(is(len, -0), '-0 is negative zero');
  1119. st.notOk(is(len, 0), '-0 is not positive zero');
  1120. var orig = [1, 2, 3];
  1121. var arr = ES.ArraySpeciesCreate(orig, len);
  1122. st.equal(ES.IsArray(arr), true);
  1123. st.ok(is(arr.length, 0));
  1124. st.equal(arr.constructor, orig.constructor);
  1125. st.end();
  1126. });
  1127. t.test('works with species construtor', { skip: !hasSpecies }, function (st) {
  1128. var sentinel = {};
  1129. var Foo = function Foo(len) {
  1130. this.length = len;
  1131. this.sentinel = sentinel;
  1132. };
  1133. var Bar = getArraySubclassWithSpeciesConstructor(Foo);
  1134. var bar = new Bar();
  1135. t.equal(ES.IsArray(bar), true, 'Bar instance is an array');
  1136. var arr = ES.ArraySpeciesCreate(bar, 3);
  1137. st.equal(arr.constructor, Foo, 'result used species constructor');
  1138. st.equal(arr.length, 3, 'length property is correct');
  1139. st.equal(arr.sentinel, sentinel, 'Foo constructor was exercised');
  1140. st.end();
  1141. });
  1142. t.test('works with null species constructor', { skip: !hasSpecies }, function (st) {
  1143. var Bar = getArraySubclassWithSpeciesConstructor(null);
  1144. var bar = new Bar();
  1145. t.equal(ES.IsArray(bar), true, 'Bar instance is an array');
  1146. var arr = ES.ArraySpeciesCreate(bar, 3);
  1147. st.equal(arr.constructor, Array, 'result used default constructor');
  1148. st.equal(arr.length, 3, 'length property is correct');
  1149. st.end();
  1150. });
  1151. t.test('works with undefined species constructor', { skip: !hasSpecies }, function (st) {
  1152. var Bar = getArraySubclassWithSpeciesConstructor();
  1153. var bar = new Bar();
  1154. t.equal(ES.IsArray(bar), true, 'Bar instance is an array');
  1155. var arr = ES.ArraySpeciesCreate(bar, 3);
  1156. st.equal(arr.constructor, Array, 'result used default constructor');
  1157. st.equal(arr.length, 3, 'length property is correct');
  1158. st.end();
  1159. });
  1160. t.test('throws with object non-construtor species constructor', { skip: !hasSpecies }, function (st) {
  1161. forEach(v.objects, function (obj) {
  1162. var Bar = getArraySubclassWithSpeciesConstructor(obj);
  1163. var bar = new Bar();
  1164. st.equal(ES.IsArray(bar), true, 'Bar instance is an array');
  1165. st['throws'](
  1166. function () { ES.ArraySpeciesCreate(bar, 3); },
  1167. TypeError,
  1168. debug(obj) + ' is not a constructor'
  1169. );
  1170. });
  1171. st.end();
  1172. });
  1173. t.end();
  1174. });
  1175. test('CreateDataProperty', function (t) {
  1176. forEach(v.primitives, function (primitive) {
  1177. t['throws'](
  1178. function () { ES.CreateDataProperty(primitive); },
  1179. TypeError,
  1180. debug(primitive) + ' is not an object'
  1181. );
  1182. });
  1183. forEach(v.nonPropertyKeys, function (nonPropertyKey) {
  1184. t['throws'](
  1185. function () { ES.CreateDataProperty({}, nonPropertyKey); },
  1186. TypeError,
  1187. debug(nonPropertyKey) + ' is not a property key'
  1188. );
  1189. });
  1190. var sentinel = {};
  1191. forEach(v.propertyKeys, function (propertyKey) {
  1192. var obj = {};
  1193. var status = ES.CreateDataProperty(obj, propertyKey, sentinel);
  1194. t.equal(status, true, 'status is true');
  1195. t.equal(
  1196. obj[propertyKey],
  1197. sentinel,
  1198. debug(sentinel) + ' is installed on "' + debug(propertyKey) + '" on the object'
  1199. );
  1200. if (typeof Object.defineProperty === 'function') {
  1201. var nonWritable = Object.defineProperty({}, propertyKey, { configurable: true, writable: false });
  1202. var nonWritableStatus = ES.CreateDataProperty(nonWritable, propertyKey, sentinel);
  1203. t.equal(nonWritableStatus, false, 'create data property failed');
  1204. t.notEqual(
  1205. nonWritable[propertyKey],
  1206. sentinel,
  1207. debug(sentinel) + ' is not installed on "' + debug(propertyKey) + '" on the object when key is nonwritable'
  1208. );
  1209. var nonConfigurable = Object.defineProperty({}, propertyKey, { configurable: false, writable: true });
  1210. var nonConfigurableStatus = ES.CreateDataProperty(nonConfigurable, propertyKey, sentinel);
  1211. t.equal(nonConfigurableStatus, false, 'create data property failed');
  1212. t.notEqual(
  1213. nonConfigurable[propertyKey],
  1214. sentinel,
  1215. debug(sentinel) + ' is not installed on "' + debug(propertyKey) + '" on the object when key is nonconfigurable'
  1216. );
  1217. }
  1218. });
  1219. t.end();
  1220. });
  1221. test('CreateDataPropertyOrThrow', function (t) {
  1222. forEach(v.primitives, function (primitive) {
  1223. t['throws'](
  1224. function () { ES.CreateDataPropertyOrThrow(primitive); },
  1225. TypeError,
  1226. debug(primitive) + ' is not an object'
  1227. );
  1228. });
  1229. forEach(v.nonPropertyKeys, function (nonPropertyKey) {
  1230. t['throws'](
  1231. function () { ES.CreateDataPropertyOrThrow({}, nonPropertyKey); },
  1232. TypeError,
  1233. debug(nonPropertyKey) + ' is not a property key'
  1234. );
  1235. });
  1236. var sentinel = {};
  1237. forEach(v.propertyKeys, function (propertyKey) {
  1238. var obj = {};
  1239. var status = ES.CreateDataPropertyOrThrow(obj, propertyKey, sentinel);
  1240. t.equal(status, true, 'status is true');
  1241. t.equal(
  1242. obj[propertyKey],
  1243. sentinel,
  1244. debug(sentinel) + ' is installed on "' + debug(propertyKey) + '" on the object'
  1245. );
  1246. if (typeof Object.preventExtensions === 'function') {
  1247. var notExtensible = {};
  1248. Object.preventExtensions(notExtensible);
  1249. t['throws'](
  1250. function () { ES.CreateDataPropertyOrThrow(notExtensible, propertyKey, sentinel); },
  1251. TypeError,
  1252. 'can not install ' + debug(propertyKey) + ' on non-extensible object'
  1253. );
  1254. t.notEqual(
  1255. notExtensible[propertyKey],
  1256. sentinel,
  1257. debug(sentinel) + ' is not installed on "' + debug(propertyKey) + '" on the object'
  1258. );
  1259. }
  1260. });
  1261. t.end();
  1262. });
  1263. test('ObjectCreate', function (t) {
  1264. forEach(v.nonNullPrimitives, function (value) {
  1265. t['throws'](
  1266. function () { ES.ObjectCreate(value); },
  1267. TypeError,
  1268. debug(value) + ' is not null, or an object'
  1269. );
  1270. });
  1271. t.test('proto arg', function (st) {
  1272. var Parent = function Parent() {};
  1273. Parent.prototype.foo = {};
  1274. var child = ES.ObjectCreate(Parent.prototype);
  1275. st.equal(child instanceof Parent, true, 'child is instanceof Parent');
  1276. st.equal(child.foo, Parent.prototype.foo, 'child inherits properties from Parent.prototype');
  1277. st.end();
  1278. });
  1279. t.test('internal slots arg', function (st) {
  1280. st.doesNotThrow(function () { ES.ObjectCreate(null, []); }, 'an empty slot list is valid');
  1281. st['throws'](
  1282. function () { ES.ObjectCreate(null, ['a']); },
  1283. SyntaxError,
  1284. 'internal slots are not supported'
  1285. );
  1286. st.end();
  1287. });
  1288. t.test('null proto', { skip: !Object.create }, function (st) {
  1289. st.equal('toString' in {}, true, 'normal objects have toString');
  1290. st.equal('toString' in ES.ObjectCreate(null), false, 'makes a null object');
  1291. st.end();
  1292. });
  1293. t.test('null proto when no native Object.create', { skip: Object.create }, function (st) {
  1294. st['throws'](
  1295. function () { ES.ObjectCreate(null); },
  1296. SyntaxError,
  1297. 'without a native Object.create, can not create null objects'
  1298. );
  1299. st.end();
  1300. });
  1301. t.end();
  1302. });
  1303. test('AdvanceStringIndex', function (t) {
  1304. forEach(v.nonStrings, function (nonString) {
  1305. t['throws'](
  1306. function () { ES.AdvanceStringIndex(nonString); },
  1307. TypeError,
  1308. '"S" argument must be a String; ' + debug(nonString) + ' is not'
  1309. );
  1310. });
  1311. var notInts = v.nonNumbers.concat(
  1312. v.nonIntegerNumbers,
  1313. v.infinities,
  1314. [NaN, [], new Date(), Math.pow(2, 53), -1]
  1315. );
  1316. forEach(notInts, function (nonInt) {
  1317. t['throws'](
  1318. function () { ES.AdvanceStringIndex('abc', nonInt); },
  1319. TypeError,
  1320. '"index" argument must be an integer, ' + debug(nonInt) + ' is not.'
  1321. );
  1322. });
  1323. forEach(v.nonBooleans, function (nonBoolean) {
  1324. t['throws'](
  1325. function () { ES.AdvanceStringIndex('abc', 0, nonBoolean); },
  1326. TypeError,
  1327. debug(nonBoolean) + ' is not a Boolean'
  1328. );
  1329. });
  1330. var str = 'a\uD83D\uDCA9c';
  1331. t.test('non-unicode mode', function (st) {
  1332. for (var i = 0; i < str.length + 2; i += 1) {
  1333. st.equal(ES.AdvanceStringIndex(str, i, false), i + 1, i + ' advances to ' + (i + 1));
  1334. }
  1335. st.end();
  1336. });
  1337. t.test('unicode mode', function (st) {
  1338. st.equal(ES.AdvanceStringIndex(str, 0, true), 1, '0 advances to 1');
  1339. st.equal(ES.AdvanceStringIndex(str, 1, true), 3, '1 advances to 3');
  1340. st.equal(ES.AdvanceStringIndex(str, 2, true), 3, '2 advances to 3');
  1341. st.equal(ES.AdvanceStringIndex(str, 3, true), 4, '3 advances to 4');
  1342. st.equal(ES.AdvanceStringIndex(str, 4, true), 5, '4 advances to 5');
  1343. st.end();
  1344. });
  1345. t.test('lone surrogates', function (st) {
  1346. var halfPoo = 'a\uD83Dc';
  1347. st.equal(ES.AdvanceStringIndex(halfPoo, 0, true), 1, '0 advances to 1');
  1348. st.equal(ES.AdvanceStringIndex(halfPoo, 1, true), 2, '1 advances to 2');
  1349. st.equal(ES.AdvanceStringIndex(halfPoo, 2, true), 3, '2 advances to 3');
  1350. st.equal(ES.AdvanceStringIndex(halfPoo, 3, true), 4, '3 advances to 4');
  1351. st.end();
  1352. });
  1353. t.test('surrogate pairs', function (st) {
  1354. var lowestPair = String.fromCharCode('0xD800') + String.fromCharCode('0xDC00');
  1355. var highestPair = String.fromCharCode('0xDBFF') + String.fromCharCode('0xDFFF');
  1356. var poop = String.fromCharCode('0xD83D') + String.fromCharCode('0xDCA9');
  1357. st.equal(ES.AdvanceStringIndex(lowestPair, 0, true), 2, 'lowest surrogate pair, 0 -> 2');
  1358. st.equal(ES.AdvanceStringIndex(highestPair, 0, true), 2, 'highest surrogate pair, 0 -> 2');
  1359. st.equal(ES.AdvanceStringIndex(poop, 0, true), 2, 'poop, 0 -> 2');
  1360. st.end();
  1361. });
  1362. t.end();
  1363. });
  1364. test('CreateMethodProperty', function (t) {
  1365. forEach(v.primitives, function (primitive) {
  1366. t['throws'](
  1367. function () { ES.CreateMethodProperty(primitive, 'key'); },
  1368. TypeError,
  1369. 'O must be an Object'
  1370. );
  1371. });
  1372. forEach(v.nonPropertyKeys, function (nonPropertyKey) {
  1373. t['throws'](
  1374. function () { ES.CreateMethodProperty({}, nonPropertyKey); },
  1375. TypeError,
  1376. debug(nonPropertyKey) + ' is not a Property Key'
  1377. );
  1378. });
  1379. t.test('defines correctly', function (st) {
  1380. var obj = {};
  1381. var key = 'the key';
  1382. var value = { foo: 'bar' };
  1383. st.equal(ES.CreateMethodProperty(obj, key, value), true, 'defines property successfully');
  1384. st.test('property descriptor', { skip: !Object.getOwnPropertyDescriptor }, function (s2t) {
  1385. s2t.deepEqual(
  1386. Object.getOwnPropertyDescriptor(obj, key),
  1387. {
  1388. configurable: true,
  1389. enumerable: false,
  1390. value: value,
  1391. writable: true
  1392. },
  1393. 'sets the correct property descriptor'
  1394. );
  1395. s2t.end();
  1396. });
  1397. st.equal(obj[key], value, 'sets the correct value');
  1398. st.end();
  1399. });
  1400. t.test('fails as expected on a frozen object', { skip: !Object.freeze }, function (st) {
  1401. var obj = Object.freeze({ foo: 'bar' });
  1402. st['throws'](
  1403. function () { ES.CreateMethodProperty(obj, 'foo', { value: 'baz' }); },
  1404. TypeError,
  1405. 'nonconfigurable key can not be defined'
  1406. );
  1407. st.end();
  1408. });
  1409. var hasNonConfigurableFunctionName = !Object.getOwnPropertyDescriptor
  1410. || !Object.getOwnPropertyDescriptor(function () {}, 'name').configurable;
  1411. t.test('fails as expected on a function with a nonconfigurable name', { skip: !hasNonConfigurableFunctionName }, function (st) {
  1412. st['throws'](
  1413. function () { ES.CreateMethodProperty(function () {}, 'name', { value: 'baz' }); },
  1414. TypeError,
  1415. 'nonconfigurable function name can not be defined'
  1416. );
  1417. st.end();
  1418. });
  1419. t.end();
  1420. });
  1421. test('DefinePropertyOrThrow', function (t) {
  1422. forEach(v.primitives, function (primitive) {
  1423. t['throws'](
  1424. function () { ES.DefinePropertyOrThrow(primitive, 'key', {}); },
  1425. TypeError,
  1426. 'O must be an Object'
  1427. );
  1428. });
  1429. forEach(v.nonPropertyKeys, function (nonPropertyKey) {
  1430. t['throws'](
  1431. function () { ES.DefinePropertyOrThrow({}, nonPropertyKey, {}); },
  1432. TypeError,
  1433. debug(nonPropertyKey) + ' is not a Property Key'
  1434. );
  1435. });
  1436. t.test('defines correctly', function (st) {
  1437. var obj = {};
  1438. var key = 'the key';
  1439. var descriptor = {
  1440. configurable: true,
  1441. enumerable: false,
  1442. value: { foo: 'bar' },
  1443. writable: true
  1444. };
  1445. st.equal(ES.DefinePropertyOrThrow(obj, key, descriptor), true, 'defines property successfully');
  1446. st.test('property descriptor', { skip: !Object.getOwnPropertyDescriptor }, function (s2t) {
  1447. s2t.deepEqual(
  1448. Object.getOwnPropertyDescriptor(obj, key),
  1449. descriptor,
  1450. 'sets the correct property descriptor'
  1451. );
  1452. s2t.end();
  1453. });
  1454. st.deepEqual(obj[key], descriptor.value, 'sets the correct value');
  1455. st.end();
  1456. });
  1457. t.test('fails as expected on a frozen object', { skip: !Object.freeze }, function (st) {
  1458. var obj = Object.freeze({ foo: 'bar' });
  1459. st['throws'](
  1460. function () {
  1461. ES.DefinePropertyOrThrow(obj, 'foo', { configurable: true, value: 'baz' });
  1462. },
  1463. TypeError,
  1464. 'nonconfigurable key can not be defined'
  1465. );
  1466. st.end();
  1467. });
  1468. var hasNonConfigurableFunctionName = !Object.getOwnPropertyDescriptor
  1469. || !Object.getOwnPropertyDescriptor(function () {}, 'name').configurable;
  1470. t.test('fails as expected on a function with a nonconfigurable name', { skip: !hasNonConfigurableFunctionName }, function (st) {
  1471. st['throws'](
  1472. function () {
  1473. ES.DefinePropertyOrThrow(function () {}, 'name', { configurable: true, value: 'baz' });
  1474. },
  1475. TypeError,
  1476. 'nonconfigurable function name can not be defined'
  1477. );
  1478. st.end();
  1479. });
  1480. t.end();
  1481. });
  1482. test('DeletePropertyOrThrow', function (t) {
  1483. forEach(v.primitives, function (primitive) {
  1484. t['throws'](
  1485. function () { ES.DeletePropertyOrThrow(primitive, 'key', {}); },
  1486. TypeError,
  1487. 'O must be an Object'
  1488. );
  1489. });
  1490. forEach(v.nonPropertyKeys, function (nonPropertyKey) {
  1491. t['throws'](
  1492. function () { ES.DeletePropertyOrThrow({}, nonPropertyKey, {}); },
  1493. TypeError,
  1494. debug(nonPropertyKey) + ' is not a Property Key'
  1495. );
  1496. });
  1497. t.test('defines correctly', function (st) {
  1498. var obj = { 'the key': 42 };
  1499. var key = 'the key';
  1500. st.equal(ES.DeletePropertyOrThrow(obj, key), true, 'deletes property successfully');
  1501. st.equal(key in obj, false, 'key is no longer in the object');
  1502. st.end();
  1503. });
  1504. t.test('fails as expected on a frozen object', { skip: !Object.freeze }, function (st) {
  1505. var obj = Object.freeze({ foo: 'bar' });
  1506. st['throws'](
  1507. function () { ES.DeletePropertyOrThrow(obj, 'foo'); },
  1508. TypeError,
  1509. 'nonconfigurable key can not be deleted'
  1510. );
  1511. st.end();
  1512. });
  1513. var hasNonConfigurableFunctionName = !Object.getOwnPropertyDescriptor
  1514. || !Object.getOwnPropertyDescriptor(function () {}, 'name').configurable;
  1515. t.test('fails as expected on a function with a nonconfigurable name', { skip: !hasNonConfigurableFunctionName }, function (st) {
  1516. st['throws'](
  1517. function () { ES.DeletePropertyOrThrow(function () {}, 'name'); },
  1518. TypeError,
  1519. 'nonconfigurable function name can not be deleted'
  1520. );
  1521. st.end();
  1522. });
  1523. t.end();
  1524. });
  1525. test('EnumerableOwnNames', { skip: skips && skips.EnumerableOwnNames }, function (t) {
  1526. var obj = testEnumerableOwnNames(t, function (O) { return ES.EnumerableOwnNames(O); });
  1527. t.deepEqual(
  1528. ES.EnumerableOwnNames(obj),
  1529. ['own'],
  1530. 'returns enumerable own names'
  1531. );
  1532. t.end();
  1533. });
  1534. test('thisNumberValue', function (t) {
  1535. forEach(v.nonNumbers, function (nonNumber) {
  1536. t['throws'](
  1537. function () { ES.thisNumberValue(nonNumber); },
  1538. TypeError,
  1539. debug(nonNumber) + ' is not a Number'
  1540. );
  1541. });
  1542. forEach(v.numbers, function (number) {
  1543. t.equal(ES.thisNumberValue(number), number, debug(number) + ' is its own thisNumberValue');
  1544. var obj = Object(number);
  1545. t.equal(ES.thisNumberValue(obj), number, debug(obj) + ' is the boxed thisNumberValue');
  1546. });
  1547. t.end();
  1548. });
  1549. test('thisBooleanValue', function (t) {
  1550. forEach(v.nonBooleans, function (nonBoolean) {
  1551. t['throws'](
  1552. function () { ES.thisBooleanValue(nonBoolean); },
  1553. TypeError,
  1554. debug(nonBoolean) + ' is not a Boolean'
  1555. );
  1556. });
  1557. forEach(v.booleans, function (boolean) {
  1558. t.equal(ES.thisBooleanValue(boolean), boolean, debug(boolean) + ' is its own thisBooleanValue');
  1559. var obj = Object(boolean);
  1560. t.equal(ES.thisBooleanValue(obj), boolean, debug(obj) + ' is the boxed thisBooleanValue');
  1561. });
  1562. t.end();
  1563. });
  1564. test('thisStringValue', function (t) {
  1565. forEach(v.nonStrings, function (nonString) {
  1566. t['throws'](
  1567. function () { ES.thisStringValue(nonString); },
  1568. TypeError,
  1569. debug(nonString) + ' is not a String'
  1570. );
  1571. });
  1572. forEach(v.strings, function (string) {
  1573. t.equal(ES.thisStringValue(string), string, debug(string) + ' is its own thisStringValue');
  1574. var obj = Object(string);
  1575. t.equal(ES.thisStringValue(obj), string, debug(obj) + ' is the boxed thisStringValue');
  1576. });
  1577. t.end();
  1578. });
  1579. test('thisTimeValue', function (t) {
  1580. forEach(v.primitives.concat(v.objects), function (nonDate) {
  1581. t['throws'](
  1582. function () { ES.thisTimeValue(nonDate); },
  1583. TypeError,
  1584. debug(nonDate) + ' is not a Date'
  1585. );
  1586. });
  1587. forEach(v.timestamps, function (timestamp) {
  1588. var date = new Date(timestamp);
  1589. t.equal(ES.thisTimeValue(date), timestamp, debug(date) + ' is its own thisTimeValue');
  1590. });
  1591. t.end();
  1592. });
  1593. test('SetIntegrityLevel', function (t) {
  1594. forEach(v.primitives, function (primitive) {
  1595. t['throws'](
  1596. function () { ES.SetIntegrityLevel(primitive); },
  1597. TypeError,
  1598. debug(primitive) + ' is not an Object'
  1599. );
  1600. });
  1601. t['throws'](
  1602. function () { ES.SetIntegrityLevel({}); },
  1603. /^TypeError: Assertion failed: `level` must be `"sealed"` or `"frozen"`$/,
  1604. '`level` must be `"sealed"` or `"frozen"`'
  1605. );
  1606. var O = { a: 1 };
  1607. t.equal(ES.SetIntegrityLevel(O, 'sealed'), true);
  1608. t['throws'](
  1609. function () { O.b = 2; },
  1610. /^TypeError: (Cannot|Can't) add property b, object is not extensible$/,
  1611. 'sealing prevent new properties from being added'
  1612. );
  1613. O.a = 2;
  1614. t.equal(O.a, 2, 'pre-frozen, existing properties are mutable');
  1615. t.equal(ES.SetIntegrityLevel(O, 'frozen'), true);
  1616. t['throws'](
  1617. function () { O.a = 3; },
  1618. /^TypeError: Cannot assign to read only property 'a' of /,
  1619. 'freezing prevents existing properties from being mutated'
  1620. );
  1621. t.end();
  1622. });
  1623. test('TestIntegrityLevel', function (t) {
  1624. forEach(v.primitives, function (primitive) {
  1625. t['throws'](
  1626. function () { ES.TestIntegrityLevel(primitive); },
  1627. TypeError,
  1628. debug(primitive) + ' is not an Object'
  1629. );
  1630. });
  1631. t['throws'](
  1632. function () { ES.TestIntegrityLevel({ a: 1 }); },
  1633. /^TypeError: Assertion failed: `level` must be `"sealed"` or `"frozen"`$/,
  1634. '`level` must be `"sealed"` or `"frozen"`'
  1635. );
  1636. t.equal(ES.TestIntegrityLevel({ a: 1 }, 'sealed'), false, 'basic object is not sealed');
  1637. t.equal(ES.TestIntegrityLevel({ a: 1 }, 'frozen'), false, 'basic object is not frozen');
  1638. t.test('preventExtensions', { skip: !Object.preventExtensions }, function (st) {
  1639. var o = Object.preventExtensions({ a: 1 });
  1640. st.equal(ES.TestIntegrityLevel(o, 'sealed'), false, 'nonextensible object is not sealed');
  1641. st.equal(ES.TestIntegrityLevel(o, 'frozen'), false, 'nonextensible object is not frozen');
  1642. var empty = Object.preventExtensions({});
  1643. st.equal(ES.TestIntegrityLevel(empty, 'sealed'), true, 'empty nonextensible object is sealed');
  1644. st.equal(ES.TestIntegrityLevel(empty, 'frozen'), true, 'empty nonextensible object is frozen');
  1645. st.end();
  1646. });
  1647. t.test('seal', { skip: !Object.seal }, function (st) {
  1648. var o = Object.seal({ a: 1 });
  1649. st.equal(ES.TestIntegrityLevel(o, 'sealed'), true, 'sealed object is sealed');
  1650. st.equal(ES.TestIntegrityLevel(o, 'frozen'), false, 'sealed object is not frozen');
  1651. var empty = Object.seal({});
  1652. st.equal(ES.TestIntegrityLevel(empty, 'sealed'), true, 'empty sealed object is sealed');
  1653. st.equal(ES.TestIntegrityLevel(empty, 'frozen'), true, 'empty sealed object is frozen');
  1654. st.end();
  1655. });
  1656. t.test('freeze', { skip: !Object.freeze }, function (st) {
  1657. var o = Object.freeze({ a: 1 });
  1658. st.equal(ES.TestIntegrityLevel(o, 'sealed'), true, 'frozen object is sealed');
  1659. st.equal(ES.TestIntegrityLevel(o, 'frozen'), true, 'frozen object is frozen');
  1660. var empty = Object.freeze({});
  1661. st.equal(ES.TestIntegrityLevel(empty, 'sealed'), true, 'empty frozen object is sealed');
  1662. st.equal(ES.TestIntegrityLevel(empty, 'frozen'), true, 'empty frozen object is frozen');
  1663. st.end();
  1664. });
  1665. t.end();
  1666. });
  1667. test('OrdinaryHasInstance', function (t) {
  1668. forEach(v.nonFunctions, function (nonFunction) {
  1669. t.equal(ES.OrdinaryHasInstance(nonFunction, {}), false, debug(nonFunction) + ' is not callable');
  1670. });
  1671. forEach(v.primitives, function (primitive) {
  1672. t.equal(ES.OrdinaryHasInstance(function () {}, primitive), false, debug(primitive) + ' is not an object');
  1673. });
  1674. var C = function C() {};
  1675. var D = function D() {};
  1676. t.equal(ES.OrdinaryHasInstance(C, new C()), true, 'constructor function has an instance of itself');
  1677. t.equal(ES.OrdinaryHasInstance(C, new D()), false, 'constructor/instance mismatch is false');
  1678. t.equal(ES.OrdinaryHasInstance(D, new C()), false, 'instance/constructor mismatch is false');
  1679. t.equal(ES.OrdinaryHasInstance(C, {}), false, 'plain object is not an instance of a constructor');
  1680. t.equal(ES.OrdinaryHasInstance(Object, {}), true, 'plain object is an instance of Object');
  1681. t.end();
  1682. });
  1683. test('OrdinaryHasProperty', function (t) {
  1684. forEach(v.primitives, function (primitive) {
  1685. t['throws'](
  1686. function () { ES.OrdinaryHasProperty(primitive, ''); },
  1687. TypeError,
  1688. debug(primitive) + ' is not an object'
  1689. );
  1690. });
  1691. forEach(v.nonPropertyKeys, function (nonPropertyKey) {
  1692. t['throws'](
  1693. function () { ES.OrdinaryHasProperty({}, nonPropertyKey); },
  1694. TypeError,
  1695. 'P: ' + debug(nonPropertyKey) + ' is not a Property Key'
  1696. );
  1697. });
  1698. t.equal(ES.OrdinaryHasProperty({ a: 1 }, 'a'), true, 'own property is true');
  1699. t.equal(ES.OrdinaryHasProperty({}, 'toString'), true, 'inherited property is true');
  1700. t.equal(ES.OrdinaryHasProperty({}, 'nope'), false, 'absent property is false');
  1701. t.end();
  1702. });
  1703. test('InstanceofOperator', function (t) {
  1704. forEach(v.primitives, function (primitive) {
  1705. t['throws'](
  1706. function () { ES.InstanceofOperator(primitive, function () {}); },
  1707. TypeError,
  1708. debug(primitive) + ' is not an object'
  1709. );
  1710. });
  1711. forEach(v.nonFunctions, function (nonFunction) {
  1712. t['throws'](
  1713. function () { ES.InstanceofOperator({}, nonFunction); },
  1714. TypeError,
  1715. debug(nonFunction) + ' is not callable'
  1716. );
  1717. });
  1718. var C = function C() {};
  1719. var D = function D() {};
  1720. t.equal(ES.InstanceofOperator(new C(), C), true, 'constructor function has an instance of itself');
  1721. t.equal(ES.InstanceofOperator(new D(), C), false, 'constructor/instance mismatch is false');
  1722. t.equal(ES.InstanceofOperator(new C(), D), false, 'instance/constructor mismatch is false');
  1723. t.equal(ES.InstanceofOperator({}, C), false, 'plain object is not an instance of a constructor');
  1724. t.equal(ES.InstanceofOperator({}, Object), true, 'plain object is an instance of Object');
  1725. t.test('Symbol.hasInstance', { skip: !v.hasSymbols || !Symbol.hasInstance }, function (st) {
  1726. st.plan(4);
  1727. var O = {};
  1728. var C2 = function () {};
  1729. st.equal(ES.InstanceofOperator(O, C2), false, 'O is not an instance of C2');
  1730. Object.defineProperty(C2, Symbol.hasInstance, {
  1731. value: function (obj) {
  1732. st.equal(this, C2, 'hasInstance receiver is C2');
  1733. st.equal(obj, O, 'hasInstance argument is O');
  1734. return {}; // testing coercion to boolean
  1735. }
  1736. });
  1737. st.equal(ES.InstanceofOperator(O, C2), true, 'O is now an instance of C2');
  1738. st.end();
  1739. });
  1740. t.end();
  1741. });
  1742. test('Abstract Equality Comparison', function (t) {
  1743. t.test('same types use ===', function (st) {
  1744. forEach(v.primitives.concat(v.objects), function (value) {
  1745. st.equal(ES['Abstract Equality Comparison'](value, value), value === value, debug(value) + ' is abstractly equal to itself');
  1746. });
  1747. st.end();
  1748. });
  1749. t.test('different types coerce', function (st) {
  1750. var pairs = [
  1751. [null, undefined],
  1752. [3, '3'],
  1753. [true, '3'],
  1754. [true, 3],
  1755. [false, 0],
  1756. [false, '0'],
  1757. [3, [3]],
  1758. ['3', [3]],
  1759. [true, [1]],
  1760. [false, [0]],
  1761. [String(v.coercibleObject), v.coercibleObject],
  1762. [Number(String(v.coercibleObject)), v.coercibleObject],
  1763. [Number(v.coercibleObject), v.coercibleObject],
  1764. [String(Number(v.coercibleObject)), v.coercibleObject]
  1765. ];
  1766. forEach(pairs, function (pair) {
  1767. var a = pair[0];
  1768. var b = pair[1];
  1769. // eslint-disable-next-line eqeqeq
  1770. st.equal(ES['Abstract Equality Comparison'](a, b), a == b, debug(a) + ' == ' + debug(b));
  1771. // eslint-disable-next-line eqeqeq
  1772. st.equal(ES['Abstract Equality Comparison'](b, a), b == a, debug(b) + ' == ' + debug(a));
  1773. });
  1774. st.end();
  1775. });
  1776. t.end();
  1777. });
  1778. test('Strict Equality Comparison', function (t) {
  1779. t.test('same types use ===', function (st) {
  1780. forEach(v.primitives.concat(v.objects), function (value) {
  1781. st.equal(ES['Strict Equality Comparison'](value, value), value === value, debug(value) + ' is strictly equal to itself');
  1782. });
  1783. st.end();
  1784. });
  1785. t.test('different types are not ===', function (st) {
  1786. var pairs = [
  1787. [null, undefined],
  1788. [3, '3'],
  1789. [true, '3'],
  1790. [true, 3],
  1791. [false, 0],
  1792. [false, '0'],
  1793. [3, [3]],
  1794. ['3', [3]],
  1795. [true, [1]],
  1796. [false, [0]],
  1797. [String(v.coercibleObject), v.coercibleObject],
  1798. [Number(String(v.coercibleObject)), v.coercibleObject],
  1799. [Number(v.coercibleObject), v.coercibleObject],
  1800. [String(Number(v.coercibleObject)), v.coercibleObject]
  1801. ];
  1802. forEach(pairs, function (pair) {
  1803. var a = pair[0];
  1804. var b = pair[1];
  1805. st.equal(ES['Strict Equality Comparison'](a, b), a === b, debug(a) + ' === ' + debug(b));
  1806. st.equal(ES['Strict Equality Comparison'](b, a), b === a, debug(b) + ' === ' + debug(a));
  1807. });
  1808. st.end();
  1809. });
  1810. t.end();
  1811. });
  1812. test('Abstract Relational Comparison', function (t) {
  1813. t.test('at least one operand is NaN', function (st) {
  1814. st.equal(ES['Abstract Relational Comparison'](NaN, {}, true), undefined, 'LeftFirst: first is NaN, returns undefined');
  1815. st.equal(ES['Abstract Relational Comparison']({}, NaN, true), undefined, 'LeftFirst: second is NaN, returns undefined');
  1816. st.equal(ES['Abstract Relational Comparison'](NaN, {}, false), undefined, '!LeftFirst: first is NaN, returns undefined');
  1817. st.equal(ES['Abstract Relational Comparison']({}, NaN, false), undefined, '!LeftFirst: second is NaN, returns undefined');
  1818. st.end();
  1819. });
  1820. t.equal(ES['Abstract Relational Comparison'](3, 4, true), true, 'LeftFirst: 3 is less than 4');
  1821. t.equal(ES['Abstract Relational Comparison'](4, 3, true), false, 'LeftFirst: 3 is not less than 4');
  1822. t.equal(ES['Abstract Relational Comparison'](3, 4, false), true, '!LeftFirst: 3 is less than 4');
  1823. t.equal(ES['Abstract Relational Comparison'](4, 3, false), false, '!LeftFirst: 3 is not less than 4');
  1824. t.equal(ES['Abstract Relational Comparison']('3', '4', true), true, 'LeftFirst: "3" is less than "4"');
  1825. t.equal(ES['Abstract Relational Comparison']('4', '3', true), false, 'LeftFirst: "3" is not less than "4"');
  1826. t.equal(ES['Abstract Relational Comparison']('3', '4', false), true, '!LeftFirst: "3" is less than "4"');
  1827. t.equal(ES['Abstract Relational Comparison']('4', '3', false), false, '!LeftFirst: "3" is not less than "4"');
  1828. t.equal(ES['Abstract Relational Comparison'](v.coercibleObject, 42, true), true, 'LeftFirst: coercible object is less than 42');
  1829. t.equal(ES['Abstract Relational Comparison'](42, v.coercibleObject, true), false, 'LeftFirst: 42 is not less than coercible object');
  1830. t.equal(ES['Abstract Relational Comparison'](v.coercibleObject, 42, false), true, '!LeftFirst: coercible object is less than 42');
  1831. t.equal(ES['Abstract Relational Comparison'](42, v.coercibleObject, false), false, '!LeftFirst: 42 is not less than coercible object');
  1832. t.equal(ES['Abstract Relational Comparison'](v.coercibleObject, '3', true), false, 'LeftFirst: coercible object is not less than "3"');
  1833. t.equal(ES['Abstract Relational Comparison']('3', v.coercibleObject, true), false, 'LeftFirst: "3" is not less than coercible object');
  1834. t.equal(ES['Abstract Relational Comparison'](v.coercibleObject, '3', false), false, '!LeftFirst: coercible object is not less than "3"');
  1835. t.equal(ES['Abstract Relational Comparison']('3', v.coercibleObject, false), false, '!LeftFirst: "3" is not less than coercible object');
  1836. t.end();
  1837. });
  1838. test('ValidateAndApplyPropertyDescriptor', function (t) {
  1839. forEach(v.nonUndefinedPrimitives, function (nonUndefinedPrimitive) {
  1840. t['throws'](
  1841. function () { ES.ValidateAndApplyPropertyDescriptor(nonUndefinedPrimitive, '', false, v.genericDescriptor(), v.genericDescriptor()); },
  1842. TypeError,
  1843. 'O: ' + debug(nonUndefinedPrimitive) + ' is not undefined or an Object'
  1844. );
  1845. });
  1846. forEach(v.nonBooleans, function (nonBoolean) {
  1847. t['throws'](
  1848. function () {
  1849. return ES.ValidateAndApplyPropertyDescriptor(
  1850. undefined,
  1851. null,
  1852. nonBoolean,
  1853. v.genericDescriptor(),
  1854. v.genericDescriptor()
  1855. );
  1856. },
  1857. TypeError,
  1858. 'extensible: ' + debug(nonBoolean) + ' is not a Boolean'
  1859. );
  1860. });
  1861. forEach(v.primitives, function (primitive) {
  1862. // Desc must be a Property Descriptor
  1863. t['throws'](
  1864. function () {
  1865. return ES.ValidateAndApplyPropertyDescriptor(
  1866. undefined,
  1867. null,
  1868. false,
  1869. primitive,
  1870. v.genericDescriptor()
  1871. );
  1872. },
  1873. TypeError,
  1874. 'Desc: ' + debug(primitive) + ' is not a Property Descriptor'
  1875. );
  1876. });
  1877. forEach(v.nonUndefinedPrimitives, function (primitive) {
  1878. // current must be undefined or a Property Descriptor
  1879. t['throws'](
  1880. function () {
  1881. return ES.ValidateAndApplyPropertyDescriptor(
  1882. undefined,
  1883. null,
  1884. false,
  1885. v.genericDescriptor(),
  1886. primitive
  1887. );
  1888. },
  1889. TypeError,
  1890. 'current: ' + debug(primitive) + ' is not a Property Descriptor or undefined'
  1891. );
  1892. });
  1893. forEach(v.nonPropertyKeys, function (nonPropertyKey) {
  1894. // if O is an object, P must be a property key
  1895. t['throws'](
  1896. function () {
  1897. return ES.ValidateAndApplyPropertyDescriptor(
  1898. {},
  1899. nonPropertyKey,
  1900. false,
  1901. v.genericDescriptor(),
  1902. v.genericDescriptor()
  1903. );
  1904. },
  1905. TypeError,
  1906. 'P: ' + debug(nonPropertyKey) + ' is not a Property Key'
  1907. );
  1908. });
  1909. t.test('current is undefined', function (st) {
  1910. var propertyKey = 'howdy';
  1911. st.test('generic descriptor', function (s2t) {
  1912. var generic = v.genericDescriptor();
  1913. generic['[[Enumerable]]'] = true;
  1914. var O = {};
  1915. ES.ValidateAndApplyPropertyDescriptor(undefined, propertyKey, true, generic);
  1916. s2t.equal(
  1917. ES.ValidateAndApplyPropertyDescriptor(O, propertyKey, false, generic),
  1918. false,
  1919. 'when extensible is false, nothing happens'
  1920. );
  1921. s2t.deepEqual(O, {}, 'no changes applied when O is undefined or extensible is false');
  1922. s2t.equal(
  1923. ES.ValidateAndApplyPropertyDescriptor(O, propertyKey, true, generic),
  1924. true,
  1925. 'operation is successful'
  1926. );
  1927. var expected = {};
  1928. expected[propertyKey] = undefined;
  1929. s2t.deepEqual(O, expected, 'generic descriptor has been defined as an own data property');
  1930. s2t.end();
  1931. });
  1932. st.test('data descriptor', function (s2t) {
  1933. var data = v.dataDescriptor();
  1934. data['[[Enumerable]]'] = true;
  1935. var O = {};
  1936. ES.ValidateAndApplyPropertyDescriptor(undefined, propertyKey, true, data);
  1937. s2t.equal(
  1938. ES.ValidateAndApplyPropertyDescriptor(O, propertyKey, false, data),
  1939. false,
  1940. 'when extensible is false, nothing happens'
  1941. );
  1942. s2t.deepEqual(O, {}, 'no changes applied when O is undefined or extensible is false');
  1943. s2t.equal(
  1944. ES.ValidateAndApplyPropertyDescriptor(O, propertyKey, true, data),
  1945. true,
  1946. 'operation is successful'
  1947. );
  1948. var expected = {};
  1949. expected[propertyKey] = data['[[Value]]'];
  1950. s2t.deepEqual(O, expected, 'data descriptor has been defined as an own data property');
  1951. s2t.end();
  1952. });
  1953. st.test('accessor descriptor', function (s2t) {
  1954. var count = 0;
  1955. var accessor = v.accessorDescriptor();
  1956. accessor['[[Enumerable]]'] = true;
  1957. accessor['[[Get]]'] = function () {
  1958. count += 1;
  1959. return count;
  1960. };
  1961. var O = {};
  1962. ES.ValidateAndApplyPropertyDescriptor(undefined, propertyKey, true, accessor);
  1963. s2t.equal(
  1964. ES.ValidateAndApplyPropertyDescriptor(O, propertyKey, false, accessor),
  1965. false,
  1966. 'when extensible is false, nothing happens'
  1967. );
  1968. s2t.deepEqual(O, {}, 'no changes applied when O is undefined or extensible is false');
  1969. s2t.equal(
  1970. ES.ValidateAndApplyPropertyDescriptor(O, propertyKey, true, accessor),
  1971. true,
  1972. 'operation is successful'
  1973. );
  1974. var expected = {};
  1975. expected[propertyKey] = accessor['[[Get]]']() + 1;
  1976. s2t.deepEqual(O, expected, 'accessor descriptor has been defined as an own accessor property');
  1977. s2t.end();
  1978. });
  1979. st.end();
  1980. });
  1981. t.test('every field in Desc is absent', { skip: 'it is unclear if having no fields qualifies Desc to be a Property Descriptor' });
  1982. forEach([v.dataDescriptor, v.accessorDescriptor, v.mutatorDescriptor], function (getDescriptor) {
  1983. t.equal(
  1984. ES.ValidateAndApplyPropertyDescriptor(undefined, 'property key', true, getDescriptor(), getDescriptor()),
  1985. true,
  1986. 'when Desc and current are the same, early return true'
  1987. );
  1988. });
  1989. t.test('current is nonconfigurable', function (st) {
  1990. // note: these must not be generic descriptors, or else the algorithm returns an early true
  1991. st.equal(
  1992. ES.ValidateAndApplyPropertyDescriptor(
  1993. undefined,
  1994. 'property key',
  1995. true,
  1996. v.descriptors.configurable(v.dataDescriptor()),
  1997. v.descriptors.nonConfigurable(v.dataDescriptor())
  1998. ),
  1999. false,
  2000. 'false if Desc is configurable'
  2001. );
  2002. st.equal(
  2003. ES.ValidateAndApplyPropertyDescriptor(
  2004. undefined,
  2005. 'property key',
  2006. true,
  2007. v.descriptors.enumerable(v.dataDescriptor()),
  2008. v.descriptors.nonEnumerable(v.dataDescriptor())
  2009. ),
  2010. false,
  2011. 'false if Desc is Enumerable and current is not'
  2012. );
  2013. st.equal(
  2014. ES.ValidateAndApplyPropertyDescriptor(
  2015. undefined,
  2016. 'property key',
  2017. true,
  2018. v.descriptors.nonEnumerable(v.dataDescriptor()),
  2019. v.descriptors.enumerable(v.dataDescriptor())
  2020. ),
  2021. false,
  2022. 'false if Desc is not Enumerable and current is'
  2023. );
  2024. var descLackingEnumerable = v.accessorDescriptor();
  2025. delete descLackingEnumerable['[[Enumerable]]'];
  2026. st.equal(
  2027. ES.ValidateAndApplyPropertyDescriptor(
  2028. undefined,
  2029. 'property key',
  2030. true,
  2031. descLackingEnumerable,
  2032. v.descriptors.enumerable(v.accessorDescriptor())
  2033. ),
  2034. true,
  2035. 'not false if Desc lacks Enumerable'
  2036. );
  2037. st.end();
  2038. });
  2039. t.test('Desc and current: one is a data descriptor, one is not', { skip: !Object.defineProperty }, function (st) {
  2040. // note: Desc must be configurable if current is nonconfigurable, to hit this branch
  2041. st.equal(
  2042. ES.ValidateAndApplyPropertyDescriptor(
  2043. undefined,
  2044. 'property key',
  2045. true,
  2046. v.descriptors.configurable(v.accessorDescriptor()),
  2047. v.descriptors.nonConfigurable(v.dataDescriptor())
  2048. ),
  2049. false,
  2050. 'false if current (data) is nonconfigurable'
  2051. );
  2052. st.equal(
  2053. ES.ValidateAndApplyPropertyDescriptor(
  2054. undefined,
  2055. 'property key',
  2056. true,
  2057. v.descriptors.configurable(v.dataDescriptor()),
  2058. v.descriptors.nonConfigurable(v.accessorDescriptor())
  2059. ),
  2060. false,
  2061. 'false if current (not data) is nonconfigurable'
  2062. );
  2063. // one is data and one is not,
  2064. // // if current is data, convert to accessor
  2065. // // else convert to data
  2066. var startsWithData = {
  2067. 'property key': 42
  2068. };
  2069. st.equal(
  2070. ES.ValidateAndApplyPropertyDescriptor(
  2071. startsWithData,
  2072. 'property key',
  2073. true,
  2074. v.descriptors.enumerable(v.descriptors.configurable(v.accessorDescriptor())),
  2075. v.descriptors.enumerable(v.descriptors.configurable(v.dataDescriptor()))
  2076. ),
  2077. true,
  2078. 'operation is successful: current is data, Desc is accessor'
  2079. );
  2080. var shouldBeAccessor = Object.getOwnPropertyDescriptor(startsWithData, 'property key');
  2081. st.equal(typeof shouldBeAccessor.get, 'function', 'has a getter');
  2082. var key = 'property key';
  2083. var startsWithAccessor = {};
  2084. Object.defineProperty(startsWithAccessor, key, {
  2085. configurable: true,
  2086. enumerable: true,
  2087. get: function get() { return 42; }
  2088. });
  2089. st.equal(
  2090. ES.ValidateAndApplyPropertyDescriptor(
  2091. startsWithAccessor,
  2092. key,
  2093. true,
  2094. v.descriptors.enumerable(v.descriptors.configurable(v.dataDescriptor())),
  2095. v.descriptors.enumerable(v.descriptors.configurable(v.accessorDescriptor(42)))
  2096. ),
  2097. true,
  2098. 'operation is successful: current is accessor, Desc is data'
  2099. );
  2100. var shouldBeData = Object.getOwnPropertyDescriptor(startsWithAccessor, 'property key');
  2101. st.deepEqual(shouldBeData, { configurable: true, enumerable: true, value: 42, writable: false }, 'is a data property');
  2102. st.end();
  2103. });
  2104. t.test('Desc and current are both data descriptors', function (st) {
  2105. st.equal(
  2106. ES.ValidateAndApplyPropertyDescriptor(
  2107. undefined,
  2108. 'property key',
  2109. true,
  2110. v.descriptors.writable(v.dataDescriptor()),
  2111. v.descriptors.nonWritable(v.descriptors.nonConfigurable(v.dataDescriptor()))
  2112. ),
  2113. false,
  2114. 'false if frozen current and writable Desc'
  2115. );
  2116. st.equal(
  2117. ES.ValidateAndApplyPropertyDescriptor(
  2118. undefined,
  2119. 'property key',
  2120. true,
  2121. v.descriptors.configurable({ '[[Value]]': 42 }),
  2122. v.descriptors.nonWritable({ '[[Value]]': 7 })
  2123. ),
  2124. false,
  2125. 'false if nonwritable current has a different value than Desc'
  2126. );
  2127. st.end();
  2128. });
  2129. t.test('current is nonconfigurable; Desc and current are both accessor descriptors', function (st) {
  2130. st.equal(
  2131. ES.ValidateAndApplyPropertyDescriptor(
  2132. undefined,
  2133. 'property key',
  2134. true,
  2135. v.mutatorDescriptor(),
  2136. v.descriptors.nonConfigurable(v.mutatorDescriptor())
  2137. ),
  2138. false,
  2139. 'false if both Sets are not equal'
  2140. );
  2141. st.equal(
  2142. ES.ValidateAndApplyPropertyDescriptor(
  2143. undefined,
  2144. 'property key',
  2145. true,
  2146. v.accessorDescriptor(),
  2147. v.descriptors.nonConfigurable(v.accessorDescriptor())
  2148. ),
  2149. false,
  2150. 'false if both Gets are not equal'
  2151. );
  2152. st.end();
  2153. });
  2154. t.end();
  2155. });
  2156. test('OrdinaryGetOwnProperty', function (t) {
  2157. forEach(v.primitives, function (primitive) {
  2158. t['throws'](
  2159. function () { ES.OrdinaryGetOwnProperty(primitive, ''); },
  2160. TypeError,
  2161. 'O: ' + debug(primitive) + ' is not an Object'
  2162. );
  2163. });
  2164. forEach(v.nonPropertyKeys, function (nonPropertyKey) {
  2165. t['throws'](
  2166. function () { ES.OrdinaryGetOwnProperty({}, nonPropertyKey); },
  2167. TypeError,
  2168. 'P: ' + debug(nonPropertyKey) + ' is not a Property Key'
  2169. );
  2170. });
  2171. t.equal(ES.OrdinaryGetOwnProperty({}, 'not in the object'), undefined, 'missing property yields undefined');
  2172. t.equal(ES.OrdinaryGetOwnProperty({}, 'toString'), undefined, 'inherited non-own property yields undefined');
  2173. t.deepEqual(
  2174. ES.OrdinaryGetOwnProperty({ a: 1 }, 'a'),
  2175. ES.ToPropertyDescriptor({
  2176. configurable: true,
  2177. enumerable: true,
  2178. value: 1,
  2179. writable: true
  2180. }),
  2181. 'own assigned data property yields expected descriptor'
  2182. );
  2183. t.deepEqual(
  2184. ES.OrdinaryGetOwnProperty(/a/, 'lastIndex'),
  2185. ES.ToPropertyDescriptor({
  2186. configurable: false,
  2187. enumerable: false,
  2188. value: 0,
  2189. writable: true
  2190. }),
  2191. 'regex lastIndex yields expected descriptor'
  2192. );
  2193. t.deepEqual(
  2194. ES.OrdinaryGetOwnProperty([], 'length'),
  2195. ES.ToPropertyDescriptor({
  2196. configurable: false,
  2197. enumerable: false,
  2198. value: 0,
  2199. writable: true
  2200. }),
  2201. 'array length yields expected descriptor'
  2202. );
  2203. t.deepEqual(
  2204. ES.OrdinaryGetOwnProperty(Object.prototype, 'toString'),
  2205. ES.ToPropertyDescriptor({
  2206. configurable: true,
  2207. enumerable: false,
  2208. value: Object.prototype.toString,
  2209. writable: true
  2210. }),
  2211. 'own non-enumerable data property yields expected descriptor'
  2212. );
  2213. t.test('ES5+', { skip: !Object.defineProperty }, function (st) {
  2214. var O = {};
  2215. Object.defineProperty(O, 'foo', {
  2216. configurable: false,
  2217. enumerable: false,
  2218. value: O,
  2219. writable: true
  2220. });
  2221. t.deepEqual(
  2222. ES.OrdinaryGetOwnProperty(O, 'foo'),
  2223. ES.ToPropertyDescriptor({
  2224. configurable: false,
  2225. enumerable: false,
  2226. value: O,
  2227. writable: true
  2228. }),
  2229. 'defined own property yields expected descriptor'
  2230. );
  2231. st.end();
  2232. });
  2233. t.end();
  2234. });
  2235. test('OrdinaryDefineOwnProperty', { skip: !Object.defineProperty }, function (t) {
  2236. forEach(v.primitives, function (primitive) {
  2237. t['throws'](
  2238. function () { ES.CopyDataProperties(primitive, {}, []); },
  2239. TypeError,
  2240. 'O: ' + debug(primitive) + ' is not an Object'
  2241. );
  2242. });
  2243. forEach(v.nonPropertyKeys, function (nonPropertyKey) {
  2244. t['throws'](
  2245. function () { ES.OrdinaryDefineOwnProperty({}, nonPropertyKey, v.genericDescriptor()); },
  2246. TypeError,
  2247. 'P: ' + debug(nonPropertyKey) + ' is not a Property Key'
  2248. );
  2249. });
  2250. forEach(v.primitives, function (primitive) {
  2251. t['throws'](
  2252. function () { ES.OrdinaryDefineOwnProperty(primitive, '', v.genericDescriptor()); },
  2253. TypeError,
  2254. 'Desc: ' + debug(primitive) + ' is not a Property Descriptor'
  2255. );
  2256. });
  2257. var O = {};
  2258. var P = 'property key';
  2259. var Desc = v.accessorDescriptor();
  2260. t.equal(
  2261. ES.OrdinaryDefineOwnProperty(O, P, Desc),
  2262. true,
  2263. 'operation is successful'
  2264. );
  2265. t.deepEqual(
  2266. Object.getOwnPropertyDescriptor(O, P),
  2267. ES.FromPropertyDescriptor(ES.CompletePropertyDescriptor(Desc)),
  2268. 'expected property descriptor is defined'
  2269. );
  2270. t.end();
  2271. });
  2272. test('ArrayCreate', function (t) {
  2273. forEach(v.nonIntegerNumbers.concat([-1]), function (nonIntegerNumber) {
  2274. t['throws'](
  2275. function () { ES.ArrayCreate(nonIntegerNumber); },
  2276. TypeError,
  2277. 'length must be an integer number >= 0'
  2278. );
  2279. });
  2280. t['throws'](
  2281. function () { ES.ArrayCreate(Math.pow(2, 32)); },
  2282. RangeError,
  2283. 'length must be < 2**32'
  2284. );
  2285. t.deepEqual(ES.ArrayCreate(-0), [], 'length of -0 creates an empty array');
  2286. t.deepEqual(ES.ArrayCreate(0), [], 'length of +0 creates an empty array');
  2287. // eslint-disable-next-line no-sparse-arrays, comma-spacing
  2288. t.deepEqual(ES.ArrayCreate(1), [,], 'length of 1 creates a sparse array of length 1');
  2289. // eslint-disable-next-line no-sparse-arrays, comma-spacing
  2290. t.deepEqual(ES.ArrayCreate(2), [,,], 'length of 2 creates a sparse array of length 2');
  2291. // eslint-disable-next-line no-proto
  2292. t.test('proto argument', { skip: [].__proto__ !== Array.prototype }, function (st) {
  2293. var fakeProto = {
  2294. push: { toString: function () { return 'not array push'; } }
  2295. };
  2296. st.equal(ES.ArrayCreate(0, fakeProto).push, fakeProto.push, 'passing the proto argument works');
  2297. st.end();
  2298. });
  2299. t.end();
  2300. });
  2301. test('ArraySetLength', function (t) {
  2302. forEach(v.primitives.concat(v.objects), function (nonArray) {
  2303. t['throws'](
  2304. function () { ES.ArraySetLength(nonArray, 0); },
  2305. TypeError,
  2306. 'A: ' + debug(nonArray) + ' is not an Array'
  2307. );
  2308. });
  2309. forEach(v.nonUndefinedPrimitives, function (primitive) {
  2310. t['throws'](
  2311. function () { ES.ArraySetLength([], primitive); },
  2312. TypeError,
  2313. 'Desc: ' + debug(primitive) + ' is not a Property Descriptor'
  2314. );
  2315. });
  2316. t.test('making length nonwritable', { skip: !Object.defineProperty }, function (st) {
  2317. var a = [];
  2318. ES.ArraySetLength(a, { '[[Writable]]': false });
  2319. st.deepEqual(
  2320. Object.getOwnPropertyDescriptor(a, 'length'),
  2321. {
  2322. configurable: false,
  2323. enumerable: false,
  2324. value: 0,
  2325. writable: false
  2326. },
  2327. 'without a value, length becomes nonwritable'
  2328. );
  2329. st.end();
  2330. });
  2331. var arr = [];
  2332. ES.ArraySetLength(arr, { '[[Value]]': 7 });
  2333. t.equal(arr.length, 7, 'array now has a length of 7');
  2334. t.end();
  2335. });
  2336. test('CreateHTML', function (t) {
  2337. forEach(v.nonStrings, function (nonString) {
  2338. t['throws'](
  2339. function () { ES.CreateHTML('', nonString, '', ''); },
  2340. TypeError,
  2341. 'tag: ' + debug(nonString) + ' is not a String'
  2342. );
  2343. t['throws'](
  2344. function () { ES.CreateHTML('', '', nonString, ''); },
  2345. TypeError,
  2346. 'attribute: ' + debug(nonString) + ' is not a String'
  2347. );
  2348. });
  2349. t.equal(
  2350. ES.CreateHTML(
  2351. { toString: function () { return 'the string'; } },
  2352. 'some HTML tag!',
  2353. ''
  2354. ),
  2355. '<some HTML tag!>the string</some HTML tag!>',
  2356. 'works with an empty string attribute value'
  2357. );
  2358. t.equal(
  2359. ES.CreateHTML(
  2360. { toString: function () { return 'the string'; } },
  2361. 'some HTML tag!',
  2362. 'attr',
  2363. 'value "with quotes"'
  2364. ),
  2365. '<some HTML tag! attr="value &quot;with quotes&quot;">the string</some HTML tag!>',
  2366. 'works with an attribute, and a value with quotes'
  2367. );
  2368. t.end();
  2369. });
  2370. test('GetOwnPropertyKeys', function (t) {
  2371. forEach(v.primitives, function (primitive) {
  2372. t['throws'](
  2373. function () { ES.GetOwnPropertyKeys(primitive, 'String'); },
  2374. TypeError,
  2375. 'O: ' + debug(primitive) + ' is not an Object'
  2376. );
  2377. });
  2378. t['throws'](
  2379. function () { ES.GetOwnPropertyKeys({}, 'not string or symbol'); },
  2380. TypeError,
  2381. 'Type: must be "String" or "Symbol"'
  2382. );
  2383. t.test('Symbols', { skip: !v.hasSymbols }, function (st) {
  2384. var O = { a: 1 };
  2385. O[Symbol.iterator] = true;
  2386. var s = Symbol('test');
  2387. Object.defineProperty(O, s, { enumerable: false, value: true });
  2388. st.deepEqual(
  2389. ES.GetOwnPropertyKeys(O, 'Symbol'),
  2390. [Symbol.iterator, s],
  2391. 'works with Symbols, enumerable or not'
  2392. );
  2393. st.end();
  2394. });
  2395. t.test('non-enumerable names', { skip: !Object.defineProperty }, function (st) {
  2396. var O = { a: 1 };
  2397. Object.defineProperty(O, 'b', { enumerable: false, value: 2 });
  2398. if (v.hasSymbols) {
  2399. O[Symbol.iterator] = true;
  2400. }
  2401. st.deepEqual(
  2402. ES.GetOwnPropertyKeys(O, 'String').sort(),
  2403. ['a', 'b'].sort(),
  2404. 'works with Strings, enumerable or not'
  2405. );
  2406. st.end();
  2407. });
  2408. t.deepEqual(
  2409. ES.GetOwnPropertyKeys({ a: 1, b: 2 }, 'String').sort(),
  2410. ['a', 'b'].sort(),
  2411. 'works with enumerable keys'
  2412. );
  2413. t.end();
  2414. });
  2415. test('SymbolDescriptiveString', function (t) {
  2416. forEach(v.nonSymbolPrimitives.concat(v.objects), function (nonSymbol) {
  2417. t['throws'](
  2418. function () { ES.SymbolDescriptiveString(nonSymbol); },
  2419. TypeError,
  2420. debug(nonSymbol) + ' is not a Symbol'
  2421. );
  2422. });
  2423. t.test('Symbols', { skip: !v.hasSymbols }, function (st) {
  2424. st.equal(ES.SymbolDescriptiveString(Symbol()), 'Symbol()', 'undefined description');
  2425. st.equal(ES.SymbolDescriptiveString(Symbol('')), 'Symbol()', 'empty string description');
  2426. st.equal(ES.SymbolDescriptiveString(Symbol.iterator), 'Symbol(Symbol.iterator)', 'well-known symbol');
  2427. st.equal(ES.SymbolDescriptiveString(Symbol('foo')), 'Symbol(foo)', 'string description');
  2428. st.end();
  2429. });
  2430. t.end();
  2431. });
  2432. test('GetSubstitution', { skip: skips && skips.GetSubstitution }, function (t) {
  2433. forEach(v.nonStrings, function (nonString) {
  2434. t['throws'](
  2435. function () { ES.GetSubstitution(nonString, '', 0, [], ''); },
  2436. TypeError,
  2437. '`matched`: ' + debug(nonString) + ' is not a String'
  2438. );
  2439. t['throws'](
  2440. function () { ES.GetSubstitution('', nonString, 0, [], ''); },
  2441. TypeError,
  2442. '`str`: ' + debug(nonString) + ' is not a String'
  2443. );
  2444. t['throws'](
  2445. function () { ES.GetSubstitution('', '', 0, [], nonString); },
  2446. TypeError,
  2447. '`replacement`: ' + debug(nonString) + ' is not a String'
  2448. );
  2449. t['throws'](
  2450. function () { ES.GetSubstitution('', '', 0, [nonString], ''); },
  2451. TypeError,
  2452. '`captures`: ' + debug([nonString]) + ' is not an Array of strings'
  2453. );
  2454. });
  2455. forEach(v.nonIntegerNumbers.concat([-1, -42, -Infinity]), function (nonNonNegativeInteger) {
  2456. t['throws'](
  2457. function () { ES.GetSubstitution('', '', nonNonNegativeInteger, [], ''); },
  2458. TypeError,
  2459. '`position`: ' + debug(nonNonNegativeInteger) + ' is not a non-negative integer'
  2460. );
  2461. });
  2462. forEach(v.nonArrays, function (nonArray) {
  2463. t['throws'](
  2464. function () { ES.GetSubstitution('', '', 0, nonArray, ''); },
  2465. TypeError,
  2466. '`captures`: ' + debug(nonArray) + ' is not an Array'
  2467. );
  2468. });
  2469. t.equal(
  2470. ES.GetSubstitution('def', 'abcdefghi', 3, [], '123'),
  2471. '123',
  2472. 'returns the substitution'
  2473. );
  2474. t.equal(
  2475. ES.GetSubstitution('abcdef', 'abcdefghi', 0, [], '$$2$'),
  2476. '$2$',
  2477. 'supports $$, and trailing $'
  2478. );
  2479. t.equal(
  2480. ES.GetSubstitution('abcdef', 'abcdefghi', 0, [], '>$&<'),
  2481. '>abcdef<',
  2482. 'supports $&'
  2483. );
  2484. t.equal(
  2485. ES.GetSubstitution('abcdef', 'abcdefghi', 0, [], '>$`<'),
  2486. '><',
  2487. 'supports $` at position 0'
  2488. );
  2489. t.equal(
  2490. ES.GetSubstitution('def', 'abcdefghi', 3, [], '>$`<'),
  2491. '>ab<',
  2492. 'supports $` at position > 0'
  2493. );
  2494. t.equal(
  2495. ES.GetSubstitution('def', 'abcdefghi', 7, [], ">$'<"),
  2496. '><',
  2497. "supports $' at a position where there's less than `matched.length` chars left"
  2498. );
  2499. t.equal(
  2500. ES.GetSubstitution('def', 'abcdefghi', 3, [], ">$'<"),
  2501. '>ghi<',
  2502. "supports $' at a position where there's more than `matched.length` chars left"
  2503. );
  2504. for (var i = 0; i < 100; i += 1) {
  2505. var captures = [];
  2506. captures[i] = 'test';
  2507. if (i > 0) {
  2508. t.equal(
  2509. ES.GetSubstitution('abcdef', 'abcdefghi', 0, [], '>$' + i + '<'),
  2510. '>undefined<',
  2511. 'supports $' + i + ' with no captures'
  2512. );
  2513. t.equal(
  2514. ES.GetSubstitution('abcdef', 'abcdefghi', 0, [], '>$' + i),
  2515. '>undefined',
  2516. 'supports $' + i + ' at the end of the replacement, with no captures'
  2517. );
  2518. t.equal(
  2519. ES.GetSubstitution('abcdef', 'abcdefghi', 0, captures, '>$' + i + '<'),
  2520. '><',
  2521. 'supports $' + i + ' with a capture at that index'
  2522. );
  2523. t.equal(
  2524. ES.GetSubstitution('abcdef', 'abcdefghi', 0, captures, '>$' + i),
  2525. '>',
  2526. 'supports $' + i + ' at the end of the replacement, with a capture at that index'
  2527. );
  2528. }
  2529. if (i < 10) {
  2530. t.equal(
  2531. ES.GetSubstitution('abcdef', 'abcdefghi', 0, [], '>$0' + i + '<'),
  2532. i === 0 ? '><' : '>undefined<',
  2533. 'supports $0' + i + ' with no captures'
  2534. );
  2535. t.equal(
  2536. ES.GetSubstitution('abcdef', 'abcdefghi', 0, [], '>$0' + i),
  2537. i === 0 ? '>' : '>undefined',
  2538. 'supports $0' + i + ' at the end of the replacement, with no captures'
  2539. );
  2540. t.equal(
  2541. ES.GetSubstitution('abcdef', 'abcdefghi', 0, captures, '>$0' + i + '<'),
  2542. '><',
  2543. 'supports $0' + i + ' with a capture at that index'
  2544. );
  2545. t.equal(
  2546. ES.GetSubstitution('abcdef', 'abcdefghi', 0, captures, '>$0' + i),
  2547. '>',
  2548. 'supports $0' + i + ' at the end of the replacement, with a capture at that index'
  2549. );
  2550. }
  2551. }
  2552. t.end();
  2553. });
  2554. test('SecFromTime', function (t) {
  2555. var now = new Date();
  2556. t.equal(ES.SecFromTime(now.getTime()), now.getUTCSeconds(), 'second from Date timestamp matches getUTCSeconds');
  2557. t.end();
  2558. });
  2559. test('MinFromTime', function (t) {
  2560. var now = new Date();
  2561. t.equal(ES.MinFromTime(now.getTime()), now.getUTCMinutes(), 'minute from Date timestamp matches getUTCMinutes');
  2562. t.end();
  2563. });
  2564. test('HourFromTime', function (t) {
  2565. var now = new Date();
  2566. t.equal(ES.HourFromTime(now.getTime()), now.getUTCHours(), 'hour from Date timestamp matches getUTCHours');
  2567. t.end();
  2568. });
  2569. test('msFromTime', function (t) {
  2570. var now = new Date();
  2571. t.equal(ES.msFromTime(now.getTime()), now.getUTCMilliseconds(), 'ms from Date timestamp matches getUTCMilliseconds');
  2572. t.end();
  2573. });
  2574. var msPerSecond = 1e3;
  2575. var msPerMinute = 60 * msPerSecond;
  2576. var msPerHour = 60 * msPerMinute;
  2577. var msPerDay = 24 * msPerHour;
  2578. test('Day', function (t) {
  2579. var time = Date.UTC(2019, 8, 10, 2, 3, 4, 5);
  2580. var add = 2.5;
  2581. var later = new Date(time + (add * msPerDay));
  2582. t.equal(ES.Day(later.getTime()), ES.Day(time) + Math.floor(add), 'adding 2.5 days worth of ms, gives a Day delta of 2');
  2583. t.end();
  2584. });
  2585. test('TimeWithinDay', function (t) {
  2586. var time = Date.UTC(2019, 8, 10, 2, 3, 4, 5);
  2587. var add = 2.5;
  2588. var later = new Date(time + (add * msPerDay));
  2589. t.equal(ES.TimeWithinDay(later.getTime()), ES.TimeWithinDay(time) + (0.5 * msPerDay), 'adding 2.5 days worth of ms, gives a TimeWithinDay delta of +0.5');
  2590. t.end();
  2591. });
  2592. test('DayFromYear', function (t) {
  2593. t.equal(ES.DayFromYear(2021) - ES.DayFromYear(2020), 366, '2021 is a leap year, has 366 days');
  2594. t.equal(ES.DayFromYear(2020) - ES.DayFromYear(2019), 365, '2020 is not a leap year, has 365 days');
  2595. t.equal(ES.DayFromYear(2019) - ES.DayFromYear(2018), 365, '2019 is not a leap year, has 365 days');
  2596. t.equal(ES.DayFromYear(2018) - ES.DayFromYear(2017), 365, '2018 is not a leap year, has 365 days');
  2597. t.equal(ES.DayFromYear(2017) - ES.DayFromYear(2016), 366, '2017 is a leap year, has 366 days');
  2598. t.end();
  2599. });
  2600. test('TimeFromYear', function (t) {
  2601. for (var i = 1900; i < 2100; i += 1) {
  2602. t.equal(ES.TimeFromYear(i), Date.UTC(i, 0, 1), 'TimeFromYear matches a Date object’s year: ' + i);
  2603. }
  2604. t.end();
  2605. });
  2606. test('YearFromTime', function (t) {
  2607. for (var i = 1900; i < 2100; i += 1) {
  2608. t.equal(ES.YearFromTime(Date.UTC(i, 0, 1)), i, 'YearFromTime matches a Date object’s year on 1/1: ' + i);
  2609. t.equal(ES.YearFromTime(Date.UTC(i, 10, 1)), i, 'YearFromTime matches a Date object’s year on 10/1: ' + i);
  2610. }
  2611. t.end();
  2612. });
  2613. test('WeekDay', function (t) {
  2614. var now = new Date();
  2615. var today = now.getUTCDay();
  2616. for (var i = 0; i < 7; i += 1) {
  2617. var weekDay = ES.WeekDay(now.getTime() + (i * msPerDay));
  2618. t.equal(weekDay, (today + i) % 7, i + ' days after today (' + today + '), WeekDay is ' + weekDay);
  2619. }
  2620. t.end();
  2621. });
  2622. test('DaysInYear', function (t) {
  2623. t.equal(ES.DaysInYear(2021), 365, '2021 is not a leap year');
  2624. t.equal(ES.DaysInYear(2020), 366, '2020 is a leap year');
  2625. t.equal(ES.DaysInYear(2019), 365, '2019 is not a leap year');
  2626. t.equal(ES.DaysInYear(2018), 365, '2018 is not a leap year');
  2627. t.equal(ES.DaysInYear(2017), 365, '2017 is not a leap year');
  2628. t.equal(ES.DaysInYear(2016), 366, '2016 is a leap year');
  2629. t.end();
  2630. });
  2631. test('InLeapYear', function (t) {
  2632. t.equal(ES.InLeapYear(Date.UTC(2021, 0, 1)), 0, '2021 is not a leap year');
  2633. t.equal(ES.InLeapYear(Date.UTC(2020, 0, 1)), 1, '2020 is a leap year');
  2634. t.equal(ES.InLeapYear(Date.UTC(2019, 0, 1)), 0, '2019 is not a leap year');
  2635. t.equal(ES.InLeapYear(Date.UTC(2018, 0, 1)), 0, '2018 is not a leap year');
  2636. t.equal(ES.InLeapYear(Date.UTC(2017, 0, 1)), 0, '2017 is not a leap year');
  2637. t.equal(ES.InLeapYear(Date.UTC(2016, 0, 1)), 1, '2016 is a leap year');
  2638. t.end();
  2639. });
  2640. test('DayWithinYear', function (t) {
  2641. t.equal(ES.DayWithinYear(Date.UTC(2019, 0, 1)), 0, '1/1 is the 1st day');
  2642. t.equal(ES.DayWithinYear(Date.UTC(2019, 11, 31)), 364, '12/31 is the 365th day in a non leap year');
  2643. t.equal(ES.DayWithinYear(Date.UTC(2016, 11, 31)), 365, '12/31 is the 366th day in a leap year');
  2644. t.end();
  2645. });
  2646. test('MonthFromTime', function (t) {
  2647. t.equal(ES.MonthFromTime(Date.UTC(2019, 0, 1)), 0, 'non-leap: 1/1 gives January');
  2648. t.equal(ES.MonthFromTime(Date.UTC(2019, 0, 31)), 0, 'non-leap: 1/31 gives January');
  2649. t.equal(ES.MonthFromTime(Date.UTC(2019, 1, 1)), 1, 'non-leap: 2/1 gives February');
  2650. t.equal(ES.MonthFromTime(Date.UTC(2019, 1, 28)), 1, 'non-leap: 2/28 gives February');
  2651. t.equal(ES.MonthFromTime(Date.UTC(2019, 1, 29)), 2, 'non-leap: 2/29 gives March');
  2652. t.equal(ES.MonthFromTime(Date.UTC(2019, 2, 1)), 2, 'non-leap: 3/1 gives March');
  2653. t.equal(ES.MonthFromTime(Date.UTC(2019, 2, 31)), 2, 'non-leap: 3/31 gives March');
  2654. t.equal(ES.MonthFromTime(Date.UTC(2019, 3, 1)), 3, 'non-leap: 4/1 gives April');
  2655. t.equal(ES.MonthFromTime(Date.UTC(2019, 3, 30)), 3, 'non-leap: 4/30 gives April');
  2656. t.equal(ES.MonthFromTime(Date.UTC(2019, 4, 1)), 4, 'non-leap: 5/1 gives May');
  2657. t.equal(ES.MonthFromTime(Date.UTC(2019, 4, 31)), 4, 'non-leap: 5/31 gives May');
  2658. t.equal(ES.MonthFromTime(Date.UTC(2019, 5, 1)), 5, 'non-leap: 6/1 gives June');
  2659. t.equal(ES.MonthFromTime(Date.UTC(2019, 5, 30)), 5, 'non-leap: 6/30 gives June');
  2660. t.equal(ES.MonthFromTime(Date.UTC(2019, 6, 1)), 6, 'non-leap: 7/1 gives July');
  2661. t.equal(ES.MonthFromTime(Date.UTC(2019, 6, 31)), 6, 'non-leap: 7/31 gives July');
  2662. t.equal(ES.MonthFromTime(Date.UTC(2019, 7, 1)), 7, 'non-leap: 8/1 gives August');
  2663. t.equal(ES.MonthFromTime(Date.UTC(2019, 7, 30)), 7, 'non-leap: 8/30 gives August');
  2664. t.equal(ES.MonthFromTime(Date.UTC(2019, 8, 1)), 8, 'non-leap: 9/1 gives September');
  2665. t.equal(ES.MonthFromTime(Date.UTC(2019, 8, 30)), 8, 'non-leap: 9/30 gives September');
  2666. t.equal(ES.MonthFromTime(Date.UTC(2019, 9, 1)), 9, 'non-leap: 10/1 gives October');
  2667. t.equal(ES.MonthFromTime(Date.UTC(2019, 9, 31)), 9, 'non-leap: 10/31 gives October');
  2668. t.equal(ES.MonthFromTime(Date.UTC(2019, 10, 1)), 10, 'non-leap: 11/1 gives November');
  2669. t.equal(ES.MonthFromTime(Date.UTC(2019, 10, 30)), 10, 'non-leap: 11/30 gives November');
  2670. t.equal(ES.MonthFromTime(Date.UTC(2019, 11, 1)), 11, 'non-leap: 12/1 gives December');
  2671. t.equal(ES.MonthFromTime(Date.UTC(2019, 11, 31)), 11, 'non-leap: 12/31 gives December');
  2672. t.equal(ES.MonthFromTime(Date.UTC(2016, 0, 1)), 0, 'leap: 1/1 gives January');
  2673. t.equal(ES.MonthFromTime(Date.UTC(2016, 0, 31)), 0, 'leap: 1/31 gives January');
  2674. t.equal(ES.MonthFromTime(Date.UTC(2016, 1, 1)), 1, 'leap: 2/1 gives February');
  2675. t.equal(ES.MonthFromTime(Date.UTC(2016, 1, 28)), 1, 'leap: 2/28 gives February');
  2676. t.equal(ES.MonthFromTime(Date.UTC(2016, 1, 29)), 1, 'leap: 2/29 gives February');
  2677. t.equal(ES.MonthFromTime(Date.UTC(2016, 2, 1)), 2, 'leap: 3/1 gives March');
  2678. t.equal(ES.MonthFromTime(Date.UTC(2016, 2, 31)), 2, 'leap: 3/31 gives March');
  2679. t.equal(ES.MonthFromTime(Date.UTC(2016, 3, 1)), 3, 'leap: 4/1 gives April');
  2680. t.equal(ES.MonthFromTime(Date.UTC(2016, 3, 30)), 3, 'leap: 4/30 gives April');
  2681. t.equal(ES.MonthFromTime(Date.UTC(2016, 4, 1)), 4, 'leap: 5/1 gives May');
  2682. t.equal(ES.MonthFromTime(Date.UTC(2016, 4, 31)), 4, 'leap: 5/31 gives May');
  2683. t.equal(ES.MonthFromTime(Date.UTC(2016, 5, 1)), 5, 'leap: 6/1 gives June');
  2684. t.equal(ES.MonthFromTime(Date.UTC(2016, 5, 30)), 5, 'leap: 6/30 gives June');
  2685. t.equal(ES.MonthFromTime(Date.UTC(2016, 6, 1)), 6, 'leap: 7/1 gives July');
  2686. t.equal(ES.MonthFromTime(Date.UTC(2016, 6, 31)), 6, 'leap: 7/31 gives July');
  2687. t.equal(ES.MonthFromTime(Date.UTC(2016, 7, 1)), 7, 'leap: 8/1 gives August');
  2688. t.equal(ES.MonthFromTime(Date.UTC(2016, 7, 30)), 7, 'leap: 8/30 gives August');
  2689. t.equal(ES.MonthFromTime(Date.UTC(2016, 8, 1)), 8, 'leap: 9/1 gives September');
  2690. t.equal(ES.MonthFromTime(Date.UTC(2016, 8, 30)), 8, 'leap: 9/30 gives September');
  2691. t.equal(ES.MonthFromTime(Date.UTC(2016, 9, 1)), 9, 'leap: 10/1 gives October');
  2692. t.equal(ES.MonthFromTime(Date.UTC(2016, 9, 31)), 9, 'leap: 10/31 gives October');
  2693. t.equal(ES.MonthFromTime(Date.UTC(2016, 10, 1)), 10, 'leap: 11/1 gives November');
  2694. t.equal(ES.MonthFromTime(Date.UTC(2016, 10, 30)), 10, 'leap: 11/30 gives November');
  2695. t.equal(ES.MonthFromTime(Date.UTC(2016, 11, 1)), 11, 'leap: 12/1 gives December');
  2696. t.equal(ES.MonthFromTime(Date.UTC(2016, 11, 31)), 11, 'leap: 12/31 gives December');
  2697. t.end();
  2698. });
  2699. test('DateFromTime', function (t) {
  2700. var i;
  2701. for (i = 1; i <= 28; i += 1) {
  2702. t.equal(ES.DateFromTime(Date.UTC(2019, 1, i)), i, '2019.02.' + i + ' is date ' + i);
  2703. }
  2704. for (i = 1; i <= 29; i += 1) {
  2705. t.equal(ES.DateFromTime(Date.UTC(2016, 1, i)), i, '2016.02.' + i + ' is date ' + i);
  2706. }
  2707. for (i = 1; i <= 30; i += 1) {
  2708. t.equal(ES.DateFromTime(Date.UTC(2019, 8, i)), i, '2019.09.' + i + ' is date ' + i);
  2709. }
  2710. for (i = 1; i <= 31; i += 1) {
  2711. t.equal(ES.DateFromTime(Date.UTC(2019, 9, i)), i, '2019.10.' + i + ' is date ' + i);
  2712. }
  2713. t.end();
  2714. });
  2715. test('MakeDay', function (t) {
  2716. var day2015 = 16687;
  2717. t.equal(ES.MakeDay(2015, 8, 9), day2015, '2015.09.09 is day 16687');
  2718. var day2016 = day2015 + 366; // 2016 is a leap year
  2719. t.equal(ES.MakeDay(2016, 8, 9), day2016, '2015.09.09 is day 17053');
  2720. var day2017 = day2016 + 365;
  2721. t.equal(ES.MakeDay(2017, 8, 9), day2017, '2017.09.09 is day 17418');
  2722. var day2018 = day2017 + 365;
  2723. t.equal(ES.MakeDay(2018, 8, 9), day2018, '2018.09.09 is day 17783');
  2724. var day2019 = day2018 + 365;
  2725. t.equal(ES.MakeDay(2019, 8, 9), day2019, '2019.09.09 is day 18148');
  2726. t.end();
  2727. });
  2728. test('MakeDate', function (t) {
  2729. forEach(v.infinities.concat(NaN), function (nonFiniteNumber) {
  2730. t.ok(is(ES.MakeDate(nonFiniteNumber, 0), NaN), debug(nonFiniteNumber) + ' is not a finite `day`');
  2731. t.ok(is(ES.MakeDate(0, nonFiniteNumber), NaN), debug(nonFiniteNumber) + ' is not a finite `time`');
  2732. });
  2733. t.equal(ES.MakeDate(0, 0), 0, 'zero day and zero time is zero date');
  2734. t.equal(ES.MakeDate(0, 123), 123, 'zero day and nonzero time is a date of the "time"');
  2735. t.equal(ES.MakeDate(1, 0), msPerDay, 'day of 1 and zero time is a date of "ms per day"');
  2736. t.equal(ES.MakeDate(3, 0), 3 * msPerDay, 'day of 3 and zero time is a date of thrice "ms per day"');
  2737. t.equal(ES.MakeDate(1, 123), msPerDay + 123, 'day of 1 and nonzero time is a date of "ms per day" plus the "time"');
  2738. t.equal(ES.MakeDate(3, 123), (3 * msPerDay) + 123, 'day of 3 and nonzero time is a date of thrice "ms per day" plus the "time"');
  2739. t.end();
  2740. });
  2741. test('MakeTime', function (t) {
  2742. forEach(v.infinities.concat(NaN), function (nonFiniteNumber) {
  2743. t.ok(is(ES.MakeTime(nonFiniteNumber, 0, 0, 0), NaN), debug(nonFiniteNumber) + ' is not a finite `hour`');
  2744. t.ok(is(ES.MakeTime(0, nonFiniteNumber, 0, 0), NaN), debug(nonFiniteNumber) + ' is not a finite `min`');
  2745. t.ok(is(ES.MakeTime(0, 0, nonFiniteNumber, 0), NaN), debug(nonFiniteNumber) + ' is not a finite `sec`');
  2746. t.ok(is(ES.MakeTime(0, 0, 0, nonFiniteNumber), NaN), debug(nonFiniteNumber) + ' is not a finite `ms`');
  2747. });
  2748. t.equal(
  2749. ES.MakeTime(1.2, 2.3, 3.4, 4.5),
  2750. (1 * msPerHour) + (2 * msPerMinute) + (3 * msPerSecond) + 4,
  2751. 'all numbers are converted to integer, multiplied by the right number of ms, and summed'
  2752. );
  2753. t.end();
  2754. });
  2755. test('TimeClip', function (t) {
  2756. forEach(v.infinities.concat(NaN), function (nonFiniteNumber) {
  2757. t.ok(is(ES.TimeClip(nonFiniteNumber), NaN), debug(nonFiniteNumber) + ' is not a finite `time`');
  2758. });
  2759. t.ok(is(ES.TimeClip(8.64e15 + 1), NaN), '8.64e15 is the largest magnitude considered "finite"');
  2760. t.ok(is(ES.TimeClip(-8.64e15 - 1), NaN), '-8.64e15 is the largest magnitude considered "finite"');
  2761. forEach(v.zeroes.concat([-10, 10, Date.now()]), function (time) {
  2762. t.equal(ES.TimeClip(time), time, debug(time) + ' is a time of ' + debug(time));
  2763. });
  2764. t.end();
  2765. });
  2766. test('modulo', function (t) {
  2767. t.equal(3 % 2, 1, '+3 % 2 is +1');
  2768. t.equal(ES.modulo(3, 2), 1, '+3 mod 2 is +1');
  2769. t.equal(-3 % 2, -1, '-3 % 2 is -1');
  2770. t.equal(ES.modulo(-3, 2), 1, '-3 mod 2 is +1');
  2771. t.end();
  2772. });
  2773. test('ToDateString', function (t) {
  2774. forEach(v.nonNumbers, function (nonNumber) {
  2775. t['throws'](
  2776. function () { ES.ToDateString(nonNumber); },
  2777. TypeError,
  2778. debug(nonNumber) + ' is not a Number'
  2779. );
  2780. });
  2781. t.equal(ES.ToDateString(NaN), 'Invalid Date', 'NaN becomes "Invalid Date"');
  2782. var now = Date.now();
  2783. t.equal(ES.ToDateString(now), Date(now), 'any timestamp becomes `Date(timestamp)`');
  2784. t.end();
  2785. });
  2786. test('CreateListFromArrayLike', function (t) {
  2787. forEach(v.primitives, function (nonObject) {
  2788. t['throws'](
  2789. function () { ES.CreateListFromArrayLike(nonObject); },
  2790. TypeError,
  2791. debug(nonObject) + ' is not an Object'
  2792. );
  2793. });
  2794. forEach(v.nonArrays, function (nonArray) {
  2795. t['throws'](
  2796. function () { ES.CreateListFromArrayLike({}, nonArray); },
  2797. TypeError,
  2798. debug(nonArray) + ' is not an Array'
  2799. );
  2800. });
  2801. t.deepEqual(
  2802. ES.CreateListFromArrayLike({ length: 2, 0: 'a', 1: 'b', 2: 'c' }),
  2803. ['a', 'b'],
  2804. 'arraylike stops at the length'
  2805. );
  2806. t.end();
  2807. });
  2808. test('GetPrototypeFromConstructor', function (t) {
  2809. forEach(v.nonFunctions, function (nonFunction) {
  2810. t['throws'](
  2811. function () { ES.GetPrototypeFromConstructor(nonFunction, '%Array%'); },
  2812. TypeError,
  2813. debug(nonFunction) + ' is not a constructor'
  2814. );
  2815. });
  2816. forEach(arrowFns, function (arrowFn) {
  2817. t['throws'](
  2818. function () { ES.GetPrototypeFromConstructor(arrowFn, '%Array%'); },
  2819. TypeError,
  2820. debug(arrowFn) + ' is not a constructor'
  2821. );
  2822. });
  2823. var f = function () {};
  2824. t.equal(
  2825. ES.GetPrototypeFromConstructor(f, '%Array.prototype%'),
  2826. f.prototype,
  2827. 'function with normal `prototype` property returns it'
  2828. );
  2829. forEach([true, 'foo', 42], function (truthyPrimitive) {
  2830. f.prototype = truthyPrimitive;
  2831. t.equal(
  2832. ES.GetPrototypeFromConstructor(f, '%Array.prototype%'),
  2833. Array.prototype,
  2834. 'function with non-object `prototype` property (' + debug(truthyPrimitive) + ') returns default intrinsic'
  2835. );
  2836. });
  2837. t.end();
  2838. });
  2839. var getNamelessFunction = function () {
  2840. var f = Object(function () {});
  2841. try {
  2842. delete f.name;
  2843. } catch (e) { /**/ }
  2844. return f;
  2845. };
  2846. test('SetFunctionName', function (t) {
  2847. t.test('non-extensible function', { skip: !Object.preventExtensions }, function (st) {
  2848. var f = getNamelessFunction();
  2849. Object.preventExtensions(f);
  2850. st['throws'](
  2851. function () { ES.SetFunctionName(f, ''); },
  2852. TypeError,
  2853. 'throws on a non-extensible function'
  2854. );
  2855. st.end();
  2856. });
  2857. t['throws'](
  2858. function () { ES.SetFunctionName(function g() {}, ''); },
  2859. TypeError,
  2860. 'throws if function has an own `name` property'
  2861. );
  2862. forEach(v.nonPropertyKeys, function (nonPropertyKey) {
  2863. t['throws'](
  2864. function () { ES.SetFunctionName(getNamelessFunction(), nonPropertyKey); },
  2865. TypeError,
  2866. debug(nonPropertyKey) + ' is not a Symbol or String'
  2867. );
  2868. });
  2869. t.test('symbols', { skip: !v.hasSymbols || has(getNamelessFunction(), 'name') }, function (st) {
  2870. var pairs = [
  2871. [Symbol(), ''],
  2872. [Symbol(undefined), ''],
  2873. [Symbol(null), '[null]'],
  2874. [Symbol(''), getInferredName ? '[]' : ''],
  2875. [Symbol.iterator, '[Symbol.iterator]'],
  2876. [Symbol('foo'), '[foo]']
  2877. ];
  2878. forEach(pairs, function (pair) {
  2879. var sym = pair[0];
  2880. var desc = pair[1];
  2881. var f = getNamelessFunction();
  2882. ES.SetFunctionName(f, sym);
  2883. st.equal(f.name, desc, debug(sym) + ' yields a name of ' + debug(desc));
  2884. });
  2885. st.end();
  2886. });
  2887. var f = getNamelessFunction();
  2888. t.test('when names are configurable', { skip: has(f, 'name') }, function (st) {
  2889. // without prefix
  2890. st.notEqual(f.name, 'foo', 'precondition');
  2891. ES.SetFunctionName(f, 'foo');
  2892. st.equal(f.name, 'foo', 'function name is set without a prefix');
  2893. // with prefix
  2894. var g = getNamelessFunction();
  2895. st.notEqual(g.name, 'pre- foo', 'precondition');
  2896. ES.SetFunctionName(g, 'foo', 'pre-');
  2897. st.equal(g.name, 'pre- foo', 'function name is set with a prefix');
  2898. st.end();
  2899. });
  2900. t.end();
  2901. });
  2902. };
  2903. var es2016 = function ES2016(ES, ops, expectedMissing, skips) {
  2904. es2015(ES, ops, expectedMissing, skips);
  2905. test('SameValueNonNumber', function (t) {
  2906. var willThrow = [
  2907. [3, 4],
  2908. [NaN, 4],
  2909. [4, ''],
  2910. ['abc', true],
  2911. [{}, false]
  2912. ];
  2913. forEach(willThrow, function (nums) {
  2914. t['throws'](function () { return ES.SameValueNonNumber.apply(ES, nums); }, TypeError, 'value must be same type and non-number');
  2915. });
  2916. forEach(v.objects.concat(v.nonNumberPrimitives), function (val) {
  2917. t.equal(val === val, ES.SameValueNonNumber(val, val), debug(val) + ' is SameValueNonNumber to itself');
  2918. });
  2919. t.end();
  2920. });
  2921. test('IterableToArrayLike', { skip: skips && skips.IterableToArrayLike }, function (t) {
  2922. t.test('custom iterables', { skip: !v.hasSymbols }, function (st) {
  2923. var O = {};
  2924. O[Symbol.iterator] = function () {
  2925. var i = -1;
  2926. return {
  2927. next: function () {
  2928. i += 1;
  2929. return {
  2930. done: i >= 5,
  2931. value: i
  2932. };
  2933. }
  2934. };
  2935. };
  2936. st.deepEqual(
  2937. ES.IterableToArrayLike(O),
  2938. [0, 1, 2, 3, 4],
  2939. 'Symbol.iterator method is called and values collected'
  2940. );
  2941. st.end();
  2942. });
  2943. t.deepEqual(ES.IterableToArrayLike('abc'), ['a', 'b', 'c'], 'a string of code units spreads');
  2944. t.deepEqual(ES.IterableToArrayLike('💩'), ['💩'], 'a string of code points spreads');
  2945. t.deepEqual(ES.IterableToArrayLike('a💩c'), ['a', '💩', 'c'], 'a string of code points and units spreads');
  2946. var arr = [1, 2, 3];
  2947. t.deepEqual(ES.IterableToArrayLike(arr), arr, 'an array becomes a similar array');
  2948. t.notEqual(ES.IterableToArrayLike(arr), arr, 'an array becomes a different, but similar, array');
  2949. var O = {};
  2950. t.equal(ES.IterableToArrayLike(O), O, 'a non-iterable non-array non-string object is returned directly');
  2951. t.end();
  2952. });
  2953. test('OrdinaryGetPrototypeOf', function (t) {
  2954. t.equal(ES.OrdinaryGetPrototypeOf([]), Array.prototype, 'array [[Prototype]] is Array.prototype');
  2955. t.equal(ES.OrdinaryGetPrototypeOf({}), Object.prototype, 'object [[Prototype]] is Object.prototype');
  2956. t.equal(ES.OrdinaryGetPrototypeOf(/a/g), RegExp.prototype, 'regex [[Prototype]] is RegExp.prototype');
  2957. t.equal(ES.OrdinaryGetPrototypeOf(Object('')), String.prototype, 'boxed string [[Prototype]] is String.prototype');
  2958. t.equal(ES.OrdinaryGetPrototypeOf(Object(42)), Number.prototype, 'boxed number [[Prototype]] is Number.prototype');
  2959. t.equal(ES.OrdinaryGetPrototypeOf(Object(true)), Boolean.prototype, 'boxed boolean [[Prototype]] is Boolean.prototype');
  2960. if (v.hasSymbols) {
  2961. t.equal(ES.OrdinaryGetPrototypeOf(Object(Symbol.iterator)), Symbol.prototype, 'boxed symbol [[Prototype]] is Symbol.prototype');
  2962. }
  2963. forEach(v.primitives, function (primitive) {
  2964. t['throws'](
  2965. function () { ES.OrdinaryGetPrototypeOf(primitive); },
  2966. TypeError,
  2967. debug(primitive) + ' is not an Object'
  2968. );
  2969. });
  2970. t.end();
  2971. });
  2972. test('OrdinarySetPrototypeOf', function (t) {
  2973. var a = [];
  2974. var proto = {};
  2975. t.equal(ES.OrdinaryGetPrototypeOf(a), Array.prototype, 'precondition');
  2976. t.equal(ES.OrdinarySetPrototypeOf(a, proto), true, 'setting prototype is successful');
  2977. t.equal(ES.OrdinaryGetPrototypeOf(a), proto, 'postcondition');
  2978. t.end();
  2979. });
  2980. };
  2981. var es2017 = function ES2017(ES, ops, expectedMissing, skips) {
  2982. es2016(ES, ops, expectedMissing, assign({}, skips, {
  2983. EnumerableOwnNames: true,
  2984. IterableToArrayLike: true
  2985. }));
  2986. test('ToIndex', function (t) {
  2987. t.ok(is(ES.ToIndex(), 0), 'no value gives 0');
  2988. t.ok(is(ES.ToIndex(undefined), 0), 'undefined value gives 0');
  2989. t['throws'](function () { ES.ToIndex(-1); }, RangeError, 'negative numbers throw');
  2990. t['throws'](function () { ES.ToIndex(MAX_SAFE_INTEGER + 1); }, RangeError, 'too large numbers throw');
  2991. t.equal(ES.ToIndex(3), 3, 'numbers work');
  2992. t.equal(ES.ToIndex(v.valueOfOnlyObject), 4, 'coercible objects are coerced');
  2993. t.end();
  2994. });
  2995. test('EnumerableOwnProperties', { skip: skips && skips.EnumerableOwnProperties }, function (t) {
  2996. var obj = testEnumerableOwnNames(t, function (O) {
  2997. return ES.EnumerableOwnProperties(O, 'key');
  2998. });
  2999. t.deepEqual(
  3000. ES.EnumerableOwnProperties(obj, 'value'),
  3001. [obj.own],
  3002. 'returns enumerable own values'
  3003. );
  3004. t.deepEqual(
  3005. ES.EnumerableOwnProperties(obj, 'key+value'),
  3006. [['own', obj.own]],
  3007. 'returns enumerable own entries'
  3008. );
  3009. t.end();
  3010. });
  3011. test('IterableToList', function (t) {
  3012. var customIterator = function () {
  3013. var i = -1;
  3014. return {
  3015. next: function () {
  3016. i += 1;
  3017. return {
  3018. done: i >= 5,
  3019. value: i
  3020. };
  3021. }
  3022. };
  3023. };
  3024. t.deepEqual(
  3025. ES.IterableToList({}, customIterator),
  3026. [0, 1, 2, 3, 4],
  3027. 'iterator method is called and values collected'
  3028. );
  3029. t.test('Symbol support', { skip: !v.hasSymbols }, function (st) {
  3030. st.deepEqual(ES.IterableToList('abc', String.prototype[Symbol.iterator]), ['a', 'b', 'c'], 'a string of code units spreads');
  3031. st.deepEqual(ES.IterableToList('☃', String.prototype[Symbol.iterator]), ['☃'], 'a string of code points spreads');
  3032. var arr = [1, 2, 3];
  3033. st.deepEqual(ES.IterableToList(arr, arr[Symbol.iterator]), arr, 'an array becomes a similar array');
  3034. st.notEqual(ES.IterableToList(arr, arr[Symbol.iterator]), arr, 'an array becomes a different, but similar, array');
  3035. st.end();
  3036. });
  3037. t['throws'](
  3038. function () { ES.IterableToList({}, void 0); },
  3039. TypeError,
  3040. 'non-function iterator method'
  3041. );
  3042. t.end();
  3043. });
  3044. };
  3045. var es2018 = function ES2018(ES, ops, expectedMissing, skips) {
  3046. es2017(ES, ops, expectedMissing, assign({}, skips, {
  3047. EnumerableOwnProperties: true,
  3048. GetSubstitution: true,
  3049. IsPropertyDescriptor: true
  3050. }));
  3051. test('thisSymbolValue', function (t) {
  3052. forEach(v.nonSymbolPrimitives.concat(v.objects), function (nonSymbol) {
  3053. t['throws'](
  3054. function () { ES.thisSymbolValue(nonSymbol); },
  3055. v.hasSymbols ? TypeError : SyntaxError,
  3056. debug(nonSymbol) + ' is not a Symbol'
  3057. );
  3058. });
  3059. t.test('no native Symbols', { skip: v.hasSymbols }, function (st) {
  3060. forEach(v.objects.concat(v.primitives), function (value) {
  3061. st['throws'](
  3062. function () { ES.thisSymbolValue(value); },
  3063. SyntaxError,
  3064. 'Symbols are not supported'
  3065. );
  3066. });
  3067. st.end();
  3068. });
  3069. t.test('symbol values', { skip: !v.hasSymbols }, function (st) {
  3070. forEach(v.symbols, function (symbol) {
  3071. st.equal(ES.thisSymbolValue(symbol), symbol, 'Symbol value of ' + debug(symbol) + ' is same symbol');
  3072. st.equal(
  3073. ES.thisSymbolValue(Object(symbol)),
  3074. symbol,
  3075. 'Symbol value of ' + debug(Object(symbol)) + ' is ' + debug(symbol)
  3076. );
  3077. });
  3078. st.end();
  3079. });
  3080. t.end();
  3081. });
  3082. test('IsStringPrefix', function (t) {
  3083. forEach(v.nonStrings, function (nonString) {
  3084. t['throws'](
  3085. function () { ES.IsStringPrefix(nonString, 'a'); },
  3086. TypeError,
  3087. 'first arg: ' + debug(nonString) + ' is not a string'
  3088. );
  3089. t['throws'](
  3090. function () { ES.IsStringPrefix('a', nonString); },
  3091. TypeError,
  3092. 'second arg: ' + debug(nonString) + ' is not a string'
  3093. );
  3094. });
  3095. forEach(v.strings, function (string) {
  3096. t.equal(ES.IsStringPrefix(string, string), true, debug(string) + ' is a prefix of itself');
  3097. t.equal(ES.IsStringPrefix('', string), true, 'the empty string is a prefix of everything');
  3098. });
  3099. t.equal(ES.IsStringPrefix('abc', 'abcd'), true, '"abc" is a prefix of "abcd"');
  3100. t.equal(ES.IsStringPrefix('abcd', 'abc'), false, '"abcd" is not a prefix of "abc"');
  3101. t.equal(ES.IsStringPrefix('a', 'bc'), false, '"a" is not a prefix of "bc"');
  3102. t.end();
  3103. });
  3104. test('NumberToString', function (t) {
  3105. forEach(v.nonNumbers, function (nonNumber) {
  3106. t['throws'](
  3107. function () { ES.NumberToString(nonNumber); },
  3108. TypeError,
  3109. debug(nonNumber) + ' is not a Number'
  3110. );
  3111. });
  3112. forEach(v.numbers, function (number) {
  3113. t.equal(ES.NumberToString(number), String(number), debug(number) + ' stringifies to ' + number);
  3114. });
  3115. t.end();
  3116. });
  3117. test('CopyDataProperties', function (t) {
  3118. t.test('first argument: target', function (st) {
  3119. forEach(v.primitives, function (primitive) {
  3120. st['throws'](
  3121. function () { ES.CopyDataProperties(primitive, {}, []); },
  3122. TypeError,
  3123. debug(primitive) + ' is not an Object'
  3124. );
  3125. });
  3126. st.end();
  3127. });
  3128. t.test('second argument: source', function (st) {
  3129. var frozenTarget = Object.freeze ? Object.freeze({}) : {};
  3130. forEach(v.nullPrimitives, function (nullish) {
  3131. st.equal(
  3132. ES.CopyDataProperties(frozenTarget, nullish, []),
  3133. frozenTarget,
  3134. debug(nullish) + ' "source" yields identical, unmodified target'
  3135. );
  3136. });
  3137. forEach(v.nonNullPrimitives, function (objectCoercible) {
  3138. var target = {};
  3139. var result = ES.CopyDataProperties(target, objectCoercible, []);
  3140. st.equal(result, target, 'result === target');
  3141. st.deepEqual(keys(result), keys(Object(objectCoercible)), 'target ends up with keys of ' + debug(objectCoercible));
  3142. });
  3143. st.end();
  3144. });
  3145. t.test('third argument: excludedItems', function (st) {
  3146. forEach(v.objects.concat(v.primitives), function (nonArray) {
  3147. st['throws'](
  3148. function () { ES.CopyDataProperties({}, {}, nonArray); },
  3149. TypeError,
  3150. debug(nonArray) + ' is not an Array'
  3151. );
  3152. });
  3153. forEach(v.nonPropertyKeys, function (nonPropertyKey) {
  3154. st['throws'](
  3155. function () { ES.CopyDataProperties({}, {}, [nonPropertyKey]); },
  3156. TypeError,
  3157. debug(nonPropertyKey) + ' is not a Property Key'
  3158. );
  3159. });
  3160. var result = ES.CopyDataProperties({}, { a: 1, b: 2, c: 3 }, ['b']);
  3161. st.deepEqual(keys(result), ['a', 'c'], 'excluded string keys are excluded');
  3162. st.test('excluding symbols', { skip: !v.hasSymbols }, function (s2t) {
  3163. var source = {};
  3164. forEach(v.symbols, function (symbol) {
  3165. source[symbol] = true;
  3166. });
  3167. var includedSymbols = v.symbols.slice(1);
  3168. var excludedSymbols = v.symbols.slice(0, 1);
  3169. var target = ES.CopyDataProperties({}, source, excludedSymbols);
  3170. forEach(includedSymbols, function (symbol) {
  3171. s2t.equal(has(target, symbol), true, debug(symbol) + ' is included');
  3172. });
  3173. forEach(excludedSymbols, function (symbol) {
  3174. s2t.equal(has(target, symbol), false, debug(symbol) + ' is excluded');
  3175. });
  3176. s2t.end();
  3177. });
  3178. st.end();
  3179. });
  3180. t.end();
  3181. });
  3182. test('PromiseResolve', function (t) {
  3183. t.test('Promises unsupported', { skip: typeof Promise === 'function' }, function (st) {
  3184. st['throws'](
  3185. function () { ES.PromiseResolve(); },
  3186. SyntaxError,
  3187. 'Promises are not supported'
  3188. );
  3189. st.end();
  3190. });
  3191. t.test('Promises supported', { skip: typeof Promise !== 'function' }, function (st) {
  3192. st.plan(2);
  3193. var a = {};
  3194. var b = {};
  3195. var fulfilled = Promise.resolve(a);
  3196. var rejected = Promise.reject(b);
  3197. ES.PromiseResolve(Promise, fulfilled).then(function (x) {
  3198. st.equal(x, a, 'fulfilled promise resolves to fulfilled');
  3199. });
  3200. ES.PromiseResolve(Promise, rejected)['catch'](function (e) {
  3201. st.equal(e, b, 'rejected promise resolves to rejected');
  3202. });
  3203. });
  3204. t.end();
  3205. });
  3206. test('EnumerableOwnPropertyNames', { skip: skips && skips.EnumerableOwnPropertyNames }, function (t) {
  3207. var obj = testEnumerableOwnNames(t, function (O) {
  3208. return ES.EnumerableOwnPropertyNames(O, 'key');
  3209. });
  3210. t.deepEqual(
  3211. ES.EnumerableOwnPropertyNames(obj, 'value'),
  3212. [obj.own],
  3213. 'returns enumerable own values'
  3214. );
  3215. t.deepEqual(
  3216. ES.EnumerableOwnPropertyNames(obj, 'key+value'),
  3217. [['own', obj.own]],
  3218. 'returns enumerable own entries'
  3219. );
  3220. t.end();
  3221. });
  3222. test('IsPromise', { skip: typeof Promise !== 'function' }, function (t) {
  3223. forEach(v.objects.concat(v.primitives), function (nonPromise) {
  3224. t.equal(ES.IsPromise(nonPromise), false, debug(nonPromise) + ' is not a Promise');
  3225. });
  3226. var thenable = { then: Promise.prototype.then };
  3227. t.equal(ES.IsPromise(thenable), false, 'generic thenable is not a Promise');
  3228. t.equal(ES.IsPromise(Promise.resolve()), true, 'Promise is a Promise');
  3229. t.end();
  3230. });
  3231. test('GetSubstitution (ES2018+)', function (t) {
  3232. forEach(v.nonStrings, function (nonString) {
  3233. t['throws'](
  3234. function () { ES.GetSubstitution(nonString, '', 0, [], undefined, ''); },
  3235. TypeError,
  3236. '`matched`: ' + debug(nonString) + ' is not a String'
  3237. );
  3238. t['throws'](
  3239. function () { ES.GetSubstitution('', nonString, 0, [], undefined, ''); },
  3240. TypeError,
  3241. '`str`: ' + debug(nonString) + ' is not a String'
  3242. );
  3243. t['throws'](
  3244. function () { ES.GetSubstitution('', '', 0, [], undefined, nonString); },
  3245. TypeError,
  3246. '`replacement`: ' + debug(nonString) + ' is not a String'
  3247. );
  3248. t['throws'](
  3249. function () { ES.GetSubstitution('', '', 0, [nonString], undefined, ''); },
  3250. TypeError,
  3251. '`captures`: ' + debug([nonString]) + ' is not an Array of strings'
  3252. );
  3253. });
  3254. forEach(v.nonIntegerNumbers.concat([-1, -42, -Infinity]), function (nonNonNegativeInteger) {
  3255. t['throws'](
  3256. function () { ES.GetSubstitution('', '', nonNonNegativeInteger, [], undefined, ''); },
  3257. TypeError,
  3258. '`position`: ' + debug(nonNonNegativeInteger) + ' is not a non-negative integer'
  3259. );
  3260. });
  3261. forEach(v.nonArrays, function (nonArray) {
  3262. t['throws'](
  3263. function () { ES.GetSubstitution('', '', 0, nonArray, undefined, ''); },
  3264. TypeError,
  3265. '`captures`: ' + debug(nonArray) + ' is not an Array'
  3266. );
  3267. });
  3268. t.equal(
  3269. ES.GetSubstitution('def', 'abcdefghi', 3, [], undefined, '123'),
  3270. '123',
  3271. 'returns the substitution'
  3272. );
  3273. t.equal(
  3274. ES.GetSubstitution('abcdef', 'abcdefghi', 0, [], undefined, '$$2$'),
  3275. '$2$',
  3276. 'supports $$, and trailing $'
  3277. );
  3278. t.equal(
  3279. ES.GetSubstitution('abcdef', 'abcdefghi', 0, [], undefined, '>$&<'),
  3280. '>abcdef<',
  3281. 'supports $&'
  3282. );
  3283. t.equal(
  3284. ES.GetSubstitution('abcdef', 'abcdefghi', 0, [], undefined, '>$`<'),
  3285. '><',
  3286. 'supports $` at position 0'
  3287. );
  3288. t.equal(
  3289. ES.GetSubstitution('def', 'abcdefghi', 3, [], undefined, '>$`<'),
  3290. '>ab<',
  3291. 'supports $` at position > 0'
  3292. );
  3293. t.equal(
  3294. ES.GetSubstitution('def', 'abcdefghi', 7, [], undefined, ">$'<"),
  3295. '><',
  3296. "supports $' at a position where there's less than `matched.length` chars left"
  3297. );
  3298. t.equal(
  3299. ES.GetSubstitution('def', 'abcdefghi', 3, [], undefined, ">$'<"),
  3300. '>ghi<',
  3301. "supports $' at a position where there's more than `matched.length` chars left"
  3302. );
  3303. for (var i = 0; i < 100; i += 1) {
  3304. var captures = [];
  3305. captures[i] = 'test';
  3306. if (i > 0) {
  3307. t.equal(
  3308. ES.GetSubstitution('abcdef', 'abcdefghi', 0, [], undefined, '>$' + i + '<'),
  3309. '>undefined<',
  3310. 'supports $' + i + ' with no captures'
  3311. );
  3312. t.equal(
  3313. ES.GetSubstitution('abcdef', 'abcdefghi', 0, [], undefined, '>$' + i),
  3314. '>undefined',
  3315. 'supports $' + i + ' at the end of the replacement, with no captures'
  3316. );
  3317. t.equal(
  3318. ES.GetSubstitution('abcdef', 'abcdefghi', 0, captures, undefined, '>$' + i + '<'),
  3319. '><',
  3320. 'supports $' + i + ' with a capture at that index'
  3321. );
  3322. t.equal(
  3323. ES.GetSubstitution('abcdef', 'abcdefghi', 0, captures, undefined, '>$' + i),
  3324. '>',
  3325. 'supports $' + i + ' at the end of the replacement, with a capture at that index'
  3326. );
  3327. }
  3328. if (i < 10) {
  3329. t.equal(
  3330. ES.GetSubstitution('abcdef', 'abcdefghi', 0, [], undefined, '>$0' + i + '<'),
  3331. i === 0 ? '><' : '>undefined<',
  3332. 'supports $0' + i + ' with no captures'
  3333. );
  3334. t.equal(
  3335. ES.GetSubstitution('abcdef', 'abcdefghi', 0, [], undefined, '>$0' + i),
  3336. i === 0 ? '>' : '>undefined',
  3337. 'supports $0' + i + ' at the end of the replacement, with no captures'
  3338. );
  3339. t.equal(
  3340. ES.GetSubstitution('abcdef', 'abcdefghi', 0, captures, undefined, '>$0' + i + '<'),
  3341. '><',
  3342. 'supports $0' + i + ' with a capture at that index'
  3343. );
  3344. t.equal(
  3345. ES.GetSubstitution('abcdef', 'abcdefghi', 0, captures, undefined, '>$0' + i),
  3346. '>',
  3347. 'supports $0' + i + ' at the end of the replacement, with a capture at that index'
  3348. );
  3349. }
  3350. }
  3351. t.end();
  3352. });
  3353. test('DateString', function (t) {
  3354. forEach(v.nonNumbers.concat(NaN), function (nonNumberOrNaN) {
  3355. t['throws'](
  3356. function () { ES.DateString(nonNumberOrNaN); },
  3357. TypeError,
  3358. debug(nonNumberOrNaN) + ' is not a non-NaN Number'
  3359. );
  3360. });
  3361. t.equal(ES.DateString(Date.UTC(2019, 8, 10, 7, 8, 9)), 'Tue Sep 10 2019');
  3362. t.equal(ES.DateString(Date.UTC(2016, 1, 29, 7, 8, 9)), 'Mon Feb 29 2016'); // leap day
  3363. t.end();
  3364. });
  3365. test('TimeString', function (t) {
  3366. forEach(v.nonNumbers.concat(NaN), function (nonNumberOrNaN) {
  3367. t['throws'](
  3368. function () { ES.TimeString(nonNumberOrNaN); },
  3369. TypeError,
  3370. debug(nonNumberOrNaN) + ' is not a non-NaN Number'
  3371. );
  3372. });
  3373. var tv = Date.UTC(2019, 8, 10, 7, 8, 9);
  3374. t.equal(ES.TimeString(tv), '07:08:09 GMT');
  3375. t.end();
  3376. });
  3377. };
  3378. var es2019 = function ES2018(ES, ops, expectedMissing, skips) {
  3379. es2018(ES, ops, expectedMissing, assign({}, skips, {
  3380. }));
  3381. test('AddEntriesFromIterable', function (t) {
  3382. t['throws'](
  3383. function () { ES.AddEntriesFromIterable({}, undefined, function () {}); },
  3384. TypeError,
  3385. 'iterable must not be undefined'
  3386. );
  3387. t['throws'](
  3388. function () { ES.AddEntriesFromIterable({}, null, function () {}); },
  3389. TypeError,
  3390. 'iterable must not be null'
  3391. );
  3392. forEach(v.nonFunctions, function (nonFunction) {
  3393. t['throws'](
  3394. function () { ES.AddEntriesFromIterable({}, {}, nonFunction); },
  3395. TypeError,
  3396. debug(nonFunction) + ' is not a function'
  3397. );
  3398. });
  3399. t.test('Symbol support', { skip: !v.hasSymbols }, function (st) {
  3400. st.plan(4);
  3401. var O = {};
  3402. st.equal(ES.AddEntriesFromIterable(O, [], function () {}), O, 'returns the target');
  3403. var adder = function (key, value) {
  3404. st.equal(this, O, 'adder gets proper receiver');
  3405. st.equal(key, 0, 'k is key');
  3406. st.equal(value, 'a', 'v is value');
  3407. };
  3408. ES.AddEntriesFromIterable(O, ['a'].entries(), adder);
  3409. st.end();
  3410. });
  3411. t.end();
  3412. });
  3413. test('FlattenIntoArray', function (t) {
  3414. t.test('no mapper function', function (st) {
  3415. var testDepth = function testDepth(tt, depth, expected) {
  3416. var a = [];
  3417. var o = [[1], 2, , [[3]], [], 4, [[[[5]]]]]; // eslint-disable-line no-sparse-arrays
  3418. ES.FlattenIntoArray(a, o, o.length, 0, depth);
  3419. tt.deepEqual(a, expected, 'depth: ' + depth);
  3420. };
  3421. testDepth(st, 1, [1, 2, [3], 4, [[[5]]]]);
  3422. testDepth(st, 2, [1, 2, 3, 4, [[5]]]);
  3423. testDepth(st, 3, [1, 2, 3, 4, [5]]);
  3424. testDepth(st, 4, [1, 2, 3, 4, 5]);
  3425. testDepth(st, Infinity, [1, 2, 3, 4, 5]);
  3426. st.end();
  3427. });
  3428. t.test('mapper function', function (st) {
  3429. var testMapper = function testMapper(tt, mapper, expected, thisArg) {
  3430. var a = [];
  3431. var o = [[1], 2, , [[3]], [], 4, [[[[5]]]]]; // eslint-disable-line no-sparse-arrays
  3432. ES.FlattenIntoArray(a, o, o.length, 0, 1, mapper, thisArg);
  3433. tt.deepEqual(a, expected);
  3434. };
  3435. var double = function double(x) {
  3436. return typeof x === 'number' ? 2 * x : x;
  3437. };
  3438. testMapper(
  3439. st,
  3440. double,
  3441. [1, 4, [3], 8, [[[5]]]]
  3442. );
  3443. testMapper(
  3444. st,
  3445. function (x) { return [this, double(x)]; },
  3446. [42, [1], 42, 4, 42, [[3]], 42, [], 42, 8, 42, [[[[5]]]]],
  3447. 42
  3448. );
  3449. st.end();
  3450. });
  3451. t.end();
  3452. });
  3453. test('TrimString', function (t) {
  3454. t.test('non-object string', function (st) {
  3455. forEach(v.nullPrimitives, function (nullish) {
  3456. st['throws'](
  3457. function () { ES.TrimString(nullish); },
  3458. debug(nullish) + ' is not an Object'
  3459. );
  3460. });
  3461. st.end();
  3462. });
  3463. var string = ' \n abc \n ';
  3464. t.equal(ES.TrimString(string, 'start'), string.slice(string.indexOf('a')));
  3465. t.equal(ES.TrimString(string, 'end'), string.slice(0, string.lastIndexOf('c') + 1));
  3466. t.equal(ES.TrimString(string, 'start+end'), string.slice(string.indexOf('a'), string.lastIndexOf('c') + 1));
  3467. t.end();
  3468. });
  3469. };
  3470. module.exports = {
  3471. es2015: es2015,
  3472. es2016: es2016,
  3473. es2017: es2017,
  3474. es2018: es2018,
  3475. es2019: es2019
  3476. };