-
-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an arbitrary able to generate records
Fixes #40
- Loading branch information
Showing
3 changed files
with
46 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}) | ||
)); | ||
}); | ||
}); |