How to use the fast-check.constantFrom 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 CJex / regulex / test / KitSpec.ts View on Github external
// Impractical, took several seconds or even minutes to complete these tests
    if (false) {
      let unicodeCats = (function() {
        let a: string[] = [];
        let U = Object.assign({}, UnicodeProperty.canonical) as {[k: string]: Set};
        delete U.NonBinary_Property;
        for (let k in U) {
          for (let cat of U[k]) {
            a.push(k + '/' + cat);
          }
        }
        return a;
      })();

      const genUnicodeCat = C.constantFrom(...unicodeCats);

      it('Unicode module', () => {
        for (let path of unicodeCats) {
          let codePoints = require(DEFAULT_UNICODE_PKG + '/' + path + '/code-points.js');
          let charset = K.Charset.fromCodePoints(codePoints);
          let [cls, cat] = path.split('/');
          assert(charset.equals((Unicode as any)[cls][cat]));
        }
      });

      testProp('Unicode category fromPattern toPattern equal', genUnicodeCat, cat => {
        let codePoints = require(DEFAULT_UNICODE_PKG + '/' + cat + '/code-points.js');
        let charset = K.Charset.fromCodePoints(codePoints);
        assert(K.Charset.fromPattern(charset.toPattern()).equals(charset));
      });
github rzeigler / waveguide / test / tools.spec.ts View on Github external
runToPromiseExit(io2)
        .then((result2) => {
          return expect(result1).to.deep.equal(result2);
        })
        .then(constTrue)
    );
}

export function exitType(io1: Wave, tag: Exit["_tag"]): Promise {
  return runToPromiseExit(io1)
    .then((result) => expect(result._tag).to.equal(tag))
    .then(() => undefined);
}

export const arbVariant: Arbitrary =
  fc.constantFrom("succeed", "complete", "suspend", "async");

export function arbIO(arb: Arbitrary<a>): Arbitrary&gt; {
  return arbVariant
    .chain((ioStep) =&gt; {
      if (ioStep === "succeed") {
        return arb.map((a) =&gt; pure(a) as Wave); // force downcast
      } else if (ioStep === "complete") {
        return arb.map((a) =&gt; completed(done(a)));
      } else if (ioStep === "suspend") {
        // We now need to do recursion... wooo
        return arbIO(arb)
          .map((nestedIO) =&gt; suspended(() =&gt; nestedIO));
      } else { // async with random delay
        return fc.tuple(fc.nat(50), arb)
          .map(
            ([delay, val]) =&gt;</a>
github sindresorhus / query-string / test / properties.js View on Github external
// - 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,
				(object, options) => deepEqual(queryString.parse(queryString.stringify(object, options), options), object)
			)
		);
	});
});
github eemeli / yaml / tests / properties.js View on Github external
test('parse stringified object', () => {
    const key = fc.fullUnicodeString()
    const values = [
      key,
      fc.lorem(1000, false), // words
      fc.lorem(100, true), // sentences
      fc.boolean(),
      fc.integer(),
      fc.double(),
      fc.constantFrom(null, Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY)
    ]
    const yamlArbitrary = fc.anything({ key: key, values: values })
    const optionsArbitrary = fc.record(
      {
        keepBlobsInJSON: fc.boolean(),
        keepCstNodes: fc.boolean(),
        keepNodeTypes: fc.boolean(),
        mapAsMap: fc.constant(false),
        merge: fc.boolean(),
        schema: fc.constantFrom('core', 'yaml-1.1') // ignore 'failsafe', 'json'
      },
      { withDeletedKeys: true }
    )

    fc.assert(
      fc.property(yamlArbitrary, optionsArbitrary, (obj, opts) => {
github facebook / jest / packages / expect / src / __tests__ / __arbitraries__ / sharedSettings.ts View on Github external
/**
 * Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
 *
 * This source code is licensed under the MIT license found in the
 * LICENSE file in the root directory of this source tree.
 *
 */

import fc from 'fast-check';

// settings for anything arbitrary
export const anythingSettings = {
  key: fc.oneof(fc.string(), fc.constantFrom('k1', 'k2', 'k3')),
  maxDepth: 2, // Limit object depth (default: 2)
  maxKeys: 5, // Limit number of keys per object (default: 5)
  withBoxedValues: true,
  // Issue #7975 have to be fixed before enabling the generation of Map
  withMap: false,
  // Issue #7975 have to be fixed before enabling the generation of Set
  withSet: false,
};

// assertion settings
export const assertSettings = {}; // eg.: {numRuns: 10000}
github eemeli / yaml / tests / properties.js View on Github external
fc.lorem(1000, false), // words
      fc.lorem(100, true), // sentences
      fc.boolean(),
      fc.integer(),
      fc.double(),
      fc.constantFrom(null, Number.NEGATIVE_INFINITY, Number.POSITIVE_INFINITY)
    ]
    const yamlArbitrary = fc.anything({ key: key, values: values })
    const optionsArbitrary = fc.record(
      {
        keepBlobsInJSON: fc.boolean(),
        keepCstNodes: fc.boolean(),
        keepNodeTypes: fc.boolean(),
        mapAsMap: fc.constant(false),
        merge: fc.boolean(),
        schema: fc.constantFrom('core', 'yaml-1.1') // ignore 'failsafe', 'json'
      },
      { withDeletedKeys: true }
    )

    fc.assert(
      fc.property(yamlArbitrary, optionsArbitrary, (obj, opts) => {
        expect(YAML.parse(YAML.stringify(obj, opts), opts)).toStrictEqual(obj)
      })
    )
  })
})
github aiden / rpc_ts / src / protocol / grpc_web / __tests__ / client_server.it.ts View on Github external
const lowerCamelCase = () =>
  fc
    .tuple(
      fc.constantFrom(...'abcdefghijklmnopqrstuvwxyz'.split('')),
      fc.stringOf(
        fc.constantFrom(
          ...'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.split(
            '',
          ),
        ),
      ),
    )
    .map(([firstLetter, remaining]) => `${firstLetter}${remaining}`);
github aiden / rpc_ts / src / protocol / grpc_web / __tests__ / client_server.it.ts View on Github external
const lowerCamelCase = () =>
  fc
    .tuple(
      fc.constantFrom(...'abcdefghijklmnopqrstuvwxyz'.split('')),
      fc.stringOf(
        fc.constantFrom(
          ...'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789'.split(
            '',
          ),
        ),
      ),
    )
    .map(([firstLetter, remaining]) => `${firstLetter}${remaining}`);
github CJex / regulex / test / utils.ts View on Github external
export function genInCharset(ch: K.Charset): C.Arbitrary {
  return C.constantFrom(...ch.ranges).chain(range =&gt;
    C.integer(K.CharRange.begin(range), K.CharRange.end(range)).map(String.fromCodePoint)
  );
}