Repositorio del curso CCOM4030 el semestre B91 del proyecto Paz para la Mujer

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782
  1. 'use strict';
  2. var ES = require('../').ES5;
  3. var test = require('tape');
  4. var forEach = require('foreach');
  5. var is = require('object-is');
  6. var debug = require('object-inspect');
  7. var v = require('./helpers/values');
  8. test('ToPrimitive', function (t) {
  9. t.test('primitives', function (st) {
  10. var testPrimitive = function (primitive) {
  11. st.ok(is(ES.ToPrimitive(primitive), primitive), debug(primitive) + ' is returned correctly');
  12. };
  13. forEach(v.primitives, testPrimitive);
  14. st.end();
  15. });
  16. t.test('objects', function (st) {
  17. st.equal(ES.ToPrimitive(v.coercibleObject), v.coercibleObject.valueOf(), 'coercibleObject coerces to valueOf');
  18. st.equal(ES.ToPrimitive(v.coercibleObject, Number), v.coercibleObject.valueOf(), 'coercibleObject with hint Number coerces to valueOf');
  19. st.equal(ES.ToPrimitive(v.coercibleObject, String), v.coercibleObject.toString(), 'coercibleObject with hint String coerces to toString');
  20. st.equal(ES.ToPrimitive(v.coercibleFnObject), v.coercibleFnObject.toString(), 'coercibleFnObject coerces to toString');
  21. st.equal(ES.ToPrimitive(v.toStringOnlyObject), v.toStringOnlyObject.toString(), 'toStringOnlyObject returns toString');
  22. st.equal(ES.ToPrimitive(v.valueOfOnlyObject), v.valueOfOnlyObject.valueOf(), 'valueOfOnlyObject returns valueOf');
  23. st.equal(ES.ToPrimitive({}), '[object Object]', '{} with no hint coerces to Object#toString');
  24. st.equal(ES.ToPrimitive({}, String), '[object Object]', '{} with hint String coerces to Object#toString');
  25. st.equal(ES.ToPrimitive({}, Number), '[object Object]', '{} with hint Number coerces to Object#toString');
  26. st['throws'](function () { return ES.ToPrimitive(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws a TypeError');
  27. st['throws'](function () { return ES.ToPrimitive(v.uncoercibleFnObject); }, TypeError, 'uncoercibleFnObject throws a TypeError');
  28. st.end();
  29. });
  30. t.end();
  31. });
  32. test('ToBoolean', function (t) {
  33. t.equal(false, ES.ToBoolean(undefined), 'undefined coerces to false');
  34. t.equal(false, ES.ToBoolean(null), 'null coerces to false');
  35. t.equal(false, ES.ToBoolean(false), 'false returns false');
  36. t.equal(true, ES.ToBoolean(true), 'true returns true');
  37. forEach([0, -0, NaN], function (falsyNumber) {
  38. t.equal(false, ES.ToBoolean(falsyNumber), 'falsy number ' + falsyNumber + ' coerces to false');
  39. });
  40. forEach([Infinity, 42, 1, -Infinity], function (truthyNumber) {
  41. t.equal(true, ES.ToBoolean(truthyNumber), 'truthy number ' + truthyNumber + ' coerces to true');
  42. });
  43. t.equal(false, ES.ToBoolean(''), 'empty string coerces to false');
  44. t.equal(true, ES.ToBoolean('foo'), 'nonempty string coerces to true');
  45. forEach(v.objects, function (obj) {
  46. t.equal(true, ES.ToBoolean(obj), 'object coerces to true');
  47. });
  48. t.equal(true, ES.ToBoolean(v.uncoercibleObject), 'uncoercibleObject coerces to true');
  49. t.end();
  50. });
  51. test('ToNumber', function (t) {
  52. t.ok(is(NaN, ES.ToNumber(undefined)), 'undefined coerces to NaN');
  53. t.ok(is(ES.ToNumber(null), 0), 'null coerces to +0');
  54. t.ok(is(ES.ToNumber(false), 0), 'false coerces to +0');
  55. t.equal(1, ES.ToNumber(true), 'true coerces to 1');
  56. t.ok(is(NaN, ES.ToNumber(NaN)), 'NaN returns itself');
  57. forEach([0, -0, 42, Infinity, -Infinity], function (num) {
  58. t.equal(num, ES.ToNumber(num), num + ' returns itself');
  59. });
  60. forEach(['foo', '0', '4a', '2.0', 'Infinity', '-Infinity'], function (numString) {
  61. t.ok(is(+numString, ES.ToNumber(numString)), '"' + numString + '" coerces to ' + Number(numString));
  62. });
  63. forEach(v.objects, function (object) {
  64. t.ok(is(ES.ToNumber(object), ES.ToNumber(ES.ToPrimitive(object))), 'object ' + object + ' coerces to same as ToPrimitive of object does');
  65. });
  66. t['throws'](function () { return ES.ToNumber(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
  67. t.end();
  68. });
  69. test('ToInteger', function (t) {
  70. t.ok(is(0, ES.ToInteger(NaN)), 'NaN coerces to +0');
  71. forEach([0, Infinity, 42], function (num) {
  72. t.ok(is(num, ES.ToInteger(num)), num + ' returns itself');
  73. t.ok(is(-num, ES.ToInteger(-num)), '-' + num + ' returns itself');
  74. });
  75. t.equal(3, ES.ToInteger(Math.PI), 'pi returns 3');
  76. t['throws'](function () { return ES.ToInteger(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
  77. t.end();
  78. });
  79. test('ToInt32', function (t) {
  80. t.ok(is(0, ES.ToInt32(NaN)), 'NaN coerces to +0');
  81. forEach([0, Infinity], function (num) {
  82. t.ok(is(0, ES.ToInt32(num)), num + ' returns +0');
  83. t.ok(is(0, ES.ToInt32(-num)), '-' + num + ' returns +0');
  84. });
  85. t['throws'](function () { return ES.ToInt32(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
  86. t.ok(is(ES.ToInt32(0x100000000), 0), '2^32 returns +0');
  87. t.ok(is(ES.ToInt32(0x100000000 - 1), -1), '2^32 - 1 returns -1');
  88. t.ok(is(ES.ToInt32(0x80000000), -0x80000000), '2^31 returns -2^31');
  89. t.ok(is(ES.ToInt32(0x80000000 - 1), 0x80000000 - 1), '2^31 - 1 returns 2^31 - 1');
  90. forEach([0, Infinity, NaN, 0x100000000, 0x80000000, 0x10000, 0x42], function (num) {
  91. t.ok(is(ES.ToInt32(num), ES.ToInt32(ES.ToUint32(num))), 'ToInt32(x) === ToInt32(ToUint32(x)) for 0x' + num.toString(16));
  92. t.ok(is(ES.ToInt32(-num), ES.ToInt32(ES.ToUint32(-num))), 'ToInt32(x) === ToInt32(ToUint32(x)) for -0x' + num.toString(16));
  93. });
  94. t.end();
  95. });
  96. test('ToUint32', function (t) {
  97. t.ok(is(0, ES.ToUint32(NaN)), 'NaN coerces to +0');
  98. forEach([0, Infinity], function (num) {
  99. t.ok(is(0, ES.ToUint32(num)), num + ' returns +0');
  100. t.ok(is(0, ES.ToUint32(-num)), '-' + num + ' returns +0');
  101. });
  102. t['throws'](function () { return ES.ToUint32(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
  103. t.ok(is(ES.ToUint32(0x100000000), 0), '2^32 returns +0');
  104. t.ok(is(ES.ToUint32(0x100000000 - 1), 0x100000000 - 1), '2^32 - 1 returns 2^32 - 1');
  105. t.ok(is(ES.ToUint32(0x80000000), 0x80000000), '2^31 returns 2^31');
  106. t.ok(is(ES.ToUint32(0x80000000 - 1), 0x80000000 - 1), '2^31 - 1 returns 2^31 - 1');
  107. forEach([0, Infinity, NaN, 0x100000000, 0x80000000, 0x10000, 0x42], function (num) {
  108. t.ok(is(ES.ToUint32(num), ES.ToUint32(ES.ToInt32(num))), 'ToUint32(x) === ToUint32(ToInt32(x)) for 0x' + num.toString(16));
  109. t.ok(is(ES.ToUint32(-num), ES.ToUint32(ES.ToInt32(-num))), 'ToUint32(x) === ToUint32(ToInt32(x)) for -0x' + num.toString(16));
  110. });
  111. t.end();
  112. });
  113. test('ToUint16', function (t) {
  114. t.ok(is(0, ES.ToUint16(NaN)), 'NaN coerces to +0');
  115. forEach([0, Infinity], function (num) {
  116. t.ok(is(0, ES.ToUint16(num)), num + ' returns +0');
  117. t.ok(is(0, ES.ToUint16(-num)), '-' + num + ' returns +0');
  118. });
  119. t['throws'](function () { return ES.ToUint16(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
  120. t.ok(is(ES.ToUint16(0x100000000), 0), '2^32 returns +0');
  121. t.ok(is(ES.ToUint16(0x100000000 - 1), 0x10000 - 1), '2^32 - 1 returns 2^16 - 1');
  122. t.ok(is(ES.ToUint16(0x80000000), 0), '2^31 returns +0');
  123. t.ok(is(ES.ToUint16(0x80000000 - 1), 0x10000 - 1), '2^31 - 1 returns 2^16 - 1');
  124. t.ok(is(ES.ToUint16(0x10000), 0), '2^16 returns +0');
  125. t.ok(is(ES.ToUint16(0x10000 - 1), 0x10000 - 1), '2^16 - 1 returns 2^16 - 1');
  126. t.end();
  127. });
  128. test('ToString', function (t) {
  129. t['throws'](function () { return ES.ToString(v.uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
  130. t.end();
  131. });
  132. test('ToObject', function (t) {
  133. t['throws'](function () { return ES.ToObject(undefined); }, TypeError, 'undefined throws');
  134. t['throws'](function () { return ES.ToObject(null); }, TypeError, 'null throws');
  135. forEach(v.numbers, function (number) {
  136. var obj = ES.ToObject(number);
  137. t.equal(typeof obj, 'object', 'number ' + number + ' coerces to object');
  138. t.equal(true, obj instanceof Number, 'object of ' + number + ' is Number object');
  139. t.ok(is(obj.valueOf(), number), 'object of ' + number + ' coerces to ' + number);
  140. });
  141. t.end();
  142. });
  143. test('CheckObjectCoercible', function (t) {
  144. t['throws'](function () { return ES.CheckObjectCoercible(undefined); }, TypeError, 'undefined throws');
  145. t['throws'](function () { return ES.CheckObjectCoercible(null); }, TypeError, 'null throws');
  146. var checkCoercible = function (value) {
  147. t.doesNotThrow(function () { return ES.CheckObjectCoercible(value); }, debug(value) + ' does not throw');
  148. };
  149. forEach(v.objects.concat(v.nonNullPrimitives), checkCoercible);
  150. t.end();
  151. });
  152. test('IsCallable', function (t) {
  153. t.equal(true, ES.IsCallable(function () {}), 'function is callable');
  154. var nonCallables = [/a/g, {}, Object.prototype, NaN].concat(v.primitives);
  155. forEach(nonCallables, function (nonCallable) {
  156. t.equal(false, ES.IsCallable(nonCallable), debug(nonCallable) + ' is not callable');
  157. });
  158. t.end();
  159. });
  160. test('SameValue', function (t) {
  161. t.equal(true, ES.SameValue(NaN, NaN), 'NaN is SameValue as NaN');
  162. t.equal(false, ES.SameValue(0, -0), '+0 is not SameValue as -0');
  163. forEach(v.objects.concat(v.primitives), function (val) {
  164. t.equal(val === val, ES.SameValue(val, val), debug(val) + ' is SameValue to itself');
  165. });
  166. t.end();
  167. });
  168. test('Type', function (t) {
  169. t.equal(ES.Type(), 'Undefined', 'Type() is Undefined');
  170. t.equal(ES.Type(undefined), 'Undefined', 'Type(undefined) is Undefined');
  171. t.equal(ES.Type(null), 'Null', 'Type(null) is Null');
  172. t.equal(ES.Type(true), 'Boolean', 'Type(true) is Boolean');
  173. t.equal(ES.Type(false), 'Boolean', 'Type(false) is Boolean');
  174. t.equal(ES.Type(0), 'Number', 'Type(0) is Number');
  175. t.equal(ES.Type(NaN), 'Number', 'Type(NaN) is Number');
  176. t.equal(ES.Type('abc'), 'String', 'Type("abc") is String');
  177. t.equal(ES.Type(function () {}), 'Object', 'Type(function () {}) is Object');
  178. t.equal(ES.Type({}), 'Object', 'Type({}) is Object');
  179. t.end();
  180. });
  181. test('IsPropertyDescriptor', function (t) {
  182. forEach(v.primitives, function (primitive) {
  183. t.equal(ES.IsPropertyDescriptor(primitive), false, debug(primitive) + ' is not a Property Descriptor');
  184. });
  185. t.equal(ES.IsPropertyDescriptor({ invalid: true }), false, 'invalid keys not allowed on a Property Descriptor');
  186. t.equal(ES.IsPropertyDescriptor({}), true, 'empty object is an incomplete Property Descriptor');
  187. t.equal(ES.IsPropertyDescriptor(v.accessorDescriptor()), true, 'accessor descriptor is a Property Descriptor');
  188. t.equal(ES.IsPropertyDescriptor(v.mutatorDescriptor()), true, 'mutator descriptor is a Property Descriptor');
  189. t.equal(ES.IsPropertyDescriptor(v.dataDescriptor()), true, 'data descriptor is a Property Descriptor');
  190. t.equal(ES.IsPropertyDescriptor(v.genericDescriptor()), true, 'generic descriptor is a Property Descriptor');
  191. t['throws'](
  192. function () { ES.IsPropertyDescriptor(v.bothDescriptor()); },
  193. TypeError,
  194. 'a Property Descriptor can not be both a Data and an Accessor Descriptor'
  195. );
  196. t['throws'](
  197. function () { ES.IsPropertyDescriptor(v.bothDescriptorWritable()); },
  198. TypeError,
  199. 'a Property Descriptor can not be both a Data and an Accessor Descriptor'
  200. );
  201. t.end();
  202. });
  203. test('IsAccessorDescriptor', function (t) {
  204. forEach(v.nonNullPrimitives.concat(null), function (primitive) {
  205. t['throws'](function () { ES.IsAccessorDescriptor(primitive); }, TypeError, debug(primitive) + ' is not a Property Descriptor');
  206. });
  207. t.equal(ES.IsAccessorDescriptor(), false, 'no value is not an Accessor Descriptor');
  208. t.equal(ES.IsAccessorDescriptor(undefined), false, 'undefined value is not an Accessor Descriptor');
  209. t.equal(ES.IsAccessorDescriptor(v.accessorDescriptor()), true, 'accessor descriptor is an Accessor Descriptor');
  210. t.equal(ES.IsAccessorDescriptor(v.mutatorDescriptor()), true, 'mutator descriptor is an Accessor Descriptor');
  211. t.equal(ES.IsAccessorDescriptor(v.dataDescriptor()), false, 'data descriptor is not an Accessor Descriptor');
  212. t.equal(ES.IsAccessorDescriptor(v.genericDescriptor()), false, 'generic descriptor is not an Accessor Descriptor');
  213. t.end();
  214. });
  215. test('IsDataDescriptor', function (t) {
  216. forEach(v.nonNullPrimitives.concat(null), function (primitive) {
  217. t['throws'](function () { ES.IsDataDescriptor(primitive); }, TypeError, debug(primitive) + ' is not a Property Descriptor');
  218. });
  219. t.equal(ES.IsDataDescriptor(), false, 'no value is not a Data Descriptor');
  220. t.equal(ES.IsDataDescriptor(undefined), false, 'undefined value is not a Data Descriptor');
  221. t.equal(ES.IsDataDescriptor(v.accessorDescriptor()), false, 'accessor descriptor is not a Data Descriptor');
  222. t.equal(ES.IsDataDescriptor(v.mutatorDescriptor()), false, 'mutator descriptor is not a Data Descriptor');
  223. t.equal(ES.IsDataDescriptor(v.dataDescriptor()), true, 'data descriptor is a Data Descriptor');
  224. t.equal(ES.IsDataDescriptor(v.genericDescriptor()), false, 'generic descriptor is not a Data Descriptor');
  225. t.end();
  226. });
  227. test('IsGenericDescriptor', function (t) {
  228. forEach(v.nonNullPrimitives.concat(null), function (primitive) {
  229. t['throws'](
  230. function () { ES.IsGenericDescriptor(primitive); },
  231. TypeError,
  232. debug(primitive) + ' is not a Property Descriptor'
  233. );
  234. });
  235. t.equal(ES.IsGenericDescriptor(), false, 'no value is not a Data Descriptor');
  236. t.equal(ES.IsGenericDescriptor(undefined), false, 'undefined value is not a Data Descriptor');
  237. t.equal(ES.IsGenericDescriptor(v.accessorDescriptor()), false, 'accessor descriptor is not a generic Descriptor');
  238. t.equal(ES.IsGenericDescriptor(v.mutatorDescriptor()), false, 'mutator descriptor is not a generic Descriptor');
  239. t.equal(ES.IsGenericDescriptor(v.dataDescriptor()), false, 'data descriptor is not a generic Descriptor');
  240. t.equal(ES.IsGenericDescriptor(v.genericDescriptor()), true, 'generic descriptor is a generic Descriptor');
  241. t.end();
  242. });
  243. test('FromPropertyDescriptor', function (t) {
  244. t.equal(ES.FromPropertyDescriptor(), undefined, 'no value begets undefined');
  245. t.equal(ES.FromPropertyDescriptor(undefined), undefined, 'undefined value begets undefined');
  246. forEach(v.nonNullPrimitives.concat(null), function (primitive) {
  247. t['throws'](
  248. function () { ES.FromPropertyDescriptor(primitive); },
  249. TypeError,
  250. debug(primitive) + ' is not a Property Descriptor'
  251. );
  252. });
  253. var accessor = v.accessorDescriptor();
  254. t.deepEqual(ES.FromPropertyDescriptor(accessor), {
  255. get: accessor['[[Get]]'],
  256. set: accessor['[[Set]]'],
  257. enumerable: !!accessor['[[Enumerable]]'],
  258. configurable: !!accessor['[[Configurable]]']
  259. });
  260. var mutator = v.mutatorDescriptor();
  261. t.deepEqual(ES.FromPropertyDescriptor(mutator), {
  262. get: mutator['[[Get]]'],
  263. set: mutator['[[Set]]'],
  264. enumerable: !!mutator['[[Enumerable]]'],
  265. configurable: !!mutator['[[Configurable]]']
  266. });
  267. var data = v.dataDescriptor();
  268. t.deepEqual(ES.FromPropertyDescriptor(data), {
  269. value: data['[[Value]]'],
  270. writable: data['[[Writable]]'],
  271. enumerable: !!data['[[Enumerable]]'],
  272. configurable: !!data['[[Configurable]]']
  273. });
  274. t['throws'](
  275. function () { ES.FromPropertyDescriptor(v.genericDescriptor()); },
  276. TypeError,
  277. 'a complete Property Descriptor is required'
  278. );
  279. t.end();
  280. });
  281. test('ToPropertyDescriptor', function (t) {
  282. forEach(v.nonNullPrimitives.concat(null), function (primitive) {
  283. t['throws'](
  284. function () { ES.ToPropertyDescriptor(primitive); },
  285. TypeError,
  286. debug(primitive) + ' is not an Object'
  287. );
  288. });
  289. var accessor = v.accessorDescriptor();
  290. t.deepEqual(ES.ToPropertyDescriptor({
  291. get: accessor['[[Get]]'],
  292. enumerable: !!accessor['[[Enumerable]]'],
  293. configurable: !!accessor['[[Configurable]]']
  294. }), accessor);
  295. var mutator = v.mutatorDescriptor();
  296. t.deepEqual(ES.ToPropertyDescriptor({
  297. set: mutator['[[Set]]'],
  298. enumerable: !!mutator['[[Enumerable]]'],
  299. configurable: !!mutator['[[Configurable]]']
  300. }), mutator);
  301. var data = v.descriptors.nonConfigurable(v.dataDescriptor());
  302. t.deepEqual(ES.ToPropertyDescriptor({
  303. value: data['[[Value]]'],
  304. writable: data['[[Writable]]'],
  305. configurable: !!data['[[Configurable]]']
  306. }), data);
  307. var both = v.bothDescriptor();
  308. t['throws'](
  309. function () {
  310. ES.ToPropertyDescriptor({ get: both['[[Get]]'], value: both['[[Value]]'] });
  311. },
  312. TypeError,
  313. 'data and accessor descriptors are mutually exclusive'
  314. );
  315. t['throws'](
  316. function () { ES.ToPropertyDescriptor({ get: 'not callable' }); },
  317. TypeError,
  318. '"get" must be undefined or callable'
  319. );
  320. t['throws'](
  321. function () { ES.ToPropertyDescriptor({ set: 'not callable' }); },
  322. TypeError,
  323. '"set" must be undefined or callable'
  324. );
  325. t.end();
  326. });
  327. test('Abstract Equality Comparison', function (t) {
  328. t.test('same types use ===', function (st) {
  329. forEach(v.primitives.concat(v.objects), function (value) {
  330. st.equal(ES['Abstract Equality Comparison'](value, value), value === value, debug(value) + ' is abstractly equal to itself');
  331. });
  332. st.end();
  333. });
  334. t.test('different types coerce', function (st) {
  335. var pairs = [
  336. [null, undefined],
  337. [3, '3'],
  338. [true, '3'],
  339. [true, 3],
  340. [false, 0],
  341. [false, '0'],
  342. [3, [3]],
  343. ['3', [3]],
  344. [true, [1]],
  345. [false, [0]],
  346. [String(v.coercibleObject), v.coercibleObject],
  347. [Number(String(v.coercibleObject)), v.coercibleObject],
  348. [Number(v.coercibleObject), v.coercibleObject],
  349. [String(Number(v.coercibleObject)), v.coercibleObject]
  350. ];
  351. forEach(pairs, function (pair) {
  352. var a = pair[0];
  353. var b = pair[1];
  354. // eslint-disable-next-line eqeqeq
  355. st.equal(ES['Abstract Equality Comparison'](a, b), a == b, debug(a) + ' == ' + debug(b));
  356. // eslint-disable-next-line eqeqeq
  357. st.equal(ES['Abstract Equality Comparison'](b, a), b == a, debug(b) + ' == ' + debug(a));
  358. });
  359. st.end();
  360. });
  361. t.end();
  362. });
  363. test('Strict Equality Comparison', function (t) {
  364. t.test('same types use ===', function (st) {
  365. forEach(v.primitives.concat(v.objects), function (value) {
  366. st.equal(ES['Strict Equality Comparison'](value, value), value === value, debug(value) + ' is strictly equal to itself');
  367. });
  368. st.end();
  369. });
  370. t.test('different types are not ===', function (st) {
  371. var pairs = [
  372. [null, undefined],
  373. [3, '3'],
  374. [true, '3'],
  375. [true, 3],
  376. [false, 0],
  377. [false, '0'],
  378. [3, [3]],
  379. ['3', [3]],
  380. [true, [1]],
  381. [false, [0]],
  382. [String(v.coercibleObject), v.coercibleObject],
  383. [Number(String(v.coercibleObject)), v.coercibleObject],
  384. [Number(v.coercibleObject), v.coercibleObject],
  385. [String(Number(v.coercibleObject)), v.coercibleObject]
  386. ];
  387. forEach(pairs, function (pair) {
  388. var a = pair[0];
  389. var b = pair[1];
  390. st.equal(ES['Strict Equality Comparison'](a, b), a === b, debug(a) + ' === ' + debug(b));
  391. st.equal(ES['Strict Equality Comparison'](b, a), b === a, debug(b) + ' === ' + debug(a));
  392. });
  393. st.end();
  394. });
  395. t.end();
  396. });
  397. test('Abstract Relational Comparison', function (t) {
  398. t.test('at least one operand is NaN', function (st) {
  399. st.equal(ES['Abstract Relational Comparison'](NaN, {}, true), undefined, 'LeftFirst: first is NaN, returns undefined');
  400. st.equal(ES['Abstract Relational Comparison']({}, NaN, true), undefined, 'LeftFirst: second is NaN, returns undefined');
  401. st.equal(ES['Abstract Relational Comparison'](NaN, {}, false), undefined, '!LeftFirst: first is NaN, returns undefined');
  402. st.equal(ES['Abstract Relational Comparison']({}, NaN, false), undefined, '!LeftFirst: second is NaN, returns undefined');
  403. st.end();
  404. });
  405. t.equal(ES['Abstract Relational Comparison'](3, 4, true), true, 'LeftFirst: 3 is less than 4');
  406. t.equal(ES['Abstract Relational Comparison'](4, 3, true), false, 'LeftFirst: 3 is not less than 4');
  407. t.equal(ES['Abstract Relational Comparison'](3, 4, false), true, '!LeftFirst: 3 is less than 4');
  408. t.equal(ES['Abstract Relational Comparison'](4, 3, false), false, '!LeftFirst: 3 is not less than 4');
  409. t.equal(ES['Abstract Relational Comparison']('3', '4', true), true, 'LeftFirst: "3" is less than "4"');
  410. t.equal(ES['Abstract Relational Comparison']('4', '3', true), false, 'LeftFirst: "3" is not less than "4"');
  411. t.equal(ES['Abstract Relational Comparison']('3', '4', false), true, '!LeftFirst: "3" is less than "4"');
  412. t.equal(ES['Abstract Relational Comparison']('4', '3', false), false, '!LeftFirst: "3" is not less than "4"');
  413. t.equal(ES['Abstract Relational Comparison'](v.coercibleObject, 42, true), true, 'LeftFirst: coercible object is less than 42');
  414. t.equal(ES['Abstract Relational Comparison'](42, v.coercibleObject, true), false, 'LeftFirst: 42 is not less than coercible object');
  415. t.equal(ES['Abstract Relational Comparison'](v.coercibleObject, 42, false), true, '!LeftFirst: coercible object is less than 42');
  416. t.equal(ES['Abstract Relational Comparison'](42, v.coercibleObject, false), false, '!LeftFirst: 42 is not less than coercible object');
  417. t.equal(ES['Abstract Relational Comparison'](v.coercibleObject, '3', true), false, 'LeftFirst: coercible object is not less than "3"');
  418. t.equal(ES['Abstract Relational Comparison']('3', v.coercibleObject, true), false, 'LeftFirst: "3" is not less than coercible object');
  419. t.equal(ES['Abstract Relational Comparison'](v.coercibleObject, '3', false), false, '!LeftFirst: coercible object is not less than "3"');
  420. t.equal(ES['Abstract Relational Comparison']('3', v.coercibleObject, false), false, '!LeftFirst: "3" is not less than coercible object');
  421. t.end();
  422. });
  423. test('FromPropertyDescriptor', function (t) {
  424. t.equal(ES.FromPropertyDescriptor(), undefined, 'no value begets undefined');
  425. t.equal(ES.FromPropertyDescriptor(undefined), undefined, 'undefined value begets undefined');
  426. forEach(v.nonUndefinedPrimitives, function (primitive) {
  427. t['throws'](
  428. function () { ES.FromPropertyDescriptor(primitive); },
  429. TypeError,
  430. debug(primitive) + ' is not a Property Descriptor'
  431. );
  432. });
  433. var accessor = v.accessorDescriptor();
  434. t.deepEqual(ES.FromPropertyDescriptor(accessor), {
  435. get: accessor['[[Get]]'],
  436. set: accessor['[[Set]]'],
  437. enumerable: !!accessor['[[Enumerable]]'],
  438. configurable: !!accessor['[[Configurable]]']
  439. });
  440. var mutator = v.mutatorDescriptor();
  441. t.deepEqual(ES.FromPropertyDescriptor(mutator), {
  442. get: mutator['[[Get]]'],
  443. set: mutator['[[Set]]'],
  444. enumerable: !!mutator['[[Enumerable]]'],
  445. configurable: !!mutator['[[Configurable]]']
  446. });
  447. var data = v.dataDescriptor();
  448. t.deepEqual(ES.FromPropertyDescriptor(data), {
  449. value: data['[[Value]]'],
  450. writable: data['[[Writable]]'],
  451. enumerable: !!data['[[Enumerable]]'],
  452. configurable: !!data['[[Configurable]]']
  453. });
  454. t['throws'](
  455. function () { ES.FromPropertyDescriptor(v.genericDescriptor()); },
  456. TypeError,
  457. 'a complete Property Descriptor is required'
  458. );
  459. t.end();
  460. });
  461. test('SecFromTime', function (t) {
  462. var now = new Date();
  463. t.equal(ES.SecFromTime(now.getTime()), now.getUTCSeconds(), 'second from Date timestamp matches getUTCSeconds');
  464. t.end();
  465. });
  466. test('MinFromTime', function (t) {
  467. var now = new Date();
  468. t.equal(ES.MinFromTime(now.getTime()), now.getUTCMinutes(), 'minute from Date timestamp matches getUTCMinutes');
  469. t.end();
  470. });
  471. test('HourFromTime', function (t) {
  472. var now = new Date();
  473. t.equal(ES.HourFromTime(now.getTime()), now.getUTCHours(), 'hour from Date timestamp matches getUTCHours');
  474. t.end();
  475. });
  476. test('msFromTime', function (t) {
  477. var now = new Date();
  478. t.equal(ES.msFromTime(now.getTime()), now.getUTCMilliseconds(), 'ms from Date timestamp matches getUTCMilliseconds');
  479. t.end();
  480. });
  481. var msPerSecond = 1e3;
  482. var msPerMinute = 60 * msPerSecond;
  483. var msPerHour = 60 * msPerMinute;
  484. var msPerDay = 24 * msPerHour;
  485. test('Day', function (t) {
  486. var time = Date.UTC(2019, 8, 10, 2, 3, 4, 5);
  487. var add = 2.5;
  488. var later = new Date(time + (add * msPerDay));
  489. 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');
  490. t.end();
  491. });
  492. test('TimeWithinDay', function (t) {
  493. var time = Date.UTC(2019, 8, 10, 2, 3, 4, 5);
  494. var add = 2.5;
  495. var later = new Date(time + (add * msPerDay));
  496. 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');
  497. t.end();
  498. });
  499. test('DayFromYear', function (t) {
  500. t.equal(ES.DayFromYear(2021) - ES.DayFromYear(2020), 366, '2021 is a leap year, has 366 days');
  501. t.equal(ES.DayFromYear(2020) - ES.DayFromYear(2019), 365, '2020 is not a leap year, has 365 days');
  502. t.equal(ES.DayFromYear(2019) - ES.DayFromYear(2018), 365, '2019 is not a leap year, has 365 days');
  503. t.equal(ES.DayFromYear(2018) - ES.DayFromYear(2017), 365, '2018 is not a leap year, has 365 days');
  504. t.equal(ES.DayFromYear(2017) - ES.DayFromYear(2016), 366, '2017 is a leap year, has 366 days');
  505. t.end();
  506. });
  507. test('TimeFromYear', function (t) {
  508. for (var i = 1900; i < 2100; i += 1) {
  509. t.equal(ES.TimeFromYear(i), Date.UTC(i, 0, 1), 'TimeFromYear matches a Date object’s year: ' + i);
  510. }
  511. t.end();
  512. });
  513. test('YearFromTime', function (t) {
  514. for (var i = 1900; i < 2100; i += 1) {
  515. t.equal(ES.YearFromTime(Date.UTC(i, 0, 1)), i, 'YearFromTime matches a Date object’s year on 1/1: ' + i);
  516. t.equal(ES.YearFromTime(Date.UTC(i, 10, 1)), i, 'YearFromTime matches a Date object’s year on 10/1: ' + i);
  517. }
  518. t.end();
  519. });
  520. test('WeekDay', function (t) {
  521. var now = new Date();
  522. var today = now.getUTCDay();
  523. for (var i = 0; i < 7; i += 1) {
  524. var weekDay = ES.WeekDay(now.getTime() + (i * msPerDay));
  525. t.equal(weekDay, (today + i) % 7, i + ' days after today (' + today + '), WeekDay is ' + weekDay);
  526. }
  527. t.end();
  528. });
  529. test('DaysInYear', function (t) {
  530. t.equal(ES.DaysInYear(2021), 365, '2021 is not a leap year');
  531. t.equal(ES.DaysInYear(2020), 366, '2020 is a leap year');
  532. t.equal(ES.DaysInYear(2019), 365, '2019 is not a leap year');
  533. t.equal(ES.DaysInYear(2018), 365, '2018 is not a leap year');
  534. t.equal(ES.DaysInYear(2017), 365, '2017 is not a leap year');
  535. t.equal(ES.DaysInYear(2016), 366, '2016 is a leap year');
  536. t.end();
  537. });
  538. test('InLeapYear', function (t) {
  539. t.equal(ES.InLeapYear(Date.UTC(2021, 0, 1)), 0, '2021 is not a leap year');
  540. t.equal(ES.InLeapYear(Date.UTC(2020, 0, 1)), 1, '2020 is a leap year');
  541. t.equal(ES.InLeapYear(Date.UTC(2019, 0, 1)), 0, '2019 is not a leap year');
  542. t.equal(ES.InLeapYear(Date.UTC(2018, 0, 1)), 0, '2018 is not a leap year');
  543. t.equal(ES.InLeapYear(Date.UTC(2017, 0, 1)), 0, '2017 is not a leap year');
  544. t.equal(ES.InLeapYear(Date.UTC(2016, 0, 1)), 1, '2016 is a leap year');
  545. t.end();
  546. });
  547. test('DayWithinYear', function (t) {
  548. t.equal(ES.DayWithinYear(Date.UTC(2019, 0, 1)), 0, '1/1 is the 1st day');
  549. t.equal(ES.DayWithinYear(Date.UTC(2019, 11, 31)), 364, '12/31 is the 365th day in a non leap year');
  550. t.equal(ES.DayWithinYear(Date.UTC(2016, 11, 31)), 365, '12/31 is the 366th day in a leap year');
  551. t.end();
  552. });
  553. test('MonthFromTime', function (t) {
  554. t.equal(ES.MonthFromTime(Date.UTC(2019, 0, 1)), 0, 'non-leap: 1/1 gives January');
  555. t.equal(ES.MonthFromTime(Date.UTC(2019, 0, 31)), 0, 'non-leap: 1/31 gives January');
  556. t.equal(ES.MonthFromTime(Date.UTC(2019, 1, 1)), 1, 'non-leap: 2/1 gives February');
  557. t.equal(ES.MonthFromTime(Date.UTC(2019, 1, 28)), 1, 'non-leap: 2/28 gives February');
  558. t.equal(ES.MonthFromTime(Date.UTC(2019, 1, 29)), 2, 'non-leap: 2/29 gives March');
  559. t.equal(ES.MonthFromTime(Date.UTC(2019, 2, 1)), 2, 'non-leap: 3/1 gives March');
  560. t.equal(ES.MonthFromTime(Date.UTC(2019, 2, 31)), 2, 'non-leap: 3/31 gives March');
  561. t.equal(ES.MonthFromTime(Date.UTC(2019, 3, 1)), 3, 'non-leap: 4/1 gives April');
  562. t.equal(ES.MonthFromTime(Date.UTC(2019, 3, 30)), 3, 'non-leap: 4/30 gives April');
  563. t.equal(ES.MonthFromTime(Date.UTC(2019, 4, 1)), 4, 'non-leap: 5/1 gives May');
  564. t.equal(ES.MonthFromTime(Date.UTC(2019, 4, 31)), 4, 'non-leap: 5/31 gives May');
  565. t.equal(ES.MonthFromTime(Date.UTC(2019, 5, 1)), 5, 'non-leap: 6/1 gives June');
  566. t.equal(ES.MonthFromTime(Date.UTC(2019, 5, 30)), 5, 'non-leap: 6/30 gives June');
  567. t.equal(ES.MonthFromTime(Date.UTC(2019, 6, 1)), 6, 'non-leap: 7/1 gives July');
  568. t.equal(ES.MonthFromTime(Date.UTC(2019, 6, 31)), 6, 'non-leap: 7/31 gives July');
  569. t.equal(ES.MonthFromTime(Date.UTC(2019, 7, 1)), 7, 'non-leap: 8/1 gives August');
  570. t.equal(ES.MonthFromTime(Date.UTC(2019, 7, 30)), 7, 'non-leap: 8/30 gives August');
  571. t.equal(ES.MonthFromTime(Date.UTC(2019, 8, 1)), 8, 'non-leap: 9/1 gives September');
  572. t.equal(ES.MonthFromTime(Date.UTC(2019, 8, 30)), 8, 'non-leap: 9/30 gives September');
  573. t.equal(ES.MonthFromTime(Date.UTC(2019, 9, 1)), 9, 'non-leap: 10/1 gives October');
  574. t.equal(ES.MonthFromTime(Date.UTC(2019, 9, 31)), 9, 'non-leap: 10/31 gives October');
  575. t.equal(ES.MonthFromTime(Date.UTC(2019, 10, 1)), 10, 'non-leap: 11/1 gives November');
  576. t.equal(ES.MonthFromTime(Date.UTC(2019, 10, 30)), 10, 'non-leap: 11/30 gives November');
  577. t.equal(ES.MonthFromTime(Date.UTC(2019, 11, 1)), 11, 'non-leap: 12/1 gives December');
  578. t.equal(ES.MonthFromTime(Date.UTC(2019, 11, 31)), 11, 'non-leap: 12/31 gives December');
  579. t.equal(ES.MonthFromTime(Date.UTC(2016, 0, 1)), 0, 'leap: 1/1 gives January');
  580. t.equal(ES.MonthFromTime(Date.UTC(2016, 0, 31)), 0, 'leap: 1/31 gives January');
  581. t.equal(ES.MonthFromTime(Date.UTC(2016, 1, 1)), 1, 'leap: 2/1 gives February');
  582. t.equal(ES.MonthFromTime(Date.UTC(2016, 1, 28)), 1, 'leap: 2/28 gives February');
  583. t.equal(ES.MonthFromTime(Date.UTC(2016, 1, 29)), 1, 'leap: 2/29 gives February');
  584. t.equal(ES.MonthFromTime(Date.UTC(2016, 2, 1)), 2, 'leap: 3/1 gives March');
  585. t.equal(ES.MonthFromTime(Date.UTC(2016, 2, 31)), 2, 'leap: 3/31 gives March');
  586. t.equal(ES.MonthFromTime(Date.UTC(2016, 3, 1)), 3, 'leap: 4/1 gives April');
  587. t.equal(ES.MonthFromTime(Date.UTC(2016, 3, 30)), 3, 'leap: 4/30 gives April');
  588. t.equal(ES.MonthFromTime(Date.UTC(2016, 4, 1)), 4, 'leap: 5/1 gives May');
  589. t.equal(ES.MonthFromTime(Date.UTC(2016, 4, 31)), 4, 'leap: 5/31 gives May');
  590. t.equal(ES.MonthFromTime(Date.UTC(2016, 5, 1)), 5, 'leap: 6/1 gives June');
  591. t.equal(ES.MonthFromTime(Date.UTC(2016, 5, 30)), 5, 'leap: 6/30 gives June');
  592. t.equal(ES.MonthFromTime(Date.UTC(2016, 6, 1)), 6, 'leap: 7/1 gives July');
  593. t.equal(ES.MonthFromTime(Date.UTC(2016, 6, 31)), 6, 'leap: 7/31 gives July');
  594. t.equal(ES.MonthFromTime(Date.UTC(2016, 7, 1)), 7, 'leap: 8/1 gives August');
  595. t.equal(ES.MonthFromTime(Date.UTC(2016, 7, 30)), 7, 'leap: 8/30 gives August');
  596. t.equal(ES.MonthFromTime(Date.UTC(2016, 8, 1)), 8, 'leap: 9/1 gives September');
  597. t.equal(ES.MonthFromTime(Date.UTC(2016, 8, 30)), 8, 'leap: 9/30 gives September');
  598. t.equal(ES.MonthFromTime(Date.UTC(2016, 9, 1)), 9, 'leap: 10/1 gives October');
  599. t.equal(ES.MonthFromTime(Date.UTC(2016, 9, 31)), 9, 'leap: 10/31 gives October');
  600. t.equal(ES.MonthFromTime(Date.UTC(2016, 10, 1)), 10, 'leap: 11/1 gives November');
  601. t.equal(ES.MonthFromTime(Date.UTC(2016, 10, 30)), 10, 'leap: 11/30 gives November');
  602. t.equal(ES.MonthFromTime(Date.UTC(2016, 11, 1)), 11, 'leap: 12/1 gives December');
  603. t.equal(ES.MonthFromTime(Date.UTC(2016, 11, 31)), 11, 'leap: 12/31 gives December');
  604. t.end();
  605. });
  606. test('DateFromTime', function (t) {
  607. var i;
  608. for (i = 1; i <= 28; i += 1) {
  609. t.equal(ES.DateFromTime(Date.UTC(2019, 1, i)), i, '2019.02.' + i + ' is date ' + i);
  610. }
  611. for (i = 1; i <= 29; i += 1) {
  612. t.equal(ES.DateFromTime(Date.UTC(2016, 1, i)), i, '2016.02.' + i + ' is date ' + i);
  613. }
  614. for (i = 1; i <= 30; i += 1) {
  615. t.equal(ES.DateFromTime(Date.UTC(2019, 8, i)), i, '2019.09.' + i + ' is date ' + i);
  616. }
  617. for (i = 1; i <= 31; i += 1) {
  618. t.equal(ES.DateFromTime(Date.UTC(2019, 9, i)), i, '2019.10.' + i + ' is date ' + i);
  619. }
  620. t.end();
  621. });
  622. test('MakeDay', function (t) {
  623. var day2015 = 16687;
  624. t.equal(ES.MakeDay(2015, 8, 9), day2015, '2015.09.09 is day 16687');
  625. var day2016 = day2015 + 366; // 2016 is a leap year
  626. t.equal(ES.MakeDay(2016, 8, 9), day2016, '2015.09.09 is day 17053');
  627. var day2017 = day2016 + 365;
  628. t.equal(ES.MakeDay(2017, 8, 9), day2017, '2017.09.09 is day 17418');
  629. var day2018 = day2017 + 365;
  630. t.equal(ES.MakeDay(2018, 8, 9), day2018, '2018.09.09 is day 17783');
  631. var day2019 = day2018 + 365;
  632. t.equal(ES.MakeDay(2019, 8, 9), day2019, '2019.09.09 is day 18148');
  633. t.end();
  634. });
  635. test('MakeDate', function (t) {
  636. forEach(v.infinities.concat(NaN), function (nonFiniteNumber) {
  637. t.ok(is(ES.MakeDate(nonFiniteNumber, 0), NaN), debug(nonFiniteNumber) + ' is not a finite `day`');
  638. t.ok(is(ES.MakeDate(0, nonFiniteNumber), NaN), debug(nonFiniteNumber) + ' is not a finite `time`');
  639. });
  640. t.equal(ES.MakeDate(0, 0), 0, 'zero day and zero time is zero date');
  641. t.equal(ES.MakeDate(0, 123), 123, 'zero day and nonzero time is a date of the "time"');
  642. t.equal(ES.MakeDate(1, 0), msPerDay, 'day of 1 and zero time is a date of "ms per day"');
  643. t.equal(ES.MakeDate(3, 0), 3 * msPerDay, 'day of 3 and zero time is a date of thrice "ms per day"');
  644. t.equal(ES.MakeDate(1, 123), msPerDay + 123, 'day of 1 and nonzero time is a date of "ms per day" plus the "time"');
  645. 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"');
  646. t.end();
  647. });
  648. test('MakeTime', function (t) {
  649. forEach(v.infinities.concat(NaN), function (nonFiniteNumber) {
  650. t.ok(is(ES.MakeTime(nonFiniteNumber, 0, 0, 0), NaN), debug(nonFiniteNumber) + ' is not a finite `hour`');
  651. t.ok(is(ES.MakeTime(0, nonFiniteNumber, 0, 0), NaN), debug(nonFiniteNumber) + ' is not a finite `min`');
  652. t.ok(is(ES.MakeTime(0, 0, nonFiniteNumber, 0), NaN), debug(nonFiniteNumber) + ' is not a finite `sec`');
  653. t.ok(is(ES.MakeTime(0, 0, 0, nonFiniteNumber), NaN), debug(nonFiniteNumber) + ' is not a finite `ms`');
  654. });
  655. t.equal(
  656. ES.MakeTime(1.2, 2.3, 3.4, 4.5),
  657. (1 * msPerHour) + (2 * msPerMinute) + (3 * msPerSecond) + 4,
  658. 'all numbers are converted to integer, multiplied by the right number of ms, and summed'
  659. );
  660. t.end();
  661. });
  662. test('TimeClip', function (t) {
  663. forEach(v.infinities.concat(NaN), function (nonFiniteNumber) {
  664. t.ok(is(ES.TimeClip(nonFiniteNumber), NaN), debug(nonFiniteNumber) + ' is not a finite `time`');
  665. });
  666. t.ok(is(ES.TimeClip(8.64e15 + 1), NaN), '8.64e15 is the largest magnitude considered "finite"');
  667. t.ok(is(ES.TimeClip(-8.64e15 - 1), NaN), '-8.64e15 is the largest magnitude considered "finite"');
  668. forEach(v.zeroes.concat([-10, 10, Date.now()]), function (time) {
  669. t.equal(ES.TimeClip(time), time, debug(time) + ' is a time of ' + debug(time));
  670. });
  671. t.end();
  672. });
  673. test('modulo', function (t) {
  674. t.equal(3 % 2, 1, '+3 % 2 is +1');
  675. t.equal(ES.modulo(3, 2), 1, '+3 mod 2 is +1');
  676. t.equal(-3 % 2, -1, '-3 % 2 is -1');
  677. t.equal(ES.modulo(-3, 2), 1, '-3 mod 2 is +1');
  678. t.end();
  679. });