From c7a271a581272e77fc41179cf381feff9723632a Mon Sep 17 00:00:00 2001 From: Suban05 Date: Fri, 21 Feb 2025 15:33:49 +0500 Subject: [PATCH 1/4] #3542 output a clearer message when too many attributes are passed --- .../src/main/java/org/eolang/AtWithRho.java | 8 ++++ .../src/main/java/org/eolang/PhDefault.java | 42 ++++++++++++++++++- .../test/java/org/eolang/PhDefaultTest.java | 14 +++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/eo-runtime/src/main/java/org/eolang/AtWithRho.java b/eo-runtime/src/main/java/org/eolang/AtWithRho.java index bb25fffa51..30f514ace3 100644 --- a/eo-runtime/src/main/java/org/eolang/AtWithRho.java +++ b/eo-runtime/src/main/java/org/eolang/AtWithRho.java @@ -71,4 +71,12 @@ public Phi get() { public void put(final Phi phi) { this.origin.put(phi); } + + /** + * Returns the original attribute. + * @return The original attribute + */ + public Attr getOrigin() { + return this.origin; + } } diff --git a/eo-runtime/src/main/java/org/eolang/PhDefault.java b/eo-runtime/src/main/java/org/eolang/PhDefault.java index 4de4f93877..d05ad51c8d 100644 --- a/eo-runtime/src/main/java/org/eolang/PhDefault.java +++ b/eo-runtime/src/main/java/org/eolang/PhDefault.java @@ -140,7 +140,17 @@ public boolean hasRho() { @Override public void put(final int pos, final Phi object) { - this.put(this.attr(pos), object); + final String name = this.attr(pos); + final AtWithRho attribute = (AtWithRho) this.attrs.get(name); + if (!(attribute.getOrigin() instanceof AtVoid)) { + throw new ExReadOnly( + String.format( + "There are no void attributes left, can't set the %s one", + PhDefault.ordinal(pos) + ) + ); + } + this.put(name, object); } @Override @@ -336,4 +346,34 @@ private static void debug(final String msg) { private static String padding() { return String.join("", Collections.nCopies(PhDefault.NESTING.get(), "·")); } + + /** + * Returns the ordinal representation of a number (e.g., "1st", "2nd", "3rd", "4th"). + * @param number The number to format + * @return The number with its ordinal suffix + */ + private static String ordinal(final int number) { + final int lasts = number % 100; + final int last = number % 10; + final String suffix; + if (lasts >= 11 && lasts <= 13) { + suffix = "th"; + } else { + switch (last) { + case 1: + suffix = "st"; + break; + case 2: + suffix = "nd"; + break; + case 3: + suffix = "rd"; + break; + default: + suffix = "th"; + break; + } + } + return number + suffix; + } } diff --git a/eo-runtime/src/test/java/org/eolang/PhDefaultTest.java b/eo-runtime/src/test/java/org/eolang/PhDefaultTest.java index 1addd5b097..8056f89ca2 100644 --- a/eo-runtime/src/test/java/org/eolang/PhDefaultTest.java +++ b/eo-runtime/src/test/java/org/eolang/PhDefaultTest.java @@ -23,6 +23,7 @@ */ package org.eolang; +import EOorg.EOeolang.EOnumber; import com.yegor256.Together; import java.security.SecureRandom; import org.cactoos.set.SetOf; @@ -488,6 +489,19 @@ void doesNotCalculateRandomTwice() { ); } + @Test + void failsCorrectlyWhenTooManyAttributesPut() { + MatcherAssert.assertThat( + "the message explains what's going on", + Assertions.assertThrows( + ExAbstract.class, + () -> new EOnumber().put(1, new Data.ToPhi(1)), + "fails when trying to set attribute with too big position" + ).getMessage(), + Matchers.equalTo("There are no void attributes left, can't set the 1st one") + ); + } + /** * Rnd. * @since 0.1.0 From 9f70b01a6c21c239888351ba740d680d16165316 Mon Sep 17 00:00:00 2001 From: Suban05 Date: Fri, 21 Feb 2025 15:49:32 +0500 Subject: [PATCH 2/4] #3542 exclude `nd` --- .github/typos.toml | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/.github/typos.toml b/.github/typos.toml index 626c8d9601..ad70f72fa7 100644 --- a/.github/typos.toml +++ b/.github/typos.toml @@ -61,3 +61,27 @@ extend-ignore-identifiers-re = [ "scrpt", # valid variable name "fle" # valid variable name ] +# The MIT License (MIT) +# +# Copyright (c) 2016-2025 Objectionary.com +# +# Permission is hereby granted, free of charge, to any person obtaining a copy +# of this software and associated documentation files (the "Software"), to deal +# in the Software without restriction, including without limitation the rights +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +# copies of the Software, and to permit persons to whom the Software is +# furnished to do so, subject to the following conditions: +# +# The above copyright notice and this permission notice shall be included +# in all copies or substantial portions of the Software. +# +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +# SOFTWARE. + +[default.extend-words] +nd = "nd" From 380790cba9e5d0f2ed18ac9a5b84b729b9cc3498 Mon Sep 17 00:00:00 2001 From: Suban05 Date: Fri, 21 Feb 2025 22:06:48 +0500 Subject: [PATCH 3/4] #3542 simplify implementation --- .github/typos.toml | 24 ------------ .../src/main/java/org/eolang/PhDefault.java | 37 ++----------------- .../test/java/org/eolang/PhDefaultTest.java | 2 +- 3 files changed, 4 insertions(+), 59 deletions(-) diff --git a/.github/typos.toml b/.github/typos.toml index ad70f72fa7..626c8d9601 100644 --- a/.github/typos.toml +++ b/.github/typos.toml @@ -61,27 +61,3 @@ extend-ignore-identifiers-re = [ "scrpt", # valid variable name "fle" # valid variable name ] -# The MIT License (MIT) -# -# Copyright (c) 2016-2025 Objectionary.com -# -# Permission is hereby granted, free of charge, to any person obtaining a copy -# of this software and associated documentation files (the "Software"), to deal -# in the Software without restriction, including without limitation the rights -# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -# copies of the Software, and to permit persons to whom the Software is -# furnished to do so, subject to the following conditions: -# -# The above copyright notice and this permission notice shall be included -# in all copies or substantial portions of the Software. -# -# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -# FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE -# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -# SOFTWARE. - -[default.extend-words] -nd = "nd" diff --git a/eo-runtime/src/main/java/org/eolang/PhDefault.java b/eo-runtime/src/main/java/org/eolang/PhDefault.java index d05ad51c8d..1e2daafecb 100644 --- a/eo-runtime/src/main/java/org/eolang/PhDefault.java +++ b/eo-runtime/src/main/java/org/eolang/PhDefault.java @@ -141,12 +141,11 @@ public boolean hasRho() { @Override public void put(final int pos, final Phi object) { final String name = this.attr(pos); - final AtWithRho attribute = (AtWithRho) this.attrs.get(name); - if (!(attribute.getOrigin() instanceof AtVoid)) { + if (!(((AtWithRho) this.attrs.get(name)).getOrigin() instanceof AtVoid)) { throw new ExReadOnly( String.format( - "There are no void attributes left, can't set the %s one", - PhDefault.ordinal(pos) + "Can't put attribute with position %d because it's not void one", + pos ) ); } @@ -346,34 +345,4 @@ private static void debug(final String msg) { private static String padding() { return String.join("", Collections.nCopies(PhDefault.NESTING.get(), "·")); } - - /** - * Returns the ordinal representation of a number (e.g., "1st", "2nd", "3rd", "4th"). - * @param number The number to format - * @return The number with its ordinal suffix - */ - private static String ordinal(final int number) { - final int lasts = number % 100; - final int last = number % 10; - final String suffix; - if (lasts >= 11 && lasts <= 13) { - suffix = "th"; - } else { - switch (last) { - case 1: - suffix = "st"; - break; - case 2: - suffix = "nd"; - break; - case 3: - suffix = "rd"; - break; - default: - suffix = "th"; - break; - } - } - return number + suffix; - } } diff --git a/eo-runtime/src/test/java/org/eolang/PhDefaultTest.java b/eo-runtime/src/test/java/org/eolang/PhDefaultTest.java index 8056f89ca2..c5b4e7eaf7 100644 --- a/eo-runtime/src/test/java/org/eolang/PhDefaultTest.java +++ b/eo-runtime/src/test/java/org/eolang/PhDefaultTest.java @@ -498,7 +498,7 @@ void failsCorrectlyWhenTooManyAttributesPut() { () -> new EOnumber().put(1, new Data.ToPhi(1)), "fails when trying to set attribute with too big position" ).getMessage(), - Matchers.equalTo("There are no void attributes left, can't set the 1st one") + Matchers.equalTo("Can't put attribute with position 1 because it's not void one") ); } From 7556abe7a4fdf740b1b75716bfbb3c043e316d18 Mon Sep 17 00:00:00 2001 From: Suban05 Date: Fri, 21 Feb 2025 23:32:52 +0500 Subject: [PATCH 4/4] #3542 rename --- eo-runtime/src/main/java/org/eolang/AtWithRho.java | 14 +++++++------- eo-runtime/src/main/java/org/eolang/PhDefault.java | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/eo-runtime/src/main/java/org/eolang/AtWithRho.java b/eo-runtime/src/main/java/org/eolang/AtWithRho.java index 30f514ace3..a646394439 100644 --- a/eo-runtime/src/main/java/org/eolang/AtWithRho.java +++ b/eo-runtime/src/main/java/org/eolang/AtWithRho.java @@ -32,7 +32,7 @@ final class AtWithRho implements Attr { /** * Original attribute. */ - private final Attr origin; + private final Attr original; /** * Rho. @@ -45,21 +45,21 @@ final class AtWithRho implements Attr { * @param rho Rho */ AtWithRho(final Attr attr, final Phi rho) { - this.origin = attr; + this.original = attr; this.rho = rho; } @Override public Attr copy(final Phi self) { return new AtWithRho( - this.origin.copy(self), + this.original.copy(self), self ); } @Override public Phi get() { - Phi ret = this.origin.get(); + Phi ret = this.original.get(); if (!ret.hasRho()) { ret = ret.copy(); ret.put(Attr.RHO, this.rho); @@ -69,14 +69,14 @@ public Phi get() { @Override public void put(final Phi phi) { - this.origin.put(phi); + this.original.put(phi); } /** * Returns the original attribute. * @return The original attribute */ - public Attr getOrigin() { - return this.origin; + Attr origin() { + return this.original; } } diff --git a/eo-runtime/src/main/java/org/eolang/PhDefault.java b/eo-runtime/src/main/java/org/eolang/PhDefault.java index 1e2daafecb..f78f54bb3d 100644 --- a/eo-runtime/src/main/java/org/eolang/PhDefault.java +++ b/eo-runtime/src/main/java/org/eolang/PhDefault.java @@ -141,7 +141,7 @@ public boolean hasRho() { @Override public void put(final int pos, final Phi object) { final String name = this.attr(pos); - if (!(((AtWithRho) this.attrs.get(name)).getOrigin() instanceof AtVoid)) { + if (!(((AtWithRho) this.attrs.get(name)).origin() instanceof AtVoid)) { throw new ExReadOnly( String.format( "Can't put attribute with position %d because it's not void one",