/
Property.generic.ts
27 lines (26 loc) · 1 KB
/
Property.generic.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import Random from '../../random/generator/Random';
import Arbitrary from '../arbitrary/definition/Arbitrary';
import Shrinkable from '../arbitrary/definition/Shrinkable';
import { tuple } from '../arbitrary/TupleArbitrary';
import IProperty from './IProperty';
/**
* Property, see {@link IProperty}
*
* Prefer using {@link property} instead
*/
export class Property<Ts> implements IProperty<Ts> {
constructor(readonly arb: Arbitrary<Ts>, readonly predicate: (t: Ts) => boolean | void) {}
isAsync = () => false;
generate(mrng: Random, runId?: number): Shrinkable<Ts> {
return runId != null ? this.arb.withBias(runId + 2).generate(mrng) : this.arb.generate(mrng);
}
run(v: Ts): string | null {
try {
const output = this.predicate(v);
return output == null || output === true ? null : 'Property failed by returning false';
} catch (err) {
if (err instanceof Error && err.stack) return `${err}\n\nStack trace: ${err.stack}`;
return `${err}`;
}
}
}