Conditions | 1 |
Paths | 4096 |
Total Lines | 225 |
Lines | 225 |
Ratio | 100 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | // spec/Exception.spec.js |
||
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 | }); |