source: imaps-frontend/node_modules/es-to-primitive/test/es6.js@ 0c6b92a

main
Last change on this file since 0c6b92a was 0c6b92a, checked in by stefan toskovski <stefantoska84@…>, 5 weeks ago

Pred finalna verzija

  • Property mode set to 100644
File size: 8.0 KB
Line 
1'use strict';
2
3var test = require('tape');
4var toPrimitive = require('../es6');
5var is = require('object-is');
6var forEach = require('for-each');
7var functionName = require('function.prototype.name');
8var debug = require('object-inspect');
9var v = require('es-value-fixtures');
10
11/** @typedef {{ toString?: unknown, valueOf?: unknown, [Symbol.toPrimitive]?: unknown }} Coercible */
12
13test('function properties', function (t) {
14 t.equal(toPrimitive.length, 1, 'length is 1');
15 t.equal(functionName(toPrimitive), 'ToPrimitive', 'name is ToPrimitive');
16
17 t.end();
18});
19
20test('primitives', function (t) {
21 forEach(v.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
29test('Symbols', { skip: !v.hasSymbols }, function (t) {
30 forEach(v.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
36 var primitiveSym = Symbol('primitiveSym');
37 var objectSym = Object(primitiveSym);
38 t.equal(toPrimitive(objectSym), primitiveSym, 'toPrimitive(' + debug(objectSym) + ') returns ' + debug(primitiveSym));
39 t.equal(toPrimitive(objectSym, String), primitiveSym, 'toPrimitive(' + debug(objectSym) + ', String) returns ' + debug(primitiveSym));
40 t.equal(toPrimitive(objectSym, Number), primitiveSym, 'toPrimitive(' + debug(objectSym) + ', Number) returns ' + debug(primitiveSym));
41 t.end();
42});
43
44test('Arrays', function (t) {
45 var arrays = [[], ['a', 'b'], [1, 2]];
46 forEach(arrays, function (arr) {
47 t.equal(toPrimitive(arr), String(arr), 'toPrimitive(' + debug(arr) + ') returns the string version of the array');
48 t.equal(toPrimitive(arr, String), String(arr), 'toPrimitive(' + debug(arr) + ') returns the string version of the array');
49 t.equal(toPrimitive(arr, Number), String(arr), 'toPrimitive(' + debug(arr) + ') returns the string version of the array');
50 });
51 t.end();
52});
53
54test('Dates', function (t) {
55 var dates = [new Date(), new Date(0), new Date(NaN)];
56 forEach(dates, function (date) {
57 t.equal(toPrimitive(date), String(date), 'toPrimitive(' + debug(date) + ') returns the string version of the date');
58 t.equal(toPrimitive(date, String), String(date), 'toPrimitive(' + debug(date) + ') returns the string version of the date');
59 t.ok(is(toPrimitive(date, Number), Number(date)), 'toPrimitive(' + debug(date) + ') returns the number version of the date');
60 });
61 t.end();
62});
63
64test('Objects', function (t) {
65 t.equal(toPrimitive(v.coercibleObject), v.coercibleObject.valueOf(), 'coercibleObject with no hint coerces to valueOf');
66 t.equal(toPrimitive(v.coercibleObject, Number), v.coercibleObject.valueOf(), 'coercibleObject with hint Number coerces to valueOf');
67 t.equal(toPrimitive(v.coercibleObject, String), v.coercibleObject.toString(), 'coercibleObject with hint String coerces to non-stringified toString');
68
69 t.equal(toPrimitive(v.coercibleFnObject), v.coercibleFnObject.toString(), 'coercibleFnObject coerces to non-stringified toString');
70 t.equal(toPrimitive(v.coercibleFnObject, Number), v.coercibleFnObject.toString(), 'coercibleFnObject with hint Number coerces to non-stringified toString');
71 t.equal(toPrimitive(v.coercibleFnObject, String), v.coercibleFnObject.toString(), 'coercibleFnObject with hint String coerces to non-stringified toString');
72
73 t.equal(toPrimitive({}), '[object Object]', '{} with no hint coerces to Object#toString');
74 t.equal(toPrimitive({}, Number), '[object Object]', '{} with hint Number coerces to Object#toString');
75 t.equal(toPrimitive({}, String), '[object Object]', '{} with hint String coerces to Object#toString');
76
77 t.equal(toPrimitive(v.toStringOnlyObject), v.toStringOnlyObject.toString(), 'toStringOnlyObject returns non-stringified toString');
78 t.equal(toPrimitive(v.toStringOnlyObject, Number), v.toStringOnlyObject.toString(), 'toStringOnlyObject with hint Number returns non-stringified toString');
79 t.equal(toPrimitive(v.toStringOnlyObject, String), v.toStringOnlyObject.toString(), 'toStringOnlyObject with hint String returns non-stringified toString');
80
81 t.equal(toPrimitive(v.valueOfOnlyObject), v.valueOfOnlyObject.valueOf(), 'valueOfOnlyObject returns valueOf');
82 t.equal(toPrimitive(v.valueOfOnlyObject, Number), v.valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint Number returns valueOf');
83 t.equal(toPrimitive(v.valueOfOnlyObject, String), v.valueOfOnlyObject.valueOf(), 'valueOfOnlyObject with hint String returns non-stringified valueOf');
84
85 t.test('Symbol.toPrimitive', { skip: !v.hasSymbols || !Symbol.toPrimitive }, function (st) {
86 /** @type {Coercible} */
87 var overriddenObject = { toString: st.fail, valueOf: st.fail };
88 overriddenObject[Symbol.toPrimitive] = /** @type {(hint: string) => unknown} */ function (hint) {
89 return String(hint);
90 };
91
92 st.equal(toPrimitive(overriddenObject), 'default', 'object with Symbol.toPrimitive + no hint invokes that');
93 st.equal(toPrimitive(overriddenObject, Number), 'number', 'object with Symbol.toPrimitive + hint Number invokes that');
94 st.equal(toPrimitive(overriddenObject, String), 'string', 'object with Symbol.toPrimitive + hint String invokes that');
95
96 /** @type {Coercible} */
97 var nullToPrimitive = { toString: v.coercibleObject.toString, valueOf: v.coercibleObject.valueOf };
98 nullToPrimitive[Symbol.toPrimitive] = null;
99 st.equal(toPrimitive(nullToPrimitive), toPrimitive(v.coercibleObject), 'object with no hint + null Symbol.toPrimitive ignores it');
100 st.equal(toPrimitive(nullToPrimitive, Number), toPrimitive(v.coercibleObject, Number), 'object with hint Number + null Symbol.toPrimitive ignores it');
101 st.equal(toPrimitive(nullToPrimitive, String), toPrimitive(v.coercibleObject, String), 'object with hint String + null Symbol.toPrimitive ignores it');
102
103 st.test('exceptions', function (sst) {
104 /** @type {Coercible} */
105 var nonFunctionToPrimitive = { toString: sst.fail, valueOf: sst.fail };
106 nonFunctionToPrimitive[Symbol.toPrimitive] = {};
107 sst['throws'](toPrimitive.bind(null, nonFunctionToPrimitive), TypeError, 'Symbol.toPrimitive returning a non-function throws');
108
109 /** @type {Coercible} */
110 var uncoercibleToPrimitive = { toString: sst.fail, valueOf: sst.fail };
111 uncoercibleToPrimitive[Symbol.toPrimitive] = /** @type {(hint: string) => unknown} */ function (hint) {
112 return { toString: function () { return hint; } };
113 };
114 sst['throws'](toPrimitive.bind(null, uncoercibleToPrimitive), TypeError, 'Symbol.toPrimitive returning an object throws');
115
116 /** @type {Coercible} */
117 var throwingToPrimitive = { toString: sst.fail, valueOf: sst.fail };
118 throwingToPrimitive[Symbol.toPrimitive] = /** @type {(hint: string) => unknown} */ function (hint) {
119 throw new RangeError(hint);
120 };
121 sst['throws'](toPrimitive.bind(null, throwingToPrimitive), RangeError, 'Symbol.toPrimitive throwing throws');
122
123 sst.end();
124 });
125
126 st.end();
127 });
128
129 t.test('exceptions', function (st) {
130 st['throws'](toPrimitive.bind(null, v.uncoercibleObject), TypeError, 'uncoercibleObject throws a TypeError');
131 st['throws'](toPrimitive.bind(null, v.uncoercibleObject, Number), TypeError, 'uncoercibleObject with hint Number throws a TypeError');
132 st['throws'](toPrimitive.bind(null, v.uncoercibleObject, String), TypeError, 'uncoercibleObject with hint String throws a TypeError');
133
134 st['throws'](toPrimitive.bind(null, v.uncoercibleFnObject), TypeError, 'uncoercibleFnObject throws a TypeError');
135 st['throws'](toPrimitive.bind(null, v.uncoercibleFnObject, Number), TypeError, 'uncoercibleFnObject with hint Number throws a TypeError');
136 st['throws'](toPrimitive.bind(null, v.uncoercibleFnObject, String), TypeError, 'uncoercibleFnObject with hint String throws a TypeError');
137 st.end();
138 });
139 t.end();
140});
Note: See TracBrowser for help on using the repository browser.