Code Duplication    Length = 256-266 lines in 4 locations

spec/RuntimeException.spec.js 1 location

@@ 2-267 (lines=266) @@
1
// spec/RuntimeException.spec.js
2
"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 RuntimeException 'class'
18
describe("RuntimeException", () => {
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
});

spec/Exception.spec.js 1 location

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

spec/Throwable.spec.js 1 location

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

spec/Error.spec.js 1 location

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