-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: make property tests easier to read
This change uses the dedicated fast-check library for Jest, which reduces the amount of code required. The main benefit is that there's not so much indentation, so it's easier to read. Refs https://github.com/dubzzz/fast-check/tree/main/packages/jest
- Loading branch information
1 parent
ff86907
commit 2ea9bea
Showing
3 changed files
with
119 additions
and
118 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,137 +1,85 @@ | ||
import { describe, expect, test } from '@jest/globals' | ||
import { test } from '@fast-check/jest' | ||
import { describe, expect } from '@jest/globals' | ||
import * as O from 'fp-ts/Option' | ||
import * as _ from '../src' | ||
import * as fc from './fc' | ||
|
||
describe('doi-ts', () => { | ||
describe('destructors', () => { | ||
test('toUrl', () => { | ||
fc.assert( | ||
fc.property(fc.doi(), doi => { | ||
expect(_.toUrl(doi)).toStrictEqual(new URL(`https://doi.org/${doi}`)) | ||
}), | ||
) | ||
test.prop([fc.doi()])('toUrl', doi => { | ||
expect(_.toUrl(doi)).toStrictEqual(new URL(`https://doi.org/${doi}`)) | ||
}) | ||
}) | ||
|
||
describe('instances', () => { | ||
describe('Eq', () => { | ||
test('with the same DOI', () => { | ||
fc.assert( | ||
fc.property(fc.doi(), doi => { | ||
expect(_.Eq.equals(doi, doi)).toBe(true) | ||
}), | ||
) | ||
test.prop([fc.doi()])('with the same DOI', doi => { | ||
expect(_.Eq.equals(doi, doi)).toBe(true) | ||
}) | ||
|
||
test('with the same DOI in different cases', () => { | ||
fc.assert( | ||
fc.property(fc.doi(), doi => { | ||
expect(_.Eq.equals(doi, doi.toLowerCase() as _.Doi)).toBe(true) | ||
}), | ||
) | ||
test.prop([fc.doi()])('with the same DOI in different cases', doi => { | ||
expect(_.Eq.equals(doi, doi.toLowerCase() as _.Doi)).toBe(true) | ||
}) | ||
|
||
test('with different DOIs', () => { | ||
fc.assert( | ||
fc.property(fc.doi(), fc.doi(), (doi1, doi2) => { | ||
expect(_.Eq.equals(doi1, doi2)).toBe(false) | ||
}), | ||
) | ||
test.prop([fc.doi(), fc.doi()])('with different DOIs', (doi1, doi2) => { | ||
expect(_.Eq.equals(doi1, doi2)).toBe(false) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('refinements', () => { | ||
describe('isDoi', () => { | ||
test('with a DOI', () => { | ||
fc.assert( | ||
fc.property(fc.doi(), doi => { | ||
expect(_.isDoi(doi)).toBe(true) | ||
}), | ||
) | ||
test.prop([fc.doi()])('with a DOI', doi => { | ||
expect(_.isDoi(doi)).toBe(true) | ||
}) | ||
|
||
test('with a non-DOI', () => { | ||
fc.assert( | ||
fc.property(fc.anything(), value => { | ||
expect(_.isDoi(value)).toBe(false) | ||
}), | ||
) | ||
test.prop([fc.anything()])('with a non-DOI', value => { | ||
expect(_.isDoi(value)).toBe(false) | ||
}) | ||
}) | ||
|
||
describe('hasRegistrant', () => { | ||
test('when the registrant matches', () => { | ||
fc.assert( | ||
fc.property( | ||
fc | ||
.array(fc.registrant(), { minLength: 1 }) | ||
.chain(registrants => fc.tuple(fc.constant(registrants), fc.doi(fc.constantFrom(...registrants)))), | ||
([registrants, doi]) => { | ||
expect(_.hasRegistrant(...registrants)(doi)).toBe(true) | ||
}, | ||
), | ||
) | ||
test.prop([ | ||
fc | ||
.array(fc.registrant(), { minLength: 1 }) | ||
.chain(registrants => fc.tuple(fc.constant(registrants), fc.doi(fc.constantFrom(...registrants)))), | ||
])('when the registrant matches', ([registrants, doi]) => { | ||
expect(_.hasRegistrant(...registrants)(doi)).toBe(true) | ||
}) | ||
|
||
test('when the registrant does not match', () => { | ||
fc.assert( | ||
fc.property(fc.array(fc.registrant()), fc.doi(), (registrants, doi) => { | ||
expect(_.hasRegistrant(...registrants)(doi)).toBe(false) | ||
}), | ||
) | ||
test.prop([fc.array(fc.registrant()), fc.doi()])('when the registrant does not match', (registrants, doi) => { | ||
expect(_.hasRegistrant(...registrants)(doi)).toBe(false) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('utils', () => { | ||
describe('parse', () => { | ||
test('when it contains a DOI', () => { | ||
fc.assert( | ||
fc.property( | ||
fc | ||
.tuple( | ||
fc.doi(), | ||
fc.stringOf(fc.constant(' ')), | ||
fc.constantFrom( | ||
'doi:', | ||
'https://doi.org/', | ||
'http://doi.org/', | ||
'https://dx.doi.org/', | ||
'http://dx.doi.org/', | ||
), | ||
fc.stringOf(fc.constant(' ')), | ||
) | ||
.map(([doi, whitespaceBefore, prefix, whitespaceAfter]) => [ | ||
doi, | ||
`${whitespaceBefore}${prefix}${doi}${whitespaceAfter}`, | ||
]), | ||
([doi, input]) => { | ||
expect(_.parse(input)).toStrictEqual(O.some(doi)) | ||
}, | ||
), | ||
) | ||
test.prop([ | ||
fc | ||
.tuple( | ||
fc.doi(), | ||
fc.stringOf(fc.constant(' ')), | ||
fc.constantFrom('doi:', 'https://doi.org/', 'http://doi.org/', 'https://dx.doi.org/', 'http://dx.doi.org/'), | ||
fc.stringOf(fc.constant(' ')), | ||
) | ||
.map(([doi, whitespaceBefore, prefix, whitespaceAfter]) => [ | ||
doi, | ||
`${whitespaceBefore}${prefix}${doi}${whitespaceAfter}`, | ||
]), | ||
])('when it contains a DOI', ([doi, input]) => { | ||
expect(_.parse(input)).toStrictEqual(O.some(doi)) | ||
}) | ||
|
||
test('when it does not contain a DOI', () => { | ||
fc.assert( | ||
fc.property(fc.unicodeString(), input => { | ||
expect(_.parse(input)).toStrictEqual(O.none) | ||
}), | ||
) | ||
test.prop([fc.unicodeString()])('when it does not contain a DOI', input => { | ||
expect(_.parse(input)).toStrictEqual(O.none) | ||
}) | ||
}) | ||
|
||
test('getRegistrant', () => { | ||
fc.assert( | ||
fc.property( | ||
fc.registrant().chain(registrant => fc.tuple(fc.constant(registrant), fc.doi(fc.constant(registrant)))), | ||
([registrant, doi]) => { | ||
expect(_.getRegistrant(doi)).toBe(registrant) | ||
}, | ||
), | ||
) | ||
test.prop([ | ||
fc.registrant().chain(registrant => fc.tuple(fc.constant(registrant), fc.doi(fc.constant(registrant)))), | ||
])('getRegistrant', ([registrant, doi]) => { | ||
expect(_.getRegistrant(doi)).toBe(registrant) | ||
}) | ||
}) | ||
}) |