From b0e9f6f7a5d4a1eeb6a70610ee04ba7add6f3bb5 Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Wed, 2 Mar 2016 23:28:13 +0300 Subject: [PATCH] Add test case for lazy-based conditional that ignores its result --- .../test/MetaprogrammingTest.java | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) diff --git a/tests/src/test/java/org/teavm/metaprogramming/test/MetaprogrammingTest.java b/tests/src/test/java/org/teavm/metaprogramming/test/MetaprogrammingTest.java index 0f601dcc4..337b35621 100644 --- a/tests/src/test/java/org/teavm/metaprogramming/test/MetaprogrammingTest.java +++ b/tests/src/test/java/org/teavm/metaprogramming/test/MetaprogrammingTest.java @@ -24,6 +24,7 @@ import static org.teavm.metaprogramming.Metaprogramming.emit; import static org.teavm.metaprogramming.Metaprogramming.exit; import static org.teavm.metaprogramming.Metaprogramming.findClass; import static org.teavm.metaprogramming.Metaprogramming.lazy; +import java.util.function.Consumer; import org.junit.Test; import org.junit.runner.RunWith; import org.teavm.junit.SkipJVM; @@ -350,6 +351,50 @@ public class MetaprogrammingTest { exit(() -> type.get()); } + @Test + public void conditionalActionWorks() { + class TypeConsumer implements Consumer { + String type; + @Override public void accept(String t) { + type = t; + } + } + TypeConsumer consumer = new TypeConsumer(); + + fieldType(Context.class, "a", consumer); + assertEquals("int", consumer.type); + + fieldType(Context.class, "b", consumer); + assertEquals("int", consumer.type); + + fieldType(Context.class, "c", consumer); + assertNull(consumer.type); + } + + @Meta + private static native void fieldType(Class cls, String name, Consumer typeConsumer); + private static void fieldType(ReflectClass cls, Value name, Value> typeConsumer) { + Value result = lazy(() -> { + typeConsumer.get().accept(null); + return null; + }); + for (ReflectField field : cls.getDeclaredFields()) { + String type = field.getType().getName(); + String fieldName = field.getName(); + Value existing = result; + result = lazy(() -> { + if (fieldName.equals(name.get())) { + typeConsumer.get().accept(type); + return null; + } else { + return existing.get(); + } + }); + } + Value type = result; + emit(() -> type); + } + static class Context { public int a; public int b;