Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Introduce additional Mockito Refaster rules #777

Merged
merged 1 commit into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wanted to add this one, just to see this PR already open 😄🚀

}
}

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));
}
}
Loading