flagpole
Version:
Simple and fast DOM integration, headless or headful browser, and REST API testing framework.
387 lines • 15.9 kB
JavaScript
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : new P(function (resolve) { resolve(result.value); }).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const value_1 = require("./value");
const _1 = require(".");
const assertionresult_1 = require("./assertionresult");
class Assertion {
constructor(context, thisValue, message) {
this._not = false;
this._optional = false;
this._result = null;
this._context = context;
this._input = thisValue;
;
this._message = typeof message == 'undefined' ? null : message;
}
get and() {
const assertion = new Assertion(this._context, this._input, this._message ? `&& ${this._message}` : null);
this._not && assertion.not;
this._optional && assertion.optional;
return assertion;
}
get type() {
const type = new value_1.Value(_1.Flagpole.toType(this._getCompareValue(this._input)), this._context, `Type of ${this._getSubject()}`);
const assertion = new Assertion(this._context, type, this._message);
this._not && assertion.not;
this._optional && assertion.optional;
return assertion;
}
get length() {
const length = (() => {
const thisValue = this._getCompareValue(this._input);
return thisValue && thisValue.length ?
thisValue.length : 0;
})();
const assertion = new Assertion(this._context, new value_1.Value(length, this._context, `Length of ${this._getSubject()}`), this._message);
this._not && assertion.not;
this._optional && assertion.optional;
return assertion;
}
get not() {
this._not = true;
return this;
}
get optional() {
this._optional = true;
return this;
}
get resolvesTo() {
if (_1.Flagpole.toType(this._input) != 'promise') {
throw new Error(`${this._getSubject()} is not a promise.`);
}
const message = this._message || undefined;
const context = this._context;
const promise = this._input;
return new Promise(function (resolve, reject) {
return __awaiter(this, void 0, void 0, function* () {
promise.then((value) => {
resolve(Assertion.create(context, value, message));
}).catch((ex) => {
resolve(Assertion.create(context, ex, message));
});
});
});
}
static create(context, thisValue, message) {
return __awaiter(this, void 0, void 0, function* () {
return new Assertion(context, thisValue, message);
});
}
exactly(value) {
const thisValue = this._getCompareValue(this._input);
const thatValue = this._getCompareValue(value);
const bool = this._eval(thisValue === thatValue);
this._assert(bool, this._not ?
`${this._getSubject()} is not exactly ${thatValue}` :
`${this._getSubject()} is exactly ${thatValue}`, thatValue);
return this;
}
equals(value) {
const thisValue = this._getCompareValue(this._input);
const thatValue = this._getCompareValue(value);
const bool = this._eval(thisValue == thatValue);
this._assert(bool, this._not ?
`${this._getSubject()} does not equal ${thatValue}` :
`${this._getSubject()} equals ${thatValue}`, thisValue);
return this;
}
like(value) {
const thisVal = String(this._getCompareValue(this._input)).toLowerCase().trim();
const thatVal = String(this._getCompareValue(value)).toLowerCase().trim();
const bool = this._eval(thisVal == thatVal);
this._assert(bool, this._not ?
`${this._getSubject()} is not like ${thatVal}` :
`${this._getSubject()} is like ${thatVal}`, thisVal);
return this;
}
greaterThan(value) {
const thisValue = this._getCompareValue(this._input);
const thatValue = this._getCompareValue(value);
const bool = this._eval(parseFloat(thisValue) > parseFloat(thatValue));
this._assert(bool, this._not ?
`${this._getSubject()} is not greater than ${thatValue}` :
`${this._getSubject()} is greater than ${thatValue}`, thisValue);
return this;
}
greaterThanOrEquals(value) {
const thisValue = this._getCompareValue(this._input);
const thatValue = this._getCompareValue(value);
const bool = this._eval(parseFloat(thisValue) >= parseFloat(thatValue));
this._assert(bool, this._not ?
`${this._getSubject()} is not greater than or equal to ${thatValue}` :
`${this._getSubject()} is greater than or equal to ${thatValue}`, thisValue);
return this;
}
lessThan(value) {
const thisValue = this._getCompareValue(this._input);
const thatValue = this._getCompareValue(value);
const bool = this._eval(parseFloat(thisValue) < parseFloat(thatValue));
this._assert(bool, this._not ?
`${this._getSubject()} is not less than ${thatValue}` :
`${this._getSubject()} is less than ${thatValue}`, thisValue);
return this;
}
lessThanOrEquals(value) {
const thisValue = this._getCompareValue(this._input);
const thatValue = this._getCompareValue(value);
const bool = this._eval(parseFloat(thisValue) <= parseFloat(thatValue));
this._assert(bool, this._not ?
`${this._getSubject()} is not less than or equal to ${thatValue}` :
`${this._getSubject()} is less than or equal to ${thatValue}`, thisValue);
return this;
}
between(min, max) {
const thisValue = this._getCompareValue(this._input);
const thatMin = parseFloat(this._getCompareValue(min));
const thatMax = parseFloat(this._getCompareValue(max));
const bool = this._eval(parseFloat(thisValue) >= thatMin &&
parseFloat(thisValue) <= thatMax);
this._assert(bool, this._not ?
`${this._getSubject()} is not between ${min} and ${max}` :
`${this._getSubject()} is between ${min} and ${max}`, thisValue);
return this;
}
matches(value) {
const thisValue = this._getCompareValue(this._input);
const thatValue = this._getCompareValue(value);
const pattern = _1.Flagpole.toType(value) == 'regexp' ? thatValue : new RegExp(value);
const bool = this._eval(pattern.test(thisValue));
this._assert(bool, this._not ?
`${this._getSubject()} does not match ${String(pattern)}` :
`${this._getSubject()} matches ${String(pattern)}`, thisValue);
return this;
}
contains(value) {
let bool = false;
const thisValue = this._getCompareValue(this._input);
const thatValue = this._getCompareValue(value);
if (_1.Flagpole.isNullOrUndefined(this._input)) {
bool = this._eval(thisValue === thatValue);
}
else if (_1.Flagpole.toType(this._input) == 'array') {
bool = this._eval(thisValue.indexOf(thatValue) >= 0);
}
else if (_1.Flagpole.toType(this._input) == 'object') {
bool = this._eval(typeof this._input[thatValue] !== 'undefined');
}
else {
bool = this._eval(String(this._input).indexOf(thatValue) >= 0);
}
this._assert(bool, this._not ?
`${this._getSubject()} does not contain ${thatValue}` :
`${this._getSubject()} contains ${thatValue}`, thisValue);
return this;
}
startsWith(value) {
let bool = false;
const thisValue = this._getCompareValue(this._input);
const thatValue = this._getCompareValue(value);
if (_1.Flagpole.toType(thisValue) == 'array') {
bool = this._eval(thisValue[0] == value);
}
if (!_1.Flagpole.isNullOrUndefined(thisValue)) {
bool = this._eval(String(thisValue).indexOf(thatValue) === 0);
}
this._assert(bool, this._not ?
`${this._getSubject()} does not start with ${thatValue}` :
`${this._getSubject()} starts with ${thatValue}`, String(thisValue));
return this;
}
endsWith(value) {
let bool = false;
const thisValue = this._getCompareValue(this._input);
const thatValue = this._getCompareValue(value);
if (_1.Flagpole.toType(thisValue) == 'array') {
bool = this._eval(thisValue[thisValue.length - 1] == thatValue);
}
if (!_1.Flagpole.isNullOrUndefined(thisValue)) {
bool = this._eval(String(thisValue).substr(0, String(thisValue).length - String(thatValue).length) == thatValue);
}
this._assert(bool, this._not ?
`${this._getSubject()} does not end with ${thatValue}` :
`${this._getSubject()} ends with ${thatValue}`, String(this._input));
return this;
}
in(values) {
const thisValue = this._getCompareValue(this._input);
let bool = this._eval(values.indexOf(thisValue) >= 0);
this._assert(bool, this._not ?
`${this._getSubject()} is not in list: ${values.join(', ')}` :
`${this._getSubject()} is in list: ${values.join(', ')}`, thisValue);
return this;
}
includes(value) {
const thisValue = this._getCompareValue(this._input);
const thatValue = String(this._getCompareValue(value));
let bool = this._eval(thisValue && thisValue.indexOf &&
thisValue.indexOf(thatValue) >= 0);
this._assert(bool, this._not ?
`${this._getSubject()} does not include ${thatValue}` :
`${this._getSubject()} includes ${thatValue}`, thisValue);
return this;
}
exists() {
const thisValue = this._getCompareValue(this._input);
const bool = this._eval(!_1.Flagpole.isNullOrUndefined(thisValue));
this._assert(bool, this._not ?
`${this._getSubject()} does not exist` :
`${this._getSubject()} exists`, thisValue);
return this;
}
resolves(continueOnReject = false) {
const assertion = this;
return new Promise((resolve, reject) => {
const result = (bool) => {
this._assert(this._eval(bool), this._not ?
`${this._getSubject()} was not resolve` :
`${this._getSubject()} was resolved`, bool);
if (bool) {
resolve(assertion);
}
else {
continueOnReject ? resolve(assertion) : reject();
}
};
if (_1.Flagpole.toType(this._input) == 'promise') {
this._input
.then(() => { result(true); })
.catch(() => { result(false); });
}
else {
result(false);
}
});
}
rejects(continueOnReject = false) {
const assertion = this;
return new Promise((resolve, reject) => {
const result = (bool) => {
this._assert(this._eval(bool), this._not ?
`${this._getSubject()} was not rejected` :
`${this._getSubject()} was rejected`, bool);
if (bool) {
resolve(assertion);
}
else {
continueOnReject ? resolve(assertion) : reject();
}
};
if (_1.Flagpole.toType(this._input) == 'promise') {
this._input
.then(() => { result(false); })
.catch(() => { result(true); });
}
else {
result(false);
}
});
}
none(callback) {
let bool = false;
const thisValue = this._getCompareValue(this._input);
if (_1.Flagpole.toType(thisValue) == 'array') {
const arr = thisValue;
bool = arr.every((value, index, array) => {
return !callback(value, index, array);
});
}
this._assert(this._eval(bool), this._not ?
`${this._getSubject()} some were true` :
`${this._getSubject()} none were true`, thisValue);
return this;
}
every(callback) {
let bool = false;
const thisValue = this._getCompareValue(this._input);
if (_1.Flagpole.toType(thisValue) == 'array') {
const arr = thisValue;
bool = arr.every((value, index, array) => {
return callback(value, index, array);
});
}
this._assert(this._eval(bool), this._not ?
`${this._getSubject()} not all were true` :
`${this._getSubject()} all were true`, thisValue);
return this;
}
some(callback) {
let bool = false;
const thisValue = this._getCompareValue(this._input);
if (_1.Flagpole.toType(thisValue) == 'array') {
const arr = thisValue;
bool = arr.some((value, index, array) => {
return callback(value, index, array);
});
}
this._assert(this._eval(bool), this._not ?
`${this._getSubject()} none were true` :
`${this._getSubject()} some were true`, thisValue);
return this;
}
_assert(statement, defaultMessage, actualValue) {
if (this._result !== null) {
throw new Error('Assertion result is immutable.');
}
if (!!statement) {
const message = this._message || defaultMessage;
this._result = assertionresult_1.AssertionResult.pass(message);
}
else {
const message = (this._message || defaultMessage);
const details = `Actual value: ${String(actualValue)}`;
this._result = this._optional ?
assertionresult_1.AssertionResult.failOptional(message, details) :
assertionresult_1.AssertionResult.fail(message, details);
}
this._context.scenario.logResult(this._result);
}
_getCompareValue(value) {
if (_1.Flagpole.toType(value) == 'value') {
return value.$;
}
else {
return value;
}
}
_getSubject() {
const type = _1.Flagpole.toType(this._input);
let name;
if (this._input && this._input.name) {
name = this._input.name;
}
else if (type == 'array') {
name = 'Array';
}
else if (type == 'object') {
name = 'Object';
}
else if (type == 'domelement') {
name = 'DOM Element';
}
else if (type == 'cssrule') {
name = 'CSS Rule';
}
else {
name = String(this._input);
}
if (String(name).length > 64) {
name = name.substr(0, 61) + '...';
}
return (_1.Flagpole.isNullOrUndefined(name) || String(name).length == 0) ?
'It' : String(name);
}
_eval(bool) {
this._not && (bool = !bool);
return bool;
}
}
exports.Assertion = Assertion;
//# sourceMappingURL=assertion.js.map