tests.js 68 KB

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