-
Notifications
You must be signed in to change notification settings - Fork 179
/
Copy pathnamed-parameters-mapping.js
136 lines (113 loc) · 4.42 KB
/
named-parameters-mapping.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
const assert = require('assert')
const { assertWarnsCount } = require('../../common/asserts')
const linter = require('../../../lib/index')
const contractWith = require('../../common/contract-builder').contractWith
const {
NAMED_MAPPING_REGULAR,
NAMED_MAPPING_NESTED,
OTHER_OK_DECLARATIONS,
NO_NAMED_MAPPING_REGULAR,
NO_NAMED_MAPPING_NESTED,
OTHER_WRONG_DECLARATIONS,
} = require('../../fixtures/naming/named-parameters-mapping')
const WRONG_DECLARATIONS = NO_NAMED_MAPPING_REGULAR.concat(
NO_NAMED_MAPPING_NESTED,
OTHER_WRONG_DECLARATIONS
)
const MAIN_KEY_ERROR = 'Main key parameter in mapping XXXXX is not named'
const VALUE_ERROR = 'Value parameter in mapping XXXXX is not named'
const getPositionErrors = (objectCode) => {
const errorArray = []
if (objectCode.error_mainKey)
errorArray.push(MAIN_KEY_ERROR.replace('XXXXX', objectCode.mapping_name))
if (objectCode.error_value) errorArray.push(VALUE_ERROR.replace('XXXXX', objectCode.mapping_name))
return errorArray
}
describe('Linter - Named parameters for mapping', () => {
it('should not raise an error if all parameters are named correctly for regular mapping', () => {
const code = contractWith(NAMED_MAPPING_REGULAR)
const report = linter.processStr(code, {
rules: { 'named-parameters-mapping': 'error' },
})
assert.equal(report.errorCount, 0)
})
it('should not raise an error if all parameters are named correctly for nested mapping', () => {
const code = contractWith(NAMED_MAPPING_NESTED)
const report = linter.processStr(code, {
rules: { 'named-parameters-mapping': 'error' },
})
assert.equal(report.errorCount, 0)
})
OTHER_OK_DECLARATIONS.forEach((objectCode) =>
it('should not raise an error if all parameters are named correctly for correct declarations', () => {
const code = contractWith(objectCode.code)
const report = linter.processStr(code, {
rules: { 'named-parameters-mapping': 'error' },
})
assert.equal(report.errorCount, 0)
})
)
WRONG_DECLARATIONS.forEach((objectCode) => {
it(`should raise an error if all parameters are not named for nested mappings`, () => {
const code = contractWith(objectCode.code)
const report = linter.processStr(code, {
rules: { 'named-parameters-mapping': 'error' },
})
const errorPositions = getPositionErrors(objectCode)
const qtyErrors = errorPositions.length
assert.equal(report.errorCount, qtyErrors)
for (let i = 0; i < errorPositions.length; i++) {
assert.equal(report.reports[i].message, errorPositions[i])
}
})
})
// WARNING FLAG
WRONG_DECLARATIONS.forEach((objectCode) =>
it('should raise a warning if all parameters are not named for nested mappings', () => {
const code = contractWith(objectCode.code)
const report = linter.processStr(code, {
rules: { 'named-parameters-mapping': 'warn' },
})
const errorPositions = getPositionErrors(objectCode)
const qtyErrors = errorPositions.length
assert.equal(report.errorCount, 0)
assertWarnsCount(report, qtyErrors)
for (let i = 0; i < errorPositions.length; i++) {
assert.equal(report.reports[i].message, errorPositions[i])
}
})
)
// WARNING FLAG
OTHER_OK_DECLARATIONS.forEach((objectCode) =>
it('should not raise an error if all parameters are named correctly for correct declarations', () => {
const code = contractWith(objectCode.code)
const report = linter.processStr(code, {
rules: { 'named-parameters-mapping': 'warn' },
})
assert.equal(report.errorCount, 0)
assertWarnsCount(report, 0)
})
)
// OFF FLAG
WRONG_DECLARATIONS.forEach((objectCode) =>
it('should raise a warning if all parameters are not named for nested mappings', () => {
const code = contractWith(objectCode.code)
const report = linter.processStr(code, {
rules: { 'named-parameters-mapping': 'off' },
})
assert.equal(report.errorCount, 0)
assertWarnsCount(report, 0)
})
)
// OFF FLAG
OTHER_OK_DECLARATIONS.forEach((objectCode) =>
it('should not raise an error if all parameters are named correctly for correct declarations', () => {
const code = contractWith(objectCode.code)
const report = linter.processStr(code, {
rules: { 'named-parameters-mapping': 'off' },
})
assert.equal(report.errorCount, 0)
assertWarnsCount(report, 0)
})
)
})