UNPKG

alsatian

Version:

TypeScript and JavaScript testing framework for beautiful and readable tests

58 lines 1.97 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); require("reflect-metadata"); const matchers_1 = require("../matchers"); const fail_1 = require("./fail"); const matchers_2 = require("../matchers"); const spying_1 = require("../spying"); const object_matcher_1 = require("../matchers/object-matcher"); function buildExpect() { const EXPECT = ExpectFunction; EXPECT.fail = fail_1.fail; EXPECT.extend = ((expectedTypeConstructor, matcherConstructor) => { MATCHER_MAP.push([expectedTypeConstructor, matcherConstructor]); return EXPECT; }); return EXPECT; } exports.buildExpect = buildExpect; const MATCHER_MAP = [ [Array, matchers_1.ContainerMatcher], [spying_1.FunctionSpy, matchers_1.FunctionMatcher], [spying_1.PropertySpy, matchers_1.PropertyMatcher], [Object, object_matcher_1.ObjectMatcher] ]; function ExpectFunction(actualValue) { const MATCHER = findMatcher(actualValue); return new MATCHER(actualValue); } function findMatcher(actualValue) { if (typeof actualValue === "object") { return getObjectMatcher(actualValue); } if (typeof actualValue === "function") { return matchers_1.FunctionMatcher; } return getPrimitiveMatcher(actualValue); } function getObjectMatcher(actualValue) { if (actualValue === null || actualValue === undefined) { return matchers_2.Matcher; } const proto = Object.getPrototypeOf(actualValue); if (proto === null) { return matchers_2.Matcher; } const match = MATCHER_MAP.find(matchPair => matchPair[0] === proto.constructor); return match ? match[1] : findMatcher(proto); } function getPrimitiveMatcher(actualValue) { if (typeof actualValue === "string") { return matchers_1.StringMatcher; } if (typeof actualValue === "number") { return matchers_1.NumberMatcher; } return matchers_2.Matcher; } //# sourceMappingURL=build-expect.js.map