quick_check v0.7.0
quick_check.js
quick_check.js is an implementation of QuickCheck in JavaScript. (Actually the implementation is written in CoffeeScript, but that doesn't matter much).
It works by integrating with existing test libraries. For example Jasmine expectations are written like this:
// implementation
function odd(n) {
return n % 2 === 1;
}
// tests
it('#odd returns true for odd numbers', function() {
expect(function(i) {
return odd(2 * i + 1);
}).forAll(qc.int)
});Notice the forAll(qc.int). This indicates that our function (called property)
should return true for all integers passed to it. This code in fact contains a bug
which quick_check.js will helpfully find for you:
PhantomJS 1.9.7 (Mac OS X) #odd returns true for odd numbers FAILED
Falsified after 3 attempts. Counter-example: -4This means that quick_check.js generated 3 random integers and one of them failed the test (in this case -4). (Why? Because modulus operator in JavaScript is botched).
QuickCheck will stop after 100 generated test cases and assume that your code works.
For more information read the docs, check out my talk or read the annotated source code or the introductory blog post.
Installing
Karma + Jasmine combo should be supported easily. Simply npm install quick_check --save-dev,
then add 'node_modules/quick_check/dist/quick-check.js' to your files config.
Generators
quick_check.js comes with batteries included. There are plenty of generators included plus it is very easy to write your own. I recommend checking out the docs, but here is a quick rundown:
Basic generators
qc.boolqc.byteqc.constructor(cons)qc.fromFunction(fn)
Number generators
Almost all number generators have a large variant for generating larger numbers,
as the standard generators tend not to generate numbers bigger than 10,000. The
generators prefixed with u generate only positive numbers.
qc.urealandqc.ureal.largeqc.realandqc.real.largeqc.uintandqc.uint.largeqc.intandqc.int.largeqc.int.between(min, max)qc.naturalandqc.natural.largeqc.range([gen])qc.range.inclusive([gen])qc.dice(diceDSL)
String generators
qc.charqc.stringqc.string.asciiqc.string.concatqc.string.matching
Array generators
qc.arrayOf(generator[, options])qc.arrayqc.array.subsetOf(array[, options])
Object generators
qc.objectqc.objectLike(template)qc.objectOf(valGen[, keyGen])
Function generators
qc.functionqc.procedure
Misc generators
qc.dateqc.anyqc.any.simpleqc.any.datatypeqc.colorqc.location
Generator combinators
These combinator functions are meant to create new generators out of other generators.
qc.oneOfqc.oneOfByPriorityqc.except
Maintained By
Jakub Hampl - http://gampleman.eu
License
The MIT License (MIT)
Copyright (c) 2014 RightScale
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.