Skip to content

Transform runtypes type to jsverify arbitrary for generate sample of data

Notifications You must be signed in to change notification settings

typeetfunc/runtypes-generate

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

22 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Runtypes-generate

standard-readme compliant npm version

Runtypes-generate convert runtypes type to jsverify arbitrary.

Table of Contents

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> - convert runtypes to jsverify arbitrary
  • addTypeToRegistry(tag: string, (x:Reflect) => jsc.Arbitrary<any>): void - add new generator for Constraint type with tag in args attribute
  • addTypeToIntersectRegistry(tags: string[], generator: (x: Reflect) => jsc.Arbitrary<any>): void) - add new generator for Intersect or custom Constraint types. TODO example
  • generateAndCheck(rt: Reflect, opts?: jsc.Options): () => void - run jsc.assert for property rt.check(generatedData) for all generatedData obtained from makeJsverifyArbitrary(rt). Uses for verification custom generators for custom Constraint type. 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

About

Transform runtypes type to jsverify arbitrary for generate sample of data

Topics

Resources

Stars

Watchers

Forks

Packages

No packages published