Skip to content

Commit

Permalink
add property test
Browse files Browse the repository at this point in the history
  • Loading branch information
gcanti committed Mar 19, 2019
1 parent 5572016 commit aa2e76f
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 7 deletions.
3 changes: 2 additions & 1 deletion jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,6 @@ module.exports = {
lines: 100,
statements: 100
}
}
},
modulePathIgnorePatterns: ['property-test']
}
35 changes: 34 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@
"doctoc": "^1.4.0",
"doctrine": "2.0.0",
"dtslint": "github:gcanti/dtslint",
"fast-check": "^1.12.1",
"glob": "^7.1.3",
"jest": "^23.6.0",
"mocha": "^5.2.0",
Expand Down
26 changes: 21 additions & 5 deletions test/Apply.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import * as assert from 'assert'
import { applyFirst, applySecond, liftA2, liftA3, liftA4, sequenceT, sequenceS } from '../src/Apply'
import { either, left, right } from '../src/Either'
import { none, option, some } from '../src/Option'
import { none, option, some, isSome, isNone } from '../src/Option'
import * as fc from 'fast-check'
import { getSome } from './property-test/Option'
import { nonEmptyArray } from './property-test/NonEmptyArray2v'
import { catOptions, getSetoid } from '../src/Array'
import { fromEquals } from '../src/Setoid'

describe('Apply', () => {
const r1 = right<string, number>(1)
Expand Down Expand Up @@ -64,13 +69,24 @@ describe('Apply', () => {

it('sequenceT', () => {
const sequenceTOption = sequenceT(option)
const sequenceTEither = sequenceT(either)
assert.deepStrictEqual(sequenceTOption(some(1)), some([1]))
assert.deepStrictEqual(sequenceTOption(some(1), some('2')), some([1, '2']))
assert.deepStrictEqual(sequenceTOption(some(1), some('2'), none), none)
assert.deepStrictEqual(sequenceTEither(right(1)), right([1]))
assert.deepStrictEqual(sequenceTEither(right(1), right('2')), right([1, '2']))
assert.deepStrictEqual(sequenceTEither(right(1), right('2'), left('foo')), left('foo'))

const S = getSetoid(fromEquals((x, y) => x === y))
const somes = getSome(fc.oneof<string | number>(fc.string(), fc.integer()))
const allSomesInput = nonEmptyArray(somes)
const maybeNoneInput = nonEmptyArray(fc.oneof(fc.constant(none), somes))
const input = fc.oneof(allSomesInput, maybeNoneInput)
fc.assert(
fc.property(input, options => {
const x = sequenceTOption(...(options as any))
return (
(options.every(isSome) && x.isSome() && S.equals(x.value as any, catOptions(options))) ||
(options.some(isNone) && x.isNone())
)
})
)
})

it('sequenceS', () => {
Expand Down
9 changes: 9 additions & 0 deletions test/property-test/NonEmptyArray2v.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import * as fc from 'fast-check'
import { NonEmptyArray } from '../../src/NonEmptyArray2v'

/**
* Returns an `Arbitrary` that yelds a non empty array
*/
export function nonEmptyArray<A>(arb: fc.Arbitrary<A>): fc.Arbitrary<NonEmptyArray<A>> {
return fc.array(arb, 1, 100) as any
}
26 changes: 26 additions & 0 deletions test/property-test/Option.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import * as fc from 'fast-check'
import { Option, some, none } from '../../src/Option'

/**
* Returns an `Arbitrary` that yelds only `some`s
* @since 0.0.2
*/
export function getSome<A>(arb: fc.Arbitrary<A>): fc.Arbitrary<Option<A>> {
return arb.map(some)
}

/**
* Returns an `Arbitrary` that yelds only `none`s
* @since 0.0.2
*/
export function getNone<A>(): fc.Arbitrary<Option<A>> {
return fc.constant(none)
}

/**
* Returns an `Arbitrary` that yelds both `some`s and `none`s
* @since 0.0.2
*/
export function getOption<A>(arb: fc.Arbitrary<A>): fc.Arbitrary<Option<A>> {
return fc.oneof(getNone(), getSome(arb))
}

0 comments on commit aa2e76f

Please sign in to comment.