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.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

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.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(leftArb: fc.Arbitrary, rightArb: fc.Arbitrary<a>): fc.Arbitrary&gt; {
  return fc.oneof(getLeft(leftArb), getRight(rightArb))
}
</a>
github gcanti / fp-ts-laws / src / Validation.ts View on Github external
export function getValidation(
  failureArb: fc.Arbitrary,
  successArb: fc.Arbitrary<a>
): fc.Arbitrary&gt; {
  return fc.oneof(getFailure(failureArb), getSuccess(successArb))
}
</a>
github gcanti / fp-ts-laws / src / Option.ts View on Github external
export function getOption<a>(arb: fc.Arbitrary</a><a>): fc.Arbitrary&gt; {
  return fc.oneof(getNone(), getSome(arb))
}
</a>
github dubzzz / fast-check / example / binary-trees / arbitraries / BinaryTreeArbitrary.ts View on Github external
export const binaryTree = (maxDepth: number): fc.Arbitrary&gt; =&gt; {
  const valueArbitrary = fc.integer();
  if (maxDepth &lt;= 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 =&gt; {
    const leftArb = binarySearchTree(maxDepth - 1, minValue, rootValue);
    const rightArb = rootValue &lt; 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)
    });
  });
};