Skip to content

Commit

Permalink
add recordbeans
Browse files Browse the repository at this point in the history
  • Loading branch information
leaderli committed Mar 4, 2024
1 parent a672543 commit 49154ee
Show file tree
Hide file tree
Showing 7 changed files with 136 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,21 +43,21 @@ public AbstractRecorder(Class<?> mockClass) {
public T called() {
actualMethodCall.remove(currentMethod);
recordMethodCall.add(currentMethod);
methodAsserts.get(currentMethod).add((method, _this, args, _return) -> actualMethodCall.add(currentMethod));
add((method, _this, args, _return) -> actualMethodCall.add(currentMethod));
return (T) this;
}


public T arg(int index, Object arg) {
methodAsserts.get(currentMethod).add((method, _this, args, _return) -> {
add((method, _this, args, _return) -> {
Assertions.assertTrue(args.length > index);
Assertions.assertEquals(arg, args[index]);
});
return (T) this;
}

public <R> T argAssert(int index, Consumer<R> argAssert, Class<R> paraType) {
methodAsserts.get(currentMethod).add((method, _this, args, _return) -> {
add((method, _this, args, _return) -> {
Assertions.assertTrue(args.length > index);
Assertions.assertEquals(ClassUtil.primitiveToWrapper(method.getParameterTypes()[index]), ClassUtil.primitiveToWrapper(paraType));
argAssert.accept((R) args[index]);
Expand All @@ -67,14 +67,21 @@ public <R> T argAssert(int index, Consumer<R> argAssert, Class<R> paraType) {


public T args(Object... compareArgs) {
methodAsserts.get(currentMethod).add((method, _this, args, _return) -> Assertions.assertArrayEquals(compareArgs, args));
add((method, _this, args, _return) -> Assertions.assertArrayEquals(compareArgs, args));
return (T) this;
}

public T assertReturn(Object compareReturn) {
methodAsserts.get(currentMethod).add((method, _this, args, _return) -> Assertions.assertEquals(compareReturn, _return));
add((method, _this, args, _return) -> Assertions.assertEquals(compareReturn, _return));
return (T) this;
}

protected void add(MethodAssert methodAssert) {
if (currentMethod == null) {
throw new IllegalStateException("method may not called , or it's abstract");
}
methodAsserts.get(currentMethod).add(methodAssert);
}

}

Original file line number Diff line number Diff line change
Expand Up @@ -394,10 +394,14 @@ public static Recorder recorder(Class<?> mockClass) {
return new Recorder(mockClass);
}

public static <T> RecordBean<T> recordBean(Class<T> mockClass) {
public static <T> RecordBeanInterface<RecordBean<T>, T> recordBean(Class<T> mockClass) {
return new RecordBean<>(mockClass);
}

@SafeVarargs
public static <T> RecordBeanInterface<RecordBeans<T>, T> recordBeans(Class<? extends T>... mockClasses) {
return new RecordBeans<>(mockClasses);
}
public static Mocker mocker(Class<?> mockClass) {
return new Mocker(mockClass, true);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ public MockBeans(Class<? extends T>... mockClasses) {
}

@Override
public MockBeans<T> consume(Consumer<T> call) {
mockBeans.forEach(mockBean -> mockBean.consume(call));
public MockBeans<T> consume(Consumer<T> consumer) {
mockBeans.forEach(mockBean -> mockBean.consume(consumer));
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import java.util.function.Consumer;
import java.util.function.Function;

public class RecordBean<T> extends AbstractRecorder<RecordBean<T>> {
public class RecordBean<T> extends AbstractRecorder<RecordBean<T>> implements RecordBeanInterface<RecordBean<T>, T> {

private final T instance;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.leaderli.litool.test;

import java.util.function.Consumer;
import java.util.function.Function;

public interface RecordBeanInterface<B, T> {

B consume(Consumer<T> call);

B when(Function<T, Object> call);

B called();

B arg(int index, Object arg);

B args(Object... args);

B assertReturn(Object compareReturn);

T build();
}

68 changes: 68 additions & 0 deletions litool-test/src/main/java/io/leaderli/litool/test/RecordBeans.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package io.leaderli.litool.test;

import io.leaderli.litool.core.exception.LiAssertUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Consumer;
import java.util.function.Function;

public class RecordBeans<T> implements RecordBeanInterface<RecordBeans<T>, T> {
private final List<RecordBean<T>> recordBeans = new ArrayList<>();
private final List<Consumer<RecordBean<T>>> recordBeanAction = new ArrayList<>();


@SuppressWarnings("unchecked")
public RecordBeans(Class<? extends T>... mockClasses) {

for (Class<? extends T> mockClass : mockClasses) {
recordBeans.add(new RecordBean<>((Class<T>) mockClass));
}
}

public RecordBeans<T> consume(Consumer<T> consumer) {
recordBeanAction.add(recordbean -> recordbean.consume(consumer));
return this;
}

public RecordBeans<T> when(Function<T, Object> function) {
recordBeanAction.add(recordbean -> recordbean.when(function));
return this;
}

@Override
public RecordBeans<T> called() {
recordBeanAction.add(AbstractRecorder::called);
return this;
}

@Override
public RecordBeans<T> arg(int index, Object arg) {
recordBeanAction.add(recordbean -> recordbean.arg(index, arg));
return this;
}

@Override
public RecordBeans<T> args(Object... args) {
recordBeanAction.add(recordbean -> recordbean.args(args));
return this;
}

@Override
public RecordBeans<T> assertReturn(Object compareReturn) {
recordBeanAction.add(recordbean -> recordbean.assertReturn(compareReturn));
return this;
}

public T build() {
T instance = null;
for (RecordBean<T> mockBean : recordBeans) {
recordBeanAction.forEach(action -> action.accept(mockBean));
instance = mockBean.build();
}
LiAssertUtil.assertFalse(instance == null, new IllegalArgumentException());
return instance;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ void testWhenBean3() {

@Test
@ExtendWith(SkipWhenJacocoExecutionCondition.class)
void testWhenBeans() {
void testMockerBeans() {

Supplier<Integer> supplier = LiMock.mockerBeans(Supplier1.class, Supplier2.class).other(Supplier::get, (m, a) -> 100).build();
Assertions.assertEquals(100, supplier.get());
Expand All @@ -258,6 +258,18 @@ void testWhenBeans() {
//
}

@Test
@ExtendWith(SkipWhenJacocoExecutionCondition.class)
void testRecordBeans() {

Supplier<Integer> supplier = LiMock.mockerBeans(Supplier1.class, Supplier2.class).other(Supplier::get, (m, a) -> 100).build();
LiMock.recordBeans(Supplier1.class, Supplier2.class).consume(Supplier::get).assertReturn(100).build();
Assertions.assertEquals(100, supplier.get());
Assertions.assertEquals(100, new Supplier1().get());
Assertions.assertEquals(100, new Supplier2().get());
//
}

@SuppressWarnings({"rawtypes", "unchecked"})
@Test
void testWhenInterface() {
Expand Down Expand Up @@ -420,6 +432,19 @@ void testDelegate() {
Assertions.assertEquals(2, new Foo().m1());
}

@Test
void testGenerate() {

System.out.println(LiMock.getCtClass(Bean2.class).getRefClasses());

Bean2 bean2 = LiMock.mockerBean(Bean2.class).when(b -> b.m1(1), "2").build();
LiMock.recordBean(Bean2.class).when(b -> b.m1(1)).called().build();

bean2.m1(1);


}

static class Delegate {
public static int m1() {
return 2;
Expand Down

0 comments on commit 49154ee

Please sign in to comment.