forked from mockito/mockito
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ArgumentMatcherStorageImpl.java
98 lines (74 loc) · 2.91 KB
/
ArgumentMatcherStorageImpl.java
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
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.progress;
import org.mockito.ArgumentMatcher;
import org.mockito.internal.matchers.And;
import org.mockito.internal.matchers.LocalizedMatcher;
import org.mockito.internal.matchers.Not;
import org.mockito.internal.matchers.Or;
import static java.util.Collections.emptyList;
import static org.mockito.internal.exceptions.Reporter.incorrectUseOfAdditionalMatchers;
import static org.mockito.internal.exceptions.Reporter.misplacedArgumentMatcher;
import static org.mockito.internal.exceptions.Reporter.reportNoSubMatchersFound;
import java.util.*;
public class ArgumentMatcherStorageImpl implements ArgumentMatcherStorage {
private static final int TWO_SUB_MATCHERS = 2;
private static final int ONE_SUB_MATCHER = 1;
private final Stack<LocalizedMatcher> matcherStack = new Stack<LocalizedMatcher>();
public void reportMatcher(ArgumentMatcher<?> matcher) {
matcherStack.push(new LocalizedMatcher(matcher));
}
public List<LocalizedMatcher> pullLocalizedMatchers() {
if (matcherStack.isEmpty()) {
return emptyList();
}
List<LocalizedMatcher> lastMatchers = resetStack();
return lastMatchers;
}
public void reportAnd() {
assertStateFor("And(?)", TWO_SUB_MATCHERS);
ArgumentMatcher<?> m1 = popMatcher();
ArgumentMatcher<?> m2 = popMatcher();
reportMatcher(new And(m1, m2));
}
public void reportOr() {
assertStateFor("Or(?)", TWO_SUB_MATCHERS);
ArgumentMatcher<?> m1 = popMatcher();
ArgumentMatcher<?> m2 = popMatcher();
reportMatcher(new Or(m1, m2));
}
public void reportNot() {
assertStateFor("Not(?)", ONE_SUB_MATCHER);
ArgumentMatcher<?> m = popMatcher();
reportMatcher(new Not(m));
}
public void validateState() {
if (!matcherStack.isEmpty()) {
List<LocalizedMatcher> lastMatchers = resetStack();
throw misplacedArgumentMatcher(lastMatchers);
}
}
public void reset() {
matcherStack.clear();
}
private void assertStateFor(String additionalMatcherName, int subMatchersCount) {
if (matcherStack.isEmpty()) {
throw reportNoSubMatchersFound(additionalMatcherName);
}
if (matcherStack.size() < subMatchersCount) {
List<LocalizedMatcher> lastMatchers = resetStack();
throw incorrectUseOfAdditionalMatchers(additionalMatcherName, subMatchersCount, lastMatchers);
}
}
@Override
public ArgumentMatcher<?> popMatcher() {
return matcherStack.pop().getMatcher();
}
private List<LocalizedMatcher> resetStack() {
ArrayList<LocalizedMatcher> lastMatchers = new ArrayList<LocalizedMatcher>(matcherStack);
reset();
return lastMatchers;
}
}