Skip to content

Instantly share code, notes, and snippets.

View garbles's full-sized avatar
🇨🇦

Gabe garbles

🇨🇦
View GitHub Profile
@garbles
garbles / gen.js
Last active March 10, 2017 21:59
const personGen = gen.object({
name: gen.string,
age: gen.int,
orders: gen.array(gen.object({
trackingId: gen.int,
name: gen.string
})),
// ...
});
{ result: false,
'failing-size': 2,
'num-tests': 3,
fail: [ 2, -1 ],
shrunk:
{ 'total-nodes-visited': 2,
depth: 1,
result: false,
smallest: [ 0, -1 ] } }
var testcheck = require('testcheck');
var gen = testcheck.gen;
testcheck.check(testcheck.property(
[gen.int, gen.int], // generate two random integers
function (a, b) {
// check that the sum of the integers is always greater than any one number
return a + b >= a && a + b >= b;
}
));
import {sampleOne} from 'babel-plugin-transform-flow-to-gen/api';
type Person = {
name: string,
age: number,
setName: (name: string) => Person
};
const person = sampleOne(Person());
import type {Person} from './types';
export default function setName(person: Person, name: string): Person {
return {
...person,
name
};
}
import setName from './setName';
import {types} from 'babel-plugin-transform-flow-to-gen/api';
import type {Person} from './types';
require('jasmine-check').install();
describe('setName', () => {
it.check('sets a new name', [Person(), types.string()], (person, newName) => {
const next = setName(person, newName);
@garbles
garbles / $Gen.js
Last active January 30, 2018 02:03
import uuid from 'uuid-v4';
import {sample} from 'babel-plugin-transform-flow-to-gen/api';
import type {$Gen} from 'babel-plugin-transform-flow-to-gen/Gen';
// FYI,
// type $Gen<T, _U> = T;
type User = {
id: $Gen<string, uuid>, // $Gen only supports references to function calls
name: string
import {sampleOne} from 'babel-plugin-transform-flow-to-gen/api';
type Person = {
name: string,
age: number
}
function setName(person: Person, name: string): Person {
return {
...person,
import {sampleOne, types} from 'babel-plugin-transform-flow-to-gen/api';
const personGen =
types.plainObject({
name: types.string(),
age: types.number()
});
console.log(sampleOne(personGen));
// { "name": "rJJ9", "age": -1 }
import {sample, sampleOne, types} from 'babel-plugin-transform-flow-to-gen/api';
import type {Animal, Dog} from './types';
const dogGen = Animal(types.literal('dog'));
const anySpeciesGen = Animal(types.string());
const dog = sampleOne(dogGen);
console.log(dog);
// { "species": "dog", "name": "ahKL9p" }