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

tests.js 131KB

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