-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
MatchersPrinter.java
59 lines (52 loc) · 2.42 KB
/
MatchersPrinter.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
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.matchers.text;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.mockito.ArgumentMatcher;
import org.mockito.internal.matchers.ContainsExtraTypeInfo;
import org.mockito.internal.reporting.PrintSettings;
@SuppressWarnings("unchecked")
public class MatchersPrinter {
public String getArgumentsLine(List<ArgumentMatcher> matchers, PrintSettings printSettings) {
Iterator args = applyPrintSettings(matchers, printSettings);
return ValuePrinter.printValues("(", ", ", ");", args);
}
public String getArgumentsBlock(List<ArgumentMatcher> matchers, PrintSettings printSettings) {
Iterator args = applyPrintSettings(matchers, printSettings);
return ValuePrinter.printValues("(\n ", ",\n ", "\n);", args);
}
private Iterator<FormattedText> applyPrintSettings(
List<ArgumentMatcher> matchers, PrintSettings printSettings) {
List<FormattedText> out = new LinkedList<>();
int i = 0;
for (final ArgumentMatcher matcher : matchers) {
if (matcher instanceof ContainsExtraTypeInfo) {
ContainsExtraTypeInfo typeInfoMatcher = (ContainsExtraTypeInfo) matcher;
Object wanted = typeInfoMatcher.getWanted();
String simpleNameOfArgument =
wanted != null ? wanted.getClass().getSimpleName() : "";
String fullyQualifiedClassName =
wanted != null ? wanted.getClass().getCanonicalName() : "";
if (printSettings.extraTypeInfoFor(i)) {
out.add(
new FormattedText(
typeInfoMatcher.toStringWithType(simpleNameOfArgument)));
} else if (printSettings.fullyQualifiedNameFor(simpleNameOfArgument)) {
out.add(
new FormattedText(
typeInfoMatcher.toStringWithType(fullyQualifiedClassName)));
} else {
out.add(new FormattedText(MatcherToString.toString(matcher)));
}
} else {
out.add(new FormattedText(MatcherToString.toString(matcher)));
}
i++;
}
return out.iterator();
}
}