Skip to content

Commit

Permalink
Merge pull request #3789 from asmirnov-backend/feat/#3461/use-expect-…
Browse files Browse the repository at this point in the history
…for-some-atoms

feat(#3461): Use Expect for EOnumber Atoms
  • Loading branch information
yegor256 authored Jan 25, 2025
2 parents 53ec85a + 2a9b19d commit 6b6ba6b
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 125 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.eolang.Attr;
import org.eolang.BytesOf;
import org.eolang.Data;
import org.eolang.Dataized;
import org.eolang.Expect;
import org.eolang.PhDefault;
import org.eolang.Phi;
import org.eolang.XmirObject;
Expand All @@ -52,7 +52,7 @@ public Phi lambda() {
0,
new Data.ToPhi(
new BytesOf(
new Dataized(this.take(Attr.RHO)).asNumber().longValue()
new Expect.Number(Expect.at(this, Attr.RHO)).it().longValue()
).take()
)
);
Expand Down
9 changes: 4 additions & 5 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOnumber$EOdiv.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.eolang.Atom;
import org.eolang.Attr;
import org.eolang.Data;
import org.eolang.Dataized;
import org.eolang.Expect;
import org.eolang.PhDefault;
import org.eolang.Phi;
import org.eolang.XmirObject;
Expand All @@ -56,9 +56,8 @@ public final class EOnumber$EOdiv extends PhDefault implements Atom {

@Override
public Phi lambda() {
return new Data.ToPhi(
new Dataized(this.take(Attr.RHO)).asNumber()
/ new Dataized(this.take("x")).asNumber()
);
final Double left = new Expect.Number(Expect.at(this, Attr.RHO)).it();
final Double right = new Expect.Number(Expect.at(this, "x")).it();
return new Data.ToPhi(left / right);
}
}
4 changes: 2 additions & 2 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOnumber$EOfloor.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
import org.eolang.Atom;
import org.eolang.Attr;
import org.eolang.Data;
import org.eolang.Dataized;
import org.eolang.Expect;
import org.eolang.PhDefault;
import org.eolang.Phi;
import org.eolang.XmirObject;
Expand All @@ -48,7 +48,7 @@ public final class EOnumber$EOfloor extends PhDefault implements Atom {
@Override
public Phi lambda() {
return new Data.ToPhi(
new Dataized(this.take(Attr.RHO)).asNumber().longValue()
new Expect.Number(Expect.at(this, Attr.RHO)).it().longValue()
);
}
}
9 changes: 4 additions & 5 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOnumber$EOgt.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.eolang.Atom;
import org.eolang.Attr;
import org.eolang.Data;
import org.eolang.Dataized;
import org.eolang.Expect;
import org.eolang.PhDefault;
import org.eolang.Phi;
import org.eolang.XmirObject;
Expand All @@ -56,9 +56,8 @@ public final class EOnumber$EOgt extends PhDefault implements Atom {

@Override
public Phi lambda() {
return new Data.ToPhi(
new Dataized(this.take(Attr.RHO)).asNumber()
> new Dataized(this.take("x")).asNumber()
);
final Double left = new Expect.Number(Expect.at(this, Attr.RHO)).it();
final Double right = new Expect.Number(Expect.at(this, "x")).it();
return new Data.ToPhi(left > right);
}
}
11 changes: 4 additions & 7 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOnumber$EOplus.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import org.eolang.Atom;
import org.eolang.Attr;
import org.eolang.Data;
import org.eolang.Dataized;
import org.eolang.Expect;
import org.eolang.PhDefault;
import org.eolang.Phi;
import org.eolang.XmirObject;
Expand All @@ -56,11 +56,8 @@ public final class EOnumber$EOplus extends PhDefault implements Atom {

@Override
public Phi lambda() {
return new Data.ToPhi(
Double.sum(
new Dataized(this.take(Attr.RHO)).asNumber(),
new Dataized(this.take("x")).asNumber()
)
);
final Double left = new Expect.Number(Expect.at(this, Attr.RHO)).it();
final Double right = new Expect.Number(Expect.at(this, "x")).it();
return new Data.ToPhi(Double.sum(left, right));
}
}
11 changes: 2 additions & 9 deletions eo-runtime/src/main/java/EOorg/EOeolang/EOnumber$EOtimes.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import org.eolang.Atom;
import org.eolang.Attr;
import org.eolang.Data;
import org.eolang.Dataized;
import org.eolang.Expect;
import org.eolang.PhDefault;
import org.eolang.Phi;
Expand All @@ -57,14 +56,8 @@ public final class EOnumber$EOtimes extends PhDefault implements Atom {

@Override
public Phi lambda() {
final Double left = Expect.at(this, Attr.RHO)
.that(phi -> new Dataized(phi).asNumber())
.otherwise("must be a number")
.it();
final Double right = Expect.at(this, "x")
.that(phi -> new Dataized(phi).asNumber())
.otherwise("must be a number")
.it();
final Double left = new Expect.Number(Expect.at(this, Attr.RHO)).it();
final Double right = new Expect.Number(Expect.at(this, "x")).it();
return new Data.ToPhi(left * right);
}
}
95 changes: 0 additions & 95 deletions eo-runtime/src/test/java/EOorg/EOeolang/EOnumber$EOtimesTest.java

This file was deleted.

64 changes: 64 additions & 0 deletions eo-runtime/src/test/java/EOorg/EOeolang/EOnumberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,18 @@
package EOorg.EOeolang; // NOPMD

import org.eolang.AtCompositeTest;
import org.eolang.Attr;
import org.eolang.Data;
import org.eolang.Dataized;
import org.eolang.ExAbstract;
import org.eolang.PhWith;
import org.eolang.Phi;
import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

/**
* Test case for {@link EOnumber}.
Expand Down Expand Up @@ -69,4 +77,60 @@ void hasDifferentHash() {
Matchers.not(new Data.ToPhi(0L).hashCode())
);
}

@ParameterizedTest
@ValueSource(classes = {
EOnumber$EOdiv.class,
EOnumber$EOgt.class,
EOnumber$EOplus.class,
EOnumber$EOtimes.class
})
void throwsCorrectErrorForXAttr(final Class<?> cls) {
MatcherAssert.assertThat(
"the message in the error is correct",
Assertions.assertThrows(
ExAbstract.class,
() -> new Dataized(
new PhWith(
new PhWith(
(Phi) cls.getDeclaredConstructor().newInstance(),
Attr.RHO,
new Data.ToPhi(42)
),
"x",
new Data.ToPhi(true)
)
).take(),
"operation with TRUE fails with a proper message that explains what happened"
).getMessage(),
Matchers.equalTo("the 'x' attribute must be a number")
);
}

@ParameterizedTest
@ValueSource(classes = {
EOnumber$EOdiv.class,
EOnumber$EOgt.class,
EOnumber$EOplus.class,
EOnumber$EOtimes.class,
EOnumber$EOas_i64.class,
EOnumber$EOfloor.class
})
void throwsCorrectErrorForRhoAttr(final Class<?> cls) {
MatcherAssert.assertThat(
"the message in the error is correct",
Assertions.assertThrows(
ExAbstract.class,
() -> new Dataized(
new PhWith(
(Phi) cls.getDeclaredConstructor().newInstance(),
Attr.RHO,
new Data.ToPhi(true)
)
).take(),
"EOnumber must be a number"
).getMessage(),
Matchers.equalTo("the 'ρ' attribute must be a number")
);
}
}

0 comments on commit 6b6ba6b

Please sign in to comment.