Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#3542 output a clearer message when too many attributes are passed. #3932

Merged
merged 5 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions .github/typos.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
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
Loading