use targetClass in demo

This commit is contained in:
金戟 2021-01-06 00:23:46 +08:00
parent 0a1b46b352
commit 31a0b49b7a
3 changed files with 31 additions and 31 deletions

View File

@ -25,38 +25,38 @@ class DemoMockTest {
return new BlackBox("mock_" + text); return new BlackBox("mock_" + text);
} }
@MockMethod @MockMethod(targetClass = DemoMock.class)
private String innerFunc(DemoMock self, String text) { private String innerFunc(String text) {
return "mock_" + text; return "mock_" + text;
} }
@MockMethod @MockMethod(targetClass = DemoMock.class)
private String staticFunc(DemoMock ignore) { private String staticFunc() {
return "_MOCK_TAIL"; return "_MOCK_TAIL";
} }
@MockMethod @MockMethod(targetClass = String.class)
private String trim(String self) { private String trim() {
return "trim_string"; return "trim_string";
} }
@MockMethod(targetMethod = "substring") @MockMethod(targetClass = String.class, targetMethod = "substring")
private String sub(String self, int i, int j) { private String sub(int i, int j) {
return "sub_string"; return "sub_string";
} }
@MockMethod @MockMethod(targetClass = String.class)
private boolean startsWith(String self, String s) { private boolean startsWith(String s) {
return false; return false;
} }
@MockMethod @MockMethod(targetClass = BlackBox.class)
private BlackBox secretBox(BlackBox ignore) { private BlackBox secretBox() {
return new BlackBox("not_secret_box"); return new BlackBox("not_secret_box");
} }
@MockMethod @MockMethod(targetClass = DemoMock.class)
private String callFromDifferentMethod(DemoMock self) { private String callFromDifferentMethod() {
if ("special_case".equals(MOCK_CONTEXT.get("case"))) { if ("special_case".equals(MOCK_CONTEXT.get("case"))) {
return "mock_special"; return "mock_special";
} }

View File

@ -22,35 +22,35 @@ internal class DemoMockTest {
@MockConstructor @MockConstructor
private fun createBlackBox(text: String) = BlackBox("mock_$text") private fun createBlackBox(text: String) = BlackBox("mock_$text")
@MockMethod @MockMethod(targetClass = DemoMock::class)
private fun innerFunc(self: DemoMock, text: String) = "mock_$text" private fun innerFunc(text: String) = "mock_$text"
@MockMethod @MockMethod(targetClass = DemoMock::class)
private fun staticFunc(ignore: DemoMock): String { private fun staticFunc(): String {
return "_MOCK_TAIL"; return "_MOCK_TAIL";
} }
@MockMethod @MockMethod(targetClass = BlackBox::class)
private fun trim(self: BlackBox) = "trim_string" private fun trim() = "trim_string"
@MockMethod(targetMethod = "substring") @MockMethod(targetClass = BlackBox::class, targetMethod = "substring")
private fun sub(self: BlackBox, i: Int, j: Int) = "sub_string" private fun sub(i: Int, j: Int) = "sub_string"
@MockMethod @MockMethod(targetClass = BlackBox::class)
private fun startsWith(self: BlackBox, s: String) = false private fun startsWith(s: String) = false
@MockMethod @MockMethod(targetClass = BlackBox::class)
private fun secretBox(ignore: BlackBox): BlackBox { private fun secretBox(): BlackBox {
return BlackBox("not_secret_box") return BlackBox("not_secret_box")
} }
@MockMethod @MockMethod(targetClass = ColorBox::class)
private fun createBox(ignore: ColorBox, color: String, box: BlackBox): BlackBox { private fun createBox(color: String, box: BlackBox): BlackBox {
return BlackBox("White_${box.get()}") return BlackBox("White_${box.get()}")
} }
@MockMethod @MockMethod(targetClass = DemoMock::class)
private fun callFromDifferentMethod(self: DemoMock): String { private fun callFromDifferentMethod(): String {
return if (MOCK_CONTEXT["case"] == "special_case") { return if (MOCK_CONTEXT["case"] == "special_case") {
"mock_special" "mock_special"
} else { } else {

View File

@ -174,7 +174,7 @@ public class TestClassHandler extends BaseClassHandler {
List<Byte> types = ClassUtil.getParameterTypes(mn.desc); List<Byte> types = ClassUtil.getParameterTypes(mn.desc);
int size = types.size(); int size = types.size();
int parameterOffset = 0; int parameterOffset = 0;
mn.maxStack += 1; mn.maxStack += 2;
il.add(getIntInsn(size)); il.add(getIntInsn(size));
il.add(new TypeInsnNode(ANEWARRAY, ClassUtil.CLASS_OBJECT)); il.add(new TypeInsnNode(ANEWARRAY, ClassUtil.CLASS_OBJECT));
for (int i = 0; i < size; i++) { for (int i = 0; i < size; i++) {