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