1
|
|
|
// spec/RuntimeException.spec.js |
2
|
|
View Code Duplication |
"use strict"; |
|
|
|
|
3
|
|
|
|
4
|
|
|
// :: DEPENDENCIES |
5
|
|
|
|
6
|
|
|
// load native dependencies |
7
|
|
|
const path = require("path"); |
8
|
|
|
|
9
|
|
|
// load local dependencies |
10
|
|
|
const root = path.dirname(__dirname); |
11
|
|
|
const Throwable = require(path.join(root, "Throwable.js")); |
12
|
|
|
const Exception = require(path.join(root, "Exception.js")); |
13
|
|
|
const RuntimeException = require(path.join(root, "RuntimeException.js")); |
14
|
|
|
|
15
|
|
|
// :: TESTING |
16
|
|
|
|
17
|
|
|
// test the Throwable 'class' |
18
|
|
|
describe("Throwable", () => { |
19
|
|
|
|
20
|
|
|
// :: INHERITED PROTOTYPE |
21
|
|
|
|
22
|
|
|
it("should inherit from 'Object'", () => { |
23
|
|
|
expect(new RuntimeException()).toEqual(jasmine.any(Object)); |
24
|
|
|
}); |
25
|
|
|
|
26
|
|
|
it("should inherit from 'Throwable'", () => { |
27
|
|
|
expect(new RuntimeException()).toEqual(jasmine.any(Throwable)); |
28
|
|
|
}); |
29
|
|
|
|
30
|
|
|
it("should inherit from 'Exception'", () => { |
31
|
|
|
expect(new RuntimeException()).toEqual(jasmine.any(Exception)); |
32
|
|
|
}); |
33
|
|
|
|
34
|
|
|
it("should have a prototype method named 'toString()'", () => { |
35
|
|
|
expect(RuntimeException.prototype).toHaveMethod("toString"); |
36
|
|
|
}); |
37
|
|
|
|
38
|
|
|
it("should have a prototype method named 'native()'", () => { |
39
|
|
|
expect(RuntimeException.prototype).toHaveMethod("native"); |
40
|
|
|
}); |
41
|
|
|
|
42
|
|
|
it("should have a prototype property string named 'name'", () => { |
43
|
|
|
expect(RuntimeException.prototype).toHaveString("name"); |
44
|
|
|
}); |
45
|
|
|
|
46
|
|
|
it("should have a prototype property string named 'message'", () => { |
47
|
|
|
expect(RuntimeException.prototype).toHaveString("message"); |
48
|
|
|
}); |
49
|
|
|
|
50
|
|
|
it("should have a prototype property string named 'code'", () => { |
51
|
|
|
expect(RuntimeException.prototype).toHaveMember("code"); |
52
|
|
|
}); |
53
|
|
|
|
54
|
|
|
// :: EXTENDED PROTOTYPE |
55
|
|
|
|
56
|
|
|
// :: PROTOTYPE VALUES |
57
|
|
|
|
58
|
|
|
it("should have the 'class' name in the prototype property named 'name'", () => { |
59
|
|
|
expect(RuntimeException.prototype.name).toEqual("RuntimeException"); |
60
|
|
|
}); |
61
|
|
|
|
62
|
|
|
it("should have a dummy default value as message", () => { |
63
|
|
|
expect(RuntimeException.prototype.message).toEqual("thrown"); |
64
|
|
|
}); |
65
|
|
|
|
66
|
|
|
it("should have a null default value as code", () => { |
67
|
|
|
expect(RuntimeException.prototype.code).toBeNull(); |
68
|
|
|
}); |
69
|
|
|
|
70
|
|
|
// :: CONSTRUCTOR |
71
|
|
|
|
72
|
|
|
it("should instantiate without parameters", () => { |
73
|
|
|
let arg1, arg2, arg3, test; |
74
|
|
|
test = (() => new RuntimeException(arg1, arg2, arg3)); |
75
|
|
|
for (let i = 0; i < 2; i += 1) { |
76
|
|
|
for (let j = 0; j < 2; j += 1) { |
77
|
|
|
for (let e = 0; e < 2; e += 1) { |
78
|
|
|
arg1 = (i % 2 === 0 ? undefined : null); |
79
|
|
|
arg2 = (j % 2 === 0 ? undefined : null); |
80
|
|
|
arg3 = (e % 2 === 0 ? undefined : null); |
81
|
|
|
expect(test).not.toThrowError("parameter 'name' must be a 'string'"); |
82
|
|
|
expect(test).not.toThrowError("parameter 'message' must be a 'string'"); |
83
|
|
|
expect(test).not.toThrowError("parameter 'code' must be a 'number'"); |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
} |
87
|
|
|
test = (() => new RuntimeException()); |
88
|
|
|
expect(test).not.toThrowError("parameter 'name' must be a 'string'"); |
89
|
|
|
expect(test).not.toThrowError("parameter 'message' must be a 'string'"); |
90
|
|
|
expect(test).not.toThrowError("parameter 'code' must be a 'number'"); |
91
|
|
|
}); |
92
|
|
|
|
93
|
|
|
it("should instantiate with parameters", () => { |
94
|
|
|
let arg1, arg2, arg3, test1, test2, test3; |
95
|
|
|
const args1 = [undefined, null, RuntimeException.prototype.name]; |
96
|
|
|
const args2 = [undefined, null, RuntimeException.prototype.message]; |
97
|
|
|
const args3 = [undefined, null, Math.round(Math.random() * 0xFFFFFFFF)]; |
98
|
|
|
test1 = (() => new RuntimeException(arg1)); |
99
|
|
|
test2 = (() => new RuntimeException(arg1, arg2)); |
100
|
|
|
test3 = (() => new RuntimeException(arg1, arg2, arg3)); |
101
|
|
|
for (let i = 0; i < args1.length; i += 1) { |
102
|
|
|
arg1 = args1[i]; |
103
|
|
|
for (let j = 0; j < args2.length; j += 1) { |
104
|
|
|
arg2 = args2[j]; |
105
|
|
|
for (let e = 0; e < args3.length; e += 1) { |
106
|
|
|
arg3 = args3[e]; |
107
|
|
|
expect(test3).not.toThrowError("parameter 'name' must be a 'string'"); |
108
|
|
|
expect(test3).not.toThrowError("parameter 'message' must be a 'string'"); |
109
|
|
|
expect(test3).not.toThrowError("parameter 'code' must be a 'number'"); |
110
|
|
|
} |
111
|
|
|
expect(test2).not.toThrowError("parameter 'name' must be a 'string'"); |
112
|
|
|
expect(test2).not.toThrowError("parameter 'message' must be a 'string'"); |
113
|
|
|
expect(test2).not.toThrowError("parameter 'code' must be a 'number'"); |
114
|
|
|
} |
115
|
|
|
expect(test1).not.toThrowError("parameter 'name' must be a 'string'"); |
116
|
|
|
expect(test1).not.toThrowError("parameter 'message' must be a 'string'"); |
117
|
|
|
expect(test1).not.toThrowError("parameter 'code' must be a 'number'"); |
118
|
|
|
} |
119
|
|
|
}); |
120
|
|
|
|
121
|
|
|
it("should throw an Error if 'name', 'message' or 'code' are invalid parameters", () => { |
122
|
|
|
let arg1, arg2, arg3, test31, test32, test33, test21, test22, test11; |
123
|
|
|
const noStr = [{}, true, false, 42, 3.1416, -42, -3.1416, () => null]; |
124
|
|
|
const noNmb = [{}, true, false, '', "qwerty", () => null]; |
125
|
|
|
test33 = (() => new RuntimeException(arg1, arg2, arg3)); |
126
|
|
|
test32 = (() => new RuntimeException(null, arg2, arg3)); |
127
|
|
|
test31 = (() => new RuntimeException(null, null, arg3)); |
128
|
|
|
test22 = (() => new RuntimeException(arg1, arg2)); |
129
|
|
|
test21 = (() => new RuntimeException(null, arg2)); |
130
|
|
|
test11 = (() => new RuntimeException(arg1)); |
131
|
|
|
if (typeof Symbol === "function") { |
132
|
|
|
noStr.push(Symbol("symbol")); |
133
|
|
|
noNmb.push(Symbol("symbol")); |
134
|
|
|
} |
135
|
|
|
for (let i = 0; i < noStr.length; i += 1) { |
136
|
|
|
arg1 = noStr[i]; |
137
|
|
|
for (let j = 0; j < noStr.length; j += 1) { |
138
|
|
|
arg2 = noStr[j]; |
139
|
|
|
for (let e = 0; e < noNmb.length; e += 1) { |
140
|
|
|
arg3 = noNmb[e]; |
141
|
|
|
expect(test33).toThrowError("parameter 'name' must be a 'string'"); |
142
|
|
|
expect(test32).toThrowError("parameter 'message' must be a 'string'"); |
143
|
|
|
expect(test31).toThrowError("parameter 'code' must be a 'number'"); |
144
|
|
|
} |
145
|
|
|
expect(test22).toThrowError("parameter 'name' must be a 'string'"); |
146
|
|
|
expect(test21).toThrowError("parameter 'message' must be a 'string'"); |
147
|
|
|
} |
148
|
|
|
expect(test11).toThrowError("parameter 'name' must be a 'string'"); |
149
|
|
|
} |
150
|
|
|
}); |
151
|
|
|
|
152
|
|
|
// :: MEMBER PROPERTIES |
153
|
|
|
|
154
|
|
|
it("should have all correct properties once instantiated", () => { |
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 RuntimeException(arg1); |
161
|
|
|
for (let j = 0; j < 2; j += 1) { |
162
|
|
|
const arg2 = (j % 2 === 0 ? message : null); |
163
|
|
|
const source2 = new RuntimeException(arg1, arg2); |
164
|
|
|
for (let e = 0; e < 2; e += 1) { |
165
|
|
|
const arg3 = (e % 2 === 0 ? code : null); |
166
|
|
|
const source3 = new RuntimeException(arg1, arg2, arg3); |
167
|
|
|
if (i % 2 === 0) { |
168
|
|
|
expect(source1.name).toEqual(name); |
169
|
|
|
expect(source2.name).toEqual(name); |
170
|
|
|
expect(source3.name).toEqual(name); |
171
|
|
|
} else { |
172
|
|
|
expect(source1.name).toEqual(RuntimeException.prototype.name); |
173
|
|
|
expect(source2.name).toEqual(RuntimeException.prototype.name); |
174
|
|
|
expect(source3.name).toEqual(RuntimeException.prototype.name); |
175
|
|
|
} |
176
|
|
|
if (j % 2 === 0) { |
177
|
|
|
expect(source2.message).toEqual(message); |
178
|
|
|
expect(source3.message).toEqual(message); |
179
|
|
|
} else { |
180
|
|
|
expect(source2.message).toEqual(RuntimeException.prototype.message); |
181
|
|
|
expect(source3.message).toEqual(RuntimeException.prototype.message); |
182
|
|
|
} |
183
|
|
|
if (e % 2 === 0) { |
184
|
|
|
expect(source3.code).toEqual(code); |
185
|
|
|
} else { |
186
|
|
|
expect(source3.code).toBeNull(); |
187
|
|
|
} |
188
|
|
|
expect(source1.message).toEqual(RuntimeException.prototype.message); |
189
|
|
|
expect(source1.code).toBeNull(); |
190
|
|
|
expect(source2.code).toBeNull(); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
}); |
195
|
|
|
|
196
|
|
|
// :: MEMBER METHODS |
197
|
|
|
|
198
|
|
|
const name = "qwerty"; |
199
|
|
|
const message = "asdf"; |
200
|
|
|
const code = Math.round(Math.random() * 0xFFFFFFFF); |
201
|
|
|
|
202
|
|
|
it("#toString()", () => { |
203
|
|
|
// test non-empty |
204
|
|
|
for (let i = 0; i < 2; i += 1) { |
205
|
|
|
const arg1 = (i % 2 === 0 ? name : null); |
206
|
|
|
const source1 = new RuntimeException(arg1); |
207
|
|
|
for (let j = 0; j < 2; j += 1) { |
208
|
|
|
const arg2 = (j % 2 === 0 ? message : null); |
209
|
|
|
const source2 = new RuntimeException(arg1, arg2); |
210
|
|
|
for (let e = 0; e < 2; e += 1) { |
211
|
|
|
const arg3 = (e % 2 === 0 ? code : null); |
212
|
|
|
const source3 = new RuntimeException(arg1, arg2, arg3); |
213
|
|
|
const str1 = source1.toString(); |
214
|
|
|
const str2 = source2.toString(); |
215
|
|
|
const str3 = source3.toString(); |
216
|
|
|
let exp1, exp2, exp3; |
217
|
|
|
if (i % 2 === 0) { |
218
|
|
|
exp1 = name + ": " + RuntimeException.prototype.message + '.'; |
219
|
|
|
exp2 = exp3 = name; |
220
|
|
|
} else { |
221
|
|
|
exp1 = RuntimeException.prototype.name + ": " + RuntimeException.prototype.message + '.'; |
222
|
|
|
exp2 = exp3 = RuntimeException.prototype.name; |
223
|
|
|
} |
224
|
|
|
if (e % 2 === 0) { |
225
|
|
|
exp3 += " (0x" + code.toString(16) + ')'; |
226
|
|
|
} |
227
|
|
|
if (j % 2 === 0) { |
228
|
|
|
exp2 += ": " + message + '.'; |
229
|
|
|
exp3 += ": " + message + '.'; |
230
|
|
|
} else { |
231
|
|
|
exp2 += ": " + RuntimeException.prototype.message + '.'; |
232
|
|
|
exp3 += ": " + RuntimeException.prototype.message + '.'; |
233
|
|
|
} |
234
|
|
|
expect(str1).toEqual(exp1); |
235
|
|
|
expect(str2).toEqual(exp2); |
236
|
|
|
expect(str3).toEqual(exp3); |
237
|
|
|
} |
238
|
|
|
} |
239
|
|
|
} |
240
|
|
|
}); |
241
|
|
|
|
242
|
|
|
it("#native()", () => { |
243
|
|
|
// test non-empty |
244
|
|
|
for (let i = 0; i < 2; i += 1) { |
245
|
|
|
const arg1 = (i % 2 === 0 ? name : null); |
246
|
|
|
const source1 = new RuntimeException(arg1); |
247
|
|
|
for (let j = 0; j < 2; j += 1) { |
248
|
|
|
const arg2 = (j % 2 === 0 ? message : null); |
249
|
|
|
const source2 = new RuntimeException(arg1, arg2); |
250
|
|
|
for (let e = 0; e < 2; e += 1) { |
251
|
|
|
const arg3 = (e % 2 === 0 ? code : null); |
252
|
|
|
const source3 = new RuntimeException(arg1, arg2, arg3); |
253
|
|
|
const err1 = source1.native(); |
254
|
|
|
const err2 = source2.native(); |
255
|
|
|
const err3 = source3.native(); |
256
|
|
|
const exp1 = RuntimeException.prototype.message; |
257
|
|
|
const exp2 = (j % 2 === 0 ? message : RuntimeException.prototype.message); |
258
|
|
|
const exp3 = exp2; |
259
|
|
|
expect(err1).toEqual(new Error(exp1)); |
260
|
|
|
expect(err2).toEqual(new Error(exp2)); |
261
|
|
|
expect(err3).toEqual(new Error(exp3)); |
262
|
|
|
} |
263
|
|
|
} |
264
|
|
|
} |
265
|
|
|
}); |
266
|
|
|
|
267
|
|
|
}); |