Skip to content

Commit

Permalink
feat(data): good data-driven test case, close #2
Browse files Browse the repository at this point in the history
  • Loading branch information
bahmutov committed Feb 23, 2017
1 parent b5cbe2d commit 19a264e
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
69 changes: 69 additions & 0 deletions __snapshots__/prime-snapshot.test.js.snap-shot
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,72 @@ exports['works for primes (all) 1'] = [
}
]

exports['can be applied 1'] = {
"is4prime": false
}

exports['applies args 1'] = {
"name": "isPrime",
"behavior": [
{
"given": 1,
"expect": false
},
{
"given": 2,
"expect": true
},
{
"given": 3,
"expect": true
},
{
"given": 4,
"expect": false
},
{
"given": 5,
"expect": true
},
{
"given": 6,
"expect": false
},
{
"given": 7,
"expect": true
},
{
"given": 8,
"expect": false
}
]
}

exports['binary function add 1'] = {
"name": "add",
"behavior": [
{
"given": [
1,
2
],
"expect": 3
},
{
"given": [
2,
-2
],
"expect": 0
},
{
"given": [
5,
10
],
"expect": 15
}
]
}

28 changes: 28 additions & 0 deletions prime-snapshot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,19 @@ function snaps(fn, inputs) {
snapshot(output)
}

// multiple arguments
function snapsMultiple(fn, ...inputs) {
const name = fn.name
const behavior = inputs.map(given => {
const args = Array.isArray(given) ? given : [given]
return {
given,
expect: fn.apply(null, args)
}
})
snapshot({name, behavior})
}

it('works for primes', () => {
snap(isPrime, [2, 3, 5, 7])
})
Expand All @@ -37,3 +50,18 @@ it('works for non-primes', () => {
it('works for primes (all)', () => {
snaps(isPrime, [2, 3, 5, 7])
})

it('can be applied', () => {
const result = {is4prime: isPrime.apply(null, [4])}
snapshot(result)
})

it('applies args', () => {
snapsMultiple(isPrime, 1, 2, 3, 4, 5, 6, 7, 8)
})

const add = (a, b) => a + b

it('binary function add', () => {
snapsMultiple(add, [1, 2], [2, -2], [5, 10])
})

0 comments on commit 19a264e

Please sign in to comment.