-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathgas-named-return-values.js
102 lines (87 loc) · 3.41 KB
/
gas-named-return-values.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
97
98
99
100
101
102
const assert = require('assert')
const linter = require('../../../lib/index')
const contractWith = require('../../common/contract-builder').contractWith
const { assertErrorCount, assertNoErrors, assertWarnsCount } = require('../../common/asserts')
describe('Linter - gas-named-return-values', () => {
it('should NOT raise error for named return values', () => {
const code = contractWith(
`function getBalanceFromTokens(address wallet) public returns(address token1, address token2, uint256 balance1, uint256 balance2) { balance = 1; }`
)
const report = linter.processStr(code, {
rules: { 'gas-named-return-values': 'error' },
})
assertNoErrors(report)
})
it('should raise error for unnamed return values', () => {
const code = contractWith(
`function getBalanceFromTokens(address wallet) public returns(address, address, uint256, uint256) { balance = 1; }`
)
const report = linter.processStr(code, {
rules: { 'gas-named-return-values': 'error' },
})
assertErrorCount(report, 4)
for (let index = 0; index < report.reports.length; index++) {
assert.equal(
report.reports[index].message,
`GC: Named return value is missing - Index ${index}`
)
}
})
it('should NOT raise error for functions without return values', () => {
const code = contractWith(`function writeOnStorage(address wallet) public { balance = 1; }`)
const report = linter.processStr(code, {
rules: { 'gas-named-return-values': 'error' },
})
assertNoErrors(report)
})
it('should raise error for 2 unnamed return values', () => {
const code = contractWith(
`function getBalanceFromTokens(address wallet) public returns(address user, address, uint256 amount, uint256) { balance = 1; }`
)
const report = linter.processStr(code, {
rules: { 'gas-named-return-values': 'error' },
})
assertErrorCount(report, 2)
assert.equal(report.reports[0].message, `GC: Named return value is missing - Index 1`)
assert.equal(report.reports[1].message, `GC: Named return value is missing - Index 3`)
})
it('should NOT raise error for solhint:recommended setup', () => {
const code = contractWith(
`function getBalanceFromTokens(address wallet) public returns(address, address, uint256, uint256) { balance = 1; }`
)
const report = linter.processStr(code, {
extends: 'solhint:recommended',
rules: { 'compiler-version': 'off' },
})
assertNoErrors(report)
})
it('should NOT raise error for solhint:default setup', () => {
const code = contractWith(
`function getBalance(address wallet) public returns(uint256) { balance = 1; }`
)
const report = linter.processStr(code, {
extends: 'solhint:default',
})
assertNoErrors(report)
})
it('should raise error for solhint:all setup', () => {
const code = contractWith(
`function getBalance(uint256 wallet) public override returns(uint256, address) { wallet = 1; }`
)
const report = linter.processStr(code, {
extends: 'solhint:all',
rules: {
'compiler-version': 'off',
'comprehensive-interface': 'off',
'foundry-test-functions': 'off',
},
})
assertWarnsCount(report, 2)
for (let index = 0; index < report.reports.length; index++) {
assert.equal(
report.reports[index].message,
`GC: Named return value is missing - Index ${index}`
)
}
})
})