| Conditions | 1 |
| Paths | > 20000 |
| Total Lines | 162 |
| Lines | 49 |
| Ratio | 30.25 % |
| 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 |
||
| 274 | function test(Klass) { |
||
| 275 | |||
| 276 | // :: CONSTRUCTOR |
||
| 277 | |||
| 278 | it("should instantiate without parameters", () => { |
||
| 279 | let arg1, arg2, arg3, test; |
||
| 280 | test = (() => new Klass(arg1, arg2, arg3)); |
||
| 281 | for (let i = 0; i < 2; i += 1) { |
||
| 282 | arg1 = (i % 2 === 0 ? undefined : null); |
||
| 283 | for (let j = 0; j < 2; j += 1) { |
||
| 284 | arg2 = (j % 2 === 0 ? undefined : null); |
||
| 285 | for (let e = 0; e < 2; e += 1) { |
||
| 286 | arg3 = (e % 2 === 0 ? undefined : null); |
||
| 287 | expect(test).not.toThrowError("parameter 'name' must be a 'string'"); |
||
| 288 | expect(test).not.toThrowError("parameter 'message' must be a 'string'"); |
||
| 289 | expect(test).not.toThrowError("parameter 'code' must be a 'number'"); |
||
| 290 | } |
||
| 291 | } |
||
| 292 | } |
||
| 293 | test = (() => new Klass()); |
||
| 294 | expect(test).not.toThrowError("parameter 'name' must be a 'string'"); |
||
| 295 | expect(test).not.toThrowError("parameter 'message' must be a 'string'"); |
||
| 296 | expect(test).not.toThrowError("parameter 'code' must be a 'number'"); |
||
| 297 | }); |
||
| 298 | |||
| 299 | it("should instantiate with parameters", () => { |
||
| 300 | let arg1, arg2, arg3, test1, test2, test3, args1, args2, args3; |
||
| 301 | test1 = (() => new Klass(arg1)); |
||
| 302 | test2 = (() => new Klass(arg1, arg2)); |
||
| 303 | test3 = (() => null); |
||
| 304 | test3 = (() => new Klass(arg1, arg2, arg3)); |
||
| 305 | args1 = [undefined, null, Klass.prototype.name]; |
||
| 306 | args2 = [undefined, null, Klass.prototype.message]; |
||
| 307 | args3 = [undefined, null, Math.round(Math.random() * 0xFFFFFFFF)]; |
||
| 308 | for (let i = 0; i < args1.length; i += 1) { |
||
| 309 | arg1 = args1[i]; |
||
| 310 | expect(test1).not.toThrowError("parameter 'name' must be a 'string'"); |
||
| 311 | expect(test1).not.toThrowError("parameter 'message' must be a 'string'"); |
||
| 312 | expect(test1).not.toThrowError("parameter 'code' must be a 'number'"); |
||
| 313 | for (let j = 0; j < args2.length; j += 1) { |
||
| 314 | arg2 = args2[j]; |
||
| 315 | expect(test2).not.toThrowError("parameter 'name' must be a 'string'"); |
||
| 316 | expect(test2).not.toThrowError("parameter 'message' must be a 'string'"); |
||
| 317 | expect(test2).not.toThrowError("parameter 'code' must be a 'number'"); |
||
| 318 | for (let e = 0; e < args3.length; e += 1) { |
||
| 319 | arg3 = args3[e]; |
||
| 320 | expect(test3).not.toThrowError("parameter 'name' must be a 'string'"); |
||
| 321 | expect(test3).not.toThrowError("parameter 'message' must be a 'string'"); |
||
| 322 | expect(test3).not.toThrowError("parameter 'code' must be a 'number'"); |
||
| 323 | } |
||
| 324 | } |
||
| 325 | } |
||
| 326 | }); |
||
| 327 | |||
| 328 | View Code Duplication | it("should throw an Error if 'message' or 'code' are invalid parameters", () => { |
|
| 329 | let arg1, arg2, arg3, test31, test32, test33, test21, test22, test11; |
||
| 330 | const noStr = [{}, true, false, 42, 3.1416, -42, -3.1416, () => null]; |
||
| 331 | const noNmb = [{}, true, false, '', "qwerty", () => null]; |
||
| 332 | test33 = (() => new Klass(arg1, arg2, arg3)); |
||
| 333 | test32 = (() => new Klass(null, arg2, arg3)); |
||
| 334 | test31 = (() => new Klass(null, null, arg3)); |
||
| 335 | test22 = (() => new Klass(arg1, arg2)); |
||
| 336 | test21 = (() => new Klass(null, arg2)); |
||
| 337 | test11 = (() => new Klass(arg1)); |
||
| 338 | if (typeof Symbol === "function") { |
||
| 339 | noStr.push(Symbol("symbol")); |
||
| 340 | noNmb.push(Symbol("symbol")); |
||
| 341 | } |
||
| 342 | for (let i = 0; i < noStr.length; i += 1) { |
||
| 343 | arg1 = noStr[i]; |
||
| 344 | expect(test11).toThrowError("parameter 'name' must be a 'string'"); |
||
| 345 | for (let j = 0; j < noStr.length; j += 1) { |
||
| 346 | arg2 = noStr[j]; |
||
| 347 | expect(test22).toThrowError("parameter 'name' must be a 'string'"); |
||
| 348 | expect(test21).toThrowError("parameter 'message' must be a 'string'"); |
||
| 349 | for (let e = 0; e < noNmb.length; e += 1) { |
||
| 350 | arg3 = noNmb[e]; |
||
| 351 | expect(test33).toThrowError("parameter 'name' must be a 'string'"); |
||
| 352 | expect(test32).toThrowError("parameter 'message' must be a 'string'"); |
||
| 353 | expect(test31).toThrowError("parameter 'code' must be a 'number'"); |
||
| 354 | } |
||
| 355 | } |
||
| 356 | } |
||
| 357 | }); |
||
| 358 | |||
| 359 | // :: MEMBER PROPERTIES |
||
| 360 | |||
| 361 | const name = "qwerty"; |
||
| 362 | const message = "asdf"; |
||
| 363 | const code = Math.round(Math.random() * 0xFFFFFFFF); |
||
| 364 | |||
| 365 | it("should have all correct properties once instantiated", () => { |
||
| 366 | instanceDefinedOrNull(Klass, name, message, code, (instance, even) => { |
||
| 367 | if (even) { |
||
| 368 | expect(instance.name).toEqual(name); |
||
| 369 | expect(instance.message).toEqual(Klass.prototype.message); |
||
| 370 | } else { |
||
| 371 | expect(instance.name).toEqual(Klass.prototype.name); |
||
| 372 | expect(instance.message).toEqual(Klass.prototype.message); |
||
| 373 | } |
||
| 374 | expect(instance.code).toBeNull(); |
||
| 375 | }, (instance, even1, even2) => { |
||
| 376 | expect(instance.name).toEqual(even1 ? name : Klass.prototype.name); |
||
| 377 | expect(instance.message).toEqual(even2 ? message : Klass.prototype.message); |
||
| 378 | expect(instance.code).toBeNull(); |
||
| 379 | }, (instance, even1, even2, even3) => { |
||
| 380 | expect(instance.name).toEqual(even1 ? name : Klass.prototype.name); |
||
| 381 | expect(instance.message).toEqual(even2 ? message : Klass.prototype.message); |
||
| 382 | expect(instance.code).toEqual(even3 ? code : null); |
||
| 383 | }, false); |
||
| 384 | }); |
||
| 385 | |||
| 386 | // :: MEMBER METHODS |
||
| 387 | |||
| 388 | it("#toString()", () => { |
||
| 389 | for (let i = 0; i < 2; i += 1) { |
||
| 390 | let exp1; |
||
| 391 | const even1 = (i % 2 === 0); |
||
| 392 | const arg1 = (even1 ? name : null); |
||
| 393 | const source1 = new Klass(arg1); |
||
| 394 | if (even1) { |
||
| 395 | exp1 = name + ": " + Klass.prototype.message + '.'; |
||
| 396 | } else { |
||
| 397 | exp1 = Klass.prototype.name + ": " + Klass.prototype.message + '.'; |
||
| 398 | } |
||
| 399 | expect(source1.toString()).toEqual(exp1); |
||
| 400 | View Code Duplication | for (let j = 0; j < 2; j += 1) { |
|
| 401 | let exp2; |
||
| 402 | const even2 = (j % 2 === 0); |
||
| 403 | const arg2 = (even2 ? message : null); |
||
| 404 | const source2 = new Klass(arg1, arg2); |
||
| 405 | exp2 = (even1 ? name : Klass.prototype.name) + ':'; |
||
| 406 | exp2 += ' ' + (even2 ? message : Klass.prototype.message) + '.'; |
||
| 407 | expect(source2.toString()).toEqual(exp2); |
||
| 408 | for (let e = 0; e < 2; e += 1) { |
||
| 409 | let exp3; |
||
| 410 | const even3 = (e % 2 === 0); |
||
| 411 | const arg3 = (even3 ? code : null); |
||
| 412 | const source3 = new Klass(arg1, arg2, arg3); |
||
| 413 | exp3 = (even1 ? name : Klass.prototype.name); |
||
| 414 | exp3 += (even3 ? " (0x" + code.toString(16) + "):" : ':') + ' '; |
||
| 415 | exp3 += (even2 ? message : Klass.prototype.message) + '.'; |
||
| 416 | expect(source3.toString()).toEqual(exp3); |
||
| 417 | } |
||
| 418 | } |
||
| 419 | } |
||
| 420 | }); |
||
| 421 | |||
| 422 | it("#native()", () => { |
||
| 423 | instanceDefinedOrNull(Klass, name, message, code, (instance) => { |
||
| 424 | const exp = Klass.prototype.message; |
||
| 425 | expect(instance.native()).toEqual(new Error(exp)); |
||
| 426 | }, (instance, even1, even2) => { |
||
| 427 | const exp = (even2 ? message : Klass.prototype.message); |
||
| 428 | expect(instance.native()).toEqual(new Error(exp)); |
||
| 429 | }, (instance, even1, even2) => { |
||
| 430 | const exp = (even2 ? message : Klass.prototype.message); |
||
| 431 | expect(instance.native()).toEqual(new Error(exp)); |
||
| 432 | }, false); |
||
| 433 | }); |
||
| 434 | |||
| 435 | } |