tests.js 53 KB

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