Skip to content

Commit

Permalink
修复拦截either返回类的错误
Browse files Browse the repository at this point in the history
  • Loading branch information
leaderli committed Jun 11, 2024
1 parent 20f0e68 commit 18fcd93
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ static <L, R> Either<L, R> right(R right) {
* @param <R> 右边值的类型
* @return 一个{@link Left}对象,其值为null
*/
@SuppressWarnings("unchecked")
static <L, R> Either<L, R> none() {
return new Left<>(null);
return (Either<L, R>) Left.NONE;
}

/**
Expand Down Expand Up @@ -239,6 +240,7 @@ public String name() {
final class Left<L, R> implements Either<L, R> {


private static final Left<?, ?> NONE = new Left<>(null);
private final L value;


Expand Down
15 changes: 13 additions & 2 deletions litool-test/src/main/java/io/leaderli/litool/test/LiMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import io.leaderli.litool.core.collection.ArrayUtils;
import io.leaderli.litool.core.exception.LiAssertUtil;
import io.leaderli.litool.core.meta.Either;
import io.leaderli.litool.core.meta.LiTuple;
import io.leaderli.litool.core.meta.Lira;
import io.leaderli.litool.core.text.StrSubstitution;
Expand All @@ -26,6 +25,18 @@
import java.util.function.Consumer;

public class LiMock {

public static class Skip {
private Skip() {
}
}

/**
* 用于标记跳过mock
*/
public static Skip SKIP_MARK = new Skip();


public static final Map<Class<?>, byte[]> originClasses = new HashMap<>();
public static final ByteBuddy byteBuddy = new ByteBuddy();
public static Instrumentation instrumentation = ByteBuddyAgent.install();
Expand Down Expand Up @@ -191,7 +202,7 @@ public static void skipClassConstructors(Class<?> mockClass, boolean detach) {
* @see MethodValueRecorder#invoke(String, Class, String, Class[], Object[])
* @see MethodProxy#apply(Method, Object[]) 根据返回值来判断是否需要真正拦截,如果返回的是 {@link Either}判断是否有右值,
* 如果有则返回右值,否则不进行拦截。
* 函数如果判断不需要拦截直接返回{@link Either#none()}即可。如果方法本身返回的是{@link Either}需要额外对在函数中额外包一层{@link Either}
* 函数如果判断不需要拦截直接返回{@link #SKIP_MARK}即可。
*/
public static void mock(Class<?> mockClass, MethodFilter methodFilter, MethodProxy<?> methodProxy, boolean detach) {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package io.leaderli.litool.test;

import io.leaderli.litool.core.function.Filter;
import io.leaderli.litool.core.meta.Either;
import io.leaderli.litool.core.meta.LiTuple;
import io.leaderli.litool.core.type.PrimitiveEnum;

Expand Down Expand Up @@ -61,7 +60,8 @@ public Object getMethodValue(Object[] args) {
throw new RuntimeException(e);
}
}
return Either.none();
return LiMock.SKIP_MARK;
// return Either.none();
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,10 @@ public MethodValueRecorder(Class<?> mockClass) {
* @see PrimitiveEnum#zero_value
*/
public static Either<Void, Object> adjustReturnValue(Object returnValue, Type clazz, Type genericReturnType) {
if (returnValue instanceof Either) {
if (((Either<?, ?>) returnValue).isLeft()) {
return Either.none();
}
returnValue = ((Either<?, ?>) returnValue).getRight();
}

if (returnValue == LiMock.SKIP_MARK) {
return Either.none();
}
// 尝试将泛型返回类型解析为实际类型
Class<?> returnType = TypeUtil.erase(TypeUtil.resolve(clazz, genericReturnType));
if (returnValue == null) {
Expand Down
12 changes: 10 additions & 2 deletions litool-test/src/test/java/io/leaderli/litool/test/LiMockTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import io.leaderli.litool.core.io.IOUtils;
import io.leaderli.litool.core.meta.Either;
import io.leaderli.litool.core.meta.LiBox;
import io.leaderli.litool.core.meta.LiTuple;
import io.leaderli.litool.core.meta.Lira;
import io.leaderli.litool.core.type.LiTypeToken;
import io.leaderli.litool.core.type.MethodFilter;
Expand Down Expand Up @@ -131,9 +132,9 @@ public void testMockStatic() {
LiMock.mockStatic(Either1.class, MethodFilter.isMethod(), (m, args) -> {

if ((int) args[0] == 0) {
return Either.none();
return LiMock.SKIP_MARK;
}
return Either.right(Either.right(2));
return Either.right(2);
});
Assertions.assertEquals(1, Either1.m1(0).get());
Assertions.assertEquals(2, Either1.m1(3).get());
Expand Down Expand Up @@ -310,6 +311,10 @@ void testWhenBean2() {
Assertions.assertEquals(1, foo.m1());
LiMock.mockerBean(Bean1.class).when(Bean1::m1, 2).build();
Assertions.assertEquals(2, foo.m1());

LiMock.mockerBean(Bean1.class).function(Bean1::e1).other(LiTuple.of(null, 10)).build();
Assertions.assertEquals(10, foo.e1().get());

}

@ExtendWith(SkipWhenJacocoExecutionCondition.class)
Expand Down Expand Up @@ -748,6 +753,9 @@ int m1() {
return 1;
}

LiTuple<Void, Integer> e1() {
return LiTuple.of(null, 1);
}
void m2(int a) {
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ void testAdjustReturnValue() {
Assertions.assertEquals(Either.none(), MethodValueRecorder.adjustReturnValue("", null, int.class));
Assertions.assertEquals(1, MethodValueRecorder.adjustReturnValue(1, null, int.class).get());
Assertions.assertEquals(1, MethodValueRecorder.adjustReturnValue(1, null, Integer.class).get());
Assertions.assertEquals(0, MethodValueRecorder.adjustReturnValue(Either.right(null), null, Integer.class).get());
Assertions.assertEquals(0, MethodValueRecorder.adjustReturnValue(null, null, Integer.class).get());
Assertions.assertEquals(Either.none(), MethodValueRecorder.adjustReturnValue(Either.left(null), null, Integer.class));
}
}

0 comments on commit 18fcd93

Please sign in to comment.