oral-ts
Version:
a testing framework for typescript
77 lines • 3.35 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Contain = void 0;
var chalk_1 = __importDefault(require("chalk"));
function resolver(found, given, key) {
if (typeof given === "object" && JSON.stringify(found).startsWith("[")) {
var newArray_1 = [];
var newGiven = given;
if (typeof found === "object" && JSON.stringify(found).startsWith("[")) {
newGiven.forEach(function (element) {
newArray_1.push(found.find(function (val) { return val === element; }));
});
}
else {
this.emit("fail", key, "Contain:Array");
console.log(chalk_1.default.red("expected array but found " + typeof found + " \n "), chalk_1.default.green("given :- " + given + "\n"), chalk_1.default.red("found :- " + found));
}
if (JSON.stringify(given) === JSON.stringify(newArray_1)) {
this.emit("pass", key, "Contain:Array");
}
else {
this.emit("fail", key, "Contain:Array");
console.log(chalk_1.default.green("given :- " + given + " \n"), chalk_1.default.red("found :- " + found));
}
}
else if (typeof given === "object" &&
JSON.stringify(found).startsWith("{")) {
var newGiven = given;
if (typeof found === "object" && JSON.stringify(found).startsWith("{")) {
var i = 0;
for (var objKey in newGiven) {
if (newGiven[objKey] === found[objKey]) {
i++;
}
}
if (Object.keys(newGiven).length === i) {
this.emit("pass", key, "Contain:Object");
}
else {
this.emit("fail", key, "Contain:Object");
console.log(chalk_1.default.green("given :-\n" + JSON.stringify(given, null, " ") + " \n"), chalk_1.default.red("found :-\n" + JSON.stringify(found, null, " ")));
}
}
else {
this.emit("fail", key, "Contain:Object");
console.log(chalk_1.default.red("expected object but found " + typeof found + " \n "), chalk_1.default.green("given :-\n" + JSON.stringify(given, null, " ") + " "), chalk_1.default.red("found :-\n" + JSON.stringify(found, null, " ")));
}
}
}
function Contain(given) {
return function (target, key, descriptor) {
var original = descriptor.value;
Reflect.defineMetadata("role", "assertion", target, key);
descriptor.value = function () {
var _this = this;
var args = [];
for (var _i = 0; _i < arguments.length; _i++) {
args[_i] = arguments[_i];
}
var found = original.apply(this, args);
if (found !== undefined) {
if (found.constructor.name === "Promise") {
found.then(function (found) { return resolver.apply(_this, [given, found, key]); });
}
else
resolver.apply(this, [given, found, key]);
}
return found;
};
return descriptor;
};
}
exports.Contain = Contain;
//# sourceMappingURL=contain.js.map