UNPKG

tslint-clean-code

Version:
251 lines 22.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); var TestHelper_1 = require("./TestHelper"); var newspaperOrderRule_1 = require("../newspaperOrderRule"); describe('newspaperOrderRule', function () { var ruleName = 'newspaper-order'; context('ClassDeclaration', function () { it('should pass on empty class', function () { var script = "\n class EmptyClass {\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, []); }); it('should pass on class with 1 method', function () { var script = "\n class SingleMethodClass {\n private onlyMethod() {\n }\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, []); }); it('should pass on class with 2 unrelated method', function () { var script = "\n class UnrelatedMethodsClass {\n private firstMethod() {\n }\n private secondMethod() {\n }\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, []); }); it('should pass on class with 2 unrelated methods using an instance field', function () { var script = "\n class UnrelatedMethodsClass {\n private field;\n private secondMethod() {\n return this.field * 2;\n }\n private firstMethod() {\n return this.field;\n }\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, []); }); it('should fail on incorrectly ordered class methods', function () { var script = "\n class BadClass {\n private secondMethod() {\n return true;\n }\n private firstMethod() {\n return this.secondMethod();\n }\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, [ { failure: newspaperOrderRule_1.FAILURE_CLASS_STRING + 'BadClass' + '\n\nMethods order:\n1. x firstMethod\n2. x secondMethod', name: 'file.ts', ruleName: ruleName, startPosition: { character: 17, line: 3 }, }, ]); }); it('should pass on correctly ordered class methods', function () { var script = "\n class GoodClass {\n private firstMethod() {\n return this.secondMethod();\n }\n private secondMethod() {\n return true;\n }\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, []); }); it('should pass on correctly ordered class getter methods', function () { var script = "\n class GoodClass {\n private get firstMethod() {\n return this.secondMethod;\n }\n private get secondMethod() {\n return true;\n }\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, []); }); it('should pass with 3 methods (2 related, 1 independent)', function () { var script = "\n class GoodClass {\n private firstRelatedMethod() {\n return this.secondRelatedMethod();\n }\n private secondRelatedMethod() {\n return true;\n }\n private independentMethod() {\n return true;\n }\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, []); }); it('should pass with 3 methods (1 independent, 2 related)', function () { var script = "\n class GoodClass {\n private independentMethod() {\n return true;\n }\n private firstRelatedMethod() {\n return this.secondRelatedMethod();\n }\n private secondRelatedMethod() {\n return true;\n }\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, []); }); it('should fail on incorrectly ordered class getter methods', function () { var script = "\n class BadClass {\n private get secondMethod() {\n return true;\n }\n private get firstMethod() {\n return this.secondMethod;\n }\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, [ { failure: newspaperOrderRule_1.FAILURE_CLASS_STRING + 'BadClass' + '\n\nMethods order:\n1. x firstMethod\n2. x secondMethod', name: 'file.ts', ruleName: ruleName, startPosition: { character: 17, line: 3 }, }, ]); }); it('should pass on SubClass by ignoring calls to BaseClass methods', function () { var script = "\n class BaseClass {\n protected baseMethod() {\n return true;\n }\n }\n class SubClass extends BaseClass {\n private firstMethod() {\n return this.secondMethod();\n }\n private secondMethod() {\n return this.baseMethod();\n }\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, []); }); it('should fail on SubClass with incorrectly ordered methods and by ignoring calls to BaseClass methods', function () { var script = "\n class BaseClass {\n protected baseMethod() {\n return true;\n }\n }\n class SubClass extends BaseClass {\n private secondMethod() {\n return this.baseMethod();\n }\n private firstMethod() {\n return this.secondMethod();\n }\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, [ { failure: newspaperOrderRule_1.FAILURE_CLASS_STRING + 'SubClass' + '\n\nMethods order:\n1. x firstMethod\n2. x secondMethod', name: 'file.ts', ruleName: ruleName, startPosition: { character: 17, line: 8 }, }, ]); }); it('should pass on class with recursive method', function () { var script = "\n class CountDownClass {\n private startCountDown() {\n this.countDown(10);\n }\n private countDown(curr: number): void {\n if (curr > 0) {\n console.log(curr);\n return this.countDown(curr - 1);\n }\n }\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, []); }); it('should fail on class with incorrectly ordered methods with a recursive method', function () { var script = "\n class CountDownClass {\n private countDown(curr: number): void {\n if (curr > 0) {\n console.log(curr);\n return this.countDown(curr - 1);\n }\n }\n private startCountDown() {\n this.countDown(10);\n }\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, [ { failure: newspaperOrderRule_1.FAILURE_CLASS_STRING + 'CountDownClass' + '\n\nMethods order:\n1. x startCountDown\n2. x countDown', name: 'file.ts', ruleName: 'newspaper-order', ruleSeverity: 'ERROR', startPosition: { character: 17, line: 3, }, }, ]); }); it('should pass on class with correctly ordered methods and indirectly recursive methods', function () { var script = "\n class CountDownClass {\n private startCountDown() {\n this.countDown(10);\n }\n private countDown(curr: number): void {\n if (curr > 0) {\n console.log(curr);\n return this.step(curr);\n }\n }\n private step(curr: number): void {\n return this.countDown(curr - 1);\n }\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, []); }); it('should pass on class with correctly ordered methods and with indirectly recursive methods', function () { var script = "\n class CountDownClass {\n private startCountDown() {\n this.countDown(10);\n }\n private step(curr: number): void {\n return this.countDown(curr - 1);\n }\n private countDown(curr: number): void {\n if (curr > 0) {\n console.log(curr);\n return this.step(curr);\n }\n }\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, []); }); it('should fail on class with incorrectly ordered methods and indirectly recursive methods', function () { var script = "\n class CountDownClass {\n private step(curr: number): void {\n return this.countDown(curr - 1);\n }\n private countDown(curr: number): void {\n if (curr > 0) {\n console.log(curr);\n return this.step(curr);\n }\n }\n private startCountDown() {\n this.countDown(10);\n }\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, [ { failure: newspaperOrderRule_1.FAILURE_CLASS_STRING + 'CountDownClass' + '\n\nMethods order:\n1. x startCountDown\n2. ✓ countDown\n3. x step', name: 'file.ts', ruleName: 'newspaper-order', ruleSeverity: 'ERROR', startPosition: { character: 17, line: 3, }, }, ]); }); it('should pass on SubClass by ignoring undefined constructor calls', function () { var script = "\n class BaseClass {\n }\n class SubClass extends BaseClass {\n private firstMethod() {\n return this.secondMethod();\n }\n private secondMethod() {\n return this.constructor();\n }\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, []); }); it('should fail on subset of incorrectly ordered class methods', function () { var script = "\n class BadClass {\n private firstMethod() {\n return this.secondMethod();\n }\n private thirdMethod() {\n true;\n }\n private secondMethod() {\n return this.thirdMethod();\n }\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, [ { failure: newspaperOrderRule_1.FAILURE_CLASS_STRING + 'BadClass' + '\n\nMethods order:\n1. ✓ firstMethod\n2. x secondMethod\n3. x thirdMethod', name: 'file.ts', ruleName: ruleName, startPosition: { character: 17, line: 6 }, }, ]); }); }); context('SourceFile', function () { it('should pass on correctly ordered functions', function () { var script = "\n function firstMethod(): number {\n return 2 + secondMethod();\n }\n function secondMethod(): number {\n return 2;\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, []); }); it('should pass on correctly ordered functions', function () { var script = "\n function firstMethod(): number {\n return 1 + thirdMethod();\n }\n function secondMethod(): number {\n return 2 + thirdMethod();\n }\n function thirdMethod(): number {\n return 2;\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, []); }); it('should pass on correctly ordered functions', function () { var script = "\n function secondMethod(): number {\n return 2 + thirdMethod();\n }\n function firstMethod(): number {\n return 1 + thirdMethod();\n }\n function thirdMethod(): number {\n return 2;\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, []); }); it('should pass with 3 functions (2 related, 1 independent)', function () { var script = "\n function firstRelatedMethod() {\n return secondRelatedMethod();\n }\n function secondRelatedMethod() {\n return true;\n }\n function independentMethod() {\n return true;\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, []); }); it('should pass with 3 functions (1 independent, 2 related)', function () { var script = "\n function independentMethod() {\n return true;\n }\n function firstRelatedMethod() {\n return secondRelatedMethod();\n }\n function secondRelatedMethod() {\n return true;\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, []); }); it('should pass with 5 functions (3 independent, 2 related)', function () { var script = "\n function independentMethod1() {\n return true;\n }\n function relatedMethod1() {\n return relatedMethod2();\n }\n function independentMethod2() {\n return true;\n }\n function relatedMethod2() {\n return true;\n }\n function independentMethod3() {\n return true;\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, []); }); it('should pass with 5 functions (4 independent, 3 related)', function () { var script = "\n function independentMethod1() {\n return true;\n }\n function relatedMethod1() {\n return relatedMethod2();\n }\n function independentMethod2() {\n return true;\n }\n function relatedMethod2() {\n return true;\n }\n function independentMethod3() {\n return true;\n }\n function relatedMethod3() {\n return true;\n }\n function independentMethod4() {\n return true;\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, []); }); it('should fail on incorrectly ordered functions', function () { var script = "\n function secondMethod(): number {\n return 2;\n }\n function firstMethod(): number {\n return 2 + secondMethod();\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, [ { failure: newspaperOrderRule_1.FAILURE_FILE_STRING + 'file.ts' + '\n\nMethods order:\n1. x firstMethod\n2. x secondMethod', name: 'file.ts', ruleName: 'newspaper-order', ruleSeverity: 'ERROR', startPosition: { character: 13, line: 2, }, }, ]); }); it('should fail on subset of incorrectly ordered functions', function () { var script = "\n function firstMethod(): number {\n return 1 + secondMethod();\n }\n function thirdMethod(): number {\n return 3;\n }\n function secondMethod(): number {\n return thirdMethod();\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, [ { failure: newspaperOrderRule_1.FAILURE_FILE_STRING + 'file.ts' + '\n\nMethods order:\n1. ✓ firstMethod\n2. x secondMethod\n3. x thirdMethod', name: 'file.ts', ruleName: 'newspaper-order', ruleSeverity: 'ERROR', startPosition: { character: 13, line: 5, }, }, ]); }); it('should pass on correctly ordered functions with a function passed as an argument', function () { var script = "\n function first(): Promise<number> {\n return Promise.resolve(2)\n .then(second);\n }\n function second(n: number) {\n return third() * n;\n }\n function third(): number {\n return 2;\n }"; TestHelper_1.TestHelper.assertViolations(ruleName, script, []); }); it('should fail on incorrrectly ordered functions with a function as a variable', function () { var script = "\n const myFunc3 = function() {\n return \"Hello\";\n }\n function myFunc2() {\n return \"World\";\n }\n function myFunc1() {\n return myFunc2() + \" \" + myFunc3();\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, [ { failure: newspaperOrderRule_1.FAILURE_FILE_STRING + 'file.ts' + '\n\nMethods order:\n1. x myFunc1\n2. x myFunc2\n3. ✓ myFunc3', name: 'file.ts', ruleName: 'newspaper-order', ruleSeverity: 'ERROR', startPosition: { character: 13, line: 5, }, }, ]); }); }); context('Block', function () { it('should pass on correctly ordered functions within named function', function () { var script = "\n function doStuff() {\n function firstMethod(): number {\n return 2 + secondMethod();\n }\n function secondMethod(): number {\n return 2;\n }\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, []); }); it('should fail on incorrectly ordered functions within named function', function () { var script = "\n function doStuff() {\n function secondMethod(): number {\n return 2;\n }\n function firstMethod(): number {\n return 2 + secondMethod();\n }\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, [ { failure: newspaperOrderRule_1.FAILURE_BLOCK_STRING + 'doStuff' + '\n\nMethods order:\n1. x firstMethod\n2. x secondMethod', name: 'file.ts', ruleName: 'newspaper-order', ruleSeverity: 'ERROR', startPosition: { character: 25, line: 3, }, }, ]); }); it('should fail on incorrectly ordered functions within anonymous function', function () { var script = "\n function () {\n function secondMethod(): number {\n return 2;\n }\n function firstMethod(): number {\n return 2 + secondMethod();\n }\n }\n "; TestHelper_1.TestHelper.assertViolations(ruleName, script, [ { failure: newspaperOrderRule_1.FAILURE_BLOCK_STRING + '<anonymous>' + '\n\nMethods order:\n1. x firstMethod\n2. x secondMethod', name: 'file.ts', ruleName: 'newspaper-order', ruleSeverity: 'ERROR', startPosition: { character: 25, line: 3, }, }, ]); }); }); }); //# sourceMappingURL=NewspaperOrderRuleTests.js.map