From 5cc3b2cdb6309f6e145cf6757ac4f20516aa1a01 Mon Sep 17 00:00:00 2001 From: Yegor Bugayenko Date: Wed, 25 May 2022 12:34:04 +0300 Subject: [PATCH] #623 fixed --- .../src/main/java/org/eolang/PhDefault.java | 19 ++-- .../test/java/org/eolang/AtVarargTest.java | 89 +++++++++++++++++++ .../test/java/org/eolang/DataizedTest.java | 81 ----------------- 3 files changed, 103 insertions(+), 86 deletions(-) diff --git a/eo-runtime/src/main/java/org/eolang/PhDefault.java b/eo-runtime/src/main/java/org/eolang/PhDefault.java index 7400f44bc7..fcc9d1f492 100644 --- a/eo-runtime/src/main/java/org/eolang/PhDefault.java +++ b/eo-runtime/src/main/java/org/eolang/PhDefault.java @@ -194,11 +194,20 @@ public final Attr attr(final int pos) { ) ); } - final int idx; - if (pos >= this.order.size()) { - idx = this.order.size() - 1; - } else { - idx = pos; + int idx; + for (idx = 0; idx < pos; ++idx) { + if (idx >= this.order.size()) { + throw new ExFailure( + String.format( + "There are just %d attributes here, can't read the %d-th one", + this.order.size(), pos + ) + ); + } + final String name = this.order.get(idx); + if (this.attrs.get(name) instanceof AtVararg) { + break; + } } return this.attr(this.order.get(idx)); } diff --git a/eo-runtime/src/test/java/org/eolang/AtVarargTest.java b/eo-runtime/src/test/java/org/eolang/AtVarargTest.java index eacaf4a7bc..c9398869e7 100644 --- a/eo-runtime/src/test/java/org/eolang/AtVarargTest.java +++ b/eo-runtime/src/test/java/org/eolang/AtVarargTest.java @@ -47,4 +47,93 @@ public void appendsElements() { ); } + @Test + public void injectsVarargs() { + final Phi bar = new AtVarargTest.Bar(Phi.Φ); + bar.attr(0).put(new Data.ToPhi(1L)); + bar.attr(1).put(new Data.ToPhi(1L)); + MatcherAssert.assertThat( + new Dataized( + bar.attr("a").get() + ).take(Long.class), + Matchers.is(1L) + ); + } + + @Test + public void datarizesEOappCallingVarargsFunc() { + MatcherAssert.assertThat( + new Dataized( + new AtVarargTest.Foo(Phi.Φ) + ).take(Boolean.class), + Matchers.is(true) + ); + } + + /** + * Main program sample. + * + * {@code + * [] > foo + * (bar 1 2 3).eq 2 > @ + * } + * + * @since 0.22 + */ + private static class Foo extends PhDefault { + Foo(final Phi sigma) { + super(sigma); + this.add( + "φ", + new AtOnce( + new AtComposite( + this, + rho -> { + Phi phi = new PhWith( + new PhCopy(new AtVarargTest.Bar(rho)), + 0, new Data.ToPhi(1L) + ); + phi = new PhWith(phi, 1, new Data.ToPhi(2L)); + phi = new PhWith(phi, 2, new Data.ToPhi(3L)); + return new PhWith( + new PhCopy(new PhMethod(phi, "eq")), + 0, new Data.ToPhi(2L) + ); + } + ) + ) + ); + } + } + + /** + * Varargs function sample. + * + * {@code + * [args...] > bar + * 1 > a + * 2 > @ + * } + * + * @since 0.22 + */ + private static class Bar extends PhDefault { + Bar(final Phi sigma) { + super(sigma); + this.add("args", new AtVararg()); + this.add( + "a", + new AtOnce( + new AtComposite(this, rho -> new Data.ToPhi(1L)) + ) + ); + this.add( + "φ", + new AtOnce( + new AtComposite(this, rho -> new Data.ToPhi(2L)) + ) + ); + } + } + } diff --git a/eo-runtime/src/test/java/org/eolang/DataizedTest.java b/eo-runtime/src/test/java/org/eolang/DataizedTest.java index 9087d0cf56..3c01021a87 100644 --- a/eo-runtime/src/test/java/org/eolang/DataizedTest.java +++ b/eo-runtime/src/test/java/org/eolang/DataizedTest.java @@ -31,7 +31,6 @@ import java.util.logging.Logger; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.parallel.Execution; import org.junit.jupiter.api.parallel.ExecutionMode; @@ -77,84 +76,4 @@ public void close() throws SecurityException { ); } - /** - * Try to datarize an EO app that calls a varargs func. - * @todo #414:30min Fix bug execute an EO program calling a varargs func. - * We reproduced by a test bug during execution with exception `You can't overwrite X` - * when EO app try to call a function that uses varargs as parameter. Now, we must fix it - * and disable the test (test below). - */ - @Test - @Disabled - public void datarizesEOappCallingVarargsFunc() { - MatcherAssert.assertThat( - new Dataized( - new EOappCallingVarargsFunc(Phi.Φ) - ).take(Long.class), - Matchers.is(2L) - ); - } - - /** - * Main program sample. - * {@code - * [] > app - * (f 1 2 3).eq 2 > @ - * } - * @since 0.22 - */ - private static class EOappCallingVarargsFunc extends PhDefault { - public EOappCallingVarargsFunc(Phi sigma) { - super(sigma); - this.add( - "φ", - new AtOnce( - new AtComposite( - this, - (rho) -> { - Phi fvar = new PhCopy(new DataizedTest.Fvarargs(rho)); - Phi var1 = new Data.ToPhi(1L); - Phi var2 = new Data.ToPhi(2L); - Phi var3 = new Data.ToPhi(3L); - Phi fvard = new PhWith(fvar, 0, var1); - fvard = new PhWith(fvard, 1, var2); - fvard = new PhWith(fvard, 2, var3); - return new PhWith( - new PhCopy(new PhMethod(fvard, "eq")), 0, new Data.ToPhi(2L) - ); - } - ) - ) - ); - } - } - - /** - * Varargs function sample. - * {@code - * [args] > f - * 1 > a - * 2 > @ - * } - * @since 0.22 - */ - @SuppressWarnings("unchecked") - private static class Fvarargs extends PhDefault { - public Fvarargs(final Phi sigma) { - super(sigma); - this.add("args", new AtVararg()); - this.add( - "a", - new AtOnce( - new AtComposite(this, (rho) -> new Data.ToPhi(1L)) - ) - ); - this.add( - "φ", - new AtOnce( - new AtComposite(this, (rho) -> new Data.ToPhi(2L)) - ) - ); - } - } }