Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
53 additions
and
53 deletions.
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 |
---|---|---|
@@ -1,53 +1,53 @@ | ||
import { Arbitrary } from './definition/Arbitrary'; | ||
import { constant } from './ConstantArbitrary'; | ||
import { oneof } from './OneOfArbitrary'; | ||
|
||
export function scenario<TModel, TAction>( | ||
initialModel: TModel, | ||
modelUpdater: (model: TModel, action: TAction) => TModel, | ||
eligibleActions: (model: TModel) => Arbitrary<TAction>[], | ||
scenarioLength?: number | ||
): Arbitrary<TAction[]> { | ||
const finalScenarioLength = typeof scenarioLength === 'number' ? scenarioLength : 10; | ||
|
||
const extendScenario = (current: { | ||
model: TModel; | ||
scenario: TAction[]; | ||
}): Arbitrary<{ model: TModel; scenario: TAction[] }> => { | ||
// Extract eligible actions | ||
const actions = scenario.length < finalScenarioLength ? eligibleActions(current.model) : []; | ||
if (actions.length === 0) return constant(current); | ||
return oneof(...eligibleActions(current.model)).chain(a => | ||
extendScenario({ model: modelUpdater(current.model, a), scenario: [...current.scenario, a] }) | ||
); | ||
}; | ||
|
||
return constant({ model: initialModel, scenario: [] as TAction[] }) | ||
.chain(extendScenario) | ||
.map(({ scenario }) => scenario); | ||
} | ||
|
||
/*const myScenarioArb = scenarioArb( | ||
[], | ||
(m, a) => { | ||
// simplified reducer | ||
switch (a.type) { | ||
case 'add': | ||
return [...m, a.value]; | ||
case 'removeAll': | ||
return m.filter(v => v !== a.value); | ||
case 'clear': | ||
return []; | ||
} | ||
}, | ||
m => { | ||
const eligible = [ | ||
fc.record({ type: fc.constant('add'), value: fc.hexa() }), | ||
fc.record({ type: fc.constant('clear') }) | ||
]; | ||
if (m.length > 0) eligible.push(fc.record({ type: fc.constant('removeAll'), value: fc.constantFrom(...m) })); | ||
return eligible; | ||
} | ||
); | ||
fc.sample(myScenarioArb, 1);*/ | ||
import { Arbitrary } from './definition/Arbitrary'; | ||
import { constant } from './ConstantArbitrary'; | ||
import { oneof } from './OneOfArbitrary'; | ||
|
||
export function scenario<TModel, TAction>( | ||
initialModel: TModel, | ||
modelUpdater: (model: TModel, action: TAction) => TModel, | ||
eligibleActions: (model: TModel) => Arbitrary<TAction>[], | ||
scenarioLength?: number | ||
): Arbitrary<TAction[]> { | ||
const finalScenarioLength = typeof scenarioLength === 'number' ? scenarioLength : 10; | ||
|
||
const extendScenario = (current: { | ||
model: TModel; | ||
scenario: TAction[]; | ||
}): Arbitrary<{ model: TModel; scenario: TAction[] }> => { | ||
// Extract eligible actions | ||
const actions = scenario.length < finalScenarioLength ? eligibleActions(current.model) : []; | ||
if (actions.length === 0) return constant(current); | ||
return oneof(...eligibleActions(current.model)).chain((a) => | ||
extendScenario({ model: modelUpdater(current.model, a), scenario: [...current.scenario, a] }) | ||
); | ||
}; | ||
|
||
return constant({ model: initialModel, scenario: [] as TAction[] }) | ||
.chain(extendScenario) | ||
.map(({ scenario }) => scenario); | ||
} | ||
|
||
/*const myScenarioArb = scenarioArb( | ||
[], | ||
(m, a) => { | ||
// simplified reducer | ||
switch (a.type) { | ||
case 'add': | ||
return [...m, a.value]; | ||
case 'removeAll': | ||
return m.filter(v => v !== a.value); | ||
case 'clear': | ||
return []; | ||
} | ||
}, | ||
m => { | ||
const eligible = [ | ||
fc.record({ type: fc.constant('add'), value: fc.hexa() }), | ||
fc.record({ type: fc.constant('clear') }) | ||
]; | ||
if (m.length > 0) eligible.push(fc.record({ type: fc.constant('removeAll'), value: fc.constantFrom(...m) })); | ||
return eligible; | ||
} | ||
); | ||
fc.sample(myScenarioArb, 1);*/ |