Skip to content

Commit 25d918b

Browse files
Use Expect in EOnumber$EOdiv
1 parent 5eabfad commit 25d918b

2 files changed

Lines changed: 106 additions & 4 deletions

File tree

eo-runtime/src/main/java/EOorg/EOeolang/EOnumber$EOdiv.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.eolang.Attr;
3434
import org.eolang.Data;
3535
import org.eolang.Dataized;
36+
import org.eolang.Expect;
3637
import org.eolang.PhDefault;
3738
import org.eolang.Phi;
3839
import org.eolang.XmirObject;
@@ -56,9 +57,14 @@ public final class EOnumber$EOdiv extends PhDefault implements Atom {
5657

5758
@Override
5859
public Phi lambda() {
59-
return new Data.ToPhi(
60-
new Dataized(this.take(Attr.RHO)).asNumber()
61-
/ new Dataized(this.take("x")).asNumber()
62-
);
60+
final Double left = Expect.at(this, Attr.RHO)
61+
.that(phi -> new Dataized(phi).asNumber())
62+
.otherwise("must be a number")
63+
.it();
64+
final Double right = Expect.at(this, "x")
65+
.that(phi -> new Dataized(phi).asNumber())
66+
.otherwise("must be a number")
67+
.it();
68+
return new Data.ToPhi(left / right);
6369
}
6470
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2016-2025 Objectionary.com
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included
14+
* in all copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
/*
26+
* @checkstyle PackageNameCheck (10 lines)
27+
* @checkstyle TrailingCommentCheck (3 lines)
28+
*/
29+
package EOorg.EOeolang; // NOPMD
30+
31+
import org.eolang.Attr;
32+
import org.eolang.Data;
33+
import org.eolang.Dataized;
34+
import org.eolang.ExAbstract;
35+
import org.eolang.PhWith;
36+
import org.hamcrest.MatcherAssert;
37+
import org.hamcrest.Matchers;
38+
import org.junit.jupiter.api.Assertions;
39+
import org.junit.jupiter.api.Test;
40+
41+
/**
42+
* Test case for {@link EOnumber$EOdiv}.
43+
*
44+
* @since 0.51
45+
* @checkstyle TypeNameCheck (3 lines)
46+
*/
47+
@SuppressWarnings("PMD.AvoidDollarSigns")
48+
final class EOnumber$EOdivTest {
49+
50+
@Test
51+
void throwsCorrectErrorForXAttr() {
52+
MatcherAssert.assertThat(
53+
"the message in the error is correct",
54+
Assertions.assertThrows(
55+
ExAbstract.class,
56+
() -> new Dataized(
57+
new PhWith(
58+
new PhWith(
59+
new EOnumber$EOdiv(),
60+
Attr.RHO,
61+
new Data.ToPhi(42)
62+
),
63+
"x",
64+
new Data.ToPhi(true)
65+
)
66+
).take(),
67+
"addition with TRUE fails with a proper message that explains what happened"
68+
).getMessage(),
69+
Matchers.equalTo("the 'x' attribute must be a number")
70+
);
71+
}
72+
73+
@Test
74+
void throwsCorrectErrorForRhoAttr() {
75+
MatcherAssert.assertThat(
76+
"the message in the error is correct",
77+
Assertions.assertThrows(
78+
ExAbstract.class,
79+
() -> new Dataized(
80+
new PhWith(
81+
new PhWith(
82+
new EOnumber$EOdiv(),
83+
Attr.RHO,
84+
new Data.ToPhi(true)
85+
),
86+
"x",
87+
new Data.ToPhi(42)
88+
)
89+
).take(),
90+
"EOnumber must be a number"
91+
).getMessage(),
92+
Matchers.equalTo("the 'ρ' attribute must be a number")
93+
);
94+
}
95+
96+
}

0 commit comments

Comments
 (0)