forked from NagRock/ts-mockito
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathMethodStubVerificator.ts
94 lines (79 loc) · 4.61 KB
/
MethodStubVerificator.ts
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
import {MethodToStub} from "./MethodToStub";
import {MethodCallToStringConverter} from "./utils/MethodCallToStringConverter";
export class MethodStubVerificator {
private methodCallToStringConverter: MethodCallToStringConverter = new MethodCallToStringConverter();
constructor(private methodToVerify: MethodToStub) {
}
public called(): void {
this.atLeast(1);
}
public never(): void {
this.times(0);
}
public once(): void {
this.times(1);
}
public twice(): void {
this.times(2);
}
public thrice(): void {
this.times(3);
}
public times(value: number): void {
const allMatchingActions = this.methodToVerify.mocker.getAllMatchingActions(this.methodToVerify.name, this.methodToVerify.matchers);
if (value !== allMatchingActions.length) {
const methodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify);
throw new Error(`Expected "${methodToVerifyAsString}to be called ${value} time(s). But has been called ${allMatchingActions.length} time(s).`);
}
}
public atLeast(value: number): void {
const allMatchingActions = this.methodToVerify.mocker.getAllMatchingActions(this.methodToVerify.name, this.methodToVerify.matchers);
if (value > allMatchingActions.length) {
const methodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify);
throw new Error(`Expected "${methodToVerifyAsString}to be called at least ${value} time(s). But has been called ${allMatchingActions.length} time(s).`);
}
}
public atMost(value: number): void {
const allMatchingActions = this.methodToVerify.mocker.getAllMatchingActions(this.methodToVerify.name, this.methodToVerify.matchers);
if (value < allMatchingActions.length) {
const methodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify);
throw new Error(`Expected "${methodToVerifyAsString}to be called at least ${value} time(s). But has been called ${allMatchingActions.length} time(s).`);
}
}
public calledBefore(method: any): void {
const firstMethodAction = this.methodToVerify.mocker.getFirstMatchingAction(this.methodToVerify.name, this.methodToVerify.matchers);
const secondMethodAction = method.mocker.getFirstMatchingAction(method.name, method.matchers);
const mainMethodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify);
const secondMethodAsString = this.methodCallToStringConverter.convert(method);
const errorBeginning = `Expected "${mainMethodToVerifyAsString} to be called before ${secondMethodAsString}`;
if (firstMethodAction && secondMethodAction) {
if (!firstMethodAction.hasBeenCalledBefore(secondMethodAction)) {
throw new Error(`${errorBeginning}but has been called after.`);
}
} else if (firstMethodAction && !secondMethodAction) {
throw new Error(`${errorBeginning}but ${secondMethodAsString}has never been called.`);
} else if (!firstMethodAction && secondMethodAction) {
throw new Error(`${errorBeginning}but ${mainMethodToVerifyAsString}has never been called.`);
} else {
throw new Error(`${errorBeginning}but none of them has been called.`);
}
}
public calledAfter(method: any): void {
const firstMethodAction = this.methodToVerify.mocker.getFirstMatchingAction(this.methodToVerify.name, this.methodToVerify.matchers);
const secondMethodAction = method.mocker.getFirstMatchingAction(method.name, method.matchers);
const mainMethodToVerifyAsString = this.methodCallToStringConverter.convert(this.methodToVerify);
const secondMethodAsString = this.methodCallToStringConverter.convert(method);
const errorBeginning = `Expected "${mainMethodToVerifyAsString}to be called after ${secondMethodAsString}`;
if (firstMethodAction && secondMethodAction) {
if (firstMethodAction.hasBeenCalledBefore(secondMethodAction)) {
throw new Error(`${errorBeginning}but has been called before.`);
}
} else if (firstMethodAction && !secondMethodAction) {
throw new Error(`${errorBeginning}but ${secondMethodAsString}has never been called.`);
} else if (!firstMethodAction && secondMethodAction) {
throw new Error(`${errorBeginning}but ${mainMethodToVerifyAsString}has never been called.`);
} else {
throw new Error(`${errorBeginning}but none of them has been called.`);
}
}
}