Skip to content

Commit

Permalink
Add an arbitrary able to generate records
Browse files Browse the repository at this point in the history
Fixes #40
  • Loading branch information
dubzzz committed Feb 19, 2018
1 parent 547b070 commit 850158b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
17 changes: 17 additions & 0 deletions src/check/arbitrary/RecordArbitrary.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Arbitrary from './definition/Arbitrary'
import MutableRandomGenerator from '../../random/generator/MutableRandomGenerator'

import { generic_tuple } from './TupleArbitrary'

function record<T>(recordModel: {[Key:string]: Arbitrary<T>}): Arbitrary<{[Key:string]: T}> {
const keys = Object.keys(recordModel);
const arbs: Arbitrary<T>[] = keys.map(v => recordModel[v]);
return generic_tuple(arbs).map((gs: T[]) => {
const obj: {[Key:string]: T} = {};
for (let idx = 0 ; idx != keys.length ; ++idx)
obj[keys[idx]] = gs[idx];
return obj;
});
}

export { record };
3 changes: 2 additions & 1 deletion src/fast-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { lorem } from './check/arbitrary/LoremArbitrary'
import { anything, object, jsonObject, unicodeJsonObject, json, unicodeJson, ObjectConstraints } from './check/arbitrary/ObjectArbitrary'
import { oneof } from './check/arbitrary/OneOfArbitrary'
import { option } from './check/arbitrary/OptionArbitrary'
import { record } from './check/arbitrary/RecordArbitrary'
import { string, asciiString, string16bits, unicodeString, fullUnicodeString, hexaString, base64String } from './check/arbitrary/StringArbitrary'
import { set } from './check/arbitrary/SetArbitrary'
import { tuple, generic_tuple } from './check/arbitrary/TupleArbitrary'
Expand All @@ -43,7 +44,7 @@ export {
integer, nat, // integer types
char, ascii, char16bits, unicode, fullUnicode, hexa, base64, // single character
string, asciiString, string16bits, unicodeString, fullUnicodeString, hexaString, base64String, lorem, // strings
constant, constantFrom, option, oneof, frequency, array, set, tuple, generic_tuple, // combination of others
constant, constantFrom, option, oneof, frequency, array, set, tuple, generic_tuple, record, // combination of others
anything, object, json, unicodeJson, // complex combinations
// extend the framework
Arbitrary, Shrinkable,
Expand Down
27 changes: 27 additions & 0 deletions test/unit/check/arbitrary/RecordArbitrary.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import * as assert from 'power-assert';
import * as fc from '../../../../lib/fast-check';

import { record } from '../../../../src/check/arbitrary/RecordArbitrary';
import MersenneTwister from '../../../../src/random/generator/MersenneTwister';
import MutableRandomGenerator from '../../../../src/random/generator/MutableRandomGenerator';

import * as stubArb from '../../stubs/arbitraries';
import * as stubRng from '../../stubs/generators';

describe("RecordArbitrary", () => {
describe('record', () => {
it('Should produce a record having the right keys', () => fc.assert(
fc.property(fc.array(fc.string()), fc.integer(), (keys, seed) => {
const mrng = stubRng.mutable.fastincrease(seed);
const expectedRecord = {};
const recordModel = {};
for (const k of keys) {
expectedRecord[k] = `_${k}_`;
recordModel[k] = stubArb.single(`_${k}_`);
}
const g = record(recordModel).generate(mrng).value;
assert.deepStrictEqual(g, expectedRecord);
})
));
});
});

0 comments on commit 850158b

Please sign in to comment.