UNPKG

cybernaut

Version:

Reliable, automated web UI testing in BDD-style.

154 lines 4.4 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const deepStrictEqual = require("deep-strict-equal"); const utils_1 = require("./utils"); class AbstractPredicate { constructor(not) { this._not = not; } get description() { return 'should ' + this._describe(this._not); } compare(actualValue) { return `expected ${utils_1.format(actualValue)} to ` + this._describe(this._not); } test(actualValue) { return this._not ? !this._test(actualValue) : this._test(actualValue); } } class ContainPredicate extends AbstractPredicate { constructor(value, not) { super(not); this._value = value; } _describe(not) { return `${not}contain ${utils_1.format(this._value)}`; } _test(actualValue) { return actualValue.indexOf(this._value) > -1; } } class EqualPredicate extends AbstractPredicate { constructor(value, not) { super(not); this._value = value; } _describe(not) { return `${not}equal ${utils_1.format(this._value)}`; } _test(actualValue) { if (actualValue !== actualValue && this._value !== this._value) { return true; } return deepStrictEqual(actualValue, this._value); } } class MatchPredicate extends AbstractPredicate { constructor(value, not) { super(not); this._value = value; } _describe(not) { return `${not}match ${utils_1.format(this._value)}`; } _test(actualValue) { return this._value.test(actualValue); } } class BeBetweenPredicate extends AbstractPredicate { constructor(minValue, maxValue, not) { super(not); this._minValue = minValue; this._maxValue = maxValue; } _describe(not) { const minValue = utils_1.format(this._minValue); const maxValue = utils_1.format(this._maxValue); return `${not}be between ${minValue} and ${maxValue}, inclusive`; } _test(actualValue) { return actualValue >= this._minValue && actualValue <= this._maxValue; } } class BeGreaterThanPredicate extends AbstractPredicate { constructor(value, not) { super(not); this._value = value; } _describe(not) { return `${not}be greater than ${utils_1.format(this._value)}`; } _test(actualValue) { return actualValue > this._value; } } class BeGreaterThanOrEqualPredicate extends AbstractPredicate { constructor(value, not) { super(not); this._value = value; } _describe(not) { return `${not}be greater than or equal ${utils_1.format(this._value)}`; } _test(actualValue) { return actualValue >= this._value; } } class BeLessThanPredicate extends AbstractPredicate { constructor(value, not) { super(not); this._value = value; } _describe(not) { return `${not}be less than ${utils_1.format(this._value)}`; } _test(actualValue) { return actualValue < this._value; } } class BeLessThanOrEqualPredicate extends AbstractPredicate { constructor(value, not) { super(not); this._value = value; } _describe(not) { return `${not}be less than or equal ${utils_1.format(this._value)}`; } _test(actualValue) { return actualValue <= this._value; } } class PredicateBuilder { constructor(negated = false) { this._not = negated ? 'not ' : ''; } get not() { return new PredicateBuilder(true); } contain(value) { return new ContainPredicate(value, this._not); } equal(value) { return new EqualPredicate(value, this._not); } match(value) { return new MatchPredicate(value, this._not); } beBetween(minValue, maxValue) { return new BeBetweenPredicate(minValue, maxValue, this._not); } beGreaterThan(value) { return new BeGreaterThanPredicate(value, this._not); } beGreaterThanOrEqual(value) { return new BeGreaterThanOrEqualPredicate(value, this._not); } beLessThan(value) { return new BeLessThanPredicate(value, this._not); } beLessThanOrEqual(value) { return new BeLessThanOrEqualPredicate(value, this._not); } } exports.PredicateBuilder = PredicateBuilder; //# sourceMappingURL=predicate.js.map