BAM Weblog

bilby.js - QuickCheck

Brian McKenna — 2012-09-16

I’ve added a QuickCheck implementation to bilby.js. The existing JavaScript implementations:

  • Make it hard to create generators
  • Require a specific test runner

bilby’s implementation fixes both problems.

You can register generators using the multimethod environment:

λ = require('bilby')
    .method('arb', strictEquals(Boolean), function(a, s) {
        return Math.random() < 0.5;
    })
    .method('shrink', isBoolean, function() {
        return b ? [False] : [];
    });

Now, λ.forAll can be called with what property and argument types should be tested:

console.log(λ.forAll(
    function(a) {
        return (a && a) == a;
    },
    [Boolean]
).fold(
    "OK",
    function(inputs, tries) {
        return "Failed after " + tries + " tries: " + inputs.toString();
    }
));

The reporting API is really simple so it’s easy to mix this with the normal test runners, like I have with nodeunit.

Please enable JavaScript to view the comments powered by Disqus.