-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathcode-complexity.js
65 lines (56 loc) · 2.1 KB
/
code-complexity.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const linter = require('../../../lib/index')
const { funcWith, modifierWith, multiLine } = require('../../common/contract-builder')
const { assertErrorCount, assertErrorMessage, assertNoErrors } = require('../../common/asserts')
describe('Linter - code-complexity', () => {
it('should raise error when cyclomatic complexity of a function is too high', () => {
const report = linter.processStr(
funcWith(require('../../fixtures/best-practices/code-complexity-high')),
{
rules: { 'code-complexity': 'error' },
}
)
assertErrorCount(report, 1)
assertErrorMessage(report, 'complexity')
})
it('should not raise error when cyclomatic complexity of a function is equal to max default allowed', () => {
const report = linter.processStr(
funcWith(require('../../fixtures/best-practices/code-complexity-low')),
{
rules: { 'code-complexity': 'error' },
}
)
assertNoErrors(report)
})
it('should raise error when cyclomatic complexity of a modifier is too high', () => {
const report = linter.processStr(
modifierWith(require('../../fixtures/best-practices/code-complexity-high')),
{
rules: { 'code-complexity': 'error' },
}
)
assertErrorCount(report, 1)
assertErrorMessage(report, 'complexity')
})
const CUSTOM_CONFIG_CHECK_CODE = funcWith(
multiLine(
' if (a > b) { ',
' if (b > c) { ',
' if (c > d) { ',
' } ',
' } ',
' } ',
'for (i = 0; i < b; i += 1) { } ',
'do { d++; } while (b > c); ',
'while (d > e) { } ',
'while (d > e) { } ',
'while (d > e) { } ',
'while (d > e) { } '
)
)
it('should not raise error when cyclomatic complexity is equal to max allowed', () => {
const report = linter.processStr(CUSTOM_CONFIG_CHECK_CODE, {
rules: { 'code-complexity': ['error', 12] },
})
assertNoErrors(report)
})
})