-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.js
110 lines (97 loc) · 2.78 KB
/
test.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
const { RuleTester } = require('eslint');
const config = require('./plugin');
const utils = require('./src/utils'),
ruleTester = new RuleTester();
function makeValidTests(words, locale) {
return words.flatMap(word =>
testCases.map(code => ({
code: code.replace('%word%', word),
options: [locale],
}))
);
}
const testCases = [
// Comments
`// Did you know that %word% is spelled differently in the US?`,
`// Did you know that not_%word% is spelled differently in the US?`,
`/* Did you know that %word% is spelled differently in the US? */`,
`/* Did you know that not_%word% is spelled differently in the US? */`,
// Strings
`const test = '%word%';`,
`const test = 'not_%word%';`,
`const test = "%word%";`,
`const test = "not_%word%";`,
// Template literals
`const test = \`%word%\`;`,
`const test = \`not_%word%\`;`,
// Variables
`const %word% = 'test';`,
`const not_%word% = 'test';`,
// Function names
`function %word%() {}`,
`function not_%word%() {}`,
// Function calls
`test(%word%);`,
`test(not_%word%);`,
`%word%();`,
`not_%word%();`,
// Class names
`class %word% {}`,
`class not_%word% {}`,
`new %word%();`,
`new not_%word%();`,
// Object keys
`const obj = { %word%: 1 };`,
`const obj = { not_%word%: 1 };`,
];
function makeInvalidTests(words, locale) {
return words.flatMap(word =>
testCases.map(code => {
const replacement = utils.contextAwareReplacement(word, utils.getLocaleFunction(locale).get(word));
return {
options: [locale],
code: code.replace('%word%', word),
errors: [{
message: `Use ${utils.localeName(locale)} spelling "${replacement}" instead of ${utils.localeName(locale === 'US' ? 'GB' : 'US')} spelling "${word}"`,
}],
output: code.replace('%word%', replacement),
};
})
);
}
ruleTester.run('consistent-spelling', config.rules['consistent-spelling'], {
valid: makeValidTests([
'color',
'center',
'favorite',
'realize',
'organize',
'recognize',
'apologize',
], 'US').concat(makeValidTests([
'colour',
'centre',
'favourite',
'realise',
'organise',
'recognise',
'apologise',
], 'GB')),
invalid: makeInvalidTests([
'colour',
'centre',
'favourite',
'realise',
'organise',
'recognise',
'apologise',
], 'US').concat(makeInvalidTests([
'color',
'center',
'favorite',
'realize',
'organize',
'recognize',
'apologize',
], 'GB')),
});