Skip to content

Commit

Permalink
Simplify integration with test frameworks
Browse files Browse the repository at this point in the history
  • Loading branch information
dubzzz committed Jun 27, 2018
1 parent e24e24f commit b92f56f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 1 deletion.
3 changes: 3 additions & 0 deletions prebuild/prebuild.ts
@@ -1,5 +1,6 @@
import { writeFileSync } from 'fs';
import { generateProperty, generatePropertySpec } from './property';
import { generateTest } from './test';
import { generateTuple, generateTupleSpec } from './tuple';

const NUM_PARAMETERS = 22;
Expand All @@ -10,5 +11,7 @@ writeFileSync('./test/unit/check/property/Property.generated.spec.ts', generateP
writeFileSync('./src/check/property/AsyncProperty.generated.ts', generateProperty(NUM_PARAMETERS, true));
writeFileSync('./test/unit/check/property/AsyncProperty.generated.spec.ts', generatePropertySpec(NUM_PARAMETERS, true));

writeFileSync('./src/check/integration/Test.generated.ts', generateTest(NUM_PARAMETERS));

writeFileSync('./src/check/arbitrary/TupleArbitrary.generated.ts', generateTuple(NUM_PARAMETERS));
writeFileSync('./test/unit/check/arbitrary/TupleArbitrary.generated.spec.ts', generateTupleSpec(NUM_PARAMETERS));
2 changes: 1 addition & 1 deletion prebuild/property.ts
@@ -1,6 +1,6 @@
import { arbCommas, commas, iota, joiner, txCommas } from './helpers';

const predicateFor = (num: number, isAsync: boolean): string =>
export const predicateFor = (num: number, isAsync: boolean): string =>
isAsync
? `(${commas(num, v => `t${v}:T${v}`)}) => Promise<boolean|void>`
: `(${commas(num, v => `t${v}:T${v}`)}) => (boolean|void)`;
Expand Down
71 changes: 71 additions & 0 deletions prebuild/test.ts
@@ -0,0 +1,71 @@
import { commas, iota, joiner, txCommas } from './helpers';
import { predicateFor } from './property';

const testPredicateFor = (num: number) => `(${predicateFor(num, true)}) | (${predicateFor(num, false)})`;

const signatureFor = (num: number): string => {
return `
/**
* Instanciate a new
* @param definition Test definition function of the test framework - eg.: it in Mocha
* @param label Label of the property
* ${joiner(num, v => `@param arb${v} Generate the parameter at position #${v + 1} of predicate`, '\n* ')}
* ${joiner(num, v => `@param predicate.t${v} Value generated by arb${v}`, '\n* ')}
* @param predicate Assess the success of the property. Would be considered falsy if its throws or if its output evaluates to false
*/
function test<TestDefinitionOutput,${txCommas(num)}>(
definition: TestDefinition<TestDefinitionOutput>,
label: string,
${commas(num, v => `arb${v}:Arbitrary<T${v}>`)},
predicate: ${testPredicateFor(num)}
): void;`;
};
const finalSignatureFor = (num: number): string => {
return `
function test<TestDefinitionOutput,${txCommas(num)}>(
definition: TestDefinition<TestDefinitionOutput>,
label: string,
${commas(num, v => `arb${v}?: Arbitrary<T${v}> | (${testPredicateFor(v)})`)},
arb${num}?: ${testPredicateFor(num)}
) {`;
};
const ifFor = (num: number): string => {
return `
if (arb${num}) {
const seed = Date.now();
const p = arb${num} as ${testPredicateFor(num)};
const pp = async (${commas(num, v => `v${v}:T${v}`)}) => await p(${commas(num, v => `v${v}`)});
definition(label + " (seed: " + seed + ")", async () => {
await assert(
asyncProperty(${commas(num, v => `arb${v} as Arbitrary<T${v}>`)}, pp),
{seed: seed});
});
return;
}`;
};

const generateTest = (num: number): string => {
const blocks = [
// imports
`import Arbitrary from '../arbitrary/definition/Arbitrary';`,
`import { asyncProperty } from '../property/AsyncProperty';`,
`import { assert } from '../runner/Runner';`,
`import { TestDefinition } from './TestDefinition';`,
// declare all signatures
...iota(num).map(id => signatureFor(id + 1)),
// start declare function
finalSignatureFor(num + 1),
// cascade ifs
...iota(num)
.reverse()
.map(id => ifFor(id + 1)),
// end declare function
`}`,
// export
`export { test };`
];

return blocks.join('\n');
};

export { generateTest };
1 change: 1 addition & 0 deletions src/check/integration/TestDefinition.ts
@@ -0,0 +1 @@
export type TestDefinition<TestOutput> = (expectation: string, callback: () => PromiseLike<any>) => TestOutput;
2 changes: 2 additions & 0 deletions src/fast-check-default.ts
@@ -1,5 +1,6 @@
import 'core-js';

import { test } from './check/integration/Test.generated';
import { asyncProperty } from './check/property/AsyncProperty';
import { property } from './check/property/Property';
import { Parameters } from './check/runner/configuration/Parameters';
Expand Down Expand Up @@ -61,6 +62,7 @@ export {
// check the property
check,
assert,
test,
// property definition
property,
asyncProperty,
Expand Down

0 comments on commit b92f56f

Please sign in to comment.