Repositorio del curso CCOM4030 el semestre B91 del proyecto kilometro0

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. 'use strict';
  2. var test = require('tape');
  3. var toPrimitive = require('../es6');
  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. var hasSymbols = typeof Symbol === 'function' && typeof Symbol.iterator === 'symbol';
  9. var hasSymbolToPrimitive = hasSymbols && typeof Symbol.toPrimitive === 'symbol';
  10. test('function properties', function (t) {
  11. t.equal(toPrimitive.length, 1, 'length is 1');
  12. t.equal(functionName(toPrimitive), 'ToPrimitive', 'name is ToPrimitive');
  13. t.end();
  14. });
  15. var primitives = [null, undefined, true, false, 0, -0, 42, NaN, Infinity, -Infinity, '', 'abc'];
  16. test('primitives', function (t) {
  17. forEach(primitives, function (i) {
  18. t.ok(is(toPrimitive(i), i), 'toPrimitive(' + debug(i) + ') returns the same value');
  19. t.ok(is(toPrimitive(i, String), i), 'toPrimitive(' + debug(i) + ', String) returns the same value');
  20. t.ok(is(toPrimitive(i, Number), i), 'toPrimitive(' + debug(i) + ', Number) returns the same value');
  21. });
  22. t.end();
  23. });
  24. test('Symbols', { skip: !hasSymbols }, function (t) {
  25. var symbols = [
  26. Symbol('foo'),
  27. Symbol.iterator,
  28. Symbol['for']('foo') // eslint-disable-line no-restricted-properties
  29. ];
  30. forEach(symbols, function (sym) {
  31. t.equal(toPrimitive(sym), sym, 'toPrimitive(' + debug(sym) + ') returns the same value');
  32. t.equal(toPrimitive(sym, String), sym, 'toPrimitive(' + debug(sym) + ', String) returns the same value');
  33. t.equal(toPrimitive(sym, Number), sym, 'toPrimitive(' + debug(sym) + ', Number) returns the same value');
  34. });
  35. var primitiveSym = Symbol('primitiveSym');
  36. var objectSym = Object(primitiveSym);
  37. t.equal(toPrimitive(objectSym), primitiveSym, 'toPrimitive(' + debug(objectSym) + ') returns ' + debug(primitiveSym));
  38. t.equal(toPrimitive(objectSym, String), primitiveSym, 'toPrimitive(' + debug(objectSym) + ', String) returns ' + debug(primitiveSym));
  39. t.equal(toPrimitive(objectSym, Number), primitiveSym, 'toPrimitive(' + debug(objectSym) + ', Number) returns ' + debug(primitiveSym));
  40. t.end();
  41. });
  42. test('Arrays', function (t) {
  43. var arrays = [[], ['a', 'b'], [1, 2]];
  44. forEach(arrays, function (arr) {
  45. t.equal(toPrimitive(arr), String(arr), 'toPrimitive(' + debug(arr) + ') returns the string version of the array');
  46. t.equal(toPrimitive(arr, String), String(arr), 'toPrimitive(' + debug(arr) + ') returns the string version of the array');
  47. t.equal(toPrimitive(arr, Number), String(arr), 'toPrimitive(' + debug(arr) + ') returns the string version of the array');
  48. });
  49. t.end();
  50. });
  51. test('Dates', function (t) {
  52. var dates = [new Date(), new Date(0), new Date(NaN)];
  53. forEach(dates, function (date) {
  54. t.equal(toPrimitive(date), String(date), 'toPrimitive(' + debug(date) + ') returns the string version of the date');
  55. t.equal(toPrimitive(date, String), String(date), 'toPrimitive(' + debug(date) + ') returns the string version of the date');
  56. t.ok(is(toPrimitive(date, Number), Number(date)), 'toPrimitive(' + debug(date) + ') returns the number version of the date');
  57. });
  58. t.end();
  59. });
  60. var coercibleObject = { valueOf: function () { return 3; }, toString: function () { return 42; } };
  61. var valueOfOnlyObject = { valueOf: function () { return 4; }, toString: function () { return {}; } };
  62. var toStringOnlyObject = { valueOf: function () { return {}; }, toString: function () { return 7; } };
  63. var coercibleFnObject = {
  64. valueOf: function () { return function valueOfFn() {}; },
  65. toString: function () { return 42; }
  66. };
  67. var uncoercibleObject = { valueOf: function () { return {}; }, toString: function () { return {}; } };
  68. var uncoercibleFnObject = {
  69. valueOf: function () { return function valueOfFn() {}; },
  70. toString: function () { return function toStrFn() {}; }
  71. };
  72. test('Objects', function (t) {
  73. t.equal(toPrimitive(coercibleObject), coercibleObject.valueOf(), 'coercibleObject with no hint coerces to valueOf');
  74. t.equal(toPrimitive(coercibleObject, Number), coercibleObject.valueOf(), 'coercibleObject with hint Number coerces to valueOf');
  75. t.equal(toPrimitive(coercibleObject, String), coercibleObject.toString(), 'coercibleObject with hint String coerces to non-stringified toString');
  76. t.equal(toPrimitive(coercibleFnObject), coercibleFnObject.toString(), 'coercibleFnObject coerces to non-stringified toString');
  77. t.equal(toPrimitive(coercibleFnObject, Number), coercibleFnObject.toString(), 'coercibleFnObject with hint Number coerces to non-stringified toString');
  78. t.equal(toPrimitive(coercibleFnObject, String), coercibleFnObject.toString(), 'coercibleFnObject with hint String coerces to non-stringified toString');
  79. t.equal(toPrimitive({}), '[object Object]', '{} with no hint coerces to Object#toString');
  80. t.equal(toPrimitive({}, Number), '[object Object]', '{} with hint Number coerces to Object#toString');
  81. t.equal(toPrimitive({}, String), '[object Object]', '{} with hint String coerces to Object#toString');
  82. t.equal(toPrimitive(toStringOnlyObject), toStringOnlyObject.toString(), 'toStringOnlyObject returns non-stringified toString');
  83. t.equal(toPrimitive(toStringOnlyObject, Number), toStringOnlyObject.toString(), 'toStringOnlyObject with hint Number returns non-stringified toString');
  84. t.equal(toPrimitive(toStringOnlyObject, String), toStringOnlyObject.toString(), 'toStringOnlyObject with hint String returns non-stringified toString');
  85. t.equal(toPrimitive(valueOfOnlyObject), valueOfOnlyObject.valueOf(), 'valueOfOnlyObject returns valueOf');
  86. t.equal(toPrimitive(valueOfOnlyObject, Number), valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint Number returns valueOf');
  87. t.equal(toPrimitive(valueOfOnlyObject, String), valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint String returns non-stringified valueOf');
  88. t.test('Symbol.toPrimitive', { skip: !hasSymbolToPrimitive }, function (st) {
  89. var overriddenObject = { toString: st.fail, valueOf: st.fail };
  90. overriddenObject[Symbol.toPrimitive] = function (hint) { return String(hint); };
  91. st.equal(toPrimitive(overriddenObject), 'default', 'object with Symbol.toPrimitive + no hint invokes that');
  92. st.equal(toPrimitive(overriddenObject, Number), 'number', 'object with Symbol.toPrimitive + hint Number invokes that');
  93. st.equal(toPrimitive(overriddenObject, String), 'string', 'object with Symbol.toPrimitive + hint String invokes that');
  94. var nullToPrimitive = { toString: coercibleObject.toString, valueOf: coercibleObject.valueOf };
  95. nullToPrimitive[Symbol.toPrimitive] = null;
  96. st.equal(toPrimitive(nullToPrimitive), toPrimitive(coercibleObject), 'object with no hint + null Symbol.toPrimitive ignores it');
  97. st.equal(toPrimitive(nullToPrimitive, Number), toPrimitive(coercibleObject, Number), 'object with hint Number + null Symbol.toPrimitive ignores it');
  98. st.equal(toPrimitive(nullToPrimitive, String), toPrimitive(coercibleObject, String), 'object with hint String + null Symbol.toPrimitive ignores it');
  99. st.test('exceptions', function (sst) {
  100. var nonFunctionToPrimitive = { toString: sst.fail, valueOf: sst.fail };
  101. nonFunctionToPrimitive[Symbol.toPrimitive] = {};
  102. sst['throws'](toPrimitive.bind(null, nonFunctionToPrimitive), TypeError, 'Symbol.toPrimitive returning a non-function throws');
  103. var uncoercibleToPrimitive = { toString: sst.fail, valueOf: sst.fail };
  104. uncoercibleToPrimitive[Symbol.toPrimitive] = function (hint) {
  105. return { toString: function () { return hint; } };
  106. };
  107. sst['throws'](toPrimitive.bind(null, uncoercibleToPrimitive), TypeError, 'Symbol.toPrimitive returning an object throws');
  108. var throwingToPrimitive = { toString: sst.fail, valueOf: sst.fail };
  109. throwingToPrimitive[Symbol.toPrimitive] = function (hint) { throw new RangeError(hint); };
  110. sst['throws'](toPrimitive.bind(null, throwingToPrimitive), RangeError, 'Symbol.toPrimitive throwing throws');
  111. sst.end();
  112. });
  113. st.end();
  114. });
  115. t.test('exceptions', function (st) {
  116. st['throws'](toPrimitive.bind(null, uncoercibleObject), TypeError, 'uncoercibleObject throws a TypeError');
  117. st['throws'](toPrimitive.bind(null, uncoercibleObject, Number), TypeError, 'uncoercibleObject with hint Number throws a TypeError');
  118. st['throws'](toPrimitive.bind(null, uncoercibleObject, String), TypeError, 'uncoercibleObject with hint String throws a TypeError');
  119. st['throws'](toPrimitive.bind(null, uncoercibleFnObject), TypeError, 'uncoercibleFnObject throws a TypeError');
  120. st['throws'](toPrimitive.bind(null, uncoercibleFnObject, Number), TypeError, 'uncoercibleFnObject with hint Number throws a TypeError');
  121. st['throws'](toPrimitive.bind(null, uncoercibleFnObject, String), TypeError, 'uncoercibleFnObject with hint String throws a TypeError');
  122. st.end();
  123. });
  124. t.end();
  125. });