Skip to content

Commit

Permalink
Added mocha helper
Browse files Browse the repository at this point in the history
  • Loading branch information
glenjamin committed Jan 25, 2015
1 parent 3c3fe15 commit b835057
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 90 deletions.
25 changes: 19 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,32 @@ checkers.forAll(
).check(1000, {seed: 1422111938215});
```

## Documentation
# Usage with Mocha

Checkers comes with a helper function to make writing tests for mocha simpler.

```js
var checking = require('checkers/mocha');
var gen = checking.gen;
describe("Addition", function() {
checking("+1", [gen.int], function(i) {
return i + 1 > i;
}, 100, {seed: 1422111938215});
});
```

The count is optional, and defaults to 1000.
The extra options are also optional.

## Full Documentation

More coming soon!

## TODO

* Generator tests
* Generator docs
* Sugar for popular testing frameworks
```js
var checking = require('checkers/mocha'), gen = checking.gen;
checking("property", [gen/int], function ...)
```
* Sugar for other testing frameworks?
* Tutorial
* Better examples

Expand Down
31 changes: 31 additions & 0 deletions mocha.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Mocha interop, assumes "it" is a globally defined function.
*/
/*eslint-env mocha */

var checkers = require("./");

exports = module.exports = checking;
exports.gen = checkers.gen;

/**
* Generate a mocha example
* @param {string} desc The example description
* @param {array} args List of generators to pass to body
* @param {function} body Function to check, should return true or false
* @param {number} n The number of iterations to check (optional)
* @param {object} options Additional options for check (optional)
*/
function checking(desc, args, body, n, options) {
if (typeof n === 'undefined') {
n = 1000;
options = {};
}
if (typeof options === 'undefined' && typeof n !== 'number') {
options = n;
n = 1000;
}
it(desc, function() {
checkers.forAll(args, body).check(n, options);
});
}
144 changes: 60 additions & 84 deletions test/generators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,96 +4,72 @@ var _ = require('lodash');
var checkers = require('..');
var gen = checkers.gen;

var checking = require('../mocha');

describe("checkers.gen", function() {
it("can generate with .boolean", function() {
checkers.forAll([gen.boolean], _.isBoolean).check(10);
});
it("can generate with .int", function() {
checkers.forAll([gen.int], function(n) {
return _.isNumber(n) && wholeNumber(n);
}).check(100);
});
it("can generate with .nat", function() {
checkers.forAll([gen.nat], function(n) {
return _.isNumber(n) && wholeNumber(n) && n >= 0;
}).check(100);
});
it("can generate with .posInt", function() {
checkers.forAll([gen.posInt], function(n) {
return _.isNumber(n) && wholeNumber(n) && n > 0;
}).check(100);
});
it("can generate with .negInt", function() {
checkers.forAll([gen.negInt], function(n) {
return _.isNumber(n) && wholeNumber(n) && n < 0;
}).check(100);
});
it("can generate with .zeroOrNegInt", function() {
checkers.forAll([gen.zeroOrNegInt], function(n) {
return _.isNumber(n) && wholeNumber(n) && n <= 0;
}).check(100);
});
it("can generate with .char", function() {
checkers.forAll([gen.char], function(c) {
return _.isString(c) && c.length === 1 &&
c.charCodeAt(0) < 256 && c.charCodeAt(0) >= 0;
}).check(1000);
});
it("can generate with .charAscii", function() {
checkers.forAll([gen.charAscii], function(c) {
return _.isString(c) && c.length === 1 &&
c.charCodeAt(0) <= 127 && c.charCodeAt(0) >= 0;
}).check(100);
});
it("can generate with .charAlphanum", function() {
checkers.forAll([gen.charAlphanum], function(c) {
return _.isString(c) && /^\w$/.test(c);
}).check(100);
});
it("can generate with .charAlpha", function() {
checkers.forAll([gen.charAlpha], function(c) {
return _.isString(c) && /^\w$/.test(c) && !/^\d$/.test(c);
}).check(100);
});
it("can generate with .string", function() {
checkers.forAll([gen.string], function(c) {
return _.isString(c);
}).check(1000);
});
it("can generate with .stringAscii", function() {
checkers.forAll([gen.stringAscii], function(s) {
return _.isString(s) && _.every(s, function(c) {
return c.charCodeAt(0) <= 127 && c.charCodeAt(0) >= 0;
});
}).check(1000);
});
it("can generate with .stringAlphanum", function() {
checkers.forAll([gen.stringAlphanum], function(s) {
return _.isString(s) && _.every(s, function(c) {
return /^\w$/.test(c);
});
}).check(1000);
});
describe(".tuple", function() {
it("can generate pairs of ints", function() {
checkers.forAll([gen.tuple(gen.int, gen.int)], function(t) {
return _.isArray(t) && t.length == 2 &&
_.isNumber(t[0]) && _.isNumber(t[1]);
}).check(1000);
checking(".boolean", [gen.boolean], _.isBoolean, 10);
checking(".int", [gen.int], function(n) {
return _.isNumber(n) && wholeNumber(n);
}, 100);
checking(".nat", [gen.nat], function(n) {
return _.isNumber(n) && wholeNumber(n) && n >= 0;
}, 100);
checking(".posInt", [gen.posInt], function(n) {
return _.isNumber(n) && wholeNumber(n) && n > 0;
}, 100);
checking(".negInt", [gen.negInt], function(n) {
return _.isNumber(n) && wholeNumber(n) && n < 0;
}, 100);
checking(".zeroOrNegInt", [gen.zeroOrNegInt], function(n) {
return _.isNumber(n) && wholeNumber(n) && n <= 0;
}, 100);
checking(".char", [gen.char], function(c) {
return _.isString(c) && c.length === 1 &&
c.charCodeAt(0) < 256 && c.charCodeAt(0) >= 0;
}, 1000);
checking(".charAscii", [gen.charAscii], function(c) {
return _.isString(c) && c.length === 1 &&
c.charCodeAt(0) <= 127 && c.charCodeAt(0) >= 0;
}, 100);
checking(".charAlphanum", [gen.charAlphanum], function(c) {
return _.isString(c) && /^\w$/.test(c);
}, 100);
checking(".charAlpha", [gen.charAlpha], function(c) {
return _.isString(c) && /^\w$/.test(c) && !/^\d$/.test(c);
}, 100);
checking(".string", [gen.string], function(c) {
return _.isString(c);
}, 1000);
checking(".stringAscii", [gen.stringAscii], function(s) {
return _.isString(s) && _.every(s, function(c) {
return c.charCodeAt(0) <= 127 && c.charCodeAt(0) >= 0;
});
it("can generate pairs of int, char", function() {
checkers.forAll([gen.tuple(gen.int, gen.char)], function(t) {
}, 1000);
checking(".stringAlphanum", [gen.stringAlphanum], function(s) {
return _.isString(s) && _.every(s, function(c) {
return /^\w$/.test(c);
});
}, 1000);
describe(".tuple", function() {
checking("pairs of ints", [gen.tuple(gen.int, gen.int)], function(t) {
return _.isArray(t) && t.length == 2 &&
_.isNumber(t[0]) && _.isNumber(t[1]);
}, 1000);
checking("pairs of int, char",
[gen.tuple(gen.int, gen.char)],
function(t) {
return _.isArray(t) && t.length == 2 &&
_.isNumber(t[0]) && _.isString(t[1]);
}).check(1000);
});
it("can generate triples", function() {
checkers.forAll(
[gen.tuple(gen.int, gen.int, gen.int)],
},
1000
);
checking("can generate triples",
[gen.tuple(gen.int, gen.int, gen.int)],
function(t) {
return _.isArray(t) && t.length == 3;
}).check(1000);
});
},
1000
);
});
});

Expand Down

0 comments on commit b835057

Please sign in to comment.