Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Avatar for KuCalc : devops.
Download
50655 views
1
'use strict';
2
3
var ES = require('../').ES5;
4
var test = require('tape');
5
6
var forEach = require('foreach');
7
var is = require('object-is');
8
9
var coercibleObject = { valueOf: function () { return '3'; }, toString: function () { return 42; } };
10
var coercibleFnObject = {
11
valueOf: function () { return function valueOfFn() {}; },
12
toString: function () { return 42; }
13
};
14
var valueOfOnlyObject = { valueOf: function () { return 4; }, toString: function () { return {}; } };
15
var toStringOnlyObject = { valueOf: function () { return {}; }, toString: function () { return 7; } };
16
var uncoercibleObject = { valueOf: function () { return {}; }, toString: function () { return {}; } };
17
var uncoercibleFnObject = {
18
valueOf: function () { return function valueOfFn() {}; },
19
toString: function () { return function toStrFn() {}; }
20
};
21
var objects = [{}, coercibleObject, toStringOnlyObject, valueOfOnlyObject];
22
var numbers = [0, -0, Infinity, -Infinity, 42];
23
var nonNullPrimitives = [true, false, 'foo', ''].concat(numbers);
24
var primitives = [undefined, null].concat(nonNullPrimitives);
25
26
test('ToPrimitive', function (t) {
27
t.test('primitives', function (st) {
28
var testPrimitive = function (primitive) {
29
st.ok(is(ES.ToPrimitive(primitive), primitive), primitive + ' is returned correctly');
30
};
31
forEach(primitives, testPrimitive);
32
st.end();
33
});
34
35
t.test('objects', function (st) {
36
st.equal(ES.ToPrimitive(coercibleObject), coercibleObject.valueOf(), 'coercibleObject coerces to valueOf');
37
st.equal(ES.ToPrimitive(coercibleObject, Number), coercibleObject.valueOf(), 'coercibleObject with hint Number coerces to valueOf');
38
st.equal(ES.ToPrimitive(coercibleObject, String), coercibleObject.toString(), 'coercibleObject with hint String coerces to toString');
39
st.equal(ES.ToPrimitive(coercibleFnObject), coercibleFnObject.toString(), 'coercibleFnObject coerces to toString');
40
st.equal(ES.ToPrimitive(toStringOnlyObject), toStringOnlyObject.toString(), 'toStringOnlyObject returns toString');
41
st.equal(ES.ToPrimitive(valueOfOnlyObject), valueOfOnlyObject.valueOf(), 'valueOfOnlyObject returns valueOf');
42
st.equal(ES.ToPrimitive({}), '[object Object]', '{} with no hint coerces to Object#toString');
43
st.equal(ES.ToPrimitive({}, String), '[object Object]', '{} with hint String coerces to Object#toString');
44
st.equal(ES.ToPrimitive({}, Number), '[object Object]', '{} with hint Number coerces to Object#toString');
45
st.throws(function () { return ES.ToPrimitive(uncoercibleObject); }, TypeError, 'uncoercibleObject throws a TypeError');
46
st.throws(function () { return ES.ToPrimitive(uncoercibleFnObject); }, TypeError, 'uncoercibleFnObject throws a TypeError');
47
st.end();
48
});
49
50
t.end();
51
});
52
53
test('ToBoolean', function (t) {
54
t.equal(false, ES.ToBoolean(undefined), 'undefined coerces to false');
55
t.equal(false, ES.ToBoolean(null), 'null coerces to false');
56
t.equal(false, ES.ToBoolean(false), 'false returns false');
57
t.equal(true, ES.ToBoolean(true), 'true returns true');
58
forEach([0, -0, NaN], function (falsyNumber) {
59
t.equal(false, ES.ToBoolean(falsyNumber), 'falsy number ' + falsyNumber + ' coerces to false');
60
});
61
forEach([Infinity, 42, 1, -Infinity], function (truthyNumber) {
62
t.equal(true, ES.ToBoolean(truthyNumber), 'truthy number ' + truthyNumber + ' coerces to true');
63
});
64
t.equal(false, ES.ToBoolean(''), 'empty string coerces to false');
65
t.equal(true, ES.ToBoolean('foo'), 'nonempty string coerces to true');
66
forEach(objects, function (obj) {
67
t.equal(true, ES.ToBoolean(obj), 'object coerces to true');
68
});
69
t.equal(true, ES.ToBoolean(uncoercibleObject), 'uncoercibleObject coerces to true');
70
t.end();
71
});
72
73
test('ToNumber', function (t) {
74
t.ok(is(NaN, ES.ToNumber(undefined)), 'undefined coerces to NaN');
75
t.ok(is(ES.ToNumber(null), 0), 'null coerces to +0');
76
t.ok(is(ES.ToNumber(false), 0), 'false coerces to +0');
77
t.equal(1, ES.ToNumber(true), 'true coerces to 1');
78
t.ok(is(NaN, ES.ToNumber(NaN)), 'NaN returns itself');
79
forEach([0, -0, 42, Infinity, -Infinity], function (num) {
80
t.equal(num, ES.ToNumber(num), num + ' returns itself');
81
});
82
forEach(['foo', '0', '4a', '2.0', 'Infinity', '-Infinity'], function (numString) {
83
t.ok(is(+numString, ES.ToNumber(numString)), '"' + numString + '" coerces to ' + Number(numString));
84
});
85
forEach(objects, function (object) {
86
t.ok(is(ES.ToNumber(object), ES.ToNumber(ES.ToPrimitive(object))), 'object ' + object + ' coerces to same as ToPrimitive of object does');
87
});
88
t.throws(function () { return ES.ToNumber(uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
89
t.end();
90
});
91
92
test('ToInteger', function (t) {
93
t.ok(is(0, ES.ToInteger(NaN)), 'NaN coerces to +0');
94
forEach([0, Infinity, 42], function (num) {
95
t.ok(is(num, ES.ToInteger(num)), num + ' returns itself');
96
t.ok(is(-num, ES.ToInteger(-num)), '-' + num + ' returns itself');
97
});
98
t.equal(3, ES.ToInteger(Math.PI), 'pi returns 3');
99
t.throws(function () { return ES.ToInteger(uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
100
t.end();
101
});
102
103
test('ToInt32', function (t) {
104
t.ok(is(0, ES.ToInt32(NaN)), 'NaN coerces to +0');
105
forEach([0, Infinity], function (num) {
106
t.ok(is(0, ES.ToInt32(num)), num + ' returns +0');
107
t.ok(is(0, ES.ToInt32(-num)), '-' + num + ' returns +0');
108
});
109
t.throws(function () { return ES.ToInt32(uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
110
t.ok(is(ES.ToInt32(0x100000000), 0), '2^32 returns +0');
111
t.ok(is(ES.ToInt32(0x100000000 - 1), -1), '2^32 - 1 returns -1');
112
t.ok(is(ES.ToInt32(0x80000000), -0x80000000), '2^31 returns -2^31');
113
t.ok(is(ES.ToInt32(0x80000000 - 1), 0x80000000 - 1), '2^31 - 1 returns 2^31 - 1');
114
forEach([0, Infinity, NaN, 0x100000000, 0x80000000, 0x10000, 0x42], function (num) {
115
t.ok(is(ES.ToInt32(num), ES.ToInt32(ES.ToUint32(num))), 'ToInt32(x) === ToInt32(ToUint32(x)) for 0x' + num.toString(16));
116
t.ok(is(ES.ToInt32(-num), ES.ToInt32(ES.ToUint32(-num))), 'ToInt32(x) === ToInt32(ToUint32(x)) for -0x' + num.toString(16));
117
});
118
t.end();
119
});
120
121
test('ToUint32', function (t) {
122
t.ok(is(0, ES.ToUint32(NaN)), 'NaN coerces to +0');
123
forEach([0, Infinity], function (num) {
124
t.ok(is(0, ES.ToUint32(num)), num + ' returns +0');
125
t.ok(is(0, ES.ToUint32(-num)), '-' + num + ' returns +0');
126
});
127
t.throws(function () { return ES.ToUint32(uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
128
t.ok(is(ES.ToUint32(0x100000000), 0), '2^32 returns +0');
129
t.ok(is(ES.ToUint32(0x100000000 - 1), 0x100000000 - 1), '2^32 - 1 returns 2^32 - 1');
130
t.ok(is(ES.ToUint32(0x80000000), 0x80000000), '2^31 returns 2^31');
131
t.ok(is(ES.ToUint32(0x80000000 - 1), 0x80000000 - 1), '2^31 - 1 returns 2^31 - 1');
132
forEach([0, Infinity, NaN, 0x100000000, 0x80000000, 0x10000, 0x42], function (num) {
133
t.ok(is(ES.ToUint32(num), ES.ToUint32(ES.ToInt32(num))), 'ToUint32(x) === ToUint32(ToInt32(x)) for 0x' + num.toString(16));
134
t.ok(is(ES.ToUint32(-num), ES.ToUint32(ES.ToInt32(-num))), 'ToUint32(x) === ToUint32(ToInt32(x)) for -0x' + num.toString(16));
135
});
136
t.end();
137
});
138
139
test('ToUint16', function (t) {
140
t.ok(is(0, ES.ToUint16(NaN)), 'NaN coerces to +0');
141
forEach([0, Infinity], function (num) {
142
t.ok(is(0, ES.ToUint16(num)), num + ' returns +0');
143
t.ok(is(0, ES.ToUint16(-num)), '-' + num + ' returns +0');
144
});
145
t.throws(function () { return ES.ToUint16(uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
146
t.ok(is(ES.ToUint16(0x100000000), 0), '2^32 returns +0');
147
t.ok(is(ES.ToUint16(0x100000000 - 1), 0x10000 - 1), '2^32 - 1 returns 2^16 - 1');
148
t.ok(is(ES.ToUint16(0x80000000), 0), '2^31 returns +0');
149
t.ok(is(ES.ToUint16(0x80000000 - 1), 0x10000 - 1), '2^31 - 1 returns 2^16 - 1');
150
t.ok(is(ES.ToUint16(0x10000), 0), '2^16 returns +0');
151
t.ok(is(ES.ToUint16(0x10000 - 1), 0x10000 - 1), '2^16 - 1 returns 2^16 - 1');
152
t.end();
153
});
154
155
test('ToString', function (t) {
156
t.throws(function () { return ES.ToString(uncoercibleObject); }, TypeError, 'uncoercibleObject throws');
157
t.end();
158
});
159
160
test('ToObject', function (t) {
161
t.throws(function () { return ES.ToObject(undefined); }, TypeError, 'undefined throws');
162
t.throws(function () { return ES.ToObject(null); }, TypeError, 'null throws');
163
forEach(numbers, function (number) {
164
var obj = ES.ToObject(number);
165
t.equal(typeof obj, 'object', 'number ' + number + ' coerces to object');
166
t.equal(true, obj instanceof Number, 'object of ' + number + ' is Number object');
167
t.ok(is(obj.valueOf(), number), 'object of ' + number + ' coerces to ' + number);
168
});
169
t.end();
170
});
171
172
test('CheckObjectCoercible', function (t) {
173
t.throws(function () { return ES.CheckObjectCoercible(undefined); }, TypeError, 'undefined throws');
174
t.throws(function () { return ES.CheckObjectCoercible(null); }, TypeError, 'null throws');
175
var checkCoercible = function (value) {
176
t.doesNotThrow(function () { return ES.CheckObjectCoercible(value); }, '"' + value + '" does not throw');
177
};
178
forEach(objects.concat(nonNullPrimitives), checkCoercible);
179
t.end();
180
});
181
182
test('IsCallable', function (t) {
183
t.equal(true, ES.IsCallable(function () {}), 'function is callable');
184
var nonCallables = [/a/g, {}, Object.prototype, NaN].concat(primitives);
185
forEach(nonCallables, function (nonCallable) {
186
t.equal(false, ES.IsCallable(nonCallable), nonCallable + ' is not callable');
187
});
188
t.end();
189
});
190
191
test('SameValue', function (t) {
192
t.equal(true, ES.SameValue(NaN, NaN), 'NaN is SameValue as NaN');
193
t.equal(false, ES.SameValue(0, -0), '+0 is not SameValue as -0');
194
forEach(objects.concat(primitives), function (val) {
195
t.equal(val === val, ES.SameValue(val, val), '"' + val + '" is SameValue to itself');
196
});
197
t.end();
198
});
199
200
test('Type', function (t) {
201
t.equal(ES.Type(), 'Undefined', 'Type() is Undefined');
202
t.equal(ES.Type(undefined), 'Undefined', 'Type(undefined) is Undefined');
203
t.equal(ES.Type(null), 'Null', 'Type(null) is Null');
204
t.equal(ES.Type(true), 'Boolean', 'Type(true) is Boolean');
205
t.equal(ES.Type(false), 'Boolean', 'Type(false) is Boolean');
206
t.equal(ES.Type(0), 'Number', 'Type(0) is Number');
207
t.equal(ES.Type(NaN), 'Number', 'Type(NaN) is Number');
208
t.equal(ES.Type('abc'), 'String', 'Type("abc") is String');
209
t.equal(ES.Type(function () {}), 'Object', 'Type(function () {}) is Object');
210
t.equal(ES.Type({}), 'Object', 'Type({}) is Object');
211
t.end();
212
});
213
214