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

es5.js 5.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. 'use strict';
  2. var test = require('tape');
  3. var toPrimitive = require('../es5');
  4. var is = require('object-is');
  5. var forEach = require('foreach');
  6. var functionName = require('function.prototype.name');
  7. var debug = require('object-inspect');
  8. test('function properties', function (t) {
  9. t.equal(toPrimitive.length, 1, 'length is 1');
  10. t.equal(functionName(toPrimitive), 'ToPrimitive', 'name is ToPrimitive');
  11. t.end();
  12. });
  13. var primitives = [null, undefined, true, false, 0, -0, 42, NaN, Infinity, -Infinity, '', 'abc'];
  14. test('primitives', function (t) {
  15. forEach(primitives, function (i) {
  16. t.ok(is(toPrimitive(i), i), 'toPrimitive(' + debug(i) + ') returns the same value');
  17. t.ok(is(toPrimitive(i, String), i), 'toPrimitive(' + debug(i) + ', String) returns the same value');
  18. t.ok(is(toPrimitive(i, Number), i), 'toPrimitive(' + debug(i) + ', Number) returns the same value');
  19. });
  20. t.end();
  21. });
  22. test('Arrays', function (t) {
  23. var arrays = [[], ['a', 'b'], [1, 2]];
  24. forEach(arrays, function (arr) {
  25. t.ok(is(toPrimitive(arr), arr.toString()), 'toPrimitive(' + debug(arr) + ') returns toString of the array');
  26. t.equal(toPrimitive(arr, String), arr.toString(), 'toPrimitive(' + debug(arr) + ') returns toString of the array');
  27. t.ok(is(toPrimitive(arr, Number), arr.toString()), 'toPrimitive(' + debug(arr) + ') returns toString of the array');
  28. });
  29. t.end();
  30. });
  31. test('Dates', function (t) {
  32. var dates = [new Date(), new Date(0), new Date(NaN)];
  33. forEach(dates, function (date) {
  34. t.equal(toPrimitive(date), date.toString(), 'toPrimitive(' + debug(date) + ') returns toString of the date');
  35. t.equal(toPrimitive(date, String), date.toString(), 'toPrimitive(' + debug(date) + ') returns toString of the date');
  36. t.ok(is(toPrimitive(date, Number), date.valueOf()), 'toPrimitive(' + debug(date) + ') returns valueOf of the date');
  37. });
  38. t.end();
  39. });
  40. var coercibleObject = { valueOf: function () { return 3; }, toString: function () { return 42; } };
  41. var valueOfOnlyObject = { valueOf: function () { return 4; }, toString: function () { return {}; } };
  42. var toStringOnlyObject = { valueOf: function () { return {}; }, toString: function () { return 7; } };
  43. var coercibleFnObject = {
  44. valueOf: function () { return function valueOfFn() {}; },
  45. toString: function () { return 42; }
  46. };
  47. var uncoercibleObject = { valueOf: function () { return {}; }, toString: function () { return {}; } };
  48. var uncoercibleFnObject = {
  49. valueOf: function () { return function valueOfFn() {}; },
  50. toString: function () { return function toStrFn() {}; }
  51. };
  52. test('Objects', function (t) {
  53. t.equal(toPrimitive(coercibleObject), coercibleObject.valueOf(), 'coercibleObject with no hint coerces to valueOf');
  54. t.equal(toPrimitive(coercibleObject, String), coercibleObject.toString(), 'coercibleObject with hint String coerces to toString');
  55. t.equal(toPrimitive(coercibleObject, Number), coercibleObject.valueOf(), 'coercibleObject with hint Number coerces to valueOf');
  56. t.equal(toPrimitive(coercibleFnObject), coercibleFnObject.toString(), 'coercibleFnObject coerces to toString');
  57. t.equal(toPrimitive(coercibleFnObject, String), coercibleFnObject.toString(), 'coercibleFnObject with hint String coerces to toString');
  58. t.equal(toPrimitive(coercibleFnObject, Number), coercibleFnObject.toString(), 'coercibleFnObject with hint Number coerces to toString');
  59. t.ok(is(toPrimitive({}), '[object Object]'), '{} with no hint coerces to Object#toString');
  60. t.equal(toPrimitive({}, String), '[object Object]', '{} with hint String coerces to Object#toString');
  61. t.ok(is(toPrimitive({}, Number), '[object Object]'), '{} with hint Number coerces to Object#toString');
  62. t.equal(toPrimitive(toStringOnlyObject), toStringOnlyObject.toString(), 'toStringOnlyObject returns toString');
  63. t.equal(toPrimitive(toStringOnlyObject, String), toStringOnlyObject.toString(), 'toStringOnlyObject with hint String returns toString');
  64. t.equal(toPrimitive(toStringOnlyObject, Number), toStringOnlyObject.toString(), 'toStringOnlyObject with hint Number returns toString');
  65. t.equal(toPrimitive(valueOfOnlyObject), valueOfOnlyObject.valueOf(), 'valueOfOnlyObject returns valueOf');
  66. t.equal(toPrimitive(valueOfOnlyObject, String), valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint String returns valueOf');
  67. t.equal(toPrimitive(valueOfOnlyObject, Number), valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint Number returns valueOf');
  68. t.test('exceptions', function (st) {
  69. st['throws'](toPrimitive.bind(null, uncoercibleObject), TypeError, 'uncoercibleObject throws a TypeError');
  70. st['throws'](toPrimitive.bind(null, uncoercibleObject, String), TypeError, 'uncoercibleObject with hint String throws a TypeError');
  71. st['throws'](toPrimitive.bind(null, uncoercibleObject, Number), TypeError, 'uncoercibleObject with hint Number throws a TypeError');
  72. st['throws'](toPrimitive.bind(null, uncoercibleFnObject), TypeError, 'uncoercibleFnObject throws a TypeError');
  73. st['throws'](toPrimitive.bind(null, uncoercibleFnObject, String), TypeError, 'uncoercibleFnObject with hint String throws a TypeError');
  74. st['throws'](toPrimitive.bind(null, uncoercibleFnObject, Number), TypeError, 'uncoercibleFnObject with hint Number throws a TypeError');
  75. st.end();
  76. });
  77. t.end();
  78. });