0.3.0 • Published 7 years ago

tcomb-generate v0.3.0

Weekly downloads
2
License
MIT
Repository
github
Last release
7 years ago

tcomb-generate

standard-readme compliant npm version

tcomb-generate convert tcomb 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 * as t from 'tcomb'

const AsteroidType = t.interface({
    type: t.enums.of('asteroid'),
    location: t.tuple([t.Number, t.Number, t.Number]),
    mass: t.Number,
})

const AsteroidArbitrary = jsc.record({
    type: jsc.constant('asteroid'),
    location: jsc.tuple(jsc.number, jsc.number, jsc.number),
    mass: jsc.number
})

But with tcomb-generate we can get AsteroidArbitrary from AsteroidType:

import { makeJsverifyArbitrary } from 'tcomb-generate'
const AsteroidType = t.interface({
    type: t.enums.of('asteroid'),
    location: t.tuple([t.Number, t.Number, t.Number]),
    mass: t.Number,
})
const AsteroidArbitrary = makeJsverifyArbitrary(AsteroidType)

Install

npm install --save tcomb-generate

Usage

API

  • makeJsverifyArbitrary(type: Type<any>): jsc.Arbitrary<any> - convert tcomb type to jsverify arbitrary
  • addTypeToRegistry(name: string, (x: Type<any>) => jsc.Arbitrary<any>): void - add new generator for type with name
  • addTypeToIntersectRegistry(names: string[], generator: (x: Type<any>) => jsc.Arbitrary<any>): void) - add new generator for custom t.intersection types. TODO example
  • generateAndCheck(rt: Type<any>, opts?: jsc.Options): () => void - run jsc.assert for property type.is(generatedData) for all generatedData obtained from makeJsverifyArbitrary(type). Uses for verification custom generators for custom named 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