UNPKG

@plugjs/expect5

Version:

Unit Testing for the PlugJS Build System ========================================

346 lines (345 loc) 11.8 kB
// expectation/matchers.ts import { Expectations } from "./expectations.mjs"; import { matcherMarker } from "./types.mjs"; var Matcher = class _Matcher { _matchers; not; constructor() { const matchers = []; this.not = new NegativeMatchers(this, matchers); this._matchers = matchers; } expect(value, parent) { let expectations = new Expectations(value, void 0, parent); for (const matcher of this._matchers) { expectations = matcher(expectations); } return expectations.value; } _push(matcher) { const matchers = new _Matcher(); matchers._matchers.push(...this._matchers, matcher); return matchers; } static { this.prototype[matcherMarker] = matcherMarker; } toBeA(type, assertionOrMatcher) { return this._push((e) => e.toBeA(type, assertionOrMatcher)); } /* ------------------------------------------------------------------------ */ /** * Expects the value to be a `Date`, a `string` parseable into a `Date`, or a * `number` indicating the milliseconds from the epoch, _strictly after_ * the specified date. * * Negation: {@link Matcher.toBeBeforeOrEqual `toBeBeforeOrEqual(...)`} */ toBeAfter(value, deltaMs) { return this._push((e) => e.toBeAfter(value, deltaMs)); } /* ------------------------------------------------------------------------ */ /** * Expects the value to be a `Date`, a `string` parseable into a `Date`, or a * `number` indicating the milliseconds from the epoch, _after or equal_ * the specified date. * * Negation: {@link Matcher.toBeBefore `toBeBefore(...)`} */ toBeAfterOrEqual(value, deltaMs) { return this._push((e) => e.toBeAfterOrEqual(value, deltaMs)); } /* ------------------------------------------------------------------------ */ /** * Expects the value to be a `Date`, a `string` parseable into a `Date`, or a * `number` indicating the milliseconds from the epoch, _strictly before_ * the specified date. * * Negation: {@link Matcher.toBeAfterOrEqual `toBeAfterOrEqual(...)`} */ toBeBefore(value, deltaMs) { return this._push((e) => e.toBeBefore(value, deltaMs)); } /* ------------------------------------------------------------------------ */ /** * Expects the value to be a `Date`, a `string` parseable into a `Date`, or a * `number` indicating the milliseconds from the epoch, _before or equal_ * the specified date. * * Negation: {@link Matcher.toBeAfter `toBeAfter(...)`} */ toBeBeforeOrEqual(value, deltaMs) { return this._push((e) => e.toBeBeforeOrEqual(value, deltaMs)); } /** * Expects the value to be a `number` or `bigint` within a given +/- _delta_ * range of the specified expected value. * * Negation: {@link NegativeMatchers.toBeCloseTo `not.toBeCloseTo(...)`} */ toBeCloseTo(value, delta) { return this._push((e) => e.toBeCloseTo(value, delta)); } /* ------------------------------------------------------------------------ */ /** * Expects the value to be neither `null` nor `undefined`. * * Negation: {@link NegativeMatchers.toBeDefined `not.toBeDefined()`} */ toBeDefined() { return this._push((e) => e.toBeDefined()); } toBeError(constructorOrMessage, maybeMessage) { const [constructor, message] = typeof constructorOrMessage === "function" ? [constructorOrMessage, maybeMessage] : [Error, constructorOrMessage]; return this._push((e) => e.toBeError(constructor, message)); } /* ------------------------------------------------------------------------ */ /** Expects the value strictly equal to `false`. */ toBeFalse() { return this._push((e) => e.toBeFalse()); } /* ------------------------------------------------------------------------ */ /** * Expects the value to be _falsy_ (zero, empty string, `false`, ...). * * Negation: {@link Matcher.toBeTruthy `toBeTruthy()`} */ toBeFalsy() { return this._push((e) => e.toBeFalsy()); } toBeGreaterThan(value) { return this._push((e) => e.toBeGreaterThan(value)); } toBeGreaterThanOrEqual(value) { return this._push((e) => e.toBeGreaterThanOrEqual(value)); } toBeInstanceOf(constructor, assertionOrMatcher) { return this._push((e) => e.toBeInstanceOf(constructor, assertionOrMatcher)); } toBeLessThan(value) { return this._push((e) => e.toBeLessThan(value)); } toBeLessThanOrEqual(value) { return this._push((e) => e.toBeLessThanOrEqual(value)); } /* ------------------------------------------------------------------------ */ /** * Expects the value to be `NaN`. * * Negation: {@link NegativeMatchers.toBeNaN `not.toBeNaN()`} */ toBeNaN() { return this._push((e) => e.toBeNaN()); } /* ------------------------------------------------------------------------ */ /** Expects the value to strictly equal `null`. */ toBeNull() { return this._push((e) => e.toBeNull()); } /* ------------------------------------------------------------------------ */ /** Expects the value to strictly equal `true`. */ toBeTrue() { return this._push((e) => e.toBeTrue()); } /* ------------------------------------------------------------------------ */ /** * Expects the value to be _falsy_ (non-zero, non-empty string, ...). * * Negation: {@link Matcher.toBeFalsy `toBeFalsy()`} */ toBeTruthy() { return this._push((e) => e.toBeTruthy()); } /* ------------------------------------------------------------------------ */ /** Expects the value to strictly equal `undefined`. */ toBeUndefined() { return this._push((e) => e.toBeUndefined()); } /** * Expects the value to be a `number` or `bigint` within the specified range * where minimum and maximum values are inclusive. * * Negation: {@link NegativeMatchers.toBeWithinRange `not.toBeWithinRange(...)`} */ toBeWithinRange(min, max) { return this._push((e) => e.toBeWithinRange(min, max)); } /* ------------------------------------------------------------------------ */ /** * Expects the value to be _deep equal to_ the specified expected one. * * Negation: {@link NegativeMatchers.toEqual `not.toEqual(...)`} */ toEqual(expected) { return this._push((e) => e.toEqual(expected)); } /* ------------------------------------------------------------------------ */ /** * Expects the value to have a `number` _property_ `length` with the specified * expected value. * * Negation: {@link NegativeMatchers.toHaveLength `not.toHaveLength(...)`} */ toHaveLength(length) { return this._push((e) => e.toHaveLength(length)); } toHaveProperty(property, assertionOrMatcher) { return this._push((e) => e.toHaveProperty(property, assertionOrMatcher)); } /* ------------------------------------------------------------------------ */ /** * Expects the value to have a `number` _property_ `size` with the specified * expected value. * * Negation: {@link NegativeMatchers.toHaveSize `not.toHaveSize(...)`} */ toHaveSize(size) { return this._push((e) => e.toHaveSize(size)); } toInclude(contents) { return this._push((e) => e.toInclude(contents)); } /* ------------------------------------------------------------------------ */ /** * Expects the value to be a `string` _matching_ the specified sub-`string` * or {@link RegExp}. * * Negation: {@link NegativeMatchers.toMatch `not.toMatch(...)`} */ toMatch(matcher) { return this._push((e) => e.toMatch(matcher)); } toMatchContents(contents) { return this._push((e) => e.toMatchContents(contents)); } /* ------------------------------------------------------------------------ */ /** * Expects the value to be _strictly equal to_ the specified expected one. * * Negation: {@link NegativeMatchers.toStrictlyEqual `not.toStrictlyEqual(...)`} */ toStrictlyEqual(expected) { return this._push((e) => e.toStrictlyEqual(expected)); } }; var NegativeMatchers = class { constructor(_instance, _matchers) { this._instance = _instance; this._matchers = _matchers; } _push(matcher) { this._matchers.push((expectations) => matcher(expectations.not)); return this._instance; } /* ------------------------------------------------------------------------ */ /** * Expects the value _**NOT**_ to be of the specified _extended_ * {@link TypeName type}. * * Negates: {@link Matcher.toBeA `toBeA(...)`} */ toBeA(type) { return this._push((e) => e.toBeA(type)); } toBeCloseTo(value, delta) { return this._push((e) => e.toBeCloseTo(value, delta)); } /* ------------------------------------------------------------------------ */ /** * Expects the value to be either `null` or `undefined`. * * Negates: {@link Matcher.toBeDefined `toBeDefined()`} */ toBeDefined() { return this._push((e) => e.toBeDefined()); } /* ------------------------------------------------------------------------ */ /** * Expects the value _**NOT**_ to be an instance of the specified * {@link Constructor}. * * Negates: {@link Matcher.toBeInstanceOf `toBeInstanceOf(...)`} */ toBeInstanceOf(constructor) { return this._push((e) => e.toBeInstanceOf(constructor)); } /* ------------------------------------------------------------------------ */ /** * Expects the value _**NOT**_ to be `NaN`. * * Negates: {@link Matcher.toBeNaN `toBeNaN()`} */ toBeNaN() { return this._push((e) => e.toBeNaN()); } toBeWithinRange(min, max) { return this._push((e) => e.toBeWithinRange(min, max)); } /* ------------------------------------------------------------------------ */ /** * Expects the value _**NOT**_ to be _deep equal to_ the specified expected * one. * * Negates: {@link Matcher.toEqual `toEqual(...)`} */ toEqual(expected) { return this._push((e) => e.toEqual(expected)); } /* ------------------------------------------------------------------------ */ /** * Expects the value to have a `number` _property_ `length` _different_ from * the specified expected value. * * Negates: {@link Matcher.toHaveLength `toHaveLength(...)`} */ toHaveLength(length) { return this._push((e) => e.toHaveLength(length)); } /* ------------------------------------------------------------------------ */ /** * Expects the value _**NOT**_ to have the specified _property_. * * Negates: {@link Matcher.toHaveProperty `toHaveProperty(...)`} */ toHaveProperty(property) { return this._push((e) => e.toHaveProperty(property)); } /* ------------------------------------------------------------------------ */ /** * Expects the value to have a `number` _property_ `size` _different_ from * the specified expected value. * * Negates: {@link Matcher.toHaveSize `toHaveSize(...)`} */ toHaveSize(size) { return this._push((e) => e.toHaveSize(size)); } toInclude(contents) { return this._push((e) => e.toInclude(contents)); } /* ------------------------------------------------------------------------ */ /** * Expects the value to be a `string` _**NOT MATCHING**_ the specified * sub-`string` or {@link RegExp}. * * Negates: {@link Matcher.toMatch `toMatch(...)`} */ toMatch(matcher) { return this._push((e) => e.toMatch(matcher)); } /* ------------------------------------------------------------------------ */ /** * Expects the value _**NOT**_ to be _strictly equal to_ the specified * expected one. * * Negates: {@link Matcher.toStrictlyEqual `toStrictlyEqual(...)`} */ toStrictlyEqual(expected) { return this._push((e) => e.toStrictlyEqual(expected)); } }; export { Matcher, NegativeMatchers }; //# sourceMappingURL=matchers.mjs.map