Skip to content

Instantly share code, notes, and snippets.

@hughfdjackson
Last active December 14, 2015 07:09
Show Gist options
  • Select an option

  • Save hughfdjackson/5048890 to your computer and use it in GitHub Desktop.

Select an option

Save hughfdjackson/5048890 to your computer and use it in GitHub Desktop.
playing with possible js quickcheck API
// 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 }
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('') })
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