Skip to content

Commit

Permalink
Make maxIterations and maxErrors configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
sunesimonsen committed Mar 18, 2016
1 parent 4bd19e6 commit 76938ce
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 9 deletions.
20 changes: 19 additions & 1 deletion documentation/assertions/function/to-be-valid-for-all.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,22 @@ expect(function (text) {

This will run 100 tests with random strings of length 0-200 and succeed.

You can specify the max number of iterations that the test should run and the
number of errors it should collect before stopping.

The algorithm searches for the smallest error output, so the more errors you
allow it to collect the better the output will be.

```js
expect(function (text) {
expect(unescape(escape(text)), 'to equal', text);
}, 'to be valid for all', {
generators: [strings],
maxIterations: 1000,
maxErrors: 30
});
```

I found to following code for
[Run-length encoding](https://en.wikipedia.org/wiki/Run-length_encoding) on the
[internet](http://rosettacode.org/wiki/Run-length_encoding#JavaScript), let's see
Expand All @@ -41,13 +57,15 @@ function rleDecode(encoded) {
return output;
}

var g = require('chance-generators')(13);
var strings = g.string({ length: g.natural({ max: 200 }) });
expect(function (text) {
expect(rleDecode(rleEncode(text)), 'to equal', text);
}, 'to be valid for all', strings);
```

```output
Ran 34 iterations and found 20 errors
Ran 86 iterations and found 20 errors
counterexample:
Generated input: ''
Expand Down
18 changes: 12 additions & 6 deletions lib/unexpected-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@
return {
name: 'unexpected-check',
installInto: function (expect) {
// TODO can these be configurable?
var maxIterations = 100;
var maxErrors = 20;

function convertToUnexpectedError(e) {
if (e && e.isUnexpected) {
return e;
Expand All @@ -50,8 +46,10 @@
}
}

expect.addAssertion('<function> to be valid for all <function+>', function (expect, subject) {
var generators = Array.prototype.slice.call(arguments, 2);
expect.addAssertion('<function> to be valid for all <object>', function (expect, subject, options) {
var generators = options.generators;
var maxIterations = options.maxIterations || 100;
var maxErrors = options.maxErrors || 20;

function createTask() {
var args = generators.map(function (g) {
Expand Down Expand Up @@ -125,6 +123,14 @@
});
}
});

expect.addAssertion('<function> to be valid for all <function+>', function (expect, subject) {
expect.errorMode = 'bubble';

expect(subject, 'to be valid for all', {
generators: Array.prototype.slice.call(arguments, 2)
});
});
}
};
});
8 changes: 6 additions & 2 deletions test/unexpected-check.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ describe('unexpected-check', function () {
expect(sorted, 'to have length', arr.length)
.and('first item to be less than or equal to all', arr)
.and('last item to be greater than or equal to all', arr);
}, 'to be valid for all', arrays);
}, 'to be valid for all', {
generators: [arrays],
maxIterations: 20,
maxErrors: 20
});
}, 'to throw',
'Ran 37 iterations and found 20 errors\n' +
'Ran 20 iterations and found 12 errors\n' +
'counterexample:\n' +
'\n' +
' Generated input: [ -15, -20 ]\n' +
Expand Down

0 comments on commit 76938ce

Please sign in to comment.