11import * as assert from 'power-assert' ;
22import * as fc from '../../../../lib/fast-check' ;
33
4+ import { constant } from '../../../../src/check/arbitrary/ConstantArbitrary' ;
45import { record } from '../../../../src/check/arbitrary/RecordArbitrary' ;
56import MersenneTwister from '../../../../src/random/generator/MersenneTwister' ;
67import MutableRandomGenerator from '../../../../src/random/generator/MutableRandomGenerator' ;
@@ -11,7 +12,7 @@ import * as stubRng from '../../stubs/generators';
1112describe ( "RecordArbitrary" , ( ) => {
1213 describe ( 'record' , ( ) => {
1314 it ( 'Should produce a record having the right keys' , ( ) => fc . assert (
14- fc . property ( fc . array ( fc . string ( ) ) , fc . integer ( ) , ( keys , seed ) => {
15+ fc . property ( fc . set ( fc . string ( ) ) , fc . integer ( ) , ( keys , seed ) => {
1516 const mrng = stubRng . mutable . fastincrease ( seed ) ;
1617 const expectedRecord = { } ;
1718 const recordModel = { } ;
@@ -23,5 +24,37 @@ describe("RecordArbitrary", () => {
2324 assert . deepStrictEqual ( g , expectedRecord ) ;
2425 } )
2526 ) ) ;
27+ it ( 'Should produce a record with missing keys' , ( ) => fc . assert (
28+ fc . property ( fc . set ( fc . string ( ) , 1 , 10 ) , fc . nat ( ) , fc . integer ( ) , ( keys , missingIdx , seed ) => {
29+ const mrng = new MutableRandomGenerator ( MersenneTwister . from ( seed ) ) ;
30+ const recordModel = { } ;
31+ for ( const k of keys )
32+ recordModel [ k ] = constant ( `_${ k } _` ) ;
33+
34+ const arb = record ( recordModel , { with_deleted_keys : true } ) ;
35+ for ( let idx = 0 ; idx != 1000 ; ++ idx ) {
36+ const g = arb . generate ( mrng ) . value ;
37+ if ( ! g . hasOwnProperty ( keys [ missingIdx % keys . length ] ) )
38+ return true ;
39+ }
40+ return false ;
41+ } )
42+ ) ) ;
43+ it ( 'Should produce a record with present keys' , ( ) => fc . assert (
44+ fc . property ( fc . set ( fc . string ( ) , 1 , 10 ) , fc . nat ( ) , fc . integer ( ) , ( keys , missingIdx , seed ) => {
45+ const mrng = new MutableRandomGenerator ( MersenneTwister . from ( seed ) ) ;
46+ const recordModel = { } ;
47+ for ( const k of keys )
48+ recordModel [ k ] = constant ( `_${ k } _` ) ;
49+
50+ const arb = record ( recordModel , { with_deleted_keys : true } ) ;
51+ for ( let idx = 0 ; idx != 1000 ; ++ idx ) {
52+ const g = arb . generate ( mrng ) . value ;
53+ if ( g [ keys [ missingIdx % keys . length ] ] === `_${ keys [ missingIdx % keys . length ] } _` )
54+ return true ;
55+ }
56+ return false ;
57+ } )
58+ ) ) ;
2659 } ) ;
2760} ) ;
0 commit comments