Skip to content

Instantly share code, notes, and snippets.

@cskeppstedt
Last active August 29, 2015 14:22
Show Gist options
  • Save cskeppstedt/9ddf07a45c4a04f2f876 to your computer and use it in GitHub Desktop.
Save cskeppstedt/9ddf07a45c4a04f2f876 to your computer and use it in GitHub Desktop.
Example of using property based testing with jsverify
# Assume this is a spec file to run in Mocha. This is an artificial example
# that tries to show the general idea of incorporating property based testing.
jsc = require 'jsverify'
mySorter = require '../src/mySorter'
describe 'My sorting algorithm', ->
######################################################
## First, unit tests to drive implementation design
######################################################
it 'should put apa before bepa', ->
# given
input = ['bepa', 'apa']
expected = ['apa', 'bepa']
output = mySorter(input)
# then
expect(output).to.eql expected
it 'should put bepa before cepa', ->
# given
input = ['ccccc', 'bepa']
expected = ['bepa', 'ccccc']
output = mySorter(input)
# then
expect(output).to.eql expected
######################################################
## Then, properties that should hold in general
######################################################
jsc.property 'should sort by length', 'array string', (input) ->
sorted = mySorter(input)
for i in [1..sorted.length]
if sorted[i-1].length > sorted[i].length
return false
return true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment