Last active
December 14, 2015 07:09
-
-
Save hughfdjackson/5048890 to your computer and use it in GitHub Desktop.
playing with possible js quickcheck API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // generators make random content of a type | |
| // -- utils | |
| var randNth = function(coll) { | |
| return coll[Math.floor(Math.random() * coll.length)] | |
| } | |
| var randChar = function(){ | |
| return randNth('qwertyuiopasdfghjklzxcvbnm') | |
| } | |
| var repeat = function(n, fn){ | |
| var arr = [] | |
| for ( var i = 0; i < n; i += 1 ) arr.push(fn()) | |
| return arr | |
| } | |
| // -- generators | |
| var str = function(){ | |
| var len = Math.floor(Math.random() * 100) | |
| return repeat(len, randChar).join('') | |
| } | |
| var int = function(){ | |
| return Math.floor(Math.random() * 10000000) | |
| } | |
| module.exports = { int: int, str: str } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var types = require('generators') | |
| var qc = require('quickcheck') | |
| // under test function | |
| var reverse = function(a){ return a.slice().reverse() } | |
| // quickcheck generates n elements in arrays, and throws an error of the agregated failures | |
| // after the tests are run, if there are any | |
| qc([types.str], function(arr){ reverse(reverse(arr)).join('') === arr.join('') }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| var types = require('generators') | |
| var qc = require('quickcheck') | |
| // under test function | |
| var reverse = function(a){ return a.slice().reverse() } | |
| suite('reverse') | |
| // qc.test is the same as qc, except that it's a shorthand for qc.bind(null, /* etc */) | |
| test('reverse reverse is the same as no reverse', qc.test([types.str], | |
| function(arr){ reverse(reverse(arr)).join('') === arr.join('') | |
| })) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment