UNPKG

fast-check

Version:

Property based testing framework for JavaScript (like QuickCheck)

45 lines (44 loc) 1.47 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Property = void 0; const PreconditionFailure_1 = require("../precondition/PreconditionFailure"); const IRawProperty_1 = require("./IRawProperty"); class Property { constructor(arb, predicate) { this.arb = arb; this.predicate = predicate; this.beforeEachHook = Property.dummyHook; this.afterEachHook = Property.dummyHook; this.isAsync = () => false; } generate(mrng, runId) { return runId != null ? this.arb.withBias(IRawProperty_1.runIdToFrequency(runId)).generate(mrng) : this.arb.generate(mrng); } run(v) { this.beforeEachHook(); try { const output = this.predicate(v); return output == null || output === true ? null : 'Property failed by returning false'; } catch (err) { if (PreconditionFailure_1.PreconditionFailure.isFailure(err)) return err; if (err instanceof Error && err.stack) return `${err}\n\nStack trace: ${err.stack}`; return `${err}`; } finally { this.afterEachHook(); } } beforeEach(hookFunction) { this.beforeEachHook = hookFunction; return this; } afterEach(hookFunction) { this.afterEachHook = hookFunction; return this; } } exports.Property = Property; Property.dummyHook = () => { };