Skip to content

Commit

Permalink
Introduce additional Mockito Refaster rules
Browse files Browse the repository at this point in the history
  • Loading branch information
Stephan202 authored and rickie committed Oct 9, 2023
1 parent 629cb57 commit 2931c7d
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

import com.google.errorprone.refaster.Refaster;
import com.google.errorprone.refaster.annotation.AfterTemplate;
import com.google.errorprone.refaster.annotation.BeforeTemplate;
import com.google.errorprone.refaster.annotation.UseImportPolicy;
import org.mockito.Mockito;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.verification.VerificationMode;
import tech.picnic.errorprone.refaster.annotation.OnlineDocumentation;

Expand Down Expand Up @@ -50,4 +52,30 @@ T after(T mock) {
return verify(mock);
}
}

static final class InvocationOnMockGetArguments {
@BeforeTemplate
Object before(InvocationOnMock invocation, int i) {
return invocation.getArguments()[i];
}

@AfterTemplate
Object after(InvocationOnMock invocation, int i) {
return invocation.getArgument(i);
}
}

static final class InvocationOnMockGetArgumentsWithTypeParameter<T> {
@BeforeTemplate
@SuppressWarnings("unchecked")
T before(InvocationOnMock invocation, int i) {
return Refaster.anyOf(
invocation.getArgument(i, Refaster.<T>clazz()), (T) invocation.getArgument(i));
}

@AfterTemplate
T after(InvocationOnMock invocation, int i) {
return invocation.<T>getArgument(i);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import static org.mockito.Mockito.verify;

import com.google.common.collect.ImmutableSet;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.verification.VerificationMode;
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;

Expand All @@ -21,4 +22,14 @@ VerificationMode testNever() {
Object testVerifyOnce() {
return verify(mock(Object.class), times(1));
}

Object testInvocationOnMockGetArguments() {
return ((InvocationOnMock) null).getArguments()[0];
}

ImmutableSet<Number> testInvocationOnMockGetArgumentsWithTypeParameter() {
return ImmutableSet.of(
((InvocationOnMock) null).getArgument(0, Integer.class),
(Double) ((InvocationOnMock) null).getArgument(1));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import static org.mockito.Mockito.verify;

import com.google.common.collect.ImmutableSet;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.verification.VerificationMode;
import tech.picnic.errorprone.refaster.test.RefasterRuleCollectionTestCase;

Expand All @@ -22,4 +23,14 @@ VerificationMode testNever() {
Object testVerifyOnce() {
return verify(mock(Object.class));
}

Object testInvocationOnMockGetArguments() {
return ((InvocationOnMock) null).getArgument(0);
}

ImmutableSet<Number> testInvocationOnMockGetArgumentsWithTypeParameter() {
return ImmutableSet.of(
((InvocationOnMock) null).<Integer>getArgument(0),
((InvocationOnMock) null).<Double>getArgument(1));
}
}

0 comments on commit 2931c7d

Please sign in to comment.