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 all 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
18 changes: 13 additions & 5 deletions eo-runtime/src/main/java/org/eolang/AtWithRho.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ final class AtWithRho implements Attr {
/**
* Original attribute.
*/
private final Attr origin;
private final Attr original;

/**
* Rho.
Expand All @@ -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);
Expand All @@ -69,6 +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
*/
Attr origin() {
return this.original;
}
}
11 changes: 10 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,16 @@ 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);
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",
pos
)
);
}
this.put(name, object);
}

@Override
Expand Down
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("Can't put attribute with position 1 because it's not void one")
);
}

/**
* Rnd.
* @since 0.1.0
Expand Down
Loading