UNPKG

1.97 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3require("reflect-metadata");
4const matchers_1 = require("../matchers");
5const fail_1 = require("./fail");
6const matchers_2 = require("../matchers");
7const spying_1 = require("../spying");
8const object_matcher_1 = require("../matchers/object-matcher");
9function buildExpect() {
10 const EXPECT = ExpectFunction;
11 EXPECT.fail = fail_1.fail;
12 EXPECT.extend = ((expectedTypeConstructor, matcherConstructor) => {
13 MATCHER_MAP.push([expectedTypeConstructor, matcherConstructor]);
14 return EXPECT;
15 });
16 return EXPECT;
17}
18exports.buildExpect = buildExpect;
19const MATCHER_MAP = [
20 [Array, matchers_1.ContainerMatcher],
21 [spying_1.FunctionSpy, matchers_1.FunctionMatcher],
22 [spying_1.PropertySpy, matchers_1.PropertyMatcher],
23 [Object, object_matcher_1.ObjectMatcher]
24];
25function ExpectFunction(actualValue) {
26 const MATCHER = findMatcher(actualValue);
27 return new MATCHER(actualValue);
28}
29function findMatcher(actualValue) {
30 if (typeof actualValue === "object") {
31 return getObjectMatcher(actualValue);
32 }
33 if (typeof actualValue === "function") {
34 return matchers_1.FunctionMatcher;
35 }
36 return getPrimitiveMatcher(actualValue);
37}
38function getObjectMatcher(actualValue) {
39 if (actualValue === null || actualValue === undefined) {
40 return matchers_2.Matcher;
41 }
42 const proto = Object.getPrototypeOf(actualValue);
43 if (proto === null) {
44 return matchers_2.Matcher;
45 }
46 const match = MATCHER_MAP.find(matchPair => matchPair[0] === proto.constructor);
47 return match ? match[1] : findMatcher(proto);
48}
49function getPrimitiveMatcher(actualValue) {
50 if (typeof actualValue === "string") {
51 return matchers_1.StringMatcher;
52 }
53 if (typeof actualValue === "number") {
54 return matchers_1.NumberMatcher;
55 }
56 return matchers_2.Matcher;
57}
58//# sourceMappingURL=build-expect.js.map
\No newline at end of file