How to use the fast-check.oneof function in fast-check

To help you get started, weโ€™ve selected a few fast-check examples, based on popular ways it is used in public projects.

github sindresorhus / query-string / test / properties.js View on Github external
import * as fastCheck from 'fast-check';
import test from 'ava';
import queryString from '..';

// Valid query parameters must follow:
// - key can be any unicode string (not empty)
// - value must be one of:
// --> any unicode string
// --> null
// --> array containing values defined above (at least two items)
const queryParamsArbitrary = fastCheck.dictionary(
	fastCheck.fullUnicodeString(1, 10),
	fastCheck.oneof(
		fastCheck.fullUnicodeString(),
		fastCheck.constant(null),
		fastCheck.array(fastCheck.oneof(fastCheck.fullUnicodeString(), fastCheck.constant(null)), 2, 10)
	)
);

const optionsArbitrary = fastCheck.record({
	arrayFormat: fastCheck.constantFrom('bracket', 'index', 'none'),
	strict: fastCheck.boolean(),
	encode: fastCheck.constant(true),
	sort: fastCheck.constant(false)
}, {withDeletedKeys: true});

test('should read correctly from stringified query params', t => {
	t.notThrows(() => {
		fastCheck.assert(
			fastCheck.property(
				queryParamsArbitrary,
				optionsArbitrary,
github sindresorhus / query-string / test / properties.js View on Github external
import deepEqual from 'deep-equal';
import * as fastCheck from 'fast-check';
import test from 'ava';
import queryString from '..';

// Valid query parameters must follow:
// - key can be any unicode string (not empty)
// - value must be one of:
// --> any unicode string
// --> null
// --> array containing values defined above (at least two items)
const queryParamsArbitrary = fastCheck.dictionary(
	fastCheck.fullUnicodeString(1, 10),
	fastCheck.oneof(
		fastCheck.fullUnicodeString(),
		fastCheck.constant(null),
		fastCheck.array(fastCheck.oneof(fastCheck.fullUnicodeString(), fastCheck.constant(null)), 2, 10)
	)
);

const optionsArbitrary = fastCheck.record({
	arrayFormat: fastCheck.constantFrom('bracket', 'index', 'none'),
	strict: fastCheck.boolean(),
	encode: fastCheck.constant(true),
	sort: fastCheck.constant(false)
}, {withDeletedKeys: true});

test('should read correctly from stringified query params', t => {
	t.notThrows(() => {
		fastCheck.assert(
github dubzzz / fuzz-rest-api / test / inferPayloadArbitrary.js View on Github external
  const wrapArb = orString === true ? arb => fc.oneof(arb, fc.string()) : arb => arb;
  switch (typeof payload) {
github giogonzo / fast-check-io-ts / src / index.ts View on Github external
return fc.dictionary(getArbitrary(type.domain), getArbitrary(type.codomain)) as any;
    case 'InterfaceType':
    case 'PartialType':
    case 'ExactType':
      return fc.record(record.map(getProps(type), getArbitrary as any) as any) as any;
    case 'TupleType':
      return (fc.tuple as any)(...type.types.map(getArbitrary));
    case 'UnionType':
      return fc.oneof(...type.types.map(getArbitrary)) as any;
    case 'IntersectionType':
      const isObjectIntersection = objectTypes.includes(type.types[0]._tag);
      return isObjectIntersection
        ? (fc.tuple as any)(...type.types.map(t => getArbitrary(t)))
            .map((values: Array<object>) => Object.assign({}, ...values))
            .filter(type.is)
        : fc.oneof(...type.types.map(t => getArbitrary(t))).filter(type.is);
    case 'RefinementType':
      return getArbitrary(type.type).filter(type.predicate) as any;
  }
}
github gcanti / fp-ts-laws / src / Either.ts View on Github external
export function getEither<L, A>(leftArb: fc.Arbitrary<L>, rightArb: fc.Arbitrary<A>): fc.Arbitrary<Either<L, A>> {
  return fc.oneof(getLeft(leftArb), getRight(rightArb))
}
github gcanti / fp-ts-laws / src / Validation.ts View on Github external
export function getValidation<L, A>(
  failureArb: fc.Arbitrary<L>,
  successArb: fc.Arbitrary<A>
): fc.Arbitrary<Validation<L, A>> {
  return fc.oneof(getFailure(failureArb), getSuccess(successArb))
}
github gcanti / fp-ts-laws / src / Option.ts View on Github external
export function getOption<A>(arb: fc.Arbitrary<A>): fc.Arbitrary<Option<A>> {
  return fc.oneof(getNone(), getSome(arb))
}
github dubzzz / fast-check / example / binary-trees / arbitraries / BinaryTreeArbitrary.ts View on Github external
export const binaryTree = (maxDepth: number): fc.Arbitrary<Tree<number>> => {
  const valueArbitrary = fc.integer();
  if (maxDepth <= 0) {
    return fc.record({
      value: valueArbitrary,
      left: fc.constant(null),
      right: fc.constant(null)
    });
  }
  const subTree = fc.oneof(fc.constant(null), binaryTree(maxDepth - 1));
  return fc.record({
    value: valueArbitrary,
    left: subTree,
    right: subTree
  });
};
github dubzzz / fast-check / example / binary-trees / arbitraries / BinarySearchTreeArbitrary.ts View on Github external
return valueArbitrary.chain(rootValue => {
    const leftArb = binarySearchTree(maxDepth - 1, minValue, rootValue);
    const rightArb = rootValue < maxValue ? binarySearchTree(maxDepth - 1, rootValue + 1, maxValue) : fc.constant(null);
    return fc.record({
      value: fc.constant(rootValue),
      left: fc.oneof(fc.constant(null), leftArb),
      right: fc.oneof(fc.constant(null), rightArb)
    });
  });
};