Skip to content

Commit 75e0bb3

Browse files
refactor: Make Mocked* more concise
1 parent c03d23f commit 75e0bb3

1 file changed

Lines changed: 7 additions & 52 deletions

File tree

src/test/java/net/marcellperger/mathexpr/MiniMock.java

Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,15 @@
44
import org.jetbrains.annotations.Contract;
55
import org.jetbrains.annotations.NotNull;
66
import org.jetbrains.annotations.Unmodifiable;
7+
import org.junit.jupiter.api.Assertions;
78

89
import java.util.*;
910
import java.util.function.Consumer;
1011
import java.util.function.Function;
1112
import java.util.function.Predicate;
1213
import java.util.function.Supplier;
1314

14-
import org.junit.jupiter.api.Assertions;
15-
1615
public class MiniMock {
17-
enum ReturnStrategy {
18-
VALUE, FUNC
19-
}
20-
2116
public static class BaseMockedCallable<A> {
2217
List<A> calls;
2318

@@ -42,97 +37,57 @@ public void reset() {
4237
}
4338
}
4439

45-
// TODO there is SO much shared code between these Mocked* classes
4640
public static class MockedSupplier<R> extends BaseMockedCallable<VoidVal> implements Supplier<R> {
47-
// Option<T> would be so useful right now
48-
R returnValue;
4941
Supplier<R> returnSupplier;
50-
ReturnStrategy returnStrategy;
5142

5243
public MockedSupplier(R returnValue_) {
53-
calls = new ZST_List();
54-
returnValue = returnValue_;
55-
returnStrategy = ReturnStrategy.VALUE;
44+
this(() -> returnValue_);
5645
}
5746
public MockedSupplier(Supplier<R> returnSupplier_) {
5847
calls = new ZST_List();
5948
returnSupplier = returnSupplier_;
60-
returnStrategy = ReturnStrategy.FUNC;
61-
}
62-
63-
protected R getReturn() {
64-
return switch (returnStrategy) {
65-
case VALUE -> returnValue;
66-
case FUNC -> returnSupplier.get();
67-
};
6849
}
6950

7051
@Override
7152
public R get() {
7253
handleCall(VoidVal.val());
73-
return getReturn();
54+
return returnSupplier.get();
7455
}
7556
}
7657

7758
public static class MockedFunction<T, R> extends BaseMockedCallable<T> implements Function<T, R> {
78-
// Option<T> would be so useful right now
79-
R returnValue;
8059
Function<? super T, ? extends R> returnFunc;
81-
ReturnStrategy returnStrategy;
8260

8361
public MockedFunction(R returnValue_) {
84-
calls = new ArrayList<>();
85-
returnValue = returnValue_;
86-
returnStrategy = ReturnStrategy.VALUE;
62+
this((_v) -> returnValue_);
8763
}
8864
public MockedFunction(Function<? super T, ? extends R> returnSupplier_) {
8965
calls = new ArrayList<>();
9066
returnFunc = returnSupplier_;
91-
returnStrategy = ReturnStrategy.FUNC;
92-
}
93-
94-
protected R getReturn(T arg) {
95-
return switch (returnStrategy) {
96-
case VALUE -> returnValue;
97-
case FUNC -> returnFunc.apply(arg);
98-
};
9967
}
10068

10169
@Override
10270
public R apply(T arg) {
10371
handleCall(arg);
104-
return getReturn(arg);
72+
return returnFunc.apply(arg);
10573
}
10674
}
10775

10876
public static class MockedPredicate<T> extends BaseMockedCallable<T> implements Predicate<T> {
109-
// Option<T> would be so useful right now
110-
boolean returnValue;
11177
Predicate<? super T> returnFunc;
112-
ReturnStrategy returnStrategy;
11378

11479
public MockedPredicate(boolean returnValue_) {
115-
calls = new ArrayList<>();
116-
returnValue = returnValue_;
117-
returnStrategy = ReturnStrategy.VALUE;
80+
this((_v) -> returnValue_);
11881
}
11982
public MockedPredicate(Predicate<? super T> returnSupplier_) {
12083
calls = new ArrayList<>();
12184
returnFunc = returnSupplier_;
122-
returnStrategy = ReturnStrategy.FUNC;
123-
}
124-
125-
protected boolean getReturn(T arg) {
126-
return switch (returnStrategy) {
127-
case VALUE -> returnValue;
128-
case FUNC -> returnFunc.test(arg);
129-
};
13085
}
13186

13287
@Override
13388
public boolean test(T arg) {
13489
handleCall(arg);
135-
return getReturn(arg);
90+
return returnFunc.test(arg);
13691
}
13792
}
13893

0 commit comments

Comments
 (0)