| Conditions | 1 |
| Paths | 16384 |
| Total Lines | 131 |
| Lines | 43 |
| Ratio | 32.82 % |
| Changes | 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/helpers/hierarchy-helper.js |
||
| 141 | function testThird(Klass) { |
||
| 142 | |||
| 143 | // :: CONSTRUCTOR |
||
| 144 | |||
| 145 | it("should instantiate without parameters", () => { |
||
| 146 | let arg1, arg2, test; |
||
| 147 | test = (() => new Klass(arg1, arg2)); |
||
| 148 | for (let i = 0; i < 2; i += 1) { |
||
| 149 | arg1 = (i % 2 === 0 ? undefined : null); |
||
| 150 | for (let j = 0; j < 2; j += 1) { |
||
| 151 | arg2 = (j % 2 === 0 ? undefined : null); |
||
| 152 | expect(test).not.toThrowError("parameter 'name' must be a 'string'"); |
||
| 153 | expect(test).not.toThrowError("parameter 'message' must be a 'string'"); |
||
| 154 | expect(test).not.toThrowError("parameter 'code' must be a 'number'"); |
||
| 155 | } |
||
| 156 | } |
||
| 157 | test = (() => new Klass()); |
||
| 158 | expect(test).not.toThrowError("parameter 'name' must be a 'string'"); |
||
| 159 | expect(test).not.toThrowError("parameter 'message' must be a 'string'"); |
||
| 160 | expect(test).not.toThrowError("parameter 'code' must be a 'number'"); |
||
| 161 | }); |
||
| 162 | |||
| 163 | it("should instantiate with parameters", () => { |
||
| 164 | let arg1, arg2; |
||
| 165 | const test1 = (() => new Klass(arg1)); |
||
| 166 | const test2 = (() => new Klass(arg1, arg2)); |
||
| 167 | const args1 = [undefined, null, Klass.prototype.message]; |
||
| 168 | const args2 = [undefined, null, Math.round(Math.random() * 0xFFFFFFFF)]; |
||
| 169 | for (let i = 0; i < args1.length; i += 1) { |
||
| 170 | arg1 = args1[i]; |
||
| 171 | expect(test1).not.toThrowError("parameter 'name' must be a 'string'"); |
||
| 172 | expect(test1).not.toThrowError("parameter 'message' must be a 'string'"); |
||
| 173 | expect(test1).not.toThrowError("parameter 'code' must be a 'number'"); |
||
| 174 | for (let j = 0; j < args2.length; j += 1) { |
||
| 175 | arg2 = args2[j]; |
||
| 176 | expect(test2).not.toThrowError("parameter 'name' must be a 'string'"); |
||
| 177 | expect(test2).not.toThrowError("parameter 'message' must be a 'string'"); |
||
| 178 | expect(test2).not.toThrowError("parameter 'code' must be a 'number'"); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | }); |
||
| 182 | |||
| 183 | View Code Duplication | it("should throw an Error if 'message' or 'code' are invalid parameters", () => { |
|
|
|
|||
| 184 | let arg1, arg2, arg3, test21, test22, test11; |
||
| 185 | const noStr = [{}, true, false, 42, 3.1416, -42, -3.1416, () => null]; |
||
| 186 | const noNmb = [{}, true, false, '', "qwerty", () => null]; |
||
| 187 | test22 = (() => new Klass(arg1, arg2)); |
||
| 188 | test21 = (() => new Klass(null, arg2)); |
||
| 189 | test11 = (() => new Klass(arg1)); |
||
| 190 | if (typeof Symbol === "function") { |
||
| 191 | noStr.push(Symbol("symbol")); |
||
| 192 | noNmb.push(Symbol("symbol")); |
||
| 193 | } |
||
| 194 | for (let i = 0; i < noStr.length; i += 1) { |
||
| 195 | arg1 = noStr[i]; |
||
| 196 | expect(test11).toThrowError("parameter 'message' must be a 'string'"); |
||
| 197 | for (let j = 0; j < noNmb.length; j += 1) { |
||
| 198 | arg2 = noNmb[j]; |
||
| 199 | expect(test22).toThrowError("parameter 'message' must be a 'string'"); |
||
| 200 | expect(test21).toThrowError("parameter 'code' must be a 'number'"); |
||
| 201 | } |
||
| 202 | } |
||
| 203 | }); |
||
| 204 | |||
| 205 | // :: MEMBER PROPERTIES |
||
| 206 | |||
| 207 | const message = "asdf"; |
||
| 208 | const code = Math.round(Math.random() * 0xFFFFFFFF); |
||
| 209 | |||
| 210 | it("should have all correct properties once instantiated", () => { |
||
| 211 | instanceDefinedOrNull(Klass, null, message, code, (instance, even) => { |
||
| 212 | if (even) { |
||
| 213 | expect(instance.name).toEqual(Klass.prototype.name); |
||
| 214 | expect(instance.message).toEqual(message); |
||
| 215 | } else { |
||
| 216 | expect(instance.name).toEqual(Klass.prototype.name); |
||
| 217 | expect(instance.message).toEqual(Klass.prototype.message); |
||
| 218 | } |
||
| 219 | expect(instance.code).toBeNull(); |
||
| 220 | }, (instance, even1, even2) => { |
||
| 221 | expect(instance.name).toEqual(Klass.prototype.name); |
||
| 222 | expect(instance.message).toEqual(even1 ? message : Klass.prototype.message); |
||
| 223 | expect(instance.code).toEqual(even2 ? code : null); |
||
| 224 | }, (instance, even1, even2, even3) => { |
||
| 225 | expect(instance.name).toEqual(even1 ? name : Klass.prototype.name); |
||
| 226 | expect(instance.message).toEqual(even2 ? message : Klass.prototype.message); |
||
| 227 | expect(instance.code).toEqual(even3 ? code : null); |
||
| 228 | }, true); |
||
| 229 | }); |
||
| 230 | |||
| 231 | // :: MEMBER METHODS |
||
| 232 | |||
| 233 | it("#toString()", () => { |
||
| 234 | View Code Duplication | for (let i = 0; i < 2; i += 1) { |
|
| 235 | let exp1; |
||
| 236 | const even1 = (i % 2 === 0); |
||
| 237 | const arg1 = (even1 ? message : null); |
||
| 238 | const source1 = new Klass(arg1); |
||
| 239 | if (even1) { |
||
| 240 | exp1 = Klass.prototype.name + ": " + message + '.'; |
||
| 241 | } else { |
||
| 242 | exp1 = Klass.prototype.name + ": " + Klass.prototype.message + '.'; |
||
| 243 | } |
||
| 244 | expect(source1.toString()).toEqual(exp1); |
||
| 245 | for (let j = 0; j < 2; j += 1) { |
||
| 246 | let exp2; |
||
| 247 | const even2 = (j % 2 === 0); |
||
| 248 | const arg2 = (even2 ? code : null); |
||
| 249 | const source2 = new Klass(arg1, arg2); |
||
| 250 | exp2 = Klass.prototype.name; |
||
| 251 | exp2 += (even2 ? " (0x" + code.toString(16) + "):" : ':' ) + ' '; |
||
| 252 | exp2 += (even1 ? message : Klass.prototype.message) + '.'; |
||
| 253 | expect(source2.toString()).toEqual(exp2); |
||
| 254 | } |
||
| 255 | } |
||
| 256 | }); |
||
| 257 | |||
| 258 | it("#native()", () => { |
||
| 259 | instanceDefinedOrNull(Klass, null, message, code, (instance, even) => { |
||
| 260 | const exp = (even ? message : Klass.prototype.message); |
||
| 261 | expect(instance.native()).toEqual(new Error(exp)); |
||
| 262 | }, (instance, even1) => { |
||
| 263 | const exp = (even1 ? message : Klass.prototype.message); |
||
| 264 | expect(instance.native()).toEqual(new Error(exp)); |
||
| 265 | }, (instance, even1, even2) => { |
||
| 266 | const exp = (even2 ? message : Klass.prototype.message); |
||
| 267 | expect(instance.native()).toEqual(new Error(exp)); |
||
| 268 | }, true); |
||
| 269 | }); |
||
| 270 | |||
| 271 | } |
||
| 272 | |||
| 435 | } |