UNPKG

alsatian-fluent-assertions

Version:

Fluent assertions extension to Alsatian xUnit framework.

117 lines 4.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const errors_1 = require("../errors"); const types_1 = require("../types"); class FluentMatcherBase extends types_1.RootNode { constructor(actualValue, nextValue, initial, prevCore, ctxt) { // not set for non-root until a fluent method is called. super(undefined, undefined); this.invert = false; if (initial) { this.parent = new types_1.RootNode("Assert", this.id(actualValue)); } this.hasNext = false; this.actualValue = actualValue; this.nextValue = nextValue; this.prevCore = prevCore; this.assertionContext = ctxt; } /** * Inverts conditionals according to any current, fluent negation. * @param {boolean} original The original boolean. * @returns {boolean} The original value maybe inverted, depending on current fluent state. */ maybeInvert(original) { if (this.invert) { return !original; } return original; } /** Whether the current fluent scope is inverted, e.g., .not.something. */ get invertedContext() { return this.invert; } nullOrUndefined(val) { return val === null || typeof val === "undefined"; } setCurrentNode(name, details) { if (this.name) { return; } this.name = name; this.details = details; } /** * Generates the actual and next (available via 'that') values of the * @param {any} actualValue The value over which future assertions will be performed. * @param {any} nextValue The next contextual value (from prior operations) the user could choose with 'that'. * @param {boolean} invert Inverts the next term. * @param {boolean} hasNext Whether a narrowable value is available, per the current assertion. * @param {IFluentCore} prevCore Previous, unnarrowed fluent scope, if scope has narrowed. * @param {string} reason The reason for the current set of assertions. Helps with maintenance. * @param {any} reasonData Data to help with future testing metrics. * @returns {INarrowableFluentCore<TActual, TNext>} The fluent context for upcoming assertions. */ generateFluentState(actualValue, nextValue, invert, hasNext = false, prevCore = null, reason = null, reasonData = null) { /** * Shh... Typescript made me do it. :) You can't return a new PropertiesMatcherWithHelpers() * from base classes of the PropertiesMatcherWithHelpers class. * No import loops, and all that. */ const self = new this.constructor(actualValue, nextValue); self.parent = this; self.actualValue = actualValue; self.nextValue = nextValue; self.invert = invert; self.hasNext = !!hasNext; self.prevCore = prevCore || this.prevCore; self.reason = reason; self.reasonData = reasonData; return self; } /** * Wraps a value in our asserts framework. Intended for use inside property assertions. * @param {TActual} actualValue The value to wrap in an Assert. * @returns {IFluentCore<TActual>} The fluent context to provide inside, e.g., a property assertion. */ wrap(actualValue) { return new this.constructor(actualValue, null); } /** * Returns " not " or "" depending on whether the chain up til this point is inverted. */ get negation() { return this.invert ? " not " : " "; } /** * Returns a string representation of a function definition up to 500 characters long. * Intended to help debugging tests. * @param {Function} fn Function to stringify. * @returns {string} A maximum of 500 characters of a function definition. */ getFnString(fn) { const mAlias = fn.toString(); return mAlias.substr(Math.max(mAlias.length, 500 /* fns can get long */)); } /** * Tries to intelligently identify a value. ATOW, returns "array" when an array, a "/regex/" * when such, or `typeof item`. * @param {any} item The item whose type to identify. * @returns {string} A string identifying the type of the item in a human-friendly way. */ id(item) { if (item instanceof Array) { return "array"; } else if (item instanceof RegExp) { return item.toString(); } return typeof item; } formatShortError(e) { return e ? `Error '${e.name}' with message '${e.message}'.` : "[no error]"; } specError(message, expected, actual) { throw new errors_1.SpecError(this, message, expected, actual); } } exports.FluentMatcherBase = FluentMatcherBase; //# sourceMappingURL=fluent-matcher-base.js.map