From 443b8b0c0917126ab6584b290a99dae26eb0f782 Mon Sep 17 00:00:00 2001 From: Daniel Holle Date: Wed, 20 Nov 2024 13:17:10 +0100 Subject: [PATCH] Test nested record matching, thankfully switch does the heavy lifting, fixes #349 --- .../javFiles/OverloadNestedPattern.jav | 20 +++++++++++++++++ src/test/java/TestComplete.java | 22 ++++++++++++++++++- 2 files changed, 41 insertions(+), 1 deletion(-) create mode 100644 resources/bytecode/javFiles/OverloadNestedPattern.jav diff --git a/resources/bytecode/javFiles/OverloadNestedPattern.jav b/resources/bytecode/javFiles/OverloadNestedPattern.jav new file mode 100644 index 00000000..77613284 --- /dev/null +++ b/resources/bytecode/javFiles/OverloadNestedPattern.jav @@ -0,0 +1,20 @@ +import java.lang.Object; +import java.lang.Integer; +import java.lang.Float; + +public record R(Object nested) {} + +public class OverloadNestedPattern { + + public Integer m(R(R(Integer a)), R(Integer b)) { + return 1; + } + + public Integer m(R(R(Float a)), R(Float b)) { + return 2; + } + + public Integer m(R(Integer a), R(Integer b)) { + return 3; + } +} \ No newline at end of file diff --git a/src/test/java/TestComplete.java b/src/test/java/TestComplete.java index ca18e748..0b1decb1 100644 --- a/src/test/java/TestComplete.java +++ b/src/test/java/TestComplete.java @@ -838,7 +838,6 @@ public class TestComplete { var instance = clazz.getDeclaredConstructor().newInstance(); } - @Ignore("Not implemented") @Test public void testOverloadPattern() throws Exception { var classFiles = generateClassFiles(new ByteArrayClassLoader(), "OverloadPattern.jav"); @@ -864,6 +863,27 @@ public class TestComplete { assertEquals(m3.invoke(instance, 10), 10); } + @Test + public void testOverloadNestedPattern() throws Exception { + var classFiles = generateClassFiles(new ByteArrayClassLoader(), "OverloadNestedPattern.jav"); + var clazz = classFiles.get("OverloadNestedPattern"); + var rec = classFiles.get("R"); + + var instance = clazz.getDeclaredConstructor().newInstance(); + var ctor = rec.getDeclaredConstructor(Object.class); + var r1 = ctor.newInstance(1); + var r2 = ctor.newInstance(r1); + + var m = clazz.getDeclaredMethod("m", rec, rec); + assertEquals(m.invoke(instance, r2, r1), 1); + + var r3 = ctor.newInstance(2f); + var r4 = ctor.newInstance(r3); + assertEquals(m.invoke(instance, r4, r3), 2); + + assertEquals(m.invoke(instance, r1, r1), 3); + } + //@Ignore("Not implemented") @Test public void testOverloadPatternMatching() throws Exception {