Total Complexity | 55 |
Complexity/F | 1.9 |
Lines of Code | 217 |
Function Count | 29 |
Duplicated Lines | 217 |
Ratio | 100 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like spec/InvalidPathException.spec.js often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
1 | // spec/InvalidPathException.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 | const IllegalArgumentException = require(path.join(root, "IllegalArgumentException.js")); |
||
15 | const InvalidPathException = require(path.join(root, "InvalidPathException.js")); |
||
16 | |||
17 | // :: TESTING |
||
18 | |||
19 | // test the InvalidPathException 'class' |
||
20 | describe("InvalidPathException", () => { |
||
21 | |||
22 | // :: INHERITED PROTOTYPE |
||
23 | |||
24 | it("should inherit from 'Object'", () => { |
||
25 | expect(new InvalidPathException()).toEqual(jasmine.any(Object)); |
||
26 | }); |
||
27 | |||
28 | it("should inherit from 'Throwable'", () => { |
||
29 | expect(new InvalidPathException()).toEqual(jasmine.any(Throwable)); |
||
30 | }); |
||
31 | |||
32 | it("should inherit from 'Exception'", () => { |
||
33 | expect(new InvalidPathException()).toEqual(jasmine.any(Exception)); |
||
34 | }); |
||
35 | |||
36 | it("should inherit from 'RuntimeException'", () => { |
||
37 | expect(new InvalidPathException()).toEqual(jasmine.any(RuntimeException)); |
||
38 | }); |
||
39 | |||
40 | it("should inherit from 'IllegalArgumentException'", () => { |
||
41 | expect(new InvalidPathException()).toEqual(jasmine.any(IllegalArgumentException)); |
||
42 | }); |
||
43 | |||
44 | it("should have a prototype method named 'toString()'", () => { |
||
45 | expect(InvalidPathException.prototype).toHaveMethod("toString"); |
||
46 | }); |
||
47 | |||
48 | it("should have a prototype method named 'native()'", () => { |
||
49 | expect(InvalidPathException.prototype).toHaveMethod("native"); |
||
50 | }); |
||
51 | |||
52 | it("should have a prototype property string named 'name'", () => { |
||
53 | expect(InvalidPathException.prototype).toHaveString("name"); |
||
54 | }); |
||
55 | |||
56 | it("should have a prototype property string named 'message'", () => { |
||
57 | expect(InvalidPathException.prototype).toHaveString("message"); |
||
58 | }); |
||
59 | |||
60 | it("should have a prototype property string named 'code'", () => { |
||
61 | expect(InvalidPathException.prototype).toHaveMember("code"); |
||
62 | }); |
||
63 | |||
64 | // :: EXTENDED PROTOTYPE |
||
65 | |||
66 | // :: PROTOTYPE VALUES |
||
67 | |||
68 | it("should have the 'class' name in the prototype property named 'name'", () => { |
||
69 | expect(InvalidPathException.prototype.name).toEqual("InvalidPathException"); |
||
70 | }); |
||
71 | |||
72 | it("should have a dummy default value as message", () => { |
||
73 | expect(InvalidPathException.prototype.message).toEqual("thrown"); |
||
74 | }); |
||
75 | |||
76 | it("should have a null default value as code", () => { |
||
77 | expect(InvalidPathException.prototype.code).toBeNull(); |
||
78 | }); |
||
79 | |||
80 | // :: CONSTRUCTOR |
||
81 | |||
82 | it("should instantiate without parameters", () => { |
||
83 | let arg1, arg2, test; |
||
84 | test = (() => new InvalidPathException(arg1, arg2)); |
||
85 | for (let i = 0; i < 2; i += 1) { |
||
86 | for (let j = 0; j < 2; j += 1) { |
||
87 | arg1 = (i % 2 === 0 ? undefined : null); |
||
88 | arg2 = (j % 2 === 0 ? undefined : null); |
||
89 | expect(test).not.toThrowError("parameter 'name' must be a 'string'"); |
||
90 | expect(test).not.toThrowError("parameter 'message' must be a 'string'"); |
||
91 | expect(test).not.toThrowError("parameter 'code' must be a 'number'"); |
||
92 | } |
||
93 | } |
||
94 | test = (() => new InvalidPathException()); |
||
95 | expect(test).not.toThrowError("parameter 'name' must be a 'string'"); |
||
96 | expect(test).not.toThrowError("parameter 'message' must be a 'string'"); |
||
97 | expect(test).not.toThrowError("parameter 'code' must be a 'number'"); |
||
98 | }); |
||
99 | |||
100 | it("should instantiate with parameters", () => { |
||
101 | let arg1, arg2, test1, test2; |
||
102 | const args1 = [undefined, null, InvalidPathException.prototype.message]; |
||
103 | const args2 = [undefined, null, Math.round(Math.random() * 0xFFFFFFFF)]; |
||
104 | test1 = (() => new InvalidPathException(arg1)); |
||
105 | test2 = (() => new InvalidPathException(arg1, arg2)); |
||
106 | for (let i = 0; i < args1.length; i += 1) { |
||
107 | arg1 = args1[i]; |
||
108 | for (let j = 0; j < args2.length; j += 1) { |
||
109 | arg2 = args2[j]; |
||
110 | expect(test2).not.toThrowError("parameter 'message' must be a 'string'"); |
||
111 | expect(test2).not.toThrowError("parameter 'code' must be a 'number'"); |
||
112 | } |
||
113 | expect(test1).not.toThrowError("parameter 'message' must be a 'string'"); |
||
114 | expect(test1).not.toThrowError("parameter 'code' must be a 'number'"); |
||
115 | } |
||
116 | }); |
||
117 | |||
118 | it("should throw an Error if 'message' or 'code' are invalid parameters", () => { |
||
119 | let arg1, arg2, test21, test22, test11; |
||
120 | const noStr = [{}, true, false, 42, 3.1416, -42, -3.1416, () => null]; |
||
121 | const noNmb = [{}, true, false, '', "qwerty", () => null]; |
||
122 | test22 = (() => new InvalidPathException(arg1, arg2)); |
||
123 | test21 = (() => new InvalidPathException(null, arg2)); |
||
124 | test11 = (() => new InvalidPathException(arg1)); |
||
125 | if (typeof Symbol === "function") { |
||
126 | noStr.push(Symbol("symbol")); |
||
127 | noNmb.push(Symbol("symbol")); |
||
128 | } |
||
129 | for (let i = 0; i < noStr.length; i += 1) { |
||
130 | arg1 = noStr[i]; |
||
131 | for (let j = 0; j < noNmb.length; j += 1) { |
||
132 | arg2 = noNmb[j]; |
||
133 | expect(test22).toThrowError("parameter 'message' must be a 'string'"); |
||
134 | expect(test21).toThrowError("parameter 'code' must be a 'number'"); |
||
135 | } |
||
136 | expect(test11).toThrowError("parameter 'message' must be a 'string'"); |
||
137 | } |
||
138 | }); |
||
139 | |||
140 | // :: MEMBER PROPERTIES |
||
141 | |||
142 | it("should have all correct properties once instantiated", () => { |
||
143 | const message = "asdf"; |
||
144 | const code = Math.round(Math.random() * 0xFFFFFFFF); |
||
145 | for (let i = 0; i < 2; i += 1) { |
||
146 | const arg1 = (i % 2 === 0 ? message : null); |
||
147 | const source1 = new InvalidPathException(arg1); |
||
148 | for (let j = 0; j < 2; j += 1) { |
||
149 | const arg2 = (j % 2 === 0 ? code : null); |
||
150 | const source2 = new InvalidPathException(arg1, arg2); |
||
151 | if (i % 2 === 0) { |
||
152 | expect(source1.message).toEqual(message); |
||
153 | expect(source2.message).toEqual(message); |
||
154 | } else { |
||
155 | expect(source1.message).toEqual(InvalidPathException.prototype.message); |
||
156 | expect(source2.message).toEqual(InvalidPathException.prototype.message); |
||
157 | } |
||
158 | if (j % 2 === 0) { |
||
159 | expect(source2.code).toEqual(code); |
||
160 | } else { |
||
161 | expect(source2.code).toBeNull(); |
||
162 | } |
||
163 | expect(source1.code).toBeNull(); |
||
164 | } |
||
165 | } |
||
166 | }); |
||
167 | |||
168 | // :: MEMBER METHODS |
||
169 | |||
170 | const name = "InvalidPathException"; |
||
171 | const message = "asdf"; |
||
172 | const code = Math.round(Math.random() * 0xFFFFFFFF); |
||
173 | |||
174 | it("#toString()", () => { |
||
175 | for (let i = 0; i < 2; i += 1) { |
||
176 | const arg1 = (i % 2 === 0 ? message : null); |
||
177 | const source1 = new InvalidPathException(arg1); |
||
178 | for (let j = 0; j < 2; j += 1) { |
||
179 | const arg2 = (j % 2 === 0 ? code : null); |
||
180 | const source2 = new InvalidPathException(arg1, arg2); |
||
181 | const str1 = source1.toString(); |
||
182 | const str2 = source2.toString(); |
||
183 | let exp1, exp2; |
||
184 | exp1 = exp2 = "InvalidPathException"; |
||
185 | if (j % 2 === 0) { |
||
186 | exp2 += " (0x" + code.toString(16) + ')'; |
||
187 | } |
||
188 | if (i % 2 === 0) { |
||
189 | exp1 += ": " + message + '.'; |
||
190 | exp2 += ": " + message + '.'; |
||
191 | } else { |
||
192 | exp1 += ": " + InvalidPathException.prototype.message + '.'; |
||
193 | exp2 += ": " + InvalidPathException.prototype.message + '.'; |
||
194 | } |
||
195 | expect(str1).toEqual(exp1); |
||
196 | expect(str2).toEqual(exp2); |
||
197 | } |
||
198 | } |
||
199 | }); |
||
200 | |||
201 | it("#native()", () => { |
||
202 | for (let i = 0; i < 2; i += 1) { |
||
203 | const arg1 = (i % 2 === 0 ? message : null); |
||
204 | const source1 = new InvalidPathException(arg1); |
||
205 | for (let j = 0; j < 2; j += 1) { |
||
206 | const arg2 = (j % 2 === 0 ? code : null); |
||
207 | const source2 = new InvalidPathException(arg1, arg2); |
||
208 | const err1 = source1.native(); |
||
209 | const err2 = source2.native(); |
||
210 | const exp1 = (i % 2 === 0 ? message : InvalidPathException.prototype.message); |
||
211 | const exp2 = exp1; |
||
212 | expect(err1).toEqual(new Error(exp1)); |
||
213 | expect(err2).toEqual(new Error(exp2)); |
||
214 | } |
||
215 | } |
||
216 | }); |
||
217 | |||
218 | }); |