Skip to content

Commit

Permalink
objectionary#3542 output a clearer message when too many attributes a…
Browse files Browse the repository at this point in the history
…re passed
  • Loading branch information
Suban05 committed Feb 21, 2025
1 parent b708178 commit c7a271a
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 1 deletion.
8 changes: 8 additions & 0 deletions eo-runtime/src/main/java/org/eolang/AtWithRho.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
42 changes: 41 additions & 1 deletion eo-runtime/src/main/java/org/eolang/PhDefault.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}
14 changes: 14 additions & 0 deletions eo-runtime/src/test/java/org/eolang/PhDefaultTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package org.eolang;

import EOorg.EOeolang.EOnumber;
import com.yegor256.Together;
import java.security.SecureRandom;
import org.cactoos.set.SetOf;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit c7a271a

Please sign in to comment.