Code Duplication    Length = 234-240 lines in 2 locations

spec/Exception.spec.js 1 location

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

spec/Throwable.spec.js 1 location

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