runtypes-generate
runtypes-generate copied to clipboard
Transform runtypes type to jsverify arbitrary for generate sample of data
Runtypes-generate
Runtypes-generate convert runtypes type to jsverify arbitrary.
Table of Contents
- Background
- Install
- Usage
- API
- Contribute
- License
Background
Property-based testing is very awesome approach for analyze and verification program. But this approach requires the writing of generators for all datatypes in our program. This process is very time-consuming, error-prone and not DRY.
Example:
import { Number, Literal, Array, Tuple, Record } from 'runtypes'
const AsteroidType = Record({
type: Literal('asteroid'),
location: Tuple(Number, Number, Number),
mass: Number,
})
const AsteroidArbitrary = jsc.record({
type: jsc.constant('asteroid'),
location: jsc.tuple(jsc.number, jsc.number, jsc.number),
mass: jsc.number
})
But with runtypes-generate we can get AsteroidArbitrary from AsteroidType:
import { makeJsverifyArbitrary } from 'runtypes-generate'
const AsteroidType = Record({
type: Literal('asteroid'),
location: Tuple(Number, Number, Number),
mass: Number,
})
const AsteroidArbitrary = makeJsverifyArbitrary(AsteroidType)
Install
npm install --save runtypes-generate
Usage
API
makeJsverifyArbitrary(type: Reflect): jsc.Arbitrary<any>- convertruntypestojsverifyarbitraryaddTypeToRegistry(tag: string, (x:Reflect) => jsc.Arbitrary<any>): void- add new generator forConstrainttype withtaginargsattributeaddTypeToIntersectRegistry(tags: string[], generator: (x: Reflect) => jsc.Arbitrary<any>): void)- add new generator forIntersector customConstrainttypes. TODO examplegenerateAndCheck(rt: Reflect, opts?: jsc.Options): () => void- runjsc.assertfor propertyrt.check(generatedData)for allgeneratedDataobtained frommakeJsverifyArbitrary(rt). Uses for verification custom generators for customConstrainttype. See example in tests.
Contribute
PRs accepted.
If you had questions just make issue or ask them in my telegram
Small note: If editing the Readme, please conform to the standard-readme specification.
License
MIT © typeetfunc