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