Skip to content

Commit

Permalink
eslint-plugin-tdd: resolves #11 - assert-enforce-least-most
Browse files Browse the repository at this point in the history
  • Loading branch information
RachelSatoyama committed Jan 18, 2016
1 parent 387ee25 commit 7e7321a
Show file tree
Hide file tree
Showing 8 changed files with 137 additions and 9 deletions.
1 change: 1 addition & 0 deletions projects/eslint-plugin-tdd/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
This change log adheres to standards from [Keep a CHANGELOG](http://keepachangelog.com).

## [Unreleased]
assert-enforce-least-most introduced

## [0.3.0] - 2015-12-29
### Added
Expand Down
6 changes: 4 additions & 2 deletions projects/eslint-plugin-tdd/README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

eslint-plugin-tdd
===================

Expand All @@ -20,7 +21,7 @@ $ npm install eslint-plugin-tdd
```

The project is still very young (literally days old),
and there are...only four rules under the hood currently.
and there are only five rules under the hood currently.
This does not mean you can not use this project - after all this project is dedicated to testing the quality of test code. Keeping in mind that, it is production ready.

# Configuration
Expand Down Expand Up @@ -49,7 +50,8 @@ Finally, enable all of the rules that you would like to use.
# List of supported rules

* [assert-enforce-below-above](docs/rules/assert-enforce-below-above.md): Enforces using of `assert.isBelow` / `assert.isAbove` instead of explicit '>' / '<' comparisons in `assert.isTrue` and `assert.isFalse`
* [assert-enforce-boolean](docs/rules/assert-enforce-boolean.md): Enforces using of `assert.isTrue` and `assert.isFalse` wherever and whenever it is possible.
* [assert-enforce-least-most](docs/rules/assert-enforce-least-most.md): Enforces using of `assert.isAtLeast` / `assert.isAtMost` instead of explicit '>=' / '<=' comparisons in `assert.isTrue` and `assert.isFalse`
* [assert-enforce-boolean](docs/rules/assert-enforce-boolean.md): Enforces using of `assert.isTrue` and `assert.isFalse` wherever and whenever it is possible.
* [assert-no-boolean-negation](docs/rules/assert-no-boolean-negation.md): Enforces using of `assert.isTrue` and `assert.isFalse` without boolean negation.
* [assert-enforce-implicit-comparison](docs/rules/assert-enforce-implicit-comparison.md): Enforces using of `assert.equal` / `assert.strictEqual` instead of explicit comparisons in assert

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
Enforces using of `assert.isBelow` / `assert.isAbove` instead of explicit '>' / '<' comparisons in `assert.isTrue`
and `assert.isFalse`

See also [assert-enforce-least-most](assert-enforce-least-most.md)

## Rule Details

The following patterns are considered problems:
Expand Down Expand Up @@ -34,6 +36,8 @@ one of two reasons actually:
* `a` is equal to `b`

The same about `assert.isFalse(a > b)`, `assert.isTrue(a <= b)`, `assert.isTrue(a <= b)`
Whether we should try to introduce stricter enforcing rules for such cases
is still an open issue so far.


There is a separate rule, [assert-enforce-least-most](assert-enforce-least-most.md)
you can use to cover this cases. The reason for having a separate rule for this is that `assert.isAtLeast`
and `assert.isAtMost` were introduced to chai.js [later](https://github.com/chaijs/chai/issues/580)
than `assert.isAbove` and `assert.isBelow`.
27 changes: 27 additions & 0 deletions projects/eslint-plugin-tdd/docs/rules/assert-enforce-least-most.md
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)
```

4 changes: 2 additions & 2 deletions projects/eslint-plugin-tdd/grunt/tasks/mocha-istanbul.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ module.exports = function(grunt) {
options.grep = argv.grep;
} else {
options.check = {
statements: 95.0,
statements: 96.0,
branches: 89.0,
functions: 99.9,
lines: 95.0
lines: 96.0
};
}

Expand Down
6 changes: 4 additions & 2 deletions projects/eslint-plugin-tdd/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ module.exports = {
'assert-enforce-boolean': require('./lib/rules/assert-enforce-boolean'),
'assert-no-boolean-negation': require('./lib/rules/assert-no-boolean-negation'),
'assert-enforce-implicit-comparison': require('./lib/rules/assert-enforce-implicit-comparison'),
'assert-enforce-below-above': require('./lib/rules/assert-enforce-below-above')
'assert-enforce-below-above': require('./lib/rules/assert-enforce-below-above'),
'assert-enforce-least-most': require('./lib/rules/assert-enforce-least-most')
},
rulesConfig: {
'assert-enforce-boolean': 0,
'assert-no-boolean-negation': 0,
'assert-enforce-implicit-comparison': 0,
'assert-enforce-below-above': 0
'assert-enforce-below-above': 0,
'assert-enforce-least-most': 0
}
};
36 changes: 36 additions & 0 deletions projects/eslint-plugin-tdd/lib/rules/assert-enforce-least-most.js
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 = [];

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)'
}]
}
]});

0 comments on commit 7e7321a

Please sign in to comment.