1
|
|
|
// spec/Throwable.spec.js |
2
|
|
|
"use strict"; |
3
|
|
|
|
4
|
|
|
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * |
5
|
|
|
* Copyright (C) EgaLabs, Inc - All Rights Reserved * |
6
|
|
|
* Unauthorized copying of this file, either completely or partially, via any medium, is strictly prohibited. * |
7
|
|
|
* Proprietary and confidential. * |
8
|
|
|
* Written by Esaú García Sánchez-Torija <[email protected]>, March 2017 * |
9
|
|
|
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
10
|
|
|
|
11
|
|
|
// :: DEPENDENCIES |
12
|
|
|
|
13
|
|
|
// load native dependencies |
14
|
|
|
const path = require("path"); |
15
|
|
|
|
16
|
|
|
// load local dependencies |
17
|
|
|
const root = path.dirname(__dirname); |
18
|
|
|
const Throwable = require(path.join(root, "Throwable.js")); |
19
|
|
|
|
20
|
|
|
// :: TESTING |
21
|
|
|
|
22
|
|
|
// test the Throwable 'class' |
23
|
|
|
describe("Throwable", () => { |
24
|
|
|
|
25
|
|
|
// :: CONSTRUCTOR |
26
|
|
|
|
27
|
|
|
it("should instantiate without parameters", () => { |
28
|
|
|
for (let i = 0; i < 2; i += 1) { |
29
|
|
|
for (let j = 0; j < 2; j += 1) { |
30
|
|
|
for (let e = 0; e < 2; e += 1) { |
31
|
|
|
const arg1 = (i % 2 === 0 ? undefined : null); |
32
|
|
|
const arg2 = (j % 2 === 0 ? undefined : null); |
33
|
|
|
const arg3 = (e % 2 === 0 ? undefined : null); |
34
|
|
|
const test = (() => new Throwable(arg1, arg2, arg3)); |
35
|
|
|
expect(test).not.toThrowError("parameter 'name' must be a 'string'"); |
36
|
|
|
expect(test).not.toThrowError("parameter 'message' must be a 'string'"); |
37
|
|
|
expect(test).not.toThrowError("parameter 'code' must be a 'number'"); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
expect(() => new Throwable).not.toThrowError("parameter 'name' must be a 'string'"); |
42
|
|
|
expect(() => new Throwable).not.toThrowError("parameter 'message' must be a 'string'"); |
43
|
|
|
expect(() => new Throwable).not.toThrowError("parameter 'code' must be a 'number'"); |
44
|
|
|
}); |
45
|
|
|
|
46
|
|
|
it("should instantiate with parameters", () => { |
47
|
|
|
const args1 = [undefined, null, "Throwable"]; |
48
|
|
|
const args2 = [undefined, null, "thrown"]; |
49
|
|
|
const args3 = [undefined, null, Math.round(Math.random() * 0xFFFFFFFF)]; |
50
|
|
|
for (let i = 0; i < args1.length; i += 1) { |
51
|
|
|
const arg1 = args1[i]; |
52
|
|
|
for (let j = 0; j < args2.length; j += 1) { |
53
|
|
|
const arg2 = args2[j]; |
54
|
|
|
for (let e = 0; e < args3.length; e += 1) { |
55
|
|
|
const arg3 = args3[e]; |
56
|
|
|
const test = (() => new Throwable(arg1, arg2, arg3)); |
57
|
|
|
expect(test).not.toThrowError("parameter 'name' must be a 'string'"); |
58
|
|
|
expect(test).not.toThrowError("parameter 'message' must be a 'string'"); |
59
|
|
|
expect(test).not.toThrowError("parameter 'code' must be a 'number'"); |
60
|
|
|
} |
61
|
|
|
const test = (() => new Throwable(arg1, arg2)); |
62
|
|
|
expect(test).not.toThrowError("parameter 'name' must be a 'string'"); |
63
|
|
|
expect(test).not.toThrowError("parameter 'message' must be a 'string'"); |
64
|
|
|
expect(test).not.toThrowError("parameter 'code' must be a 'number'"); |
65
|
|
|
} |
66
|
|
|
const test = (() => new Throwable(arg1)); |
67
|
|
|
expect(test).not.toThrowError("parameter 'name' must be a 'string'"); |
68
|
|
|
expect(test).not.toThrowError("parameter 'message' must be a 'string'"); |
69
|
|
|
expect(test).not.toThrowError("parameter 'code' must be a 'number'"); |
70
|
|
|
} |
71
|
|
|
}); |
72
|
|
|
|
73
|
|
|
it("should throw an Error if 'name' parameter is invalid", () => { |
74
|
|
|
const noStr = [{}, true, false, 42, 3.1416, -42, -3.1416, () => { |
75
|
|
|
}]; |
76
|
|
|
const noNmb = [{}, true, false, "", "qwerty", () => { |
77
|
|
|
}]; |
78
|
|
|
if (typeof Symbol === "function") { |
|
|
|
|
79
|
|
|
noStr.push(Symbol("symbol")); |
80
|
|
|
noNmb.push(Symbol("symbol")); |
81
|
|
|
} |
82
|
|
|
for (let i = 0; i < noStr.length; i += 1) { |
83
|
|
|
const arg1 = noStr[i]; |
84
|
|
|
for (let j = 0; j < noStr.length; j += 1) { |
85
|
|
|
const arg2 = noStr[j]; |
86
|
|
|
for (let e = 0; e < noNmb.length; e += 1) { |
87
|
|
|
const arg3 = noNmb[e]; |
88
|
|
|
const test3 = (() => new Throwable(arg1, arg2, arg3)); |
89
|
|
|
expect(test3).toThrowError("parameter 'name' must be a 'string'"); |
90
|
|
|
const test2 = (() => new Throwable(null, arg2, arg3)); |
91
|
|
|
expect(test2).toThrowError("parameter 'message' must be a 'string'"); |
92
|
|
|
const test1 = (() => new Throwable(null, null, arg3)); |
93
|
|
|
expect(test1).toThrowError("parameter 'code' must be a 'number'"); |
94
|
|
|
} |
95
|
|
|
const test2 = (() => new Throwable(arg1, arg2)); |
96
|
|
|
expect(test2).toThrowError("parameter 'name' must be a 'string'"); |
97
|
|
|
const test1 = (() => new Throwable(null, arg2)); |
98
|
|
|
expect(test1).toThrowError("parameter 'message' must be a 'string'"); |
99
|
|
|
} |
100
|
|
|
const test1 = (() => new Throwable(arg1)); |
101
|
|
|
expect(test1).toThrowError("parameter 'name' must be a 'string'"); |
102
|
|
|
} |
103
|
|
|
}); |
104
|
|
|
|
105
|
|
|
// :: PROPERTIES |
106
|
|
|
|
107
|
|
|
it("should have all correct properties", () => { |
108
|
|
|
const name = "qwerty"; |
109
|
|
|
const message = "asdf"; |
110
|
|
|
const code = Math.round(Math.random() * 0xFFFFFFFF); |
111
|
|
|
for (let i = 0; i < 2; i += 1) { |
112
|
|
|
const arg1 = (i % 2 === 0 ? name : null); |
113
|
|
|
const source1 = new Throwable(arg1); |
114
|
|
|
for (let j = 0; j < 2; j += 1) { |
115
|
|
|
const arg2 = (j % 2 === 0 ? message : null); |
116
|
|
|
const source2 = new Throwable(arg1, arg2); |
117
|
|
|
for (let e = 0; e < 2; e += 1) { |
118
|
|
|
const arg3 = (e % 2 === 0 ? code : null); |
119
|
|
|
const source3 = new Throwable(arg1, arg2, arg3); |
120
|
|
|
if (i % 2 === 0) { |
121
|
|
|
expect(source1.name).toEqual(name); |
122
|
|
|
expect(source2.name).toEqual(name); |
123
|
|
|
expect(source3.name).toEqual(name); |
124
|
|
|
} else { |
125
|
|
|
expect(source1.name).toBeNull(); |
126
|
|
|
expect(source2.name).toBeNull(); |
127
|
|
|
expect(source3.name).toBeNull(); |
128
|
|
|
} |
129
|
|
|
if (j % 2 === 0) { |
130
|
|
|
expect(source2.message).toEqual(message); |
131
|
|
|
expect(source3.message).toEqual(message); |
132
|
|
|
} else { |
133
|
|
|
expect(source2.message).toBeNull(); |
134
|
|
|
expect(source3.message).toBeNull(); |
135
|
|
|
} |
136
|
|
|
if (e % 2 === 0) expect(source3.code).toEqual(code); |
|
|
|
|
137
|
|
|
else expect(source3.code).toBeNull(); |
138
|
|
|
expect(source1.message).toBeNull(); |
139
|
|
|
expect(source1.code).toBeNull(); |
140
|
|
|
expect(source2.code).toBeNull(); |
141
|
|
|
} |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
}); |
145
|
|
|
|
146
|
|
|
// :: MISCELLANEOUS |
147
|
|
|
|
148
|
|
|
it("should inherit from 'Object'", () => { |
149
|
|
|
const source = new Throwable; |
150
|
|
|
expect(source).toEqual(jasmine.any(Object)); |
151
|
|
|
}); |
152
|
|
|
|
153
|
|
|
it("#toString()", () => { |
154
|
|
|
// test non-empty |
155
|
|
|
const name = "qwerty"; |
156
|
|
|
const message = "asdf"; |
157
|
|
|
const code = Math.round(Math.random() * 0xFFFFFFFF); |
158
|
|
|
for (let i = 0; i < 2; i += 1) { |
159
|
|
|
const arg1 = (i % 2 === 0 ? name : null); |
160
|
|
|
const source1 = new Throwable(arg1); |
161
|
|
|
for (let j = 0; j < 2; j += 1) { |
162
|
|
|
const arg2 = (j % 2 === 0 ? message : null); |
163
|
|
|
const source2 = new Throwable(arg1, arg2); |
164
|
|
|
for (let e = 0; e < 2; e += 1) { |
165
|
|
|
const arg3 = (e % 2 === 0 ? code : null); |
166
|
|
|
const source3 = new Throwable(arg1, arg2, arg3); |
167
|
|
|
const str1 = source1.toString(); |
168
|
|
|
const str2 = source2.toString(); |
169
|
|
|
const str3 = source3.toString(); |
170
|
|
|
if (i % 2 === 0) { |
171
|
|
|
expect(str1).toEqual(name + ": thrown."); |
172
|
|
View Code Duplication |
if (j % 2 === 0) { |
|
|
|
|
173
|
|
|
expect(str2).toEqual(name + ": " + message + "."); |
174
|
|
|
if (e % 2 === 0) expect(str3).toEqual(name + " (0x" + code.toString(16) + "): " + message + "."); |
|
|
|
|
175
|
|
|
else expect(str3).toEqual(name + ": " + message + "."); |
176
|
|
|
} else { |
177
|
|
|
expect(str2).toEqual(name + ": thrown."); |
178
|
|
|
if (e % 2 === 0) expect(str3).toEqual(name + " (0x" + code.toString(16) + "): thrown."); |
|
|
|
|
179
|
|
|
else expect(str3).toEqual(name + ": thrown."); |
180
|
|
|
} |
181
|
|
|
} else { |
182
|
|
|
expect(str1).toEqual("Throwable: thrown."); |
183
|
|
|
if (j % 2 === 0) { |
184
|
|
|
expect(str2).toEqual("Throwable: " + message + "."); |
185
|
|
|
if (e % 2 === 0) expect(str3).toEqual("Throwable (0x" + code.toString(16) + "): " + message + "."); |
|
|
|
|
186
|
|
|
else expect(str3).toEqual("Throwable: " + message + "."); |
187
|
|
|
} else { |
188
|
|
|
expect(str2).toEqual("Throwable: thrown."); |
189
|
|
|
if (e % 2 === 0) expect(str3).toEqual("Throwable (0x" + code.toString(16) + "): thrown."); |
|
|
|
|
190
|
|
|
else expect(str3).toEqual("Throwable: thrown."); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
} |
195
|
|
|
} |
196
|
|
|
}); |
197
|
|
|
|
198
|
|
|
it("#native()", () => { |
199
|
|
|
// test non-empty |
200
|
|
|
const name = "qwerty"; |
201
|
|
|
const message = "asdf"; |
202
|
|
|
const code = Math.round(Math.random() * 0xFFFFFFFF); |
203
|
|
|
for (let i = 0; i < 2; i += 1) { |
204
|
|
|
const arg1 = (i % 2 === 0 ? name : null); |
205
|
|
|
const source1 = new Throwable(arg1); |
206
|
|
|
for (let j = 0; j < 2; j += 1) { |
207
|
|
|
const arg2 = (j % 2 === 0 ? message : null); |
208
|
|
|
const source2 = new Throwable(arg1, arg2); |
209
|
|
|
for (let e = 0; e < 2; e += 1) { |
210
|
|
|
const arg3 = (e % 2 === 0 ? code : null); |
211
|
|
|
const source3 = new Throwable(arg1, arg2, arg3); |
212
|
|
|
const err1 = source1.native(); |
213
|
|
|
const err2 = source2.native(); |
214
|
|
|
const err3 = source3.native(); |
215
|
|
|
if (i % 2 === 0) { |
216
|
|
|
expect(err1).toEqual(new Error(name + ": thrown")); |
217
|
|
View Code Duplication |
if (j % 2 === 0) { |
|
|
|
|
218
|
|
|
expect(err2).toEqual(new Error(name + ": " + message)); |
219
|
|
|
if (e % 2 === 0) expect(err3).toEqual(new Error(name + " (0x" + code.toString(16) + "): " + message)); |
|
|
|
|
220
|
|
|
else expect(err3).toEqual(new Error(name + ": " + message)); |
221
|
|
|
} else { |
222
|
|
|
expect(err2).toEqual(new Error(name + ": thrown")); |
223
|
|
|
if (e % 2 === 0) expect(err3).toEqual(new Error(name + " (0x" + code.toString(16) + "): thrown")); |
|
|
|
|
224
|
|
|
else expect(err3).toEqual(new Error(name + ": thrown")); |
225
|
|
|
} |
226
|
|
|
} else { |
227
|
|
|
expect(err1).toEqual(new Error("Throwable: thrown")); |
228
|
|
|
if (j % 2 === 0) { |
229
|
|
|
expect(err2).toEqual(new Error("Throwable: " + message)); |
230
|
|
|
if (e % 2 === 0) expect(err3).toEqual(new Error("Throwable (0x" + code.toString(16) + "): " + message)); |
|
|
|
|
231
|
|
|
else expect(err3).toEqual(new Error("Throwable: " + message)); |
232
|
|
|
} else { |
233
|
|
|
expect(err2).toEqual(new Error("Throwable: thrown")); |
234
|
|
|
if (e % 2 === 0) expect(err3).toEqual(new Error("Throwable (0x" + code.toString(16) + "): thrown")); |
|
|
|
|
235
|
|
|
else expect(err3).toEqual(new Error("Throwable: thrown")); |
236
|
|
|
} |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
}); |
242
|
|
|
|
243
|
|
|
}); |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.