forked from ffMathy/FluffySpoon.JavaScript.Testing.Faking
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtilities.ts
More file actions
99 lines (77 loc) · 2.99 KB
/
Copy pathUtilities.ts
File metadata and controls
99 lines (77 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import { Argument, AllArguments } from './Arguments';
import { GetPropertyState } from './states/GetPropertyState';
import { InitialState } from './states/InitialState';
import { Context } from './Context';
import * as util from 'util';
export type Call = any[] // list of args
export enum PropertyType {
method = 'method',
property = 'property'
}
export enum SubstituteMethods {
received = 'received',
didNotReceive = 'didNotReceive',
mimicks = 'mimicks',
throws = 'throws',
returns = 'returns',
resolves = 'resolves',
rejects = 'rejects'
}
const seenObject = Symbol();
export function stringifyArguments(args: any[]) {
args = args.map(x => util.inspect(x));
return args && args.length > 0 ? 'arguments [' + args.join(', ') + ']' : 'no arguments';
};
export function areArgumentArraysEqual(a: any[], b: any[]) {
if (a.find(x => x instanceof AllArguments) || b.find(b => b instanceof AllArguments)) {
return true;
}
for (let i = 0; i < Math.max(b.length, a.length); i++) {
if (!areArgumentsEqual(b[i], a[i])) {
return false;
}
}
return true;
}
export function stringifyCalls(calls: Call[]) {
if (calls.length === 0)
return ' (no calls)';
let output = '';
for (let call of calls) {
output += '\n-> call with ' + (call.length ? stringifyArguments(call) : '(no arguments)')
}
return output;
};
export function areArgumentsEqual(a: any, b: any) {
if (a instanceof Argument && b instanceof Argument)
return false;
if (a instanceof AllArguments || b instanceof AllArguments)
return true;
if (a instanceof Argument)
return a.matches(b);
if (b instanceof Argument)
return b.matches(a);
return deepEqual(a, b);
};
function deepEqual(realA: any, realB: any, objectReferences: object[] = []): boolean {
const a = objectReferences.includes(realA) ? seenObject : realA;
const b = objectReferences.includes(realB) ? seenObject : realB;
const newObjectReferences = updateObjectReferences(objectReferences, a, b);
if (nonNullObject(a) && nonNullObject(b)) {
if (a.constructor !== b.constructor) return false;
const objectAKeys = Object.keys(a);
if (objectAKeys.length !== Object.keys(b).length) return false;
for (const key of objectAKeys) {
if (!deepEqual(a[key], b[key], newObjectReferences)) return false;
}
return true;
}
return a === b;
}
function updateObjectReferences(objectReferences: Array<object>, a: any, b: any) {
const tempObjectReferences = [...objectReferences, nonNullObject(a) && !objectReferences.includes(a) ? a : void 0];
return [...tempObjectReferences, nonNullObject(b) && !tempObjectReferences.includes(b) ? b : void 0];
}
function nonNullObject(value: any): value is { [key: string]: any } {
return typeof value === 'object' && value !== null;
}