Skip to content

Commit

Permalink
visualizeRuleTester: Support options (#840)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Sep 29, 2020
1 parent 18bb3a3 commit 2105707
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 22 deletions.
27 changes: 13 additions & 14 deletions test/explicit-length-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -182,18 +182,17 @@ ruleTester.run('explicit-length-check', rule, {
const visualizeTester = visualizeRuleTester(test);
visualizeTester.run('explicit-length-check', rule, [
'if ([].length) {}',
'if (array.length < 1) {}'
// `visualizeRuleTester` don't support options
// {
// code: 'if (array.length > 0) {}',
// options: ['not-equal']
// },
// {
// code: 'if (array.length != 0) {}',
// options: ['greater-than']
// },
// {
// code: 'if (array.length != 0) {}',
// options: ['greater-than-or-equal']
// }
'if (array.length < 1) {}',
{
code: 'if (array.length > 0) {}',
options: [{'non-zero': 'not-equal'}]
},
{
code: 'if (array.length != 0) {}',
options: [{'non-zero': 'greater-than'}]
},
{
code: 'if (array.length != 0) {}',
options: [{'non-zero': 'greater-than-or-equal'}]
}
]);
27 changes: 27 additions & 0 deletions test/snapshots/explicit-length-check.js.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,30 @@ Generated by [AVA](https://avajs.dev).
> 1 | if (array.length < 1) {}␊
| ^^^^^^^^^^^^^^^^ Zero `.length` should be compared with `=== 0`.␊
`

## explicit-length-check - #3

> Snapshot 1
`␊
> 1 | if (array.length > 0) {}␊
| ^^^^^^^^^^^^^^^^ Non-zero `.length` should be compared with `!== 0`.␊
`

## explicit-length-check - #4

> Snapshot 1
`␊
> 1 | if (array.length != 0) {}␊
| ^^^^^^^^^^^^^^^^^ Non-zero `.length` should be compared with `> 0`.␊
`

## explicit-length-check - #5

> Snapshot 1
`␊
> 1 | if (array.length != 0) {}␊
| ^^^^^^^^^^^^^^^^^ Non-zero `.length` should be compared with `>= 1`.␊
`
Binary file modified test/snapshots/explicit-length-check.js.snap
Binary file not shown.
19 changes: 11 additions & 8 deletions test/utils/visualize-rule-tester.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,13 @@ function visualizeEslintResult(text, result) {
return `\n${visualizeRange(text, location, message)}\n`;
}

const getVerifyConfig = (ruleId, testerConfig, options) => ({
...testerConfig,
rules: {
[ruleId]: ['error', ...(Array.isArray(options) ? options : [])]
}
});

class VisualizeRuleTester {
constructor(test, config) {
this.test = test;
Expand All @@ -42,16 +49,12 @@ class VisualizeRuleTester {
run(ruleId, rule, tests) {
const {test, config} = this;
const linter = new Linter();
const verifyConfig = {
...config,
rules: {
[ruleId]: 'error'
}
};

linter.defineRule(ruleId, rule);

for (const [index, code] of tests.entries()) {
for (const [index, testCase] of tests.entries()) {
const {code, options} = typeof testCase === 'string' ? {code: testCase} : testCase;
const verifyConfig = getVerifyConfig(ruleId, config, options);

test(`${ruleId} - #${index + 1}`, t => {
const results = linter.verify(code, verifyConfig);

Expand Down

0 comments on commit 2105707

Please sign in to comment.