UNPKG

3.77 kBJavaScriptView Raw
1const assert = require('assert')
2const linter = require('./../../../lib/index')
3const { contractWith, libraryWith } = require('./../../common/contract-builder')
4
5describe('Linter - private-vars-leading-underscore', () => {
6 const SHOULD_WARN_CASES = [
7 // warn when private/internal names don't start with _
8 contractWith('uint foo;'),
9 contractWith('uint private foo;'),
10 contractWith('uint internal foo;'),
11 contractWith('function foo() private {}'),
12 contractWith('function foo() internal {}'),
13 libraryWith('function foo() private {}'),
14
15 // warn when public/external/default names start with _
16 contractWith('uint public _foo;'),
17 contractWith('function _foo() {}'),
18 contractWith('function _foo() public {}'),
19 contractWith('function _foo() external {}'),
20 libraryWith('function _foo() {}'),
21 libraryWith('function _foo() public {}'),
22 libraryWith('function _foo() internal {}')
23 ]
24 const SHOULD_WARN_STRICT_CASES = [
25 contractWith('function foo() { uint _bar; }'),
26 contractWith('function foo(uint _bar) {}'),
27 contractWith('function foo() returns (uint256 _bar) {}'),
28 libraryWith('function foo() returns (uint256 _bar) {}'),
29 libraryWith('function foo(uint _bar) {}')
30 ]
31 const SHOULD_NOT_WARN_CASES = [
32 // don't warn when private/internal names start with _
33 contractWith('uint _foo;'),
34 contractWith('uint private _foo;'),
35 contractWith('uint internal _foo;'),
36 contractWith('function _foo() private {}'),
37 contractWith('function _foo() internal {}'),
38 libraryWith('function _foo() private {}'),
39
40 // don't warn when public/external/default names don't start with _
41 contractWith('uint public foo;'),
42 contractWith('function foo() {}'),
43 contractWith('function foo() public {}'),
44 contractWith('function foo() external {}'),
45 libraryWith('function foo() {}'),
46 libraryWith('function foo() public {}'),
47 libraryWith('function foo() internal {}'),
48
49 // don't warn for constructors
50 contractWith('constructor() public {}'),
51
52 // other names (variables, parameters, returns) shouldn't be affected by this rule
53 contractWith('function foo(uint bar) {}'),
54 contractWith('function foo() { uint bar; }'),
55 contractWith('function foo() returns (uint256) {}'),
56 contractWith('function foo() returns (uint256 bar) {}'),
57 libraryWith('function foo(uint bar) {}'),
58 libraryWith('function foo() { uint bar; }'),
59 libraryWith('function foo() returns (uint256) {}'),
60 libraryWith('function foo() returns (uint256 bar) {}')
61 ]
62
63 SHOULD_WARN_CASES.forEach((code, index) => {
64 it(`should emit a warning (${index})`, () => {
65 const report = linter.processStr(code, {
66 rules: { 'private-vars-leading-underscore': 'error' }
67 })
68
69 assert.equal(report.errorCount, 1)
70 })
71 })
72
73 SHOULD_WARN_STRICT_CASES.concat(SHOULD_NOT_WARN_CASES).forEach((code, index) => {
74 it(`should not emit a warning (strict) (${index})`, () => {
75 const report = linter.processStr(code, {
76 rules: { 'private-vars-leading-underscore': 'error' }
77 })
78
79 assert.equal(report.errorCount, 0)
80 })
81 })
82
83 SHOULD_NOT_WARN_CASES.forEach((code, index) => {
84 it(`should not emit a warning (${index})`, () => {
85 const report = linter.processStr(code, {
86 rules: { 'private-vars-leading-underscore': ['error', { strict: true }] }
87 })
88
89 assert.equal(report.errorCount, 0)
90 })
91 })
92
93 SHOULD_WARN_STRICT_CASES.forEach((code, index) => {
94 it(`should not emit a warning (strict) (${index})`, () => {
95 const report = linter.processStr(code, {
96 rules: { 'private-vars-leading-underscore': ['error', { strict: true }] }
97 })
98
99 assert.equal(report.errorCount, 1)
100 })
101 })
102})