Skip to content

Commit

Permalink
Minor polish
Browse files Browse the repository at this point in the history
  • Loading branch information
timtebeek committed Oct 11, 2024
1 parent 2137491 commit 9ddffbb
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,7 @@ class ArgumentMatchersRewriter {
J.Block rewriteJMockitBlock() {
List<Statement> newStatements = new ArrayList<>(expectationsBlock.getStatements().size());
for (Statement expectationStatement : expectationsBlock.getStatements()) {
// for each statement, check if it's a method invocation and replace any
// argument matchers
// for each statement, check if it's a method invocation and replace any argument matchers
if (!(expectationStatement instanceof J.MethodInvocation)) {
newStatements.add(expectationStatement);
continue;
Expand All @@ -108,15 +107,15 @@ private J.MethodInvocation rewriteMethodInvocation(J.MethodInvocation invocation
if (arg instanceof J.FieldAccess) {
J.FieldAccess fieldAccess = (J.FieldAccess) arg;
if (fieldAccess.getTarget() instanceof J.Identifier &&
((J.Identifier) fieldAccess.getTarget()).getSimpleName().equals("this")) {
"this".equals(((J.Identifier) fieldAccess.getTarget()).getSimpleName())) {
return fieldAccess.getName();
}
}
return arg;
});

// if there are no argument matchers, return the invocation as-is
if (!arguments.stream().anyMatch(arg -> isJmockitArgumentMatcher(arg))) {
if (arguments.stream().noneMatch(ArgumentMatchersRewriter::isJmockitArgumentMatcher)) {
return invocation;
}
// replace each argument with the appropriate argument matcher
Expand Down Expand Up @@ -193,19 +192,17 @@ private Expression rewriteAnyWithClassParameterToArgumentMatcher(Expression meth
String template;
List<Object> templateParams = new ArrayList<>();

String argumentMatcher = null;

if (type instanceof JavaType.FullyQualified) {
JavaType.FullyQualified fq = (JavaType.FullyQualified) type;
argumentMatcher = FQN_TO_MOCKITO_ARGUMENT_MATCHER.get(fq.getFullyQualifiedName());
String argumentMatcher = FQN_TO_MOCKITO_ARGUMENT_MATCHER.get(fq.getFullyQualifiedName());
if (argumentMatcher != null) {
// mockito has convenience argument matchers
template = argumentMatcher + "()";
return applyArgumentTemplate(methodArgument, argumentMatcher, template, templateParams);
}
}
// mockito uses any(Class) for all other types
argumentMatcher = "any";
String argumentMatcher = "any";
template = argumentMatcher + "(#{any(java.lang.Class)})";

if (type instanceof JavaType.FullyQualified) {
Expand Down Expand Up @@ -235,7 +232,6 @@ private Expression rewriteAnyWithClassParameterToArgumentMatcher(Expression meth
}

private Expression applyArrayClassArgumentTemplate(Expression methodArgument, JavaType elementType) {

String newArrayElementClassName = "";
if (elementType instanceof JavaType.FullyQualified) {
newArrayElementClassName = ((JavaType.FullyQualified) elementType).getClassName();
Expand All @@ -245,13 +241,13 @@ private Expression applyArrayClassArgumentTemplate(Expression methodArgument, Ja
newArrayElementClassName = elementType.getClass().getName();
}

return ((Expression) JavaTemplate.builder("#{}[].class")
return JavaTemplate.builder("#{}[].class")
.javaParser(JavaParser.fromJavaVersion())
.build()
.apply(
new Cursor(visitor.getCursor(), methodArgument),
methodArgument.getCoordinates().replace(),
newArrayElementClassName));
newArrayElementClassName);
}

private static boolean isJmockitArgumentMatcher(Expression expression) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ void test() {
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.when;
Expand Down Expand Up @@ -1083,7 +1083,7 @@ void whenMinTimes() {
class MyTest {
@Mocked
Object myObject;
void test() {
new Expectations() {{
myObject.wait(anyLong, anyInt);
Expand All @@ -1104,7 +1104,7 @@ void test() {
class MyTest {
@Mock
Object myObject;
void test() {
myObject.wait(10L, 10);
verify(myObject, atLeast(2)).wait(anyLong(), anyInt());
Expand All @@ -1130,7 +1130,7 @@ void whenMaxTimes() {
class MyTest {
@Mocked
Object myObject;
void test() {
new Expectations() {{
myObject.wait(anyLong, anyInt);
Expand All @@ -1151,7 +1151,7 @@ void test() {
class MyTest {
@Mock
Object myObject;
void test() {
myObject.wait(10L, 10);
verify(myObject, atMost(5)).wait(anyLong(), anyInt());
Expand All @@ -1177,7 +1177,7 @@ void whenMinTimesMaxTimes() {
class MyTest {
@Mocked
Object myObject;
void test() {
new Expectations() {{
myObject.wait(anyLong, anyInt);
Expand All @@ -1199,7 +1199,7 @@ void test() {
class MyTest {
@Mock
Object myObject;
void test() {
myObject.wait(10L, 10);
verify(myObject, atLeast(1)).wait(anyLong(), anyInt());
Expand Down Expand Up @@ -1500,7 +1500,7 @@ void test() {
}

@Test
void whenMultipleExpectationsNoResults() {
void whenMultipleExpectationsNoResults() {
//language=java
rewriteRun(
java(
Expand Down Expand Up @@ -1558,7 +1558,7 @@ void test() {
}

@Test
void whenWithRedundantThisModifier() {
void whenWithRedundantThisModifier() {
//language=java
rewriteRun(
java(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -894,5 +894,4 @@ void test() {
)
);
}

}

0 comments on commit 9ddffbb

Please sign in to comment.