Skip to content
This repository has been archived by the owner on Aug 31, 2018. It is now read-only.

Commit

Permalink
test: add tests for eslint rules
Browse files Browse the repository at this point in the history
This adds tests for the custom eslint rules in this repository, using
the `RuleTester` test utility bundled with eslint.

PR-URL: nodejs/node#16138
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Yuta Hiroto <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
not-an-aardvark authored and addaleax committed Oct 15, 2017
1 parent 671bfbf commit 7699238
Show file tree
Hide file tree
Showing 11 changed files with 319 additions and 0 deletions.
26 changes: 26 additions & 0 deletions test/parallel/test-eslint-alphabetize-errors.js
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 }]
}
]
});
26 changes: 26 additions & 0 deletions test/parallel/test-eslint-buffer-constructor.js
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 }]
}
]
});
32 changes: 32 additions & 0 deletions test/parallel/test-eslint-crypto-check.js
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 }]
}
]
});
23 changes: 23 additions & 0 deletions test/parallel/test-eslint-inspector-check.js
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 }]
}
]
});
38 changes: 38 additions & 0 deletions test/parallel/test-eslint-no-let-in-for-declaration.js
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 }]
}
]
});
28 changes: 28 additions & 0 deletions test/parallel/test-eslint-no-unescaped-regexp-dot.js
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' }]
}
]
});
25 changes: 25 additions & 0 deletions test/parallel/test-eslint-prefer-assert-iferror.js
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.' }]
}
]
});
37 changes: 37 additions & 0 deletions test/parallel/test-eslint-prefer-assert-methods.js
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 '!=='"
}]
},
]
});
27 changes: 27 additions & 0 deletions test/parallel/test-eslint-prefer-common-mustnotcall.js
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 }]
}
]
});
26 changes: 26 additions & 0 deletions test/parallel/test-eslint-require-buffer.js
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 }]
}
]
});
31 changes: 31 additions & 0 deletions test/parallel/test-eslint-required-modules.js
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.' }]
}
]
});

0 comments on commit 7699238

Please sign in to comment.