forked from ffMathy/FluffySpoon.JavaScript.Testing.Faking
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSubstituteBase.ts
More file actions
73 lines (67 loc) · 3.12 KB
/
Copy pathSubstituteBase.ts
File metadata and controls
73 lines (67 loc) · 3.12 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
import { inspect } from 'util';
import { PropertyType, stringifyArguments, stringifyCalls, Call } from './Utilities';
export class SubstituteJS {
private _lastRegisteredSubstituteJSMethodOrProperty: string
set lastRegisteredSubstituteJSMethodOrProperty(value: string) {
this._lastRegisteredSubstituteJSMethodOrProperty = value;
}
get lastRegisteredSubstituteJSMethodOrProperty() {
return typeof this._lastRegisteredSubstituteJSMethodOrProperty === 'undefined' ? 'root' : this._lastRegisteredSubstituteJSMethodOrProperty;
}
[Symbol.toPrimitive]() {
return `[Function: ${this.constructor.name}] -> ${this.lastRegisteredSubstituteJSMethodOrProperty}`;
}
[Symbol.toStringTag]() {
return `[Function: ${this.constructor.name}] -> ${this.lastRegisteredSubstituteJSMethodOrProperty}`;
}
[Symbol.iterator]() {
return `[Function: ${this.constructor.name}] -> ${this.lastRegisteredSubstituteJSMethodOrProperty}`;
}
[inspect.custom]() {
return `[Function: ${this.constructor.name}] -> ${this.lastRegisteredSubstituteJSMethodOrProperty}`;
}
valueOf() {
return `[Function: ${this.constructor.name}] -> ${this.lastRegisteredSubstituteJSMethodOrProperty}`;
}
$$typeof() {
return `[Function: ${this.constructor.name}] -> ${this.lastRegisteredSubstituteJSMethodOrProperty}`;
}
toString() {
return `[Function: ${this.constructor.name}] -> ${this.lastRegisteredSubstituteJSMethodOrProperty}`;
}
inspect() {
return `[Function: ${this.constructor.name}] -> ${this.lastRegisteredSubstituteJSMethodOrProperty}`;
}
length = `[Function: ${this.constructor.name}] -> ${this.lastRegisteredSubstituteJSMethodOrProperty}`;
}
enum SubstituteExceptionTypes {
CallCountMissMatch = 'CallCountMissMatch',
PropertyNotMocked = 'PropertyNotMocked'
}
export class SubstituteException extends Error {
type: SubstituteExceptionTypes
constructor(msg: string, exceptionType?: SubstituteExceptionTypes) {
super(msg);
Error.captureStackTrace(this, SubstituteException);
this.name = new.target.name;
this.type = exceptionType
}
static forCallCountMissMatch(
callCount: { expected: number | null, received: number },
property: { type: PropertyType, value: PropertyKey },
calls: { expectedArguments: any[], received: Call[] }
) {
const message = 'Expected ' + (callCount.expected === null ? '1 or more' : callCount.expected) +
' call' + (callCount.expected === 1 ? '' : 's') + ' to the ' + property.type + ' ' + property.value.toString() +
' with ' + stringifyArguments(calls.expectedArguments) + ', but received ' + (callCount.received === 0 ? 'none' : callCount.received) +
' of such call' + (callCount.received === 1 ? '' : 's') +
'.\nAll calls received to ' + property.type + ' ' + property.value.toString() + ':' + stringifyCalls(calls.received);
return new this(message, SubstituteExceptionTypes.CallCountMissMatch);
}
static forPropertyNotMocked(property: PropertyKey) {
return new this(`There is no mock for property: ${String(property)}`, SubstituteExceptionTypes.PropertyNotMocked)
}
static generic(message: string) {
return new this(message)
}
}