es5.js 5.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. 'use strict';
  2. var test = require('tape');
  3. var toPrimitive = require('../es5');
  4. var is = require('object-is');
  5. var forEach = require('for-each');
  6. var functionName = require('function.prototype.name');
  7. var debug = require('object-inspect');
  8. var v = require('es-value-fixtures');
  9. test('function properties', function (t) {
  10. t.equal(toPrimitive.length, 1, 'length is 1');
  11. t.equal(functionName(toPrimitive), 'ToPrimitive', 'name is ToPrimitive');
  12. t.end();
  13. });
  14. test('primitives', function (t) {
  15. forEach(v.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('Symbols', { skip: !v.hasSymbols }, function (t) {
  23. forEach(v.symbols, function (sym) {
  24. t.equal(toPrimitive(sym), sym, 'toPrimitive(' + debug(sym) + ') returns the same value');
  25. t.equal(toPrimitive(sym, String), sym, 'toPrimitive(' + debug(sym) + ', String) returns the same value');
  26. t.equal(toPrimitive(sym, Number), sym, 'toPrimitive(' + debug(sym) + ', Number) returns the same value');
  27. });
  28. var primitiveSym = Symbol('primitiveSym');
  29. var stringSym = Symbol.prototype.toString.call(primitiveSym);
  30. var objectSym = Object(primitiveSym);
  31. t.equal(toPrimitive(objectSym), primitiveSym, 'toPrimitive(' + debug(objectSym) + ') returns ' + debug(primitiveSym));
  32. // This is different from ES2015, as the ES5 algorithm doesn't account for the existence of Symbols:
  33. t.equal(toPrimitive(objectSym, String), stringSym, 'toPrimitive(' + debug(objectSym) + ', String) returns ' + debug(stringSym));
  34. t.equal(toPrimitive(objectSym, Number), primitiveSym, 'toPrimitive(' + debug(objectSym) + ', Number) returns ' + debug(primitiveSym));
  35. t.end();
  36. });
  37. test('Arrays', function (t) {
  38. var arrays = [[], ['a', 'b'], [1, 2]];
  39. forEach(arrays, function (arr) {
  40. t.ok(is(toPrimitive(arr), arr.toString()), 'toPrimitive(' + debug(arr) + ') returns toString of the array');
  41. t.equal(toPrimitive(arr, String), arr.toString(), 'toPrimitive(' + debug(arr) + ') returns toString of the array');
  42. t.ok(is(toPrimitive(arr, Number), arr.toString()), 'toPrimitive(' + debug(arr) + ') returns toString of the array');
  43. });
  44. t.end();
  45. });
  46. test('Dates', function (t) {
  47. var dates = [new Date(), new Date(0), new Date(NaN)];
  48. forEach(dates, function (date) {
  49. t.equal(toPrimitive(date), date.toString(), 'toPrimitive(' + debug(date) + ') returns toString of the date');
  50. t.equal(toPrimitive(date, String), date.toString(), 'toPrimitive(' + debug(date) + ') returns toString of the date');
  51. t.ok(is(toPrimitive(date, Number), date.valueOf()), 'toPrimitive(' + debug(date) + ') returns valueOf of the date');
  52. });
  53. t.end();
  54. });
  55. test('Objects', function (t) {
  56. t.equal(toPrimitive(v.coercibleObject), v.coercibleObject.valueOf(), 'coercibleObject with no hint coerces to valueOf');
  57. t.equal(toPrimitive(v.coercibleObject, String), v.coercibleObject.toString(), 'coercibleObject with hint String coerces to toString');
  58. t.equal(toPrimitive(v.coercibleObject, Number), v.coercibleObject.valueOf(), 'coercibleObject with hint Number coerces to valueOf');
  59. t.equal(toPrimitive(v.coercibleFnObject), v.coercibleFnObject.toString(), 'coercibleFnObject coerces to toString');
  60. t.equal(toPrimitive(v.coercibleFnObject, String), v.coercibleFnObject.toString(), 'coercibleFnObject with hint String coerces to toString');
  61. t.equal(toPrimitive(v.coercibleFnObject, Number), v.coercibleFnObject.toString(), 'coercibleFnObject with hint Number coerces to toString');
  62. t.ok(is(toPrimitive({}), '[object Object]'), '{} with no hint coerces to Object#toString');
  63. t.equal(toPrimitive({}, String), '[object Object]', '{} with hint String coerces to Object#toString');
  64. t.ok(is(toPrimitive({}, Number), '[object Object]'), '{} with hint Number coerces to Object#toString');
  65. t.equal(toPrimitive(v.toStringOnlyObject), v.toStringOnlyObject.toString(), 'toStringOnlyObject returns toString');
  66. t.equal(toPrimitive(v.toStringOnlyObject, String), v.toStringOnlyObject.toString(), 'toStringOnlyObject with hint String returns toString');
  67. t.equal(toPrimitive(v.toStringOnlyObject, Number), v.toStringOnlyObject.toString(), 'toStringOnlyObject with hint Number returns toString');
  68. t.equal(toPrimitive(v.valueOfOnlyObject), v.valueOfOnlyObject.valueOf(), 'valueOfOnlyObject returns valueOf');
  69. t.equal(toPrimitive(v.valueOfOnlyObject, String), v.valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint String returns valueOf');
  70. t.equal(toPrimitive(v.valueOfOnlyObject, Number), v.valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint Number returns valueOf');
  71. t.test('exceptions', function (st) {
  72. st['throws'](toPrimitive.bind(null, v.uncoercibleObject), TypeError, 'uncoercibleObject throws a TypeError');
  73. st['throws'](toPrimitive.bind(null, v.uncoercibleObject, String), TypeError, 'uncoercibleObject with hint String throws a TypeError');
  74. st['throws'](toPrimitive.bind(null, v.uncoercibleObject, Number), TypeError, 'uncoercibleObject with hint Number throws a TypeError');
  75. st['throws'](toPrimitive.bind(null, v.uncoercibleFnObject), TypeError, 'uncoercibleFnObject throws a TypeError');
  76. st['throws'](toPrimitive.bind(null, v.uncoercibleFnObject, String), TypeError, 'uncoercibleFnObject with hint String throws a TypeError');
  77. st['throws'](toPrimitive.bind(null, v.uncoercibleFnObject, Number), TypeError, 'uncoercibleFnObject with hint Number throws a TypeError');
  78. st.end();
  79. });
  80. t.end();
  81. });