1 | "use strict";
|
2 | Object.defineProperty(exports, "__esModule", { value: true });
|
3 | const diff_1 = require("./diff");
|
4 | const empty_matcher_1 = require("./empty-matcher");
|
5 | class ObjectMatcher extends empty_matcher_1.EmptyMatcher {
|
6 | toEqual(expectedValue) {
|
7 | if (Buffer.isBuffer(expectedValue) || Buffer.isBuffer(this.actualValue)) {
|
8 | this._checkTypeMatcherEqual(expectedValue, this.buffersEqual);
|
9 | }
|
10 | else {
|
11 | this._checkTypeMatcherEqual(expectedValue, this.objectsEqual);
|
12 | }
|
13 | }
|
14 | buffersEqual(expectedValue) {
|
15 | this._registerMatcher(this._checkBuffersAreEqual(expectedValue, this.actualValue) === this.shouldMatch, `Expected values ${!this.shouldMatch ? "not " : ""}to be equal`, expectedValue, {
|
16 | diff: diff_1.diff(expectedValue, this.actualValue)
|
17 | });
|
18 | }
|
19 | _checkBuffersAreEqual(buffer, other) {
|
20 | if (this._isBufferable(other)) {
|
21 | const otherBuffer = Buffer.isBuffer(other)
|
22 | ? other
|
23 | : Buffer.from(other);
|
24 | return buffer.equals(otherBuffer);
|
25 | }
|
26 | else {
|
27 | return false;
|
28 | }
|
29 | }
|
30 | _isBufferable(obj) {
|
31 | return ("string" === typeof obj ||
|
32 | Buffer.isBuffer(obj) ||
|
33 | Array.isArray(obj) ||
|
34 | obj instanceof ArrayBuffer ||
|
35 | (null != obj &&
|
36 | "object" === typeof obj &&
|
37 | obj.hasOwnProperty("length") &&
|
38 | "number" === typeof obj.length &&
|
39 | (obj.length === 0 || (obj.length > 0 && obj.length - 1 in obj))));
|
40 | }
|
41 | objectsEqual(expectedValue) {
|
42 | this._registerMatcher(this._checkObjectsAreDeepEqual(expectedValue, this.actualValue) === this.shouldMatch, `Expected objects ${!this.shouldMatch ? "not " : ""}to be equal`, expectedValue, {
|
43 | diff: diff_1.diff(expectedValue, this.actualValue)
|
44 | });
|
45 | }
|
46 | _checkObjectsAreDeepEqual(objectA, objectB) {
|
47 | if (Array.isArray(objectA) !== Array.isArray(objectB)) {
|
48 | return false;
|
49 | }
|
50 | const OBJECT_A_KEYS = Object.keys(objectA);
|
51 | const OBJECT_B_KEYS = Object.keys(objectB);
|
52 | if (OBJECT_A_KEYS.length !== OBJECT_B_KEYS.length) {
|
53 | return false;
|
54 | }
|
55 | for (const objectAKey of OBJECT_A_KEYS) {
|
56 | if (objectA[objectAKey] !== objectB[objectAKey]) {
|
57 | if (objectA[objectAKey] === null ||
|
58 | typeof objectA[objectAKey] !== "object" ||
|
59 | this._checkObjectsAreDeepEqual(objectA[objectAKey], objectB[objectAKey]) === false) {
|
60 | return false;
|
61 | }
|
62 | }
|
63 | }
|
64 | return true;
|
65 | }
|
66 | }
|
67 | exports.ObjectMatcher = ObjectMatcher;
|
68 |
|
\ | No newline at end of file |