-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathconst-name-snakecase.js
96 lines (72 loc) · 3.14 KB
/
const-name-snakecase.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
89
90
91
92
93
94
95
96
const assert = require('assert')
const linter = require('../../../lib/index')
const contractWith = require('../../common/contract-builder').contractWith
describe('Linter - const-name-snakecase', () => {
it('should raise const name error', () => {
const code = contractWith('uint private constant a;')
const report = linter.processStr(code, {
rules: { 'const-name-snakecase': 'error' },
})
assert.equal(report.errorCount, 1)
assert.ok(report.messages[0].message.includes('SNAKE_CASE'))
})
it('should not raise const name error for constants in snake case', () => {
const code = contractWith('uint32 private constant THE_CONSTANT = 10;')
const report = linter.processStr(code, {
rules: { 'const-name-snakecase': 'error' },
})
assert.equal(report.errorCount, 0)
})
it('should not raise const name error for constants in snake case with single leading underscore', () => {
const code = contractWith('uint32 private constant _THE_CONSTANT = 10;')
const report = linter.processStr(code, {
rules: { 'const-name-snakecase': 'error' },
})
assert.equal(report.errorCount, 0)
})
it('should not raise const name error for constants in snake case with double leading underscore', () => {
const code = contractWith('uint32 private constant __THE_CONSTANT = 10;')
const report = linter.processStr(code, {
rules: { 'const-name-snakecase': 'error' },
})
assert.equal(report.errorCount, 0)
})
it('should raise const name error for constants in snake case with more than two leading underscores', () => {
const code = contractWith('uint32 private constant ___THE_CONSTANT = 10;')
const report = linter.processStr(code, {
rules: { 'const-name-snakecase': 'error' },
})
assert.equal(report.errorCount, 1)
assert.ok(report.messages[0].message.includes('SNAKE_CASE'))
})
it('should not raise const name error for immutable variables in SNAKE_CASE', () => {
const code = contractWith('uint32 private immutable SNAKE_CASE;')
const report = linter.processStr(code, {
rules: { 'const-name-snakecase': 'error' },
})
assert.equal(report.errorCount, 0)
})
it('should not raise const name error for immutable variables in mixedCase', () => {
const code = contractWith('uint32 private immutable SNAKE_CASE;')
const report = linter.processStr(code, {
rules: { 'const-name-snakecase': 'error' },
})
assert.equal(report.errorCount, 0)
})
describe('constant name with $ character', () => {
const WITH_$ = {
'starting with $': contractWith('uint32 private constant $THE_CONSTANT = 10;'),
'containing a $': contractWith('uint32 private constant THE_$_CONSTANT = 10;'),
'ending with $': contractWith('uint32 private constant THE_CONSTANT$ = 10;'),
'only with $': contractWith('uint32 private constant $ = 10;'),
}
for (const [key, code] of Object.entries(WITH_$)) {
it(`should not raise error for ${key}`, () => {
const report = linter.processStr(code, {
rules: { 'const-name-snakecase': 'error' },
})
assert.equal(report.errorCount, 0)
})
}
})
})