From 18fcd93840f7d0d42d9fef67bda185c39c3a871d Mon Sep 17 00:00:00 2001 From: leaderli <429243408@qq.com> Date: Tue, 11 Jun 2024 19:51:29 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E6=8B=A6=E6=88=AAeither?= =?UTF-8?q?=E8=BF=94=E5=9B=9E=E7=B1=BB=E7=9A=84=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/io/leaderli/litool/core/meta/Either.java | 4 +++- .../main/java/io/leaderli/litool/test/LiMock.java | 15 +++++++++++++-- .../java/io/leaderli/litool/test/MethodValue.java | 4 ++-- .../leaderli/litool/test/MethodValueRecorder.java | 9 +++------ .../java/io/leaderli/litool/test/LiMockTest.java | 12 ++++++++++-- .../litool/test/MethodValueRecorderTest.java | 2 +- 6 files changed, 32 insertions(+), 14 deletions(-) diff --git a/litool-core/src/main/java/io/leaderli/litool/core/meta/Either.java b/litool-core/src/main/java/io/leaderli/litool/core/meta/Either.java index aa953471..fa94e2ff 100644 --- a/litool-core/src/main/java/io/leaderli/litool/core/meta/Either.java +++ b/litool-core/src/main/java/io/leaderli/litool/core/meta/Either.java @@ -34,8 +34,9 @@ static Either right(R right) { * @param 右边值的类型 * @return 一个{@link Left}对象,其值为null */ + @SuppressWarnings("unchecked") static Either none() { - return new Left<>(null); + return (Either) Left.NONE; } /** @@ -239,6 +240,7 @@ public String name() { final class Left implements Either { + private static final Left NONE = new Left<>(null); private final L value; diff --git a/litool-test/src/main/java/io/leaderli/litool/test/LiMock.java b/litool-test/src/main/java/io/leaderli/litool/test/LiMock.java index 731dc63a..c7aa0ad8 100644 --- a/litool-test/src/main/java/io/leaderli/litool/test/LiMock.java +++ b/litool-test/src/main/java/io/leaderli/litool/test/LiMock.java @@ -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; @@ -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, byte[]> originClasses = new HashMap<>(); public static final ByteBuddy byteBuddy = new ByteBuddy(); public static Instrumentation instrumentation = ByteBuddyAgent.install(); @@ -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 { diff --git a/litool-test/src/main/java/io/leaderli/litool/test/MethodValue.java b/litool-test/src/main/java/io/leaderli/litool/test/MethodValue.java index 68d3a3c1..a0ddbb54 100644 --- a/litool-test/src/main/java/io/leaderli/litool/test/MethodValue.java +++ b/litool-test/src/main/java/io/leaderli/litool/test/MethodValue.java @@ -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; @@ -61,7 +60,8 @@ public Object getMethodValue(Object[] args) { throw new RuntimeException(e); } } - return Either.none(); + return LiMock.SKIP_MARK; +// return Either.none(); } diff --git a/litool-test/src/main/java/io/leaderli/litool/test/MethodValueRecorder.java b/litool-test/src/main/java/io/leaderli/litool/test/MethodValueRecorder.java index 8efb3d4c..1d979b1d 100644 --- a/litool-test/src/main/java/io/leaderli/litool/test/MethodValueRecorder.java +++ b/litool-test/src/main/java/io/leaderli/litool/test/MethodValueRecorder.java @@ -43,13 +43,10 @@ public MethodValueRecorder(Class mockClass) { * @see PrimitiveEnum#zero_value */ public static Either 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) { diff --git a/litool-test/src/test/java/io/leaderli/litool/test/LiMockTest.java b/litool-test/src/test/java/io/leaderli/litool/test/LiMockTest.java index d97a7b29..774126b2 100644 --- a/litool-test/src/test/java/io/leaderli/litool/test/LiMockTest.java +++ b/litool-test/src/test/java/io/leaderli/litool/test/LiMockTest.java @@ -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; @@ -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()); @@ -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) @@ -748,6 +753,9 @@ int m1() { return 1; } + LiTuple e1() { + return LiTuple.of(null, 1); + } void m2(int a) { } } diff --git a/litool-test/src/test/java/io/leaderli/litool/test/MethodValueRecorderTest.java b/litool-test/src/test/java/io/leaderli/litool/test/MethodValueRecorderTest.java index c3dc1db1..414c97d9 100644 --- a/litool-test/src/test/java/io/leaderli/litool/test/MethodValueRecorderTest.java +++ b/litool-test/src/test/java/io/leaderli/litool/test/MethodValueRecorderTest.java @@ -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)); } }