Last active
August 29, 2015 14:22
-
-
Save cskeppstedt/9ddf07a45c4a04f2f876 to your computer and use it in GitHub Desktop.
Example of using property based testing with jsverify
This file contains 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
# 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