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