-
-
Notifications
You must be signed in to change notification settings - Fork 182
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the ability to select one of the constants using fc.constantFrom
- Loading branch information
Showing
3 changed files
with
54 additions
and
7 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,18 +1,34 @@ | ||
import Arbitrary from './definition/Arbitrary' | ||
import Shrinkable from './definition/Shrinkable' | ||
import MutableRandomGenerator from '../../random/generator/MutableRandomGenerator' | ||
import { nat } from './IntegerArbitrary' | ||
import { stream, Stream } from '../../stream/Stream' | ||
|
||
class ConstantArbitrary<T> extends Arbitrary<T> { | ||
constructor(readonly value: T) { | ||
readonly idArb: Arbitrary<number>; | ||
constructor(readonly values: T[]) { | ||
super(); | ||
this.idArb = nat(values.length -1); | ||
} | ||
generate(mrng: MutableRandomGenerator): Shrinkable<T> { | ||
return new Shrinkable(this.value); | ||
if (this.values.length === 1) | ||
return new Shrinkable(this.values[0]); | ||
|
||
const id = this.idArb.generate(mrng).value; | ||
if (id === 0) | ||
return new Shrinkable(this.values[0]); | ||
|
||
function* g(v: T) { yield new Shrinkable(v); } | ||
return new Shrinkable(this.values[id], () => stream(g(this.values[0]))); | ||
} | ||
} | ||
|
||
function constant<T>(value: T): Arbitrary<T> { | ||
return new ConstantArbitrary<T>(value); | ||
return new ConstantArbitrary<T>([value]); | ||
} | ||
|
||
export { constant }; | ||
function constantFrom<T>(v0: T, ...values: T[]): Arbitrary<T> { | ||
return new ConstantArbitrary<T>([v0, ...values]); | ||
} | ||
|
||
export { constant, constantFrom }; |
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