1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | require("reflect-metadata");
|
4 | const matchers_1 = require("../matchers");
|
5 | const fail_1 = require("./fail");
|
6 | const matchers_2 = require("../matchers");
|
7 | const spying_1 = require("../spying");
|
8 | const object_matcher_1 = require("../matchers/object-matcher");
|
9 | function 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 | }
|
18 | exports.buildExpect = buildExpect;
|
19 | const 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 | ];
|
25 | function ExpectFunction(actualValue) {
|
26 | const MATCHER = findMatcher(actualValue);
|
27 | return new MATCHER(actualValue);
|
28 | }
|
29 | function 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 | }
|
38 | function 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 | }
|
49 | function 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 |
|
\ | No newline at end of file |