smartexpect/ts/smartexpect.classes.assertion.ts

199 lines
5.5 KiB
TypeScript
Raw Normal View History

2022-01-21 02:33:24 +00:00
import * as plugins from './smartexpect.plugins';
export type TExecutionType = 'sync' | 'async';
export class Assertion {
executionMode: TExecutionType;
baseReference: any;
private notSetting = false;
private timeoutSetting = 0;
constructor(baseReferenceArg: any, executionModeArg: TExecutionType) {
this.baseReference = baseReferenceArg;
this.executionMode = executionModeArg;
}
public get not() {
this.notSetting = true;
return this;
}
public timeout(millisArg: number) {
this.timeoutSetting = millisArg;
return this;
}
private runCheck(checkFunction: () => any) {
const runDirectOrNegated = (checkFunction: () => any) => {
if (!this.notSetting) {
return checkFunction();
} else {
let isOk = false;
try {
2022-02-02 03:24:39 +00:00
runDirectOrNegated(checkFunction());
2022-01-21 02:33:24 +00:00
} catch (e) {
isOk = true;
}
if (!isOk) {
throw new Error('Negated assertion is not ok!');
}
}
2022-02-02 03:24:39 +00:00
};
2022-01-21 02:33:24 +00:00
if (this.executionMode === 'async') {
const done = plugins.smartpromise.defer();
if (!(this.baseReference instanceof Promise)) {
done.reject(new Error(`${this.baseReference} is not of type promise.`));
} else {
if (this.timeoutSetting) {
plugins.smartdelay.delayFor(this.timeoutSetting).then(() => {
if (done.status === 'pending') {
2022-02-02 03:24:39 +00:00
done.reject(new Error(`${this.baseReference} timed out at ${this.timeoutSetting}!`));
}
2022-01-21 02:33:24 +00:00
});
}
2022-02-02 03:24:39 +00:00
this.baseReference.then((promiseResultArg) => {
2022-01-21 02:33:24 +00:00
this.baseReference = promiseResultArg;
done.resolve(runDirectOrNegated(checkFunction));
2022-02-02 03:24:39 +00:00
});
2022-01-21 02:33:24 +00:00
}
return done.promise;
} else {
return runDirectOrNegated(checkFunction);
}
}
public toBeTypeofString() {
return this.runCheck(() => {
if (typeof this.baseReference !== 'string') {
throw new Error(
2022-02-02 03:24:39 +00:00
`Assertion failed: ${this.baseReference} is not of type string, but typeof ${typeof this
.baseReference}`
2022-01-21 02:33:24 +00:00
);
}
});
}
public toBeTypeofNumber() {
return this.runCheck(() => {
if (typeof this.baseReference !== 'number') {
throw new Error(
2022-02-02 03:24:39 +00:00
`Assertion failed: ${this.baseReference} is not of type string, but typeof ${typeof this
.baseReference}`
2022-01-21 02:33:24 +00:00
);
}
});
}
public toBeTypeofBoolean() {
return this.runCheck(() => {
if (typeof this.baseReference !== 'boolean') {
throw new Error(
2022-02-02 03:24:39 +00:00
`Assertion failed: ${this.baseReference} is not of type string, but typeof ${typeof this
.baseReference}`
2022-01-21 02:33:24 +00:00
);
}
});
}
2022-01-21 16:37:30 +00:00
public toEqual(comparisonObject: any) {
return this.runCheck(() => {
const result = plugins.fastDeepEqual(this.baseReference, comparisonObject);
if (!result) {
throw new Error(`${this.baseReference} does not equal ${comparisonObject}`);
}
});
}
2022-01-21 18:40:30 +00:00
public toBeTrue() {
return this.runCheck(() => {
const result = typeof this.baseReference === 'boolean' && this.baseReference === true;
if (!result) {
throw new Error(`${this.baseReference} is not true or not of type boolean`);
}
});
}
public toBeFalse() {
return this.runCheck(() => {
const result = typeof this.baseReference === 'boolean' && this.baseReference === false;
if (!result) {
throw new Error(`${this.baseReference} is not false or not of type boolean`);
}
});
}
public toBeInstanceOf(classArg: any) {
return this.runCheck(() => {
const result = this.baseReference instanceof classArg;
if (!result) {
throw new Error(`${this.baseReference} is not an instance of ${classArg}`);
}
});
}
2022-02-02 01:45:45 +00:00
public toHaveProperty(propertyArg: string) {
return this.runCheck(() => {
const result = !!this.baseReference[propertyArg];
if (!result) {
throw new Error(`${this.baseReference} does not have property ${propertyArg}`);
}
});
}
public toBeGreaterThan(numberArg: number) {
return this.runCheck(() => {
const result = this.baseReference > numberArg;
if (!result) {
throw new Error(`${this.baseReference} is not greater than ${numberArg}`);
}
});
}
public toBeLessThan(numberArg: number) {
return this.runCheck(() => {
const result = this.baseReference < numberArg;
if (!result) {
throw new Error(`${this.baseReference} is not less than ${numberArg}`);
}
});
}
2022-02-02 03:24:39 +00:00
public toBeNull() {
return this.runCheck(() => {
const result = this.baseReference === null;
if (!result) {
throw new Error(`${this.baseReference} is not null`);
}
});
}
public toBeUndefined() {
return this.runCheck(() => {
const result = this.baseReference === undefined;
if (!result) {
throw new Error(`${this.baseReference} is not undefined`);
}
});
}
public toBeNullOrUndefined() {
return this.runCheck(() => {
const result = this.baseReference === null || this.baseReference === undefined;
if (!result) {
throw new Error(`${this.baseReference} is not null or undefined`);
}
});
}
public toContain(itemArg: any) {
return this.runCheck(() => {
2022-02-02 03:26:22 +00:00
const result =
this.baseReference instanceof Array &&
2022-02-02 07:02:26 +00:00
this.baseReference.includes(itemArg);
2022-02-02 03:24:39 +00:00
if (!result) {
throw new Error(`${this.baseReference} is not contain ${itemArg}`);
}
});
}
2022-01-21 02:33:24 +00:00
}