-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
eslint-plugin-tdd: resolves #11 - assert-enforce-least-most
- Loading branch information
1 parent
387ee25
commit 7e7321a
Showing
8 changed files
with
137 additions
and
9 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
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
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
27 changes: 27 additions & 0 deletions
27
projects/eslint-plugin-tdd/docs/rules/assert-enforce-least-most.md
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 @@ | ||
# assert-enforce-least-most | ||
Enforces using of `assert.isAtLeast` / `assert.isAtMost` instead of explicit '>=' / '<=' comparisons in `assert.isTrue` | ||
and `assert.isFalse` | ||
|
||
See also [assert-enforce-below-above](assert-enforce-below-above.md) | ||
|
||
## Rule Details | ||
|
||
The following patterns are considered problems: | ||
|
||
```js | ||
assert.isFalse(a < b) | ||
assert.isFalse(a > b) | ||
assert.isTrue(a <= b) | ||
assert.isTrue(a >= b) | ||
``` | ||
|
||
|
||
The following patterns are considered okay and do not cause warnings: | ||
|
||
```js | ||
assert.isFalse(a <= b) | ||
assert.isFalse(a >= b) | ||
assert.isTrue(a < b) | ||
assert.isTrue(a > b) | ||
``` | ||
|
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
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
36 changes: 36 additions & 0 deletions
36
projects/eslint-plugin-tdd/lib/rules/assert-enforce-least-most.js
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,36 @@ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Rule Definition | ||
// ------------------------------------------------------------------------------ | ||
|
||
var expression = require('../expression'); | ||
|
||
module.exports = function(context) { | ||
|
||
// -------------------------------------------------------------------------- | ||
// Public | ||
// -------------------------------------------------------------------------- | ||
|
||
return { | ||
CallExpression: function(node) { | ||
if (expression.isAssertIsFalse(node)) { | ||
if (expression.argumentIsBelowComparison(node.arguments, 0)) { | ||
context.report(node, 'instead of assert.isFalse(a < b) use assert.isAtLeast(a, b)'); | ||
} else if (expression.argumentIsAboveComparison(node.arguments, 0)) { | ||
context.report(node, 'instead of assert.isFalse(a > b) use assert.isAtMost(a, b)'); | ||
} | ||
} else if (expression.isAssertIsTrue(node)) { | ||
if (expression.argumentIsBelowOrEqualComparison(node.arguments, 0)) { | ||
context.report(node, 'instead of assert.isTrue(a <= b) use assert.isAtMost(a, b)'); | ||
} else if (expression.argumentIsAboveOrEqualComparison(node.arguments, 0)) { | ||
context.report(node, 'instead of using assert.isTrue(a >= b) use assert.isAtLeast(a, b)'); | ||
} | ||
} | ||
} | ||
}; | ||
|
||
}; | ||
|
||
module.exports.schema = []; | ||
|
56 changes: 56 additions & 0 deletions
56
projects/eslint-plugin-tdd/tests/lib/rules/assert-enforce-least-most.js
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,56 @@ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var app = require('../../../index.js'); | ||
var rule = app.rules['assert-enforce-least-most']; | ||
|
||
var RuleTester = require('eslint').RuleTester; | ||
|
||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester(); | ||
ruleTester.run('assert-enforce-below-above', rule, { | ||
valid: [ | ||
{ | ||
code: 'assert.isFalse(a <= b)' | ||
}, | ||
{ | ||
code: 'assert.isFalse(a >= b)' | ||
}, | ||
{ | ||
code: 'assert.isTrue(a < b)' | ||
}, | ||
{ | ||
code: 'assert.isTrue(a > b)' | ||
} | ||
], | ||
invalid: [ | ||
{ | ||
code: 'assert.isFalse(a < b)', | ||
errors: [{ | ||
message: 'instead of assert.isFalse(a < b) use assert.isAtLeast(a, b)' | ||
}] | ||
}, | ||
{ | ||
code: 'assert.isFalse(a > b)', | ||
errors: [{ | ||
message: 'instead of assert.isFalse(a > b) use assert.isAtMost(a, b)' | ||
}] | ||
}, | ||
{ | ||
code: 'assert.isTrue(a <= b)', | ||
errors: [{ | ||
message: 'instead of assert.isTrue(a <= b) use assert.isAtMost(a, b)' | ||
}] | ||
}, | ||
{ | ||
code: 'assert.isTrue(a >= b)', | ||
errors: [{ | ||
message: 'instead of using assert.isTrue(a >= b) use assert.isAtLeast(a, b)' | ||
}] | ||
} | ||
]}); | ||
|