-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This adds tests for the custom eslint rules in this repository, using the `RuleTester` test utility bundled with eslint. PR-URL: #16138 Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: Yuta Hiroto <hello@about-hiroppy.com> Reviewed-By: James M Snell <jasnell@gmail.com>
- Loading branch information
1 parent
f63acdb
commit 131cf42
Showing
11 changed files
with
319 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
|
||
const RuleTester = require('../../tools/eslint').RuleTester; | ||
const rule = require('../../tools/eslint-rules/alphabetize-errors'); | ||
|
||
new RuleTester().run('alphabetize-errors', rule, { | ||
valid: [ | ||
` | ||
E('AAA', 'foo'); | ||
E('BBB', 'bar'); | ||
E('CCC', 'baz'); | ||
` | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
E('BBB', 'bar'); | ||
E('AAA', 'foo'); | ||
E('CCC', 'baz'); | ||
`, | ||
errors: [{ message: 'Out of ASCIIbetical order - BBB >= AAA', line: 3 }] | ||
} | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
|
||
const RuleTester = require('../../tools/eslint').RuleTester; | ||
const rule = require('../../tools/eslint-rules/buffer-constructor'); | ||
|
||
const message = 'Use of the Buffer() constructor has been deprecated. ' + | ||
'Please use either Buffer.alloc(), Buffer.allocUnsafe(), ' + | ||
'or Buffer.from()'; | ||
|
||
new RuleTester().run('buffer-constructor', rule, { | ||
valid: [ | ||
'Buffer.from(foo)' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'Buffer(foo)', | ||
errors: [{ message }] | ||
}, | ||
{ | ||
code: 'new Buffer(foo)', | ||
errors: [{ message }] | ||
} | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
|
||
const RuleTester = require('../../tools/eslint').RuleTester; | ||
const rule = require('../../tools/eslint-rules/crypto-check'); | ||
|
||
const message = 'Please add a hasCrypto check to allow this test to be ' + | ||
'skipped when Node is built "--without-ssl".'; | ||
|
||
new RuleTester().run('crypto-check', rule, { | ||
valid: [ | ||
'foo', | ||
'crypto', | ||
` | ||
if (!common.hasCrypto) { | ||
common.skip(); | ||
} | ||
require('crypto'); | ||
` | ||
], | ||
invalid: [ | ||
{ | ||
code: 'require("crypto")', | ||
errors: [{ message }] | ||
}, | ||
{ | ||
code: 'if (common.foo) {} require("crypto")', | ||
errors: [{ message }] | ||
} | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
|
||
const RuleTester = require('../../tools/eslint').RuleTester; | ||
const rule = require('../../tools/eslint-rules/inspector-check'); | ||
|
||
const message = 'Please add a skipIfInspectorDisabled() call to allow this ' + | ||
'test to be skippped when Node is built ' + | ||
'\'--without-inspector\'.'; | ||
|
||
new RuleTester().run('inspector-check', rule, { | ||
valid: [ | ||
'foo;', | ||
'common.skipIfInspectorDisabled(); require("inspector");' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'require("inspector")', | ||
errors: [{ message }] | ||
} | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
|
||
const RuleTester = require('../../tools/eslint').RuleTester; | ||
const rule = require('../../tools/eslint-rules/no-let-in-for-declaration'); | ||
|
||
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } }); | ||
|
||
const message = 'Use of `let` as the loop variable in a for-loop is ' + | ||
'not recommended. Please use `var` instead.'; | ||
|
||
ruleTester.run('no-let-in-for-declaration', rule, { | ||
valid: [ | ||
'let foo;', | ||
'for (var foo = 1;;);', | ||
'for (foo = 1;;);', | ||
'for (;;);', | ||
'for (const foo of bar);', | ||
'for (var foo of bar);', | ||
'for (const foo in bar);', | ||
'for (var foo in bar);' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'for (let foo = 1;;);', | ||
errors: [{ message }] | ||
}, | ||
{ | ||
code: 'for (let foo in bar);', | ||
errors: [{ message }] | ||
}, | ||
{ | ||
code: 'for (let foo of bar);', | ||
errors: [{ message }] | ||
} | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
|
||
const RuleTester = require('../../tools/eslint').RuleTester; | ||
const rule = require('../../tools/eslint-rules/no-unescaped-regexp-dot'); | ||
|
||
new RuleTester().run('no-unescaped-regexp-dot', rule, { | ||
valid: [ | ||
'/foo/', | ||
String.raw`/foo\./`, | ||
'/.+/', | ||
'/.*/', | ||
'/.?/', | ||
'/.{5}/', | ||
String.raw`/\\\./` | ||
], | ||
invalid: [ | ||
{ | ||
code: '/./', | ||
errors: [{ message: 'Unescaped dot character in regular expression' }] | ||
}, | ||
{ | ||
code: String.raw`/\\./`, | ||
errors: [{ message: 'Unescaped dot character in regular expression' }] | ||
} | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
|
||
const RuleTester = require('../../tools/eslint').RuleTester; | ||
const rule = require('../../tools/eslint-rules/prefer-assert-iferror'); | ||
|
||
new RuleTester().run('prefer-assert-iferror', rule, { | ||
valid: [ | ||
'assert.ifError(err);', | ||
'if (err) throw somethingElse;', | ||
'throw err;', | ||
'if (err) { throw somethingElse; }' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'if (err) throw err;', | ||
errors: [{ message: 'Use assert.ifError(err) instead.' }] | ||
}, | ||
{ | ||
code: 'if (error) { throw error; }', | ||
errors: [{ message: 'Use assert.ifError(error) instead.' }] | ||
} | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
|
||
const RuleTester = require('../../tools/eslint').RuleTester; | ||
const rule = require('../../tools/eslint-rules/prefer-assert-methods'); | ||
|
||
new RuleTester().run('prefer-assert-methods', rule, { | ||
valid: [ | ||
'assert.strictEqual(foo, bar)', | ||
'assert(foo === bar && baz)' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'assert(foo == bar)', | ||
errors: [{ message: "'assert.equal' should be used instead of '=='" }] | ||
}, | ||
{ | ||
code: 'assert(foo === bar)', | ||
errors: [{ | ||
message: "'assert.strictEqual' should be used instead of '==='" | ||
}] | ||
}, | ||
{ | ||
code: 'assert(foo != bar)', | ||
errors: [{ | ||
message: "'assert.notEqual' should be used instead of '!='" | ||
}] | ||
}, | ||
{ | ||
code: 'assert(foo !== bar)', | ||
errors: [{ | ||
message: "'assert.notStrictEqual' should be used instead of '!=='" | ||
}] | ||
}, | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
|
||
const RuleTester = require('../../tools/eslint').RuleTester; | ||
const rule = require('../../tools/eslint-rules/prefer-common-mustnotcall'); | ||
|
||
const message = 'Please use common.mustNotCall(msg) instead of ' + | ||
'common.mustCall(fn, 0) or common.mustCall(0).'; | ||
|
||
new RuleTester().run('prefer-common-mustnotcall', rule, { | ||
valid: [ | ||
'common.mustNotCall(fn)', | ||
'common.mustCall(fn)', | ||
'common.mustCall(fn, 1)' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'common.mustCall(fn, 0)', | ||
errors: [{ message }] | ||
}, | ||
{ | ||
code: 'common.mustCall(0)', | ||
errors: [{ message }] | ||
} | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
|
||
const RuleTester = require('../../tools/eslint').RuleTester; | ||
const rule = require('../../tools/eslint-rules/require-buffer'); | ||
const ruleTester = new RuleTester({ | ||
parserOptions: { ecmaVersion: 6 }, | ||
env: { node: true } | ||
}); | ||
|
||
const message = "Use const Buffer = require('buffer').Buffer; " + | ||
'at the beginning of this file'; | ||
|
||
ruleTester.run('require-buffer', rule, { | ||
valid: [ | ||
'foo', | ||
'const Buffer = require("Buffer"); Buffer;' | ||
], | ||
invalid: [ | ||
{ | ||
code: 'Buffer;', | ||
errors: [{ message }] | ||
} | ||
] | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
'use strict'; | ||
|
||
require('../common'); | ||
|
||
const RuleTester = require('../../tools/eslint').RuleTester; | ||
const rule = require('../../tools/eslint-rules/required-modules'); | ||
|
||
new RuleTester().run('required-modules', rule, { | ||
valid: [ | ||
{ | ||
code: 'require("common")', | ||
options: ['common'] | ||
}, | ||
{ | ||
code: 'foo', | ||
options: [] | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: 'foo', | ||
options: ['common'], | ||
errors: [{ message: 'Mandatory module "common" must be loaded.' }] | ||
}, | ||
{ | ||
code: 'require("somethingElse")', | ||
options: ['common'], | ||
errors: [{ message: 'Mandatory module "common" must be loaded.' }] | ||
} | ||
] | ||
}); |