Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(rule): template-cyclomatic-complexity not reporting failures for …
…'[ngForOf]' and '[ngIf]' (#612)
- Loading branch information
1 parent
5e34f41
commit fedd331
Showing
2 changed files
with
114 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,77 +1,123 @@ | ||
import { assertSuccess, assertAnnotated } from './testHelper'; | ||
import { getFailureMessage, Rule } from '../src/templateCyclomaticComplexityRule'; | ||
import { assertAnnotated, assertSuccess } from './testHelper'; | ||
|
||
describe('cyclomatic complexity', () => { | ||
describe('success', () => { | ||
it('should work with a lower level of complexity', () => { | ||
let source = ` | ||
const { | ||
metadata: { ruleName } | ||
} = Rule; | ||
|
||
describe(ruleName, () => { | ||
describe('failure', () => { | ||
it('should fail with a higher level of complexity', () => { | ||
const source = ` | ||
@Component({ | ||
template: \` | ||
<div *ngIf="a === '1'"> | ||
<li *ngFor="let person of persons; trackBy: trackByFn"> | ||
{{ person.name }} | ||
<div *ngFor="let person of persons; trackBy: trackByFn"> | ||
<div *ngIf="a === '1'">{{ person.name }}</div> | ||
<div [ngSwitch]="person.emotion"> | ||
<app-happy-hero *ngSwitchCase="'happy'" [hero]="currentHero"></app-happy-hero> | ||
<app-sad-hero *ngSwitchCase="'sad'" [hero]="currentHero"></app-sad-hero> | ||
<app-unknown-hero *ngSwitchDefault [hero]="currentHero"></app-unknown-hero> | ||
<app-happy-hero *ngSwitchCase="'happy'" [hero]="currentHero"></app-happy-hero> | ||
<app-sad-hero *ngSwitchCase="'sad'" [hero]="currentHero"></app-sad-hero> | ||
<app-confused-hero *ngSwitchCase="'confused'" [hero]="currentHero"></app-confused-hero> | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
<app-unknown-hero *ngSwitchDefault [hero]="currentHero"></app-unknown-hero> | ||
</div> | ||
</li> | ||
</div> | ||
</div> | ||
\` | ||
}) | ||
class Bar {} | ||
`; | ||
assertSuccess('template-cyclomatic-complexity', source); | ||
assertAnnotated({ | ||
message: getFailureMessage(), | ||
ruleName, | ||
source | ||
}); | ||
}); | ||
|
||
it('should work with a higher level of complexity', () => { | ||
let source = ` | ||
it('should fail with a higher level of complexity using directives with ng-template', () => { | ||
const source = ` | ||
@Component({ | ||
template: \` | ||
<div [fakeDirective]="'test'"></div> | ||
<ng-template ngFor let-person [ngForOf]="persons" let-i="index"> | ||
{{ person.name }} | ||
</ng-template> | ||
<ng-template [ngIf]="a === '1'"> | ||
something here | ||
</ng-template> | ||
<div *ngIf="a === '1'"> | ||
<li *ngFor="let person of persons; trackBy: trackByFn"> | ||
<div *ngFor="let person of persons; trackBy: trackByFn"> | ||
<div *ngIf="a === '1'">{{ person.name }}</div> | ||
<div [ngSwitch]="person.emotion"> | ||
<app-happy-hero *ngSwitchCase="'happy'" [hero]="currentHero"></app-happy-hero> | ||
<app-sad-hero *ngSwitchCase="'sad'" [hero]="currentHero"></app-sad-hero> | ||
~~~~~~~~~~~~~~~~~~~~~ | ||
<app-confused-hero *ngSwitchCase="'confused'" [hero]="currentHero"></app-confused-hero> | ||
<app-unknown-hero *ngSwitchDefault [hero]="currentHero"></app-unknown-hero> | ||
</div> | ||
</li> | ||
</div> | ||
</div> | ||
\` | ||
}) | ||
class Bar {} | ||
`; | ||
assertSuccess('template-cyclomatic-complexity', source, [7]); | ||
assertAnnotated({ | ||
message: getFailureMessage(6), | ||
options: [6], | ||
ruleName, | ||
source | ||
}); | ||
}); | ||
}); | ||
|
||
describe('failure', () => { | ||
it('should fail with a higher level of complexity', () => { | ||
let source = ` | ||
describe('success', () => { | ||
it('should work with a lower level of complexity', () => { | ||
const source = ` | ||
@Component({ | ||
template: \` | ||
<div *ngIf="a === '1'"> | ||
<div *ngFor="let person of persons; trackBy: trackByFn"> | ||
{{ person.name }} | ||
<div [ngSwitch]="person.emotion"> | ||
<app-happy-hero *ngSwitchCase="'happy'" [hero]="currentHero"></app-happy-hero> | ||
<app-sad-hero *ngSwitchCase="'sad'" [hero]="currentHero"></app-sad-hero> | ||
<app-unknown-hero *ngSwitchDefault [hero]="currentHero"></app-unknown-hero> | ||
</div> | ||
</div> | ||
</div> | ||
\` | ||
}) | ||
class Bar {} | ||
`; | ||
assertSuccess(ruleName, source); | ||
}); | ||
|
||
it('should work with a higher level of complexity', () => { | ||
const source = ` | ||
@Component({ | ||
template: \` | ||
<div *ngIf="a === '1'"> | ||
<li *ngFor="let person of persons; trackBy: trackByFn"> | ||
<div *ngFor="let person of persons; trackBy: trackByFn"> | ||
<div *ngIf="a === '1'">{{ person.name }}</div> | ||
<div [ngSwitch]="person.emotion"> | ||
<app-happy-hero *ngSwitchCase="'happy'" [hero]="currentHero"></app-happy-hero> | ||
<app-sad-hero *ngSwitchCase="'sad'" [hero]="currentHero"></app-sad-hero> | ||
<app-confused-hero *ngSwitchCase="'confused'" [hero]="currentHero"></app-confused-hero> | ||
~~~~~~~~~~~~~~~~~~~~~~~~~~ | ||
<app-unknown-hero *ngSwitchDefault [hero]="currentHero"></app-unknown-hero> | ||
</div> | ||
</li> | ||
</div> | ||
</div> | ||
\` | ||
}) | ||
class Bar {} | ||
`; | ||
assertAnnotated({ | ||
ruleName: 'template-cyclomatic-complexity', | ||
message: "The cyclomatic complexity exceeded the defined limit (cost '5'). Your template should be refactored.", | ||
source | ||
}); | ||
assertSuccess(ruleName, source, [7]); | ||
}); | ||
}); | ||
}); |