-
Notifications
You must be signed in to change notification settings - Fork 180
/
Copy pathexplicit-types.js
88 lines (76 loc) · 3.12 KB
/
explicit-types.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const linter = require('../../../lib/index')
const contractWith = require('../../common/contract-builder').contractWith
const { assertErrorCount, assertNoErrors, assertErrorMessage } = require('../../common/asserts')
const VAR_DECLARATIONS = require('../../fixtures/best-practices/explicit-types')
const getZeroErrosObject = () => {
const zeroErrorsExplicit = {}
const zeroErrorsImplicit = {}
for (const key in VAR_DECLARATIONS) {
const obj = VAR_DECLARATIONS[key]
if (obj.errorsImplicit === 0) {
zeroErrorsImplicit[key] = obj
}
if (obj.errorsExplicit === 0) {
zeroErrorsExplicit[key] = obj
}
}
return { zeroErrorsImplicit, zeroErrorsExplicit }
}
describe('Linter - explicit-types rule', () => {
it('should throw an error with a wrong configuration rule', () => {
const code = contractWith('uint256 public constant SNAKE_CASE = 1;')
const report = linter.processStr(code, {
rules: { 'explicit-types': ['error', 'implicito'] },
})
assertErrorCount(report, 1)
assertErrorMessage(report, `Error: Config error on [explicit-types]. See explicit-types.md.`)
})
it('should NOT throw error without the config array and default should take place', () => {
const code = contractWith('uint256 public constant SNAKE_CASE = 1;')
const report = linter.processStr(code, {
rules: { 'explicit-types': 'error' },
})
assertNoErrors(report)
})
for (const key in VAR_DECLARATIONS) {
it(`should raise error for ${key} on 'implicit' mode`, () => {
const { code, errorsImplicit } = VAR_DECLARATIONS[key]
const report = linter.processStr(contractWith(code), {
rules: { 'explicit-types': ['error', 'implicit'] },
})
assertErrorCount(report, errorsImplicit)
if (errorsImplicit > 0) assertErrorMessage(report, `Rule is set with implicit type [var/s:`)
})
}
for (const key in VAR_DECLARATIONS) {
it(`should raise error for ${key} on 'explicit' mode`, () => {
const { code, errorsExplicit } = VAR_DECLARATIONS[key]
const report = linter.processStr(contractWith(code), {
rules: { 'explicit-types': ['error', 'explicit'] },
})
assertErrorCount(report, errorsExplicit)
if (errorsExplicit > 0) assertErrorMessage(report, `Rule is set with explicit type [var/s:`)
})
}
describe('Rule [explicit-types] - should not raise any error', () => {
const { zeroErrorsImplicit, zeroErrorsExplicit } = getZeroErrosObject()
for (const key in zeroErrorsExplicit) {
it(`should NOT raise error for ${key} on 'explicit' mode`, () => {
const { code } = zeroErrorsExplicit[key]
const report = linter.processStr(contractWith(code), {
rules: { 'explicit-types': ['error', 'explicit'] },
})
assertNoErrors(report)
})
}
for (const key in zeroErrorsImplicit) {
it(`should NOT raise error for ${key} on 'implicit' mode`, () => {
const { code } = zeroErrorsImplicit[key]
const report = linter.processStr(contractWith(code), {
rules: { 'explicit-types': ['error', 'implicit'] },
})
assertNoErrors(report)
})
}
})
})