Skip to content

Commit a0b7456

Browse files
use Expect for EOmalloc$EOof$EOallocated$EOread
1 parent 6c6cae8 commit a0b7456

File tree

7 files changed

+248
-20
lines changed

7 files changed

+248
-20
lines changed

eo-runtime/src/main/java/EOorg/EOeolang/EOmalloc$EOof$EOallocated$EOread.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import org.eolang.Atom;
3333
import org.eolang.Attr;
3434
import org.eolang.Data;
35-
import org.eolang.Dataized;
35+
import org.eolang.Expect;
3636
import org.eolang.PhDefault;
3737
import org.eolang.Phi;
3838
import org.eolang.XmirObject;
@@ -58,9 +58,9 @@ public final class EOmalloc$EOof$EOallocated$EOread extends PhDefault implements
5858
public Phi lambda() {
5959
return new Data.ToPhi(
6060
Heaps.INSTANCE.read(
61-
new Dataized(this.take(Attr.RHO).take("id")).asNumber().intValue(),
62-
new Dataized(this.take("offset")).asNumber().intValue(),
63-
new Dataized(this.take("length")).asNumber().intValue()
61+
new Expect.Int(Expect.at(this.take(Attr.RHO), "id")).it(),
62+
new Expect.Int(Expect.at(this, "offset")).it(),
63+
new Expect.Int(Expect.at(this, "length")).it()
6464
)
6565
);
6666
}

eo-runtime/src/main/java/EOorg/EOeolang/EOmalloc$EOof$EOallocated$EOresized.java

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import org.eolang.AtVoid;
3232
import org.eolang.Atom;
3333
import org.eolang.Attr;
34-
import org.eolang.Dataized;
3534
import org.eolang.Expect;
3635
import org.eolang.PhDefault;
3736
import org.eolang.Phi;
@@ -56,16 +55,8 @@ public final class EOmalloc$EOof$EOallocated$EOresized extends PhDefault impleme
5655
@Override
5756
public Phi lambda() {
5857
final Phi rho = this.take(Attr.RHO);
59-
final int id = Expect.at(rho, "id")
60-
.that(phi -> new Dataized(phi).asNumber())
61-
.otherwise("must be a number")
62-
.that(Double::intValue)
63-
.it();
64-
final int size = Expect.at(this, "new-size")
65-
.that(phi -> new Dataized(phi).asNumber())
66-
.otherwise("must be a number")
67-
.that(Double::intValue)
68-
.it();
58+
final int id = new Expect.Int(Expect.at(rho, "id")).it();
59+
final int size = new Expect.Int(Expect.at(this, "new-size")).it();
6960
Heaps.INSTANCE.resize(id, size);
7061
return rho;
7162
}

eo-runtime/src/main/java/EOorg/EOeolang/EOmalloc$EOof$EOallocated$EOsize.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import org.eolang.Atom;
3232
import org.eolang.Attr;
3333
import org.eolang.Data;
34-
import org.eolang.Dataized;
34+
import org.eolang.Expect;
3535
import org.eolang.PhDefault;
3636
import org.eolang.Phi;
3737
import org.eolang.XmirObject;
@@ -48,7 +48,7 @@ public final class EOmalloc$EOof$EOallocated$EOsize extends PhDefault implements
4848
public Phi lambda() {
4949
return new Data.ToPhi(
5050
Heaps.INSTANCE.size(
51-
new Dataized(this.take(Attr.RHO).take("id")).asNumber().intValue()
51+
new Expect.Int(Expect.at(this.take(Attr.RHO), "id")).it()
5252
)
5353
);
5454
}

eo-runtime/src/main/java/EOorg/EOeolang/EOmalloc$EOof$EOallocated$EOwrite.java

Lines changed: 3 additions & 2 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;
@@ -57,8 +58,8 @@ public final class EOmalloc$EOof$EOallocated$EOwrite extends PhDefault implement
5758
@Override
5859
public Phi lambda() {
5960
Heaps.INSTANCE.write(
60-
new Dataized(this.take(Attr.RHO).take("id")).asNumber().intValue(),
61-
new Dataized(this.take("offset")).asNumber().intValue(),
61+
new Expect.Int(Expect.at(this.take(Attr.RHO), "id")).it(),
62+
new Expect.Int(Expect.at(this, "offset")).it(),
6263
new Dataized(this.take("data")).take()
6364
);
6465
return new Data.ToPhi(true);

eo-runtime/src/main/java/EOorg/EOeolang/EOmalloc$EOof$EOφ.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import org.eolang.Attr;
3333
import org.eolang.Data;
3434
import org.eolang.Dataized;
35+
import org.eolang.Expect;
3536
import org.eolang.PhDefault;
3637
import org.eolang.Phi;
3738
import org.eolang.XmirObject;
@@ -48,7 +49,7 @@ public final class EOmalloc$EOof$EOφ extends PhDefault implements Atom {
4849
public Phi lambda() {
4950
final Phi rho = this.take(Attr.RHO);
5051
final int identifier = Heaps.INSTANCE.malloc(
51-
this, new Dataized(rho.take("size")).asNumber().intValue()
52+
this, new Expect.Int(Expect.at(rho, "size")).it()
5253
);
5354
final Phi res;
5455
try {
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
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 (4 lines)
27+
* @checkstyle TrailingCommentCheck (3 lines)
28+
*/
29+
package EOorg.EOeolang;
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 EOmalloc$EOof$EOallocated$EOread}.
43+
*
44+
* @since 0.51.2
45+
*/
46+
@SuppressWarnings("PMD.AvoidDollarSigns")
47+
final class EOmalloc$EOof$EOallocated$EOreadTest {
48+
49+
@Test
50+
void throwsCorrectErrorForOffsetAttrNaN() {
51+
MatcherAssert.assertThat(
52+
"the message in the error is correct",
53+
Assertions.assertThrows(
54+
ExAbstract.class,
55+
() -> new Dataized(
56+
new PhWith(
57+
new PhWith(
58+
new EOmalloc$EOof$EOallocated$EOread(),
59+
Attr.RHO,
60+
new PhWith(
61+
new EOmallocTest.IdDummy(),
62+
"id",
63+
new Data.ToPhi(42)
64+
)
65+
),
66+
"offset",
67+
new Data.ToPhi(true)
68+
)
69+
).take(),
70+
"put TRUE in int attr fails with a proper message that explains what happened"
71+
).getMessage(),
72+
Matchers.equalTo("the 'offset' attribute must be a number")
73+
);
74+
}
75+
76+
@Test
77+
void throwsCorrectErrorForOffsetAttrNotAnInt() {
78+
MatcherAssert.assertThat(
79+
"the message in the error is correct",
80+
Assertions.assertThrows(
81+
ExAbstract.class,
82+
() -> new Dataized(
83+
new PhWith(
84+
new PhWith(
85+
new EOmalloc$EOof$EOallocated$EOread(),
86+
Attr.RHO,
87+
new PhWith(
88+
new EOmallocTest.IdDummy(),
89+
"id",
90+
new Data.ToPhi(42)
91+
)
92+
),
93+
"offset",
94+
new Data.ToPhi(42.42)
95+
)
96+
).take(),
97+
"put double in int attr fails with a proper message that explains what happened"
98+
).getMessage(),
99+
Matchers.equalTo("the 'offset' attribute (42.42) must be an integer")
100+
);
101+
}
102+
103+
@Test
104+
void throwsCorrectErrorForLengthAttrNaN() {
105+
MatcherAssert.assertThat(
106+
"the message in the error is correct",
107+
Assertions.assertThrows(
108+
ExAbstract.class,
109+
() -> new Dataized(
110+
new PhWith(
111+
new PhWith(
112+
new PhWith(
113+
new EOmalloc$EOof$EOallocated$EOread(),
114+
Attr.RHO,
115+
new PhWith(
116+
new EOmallocTest.IdDummy(),
117+
"id",
118+
new Data.ToPhi(42)
119+
)
120+
),
121+
"offset",
122+
new Data.ToPhi(42)
123+
),
124+
"length",
125+
new Data.ToPhi(true)
126+
)
127+
).take(),
128+
"put TRUE in int attr fails with a proper message that explains what happened"
129+
).getMessage(),
130+
Matchers.equalTo("the 'length' attribute must be a number")
131+
);
132+
}
133+
134+
@Test
135+
void throwsCorrectErrorForLengthAttrNotAnInt() {
136+
MatcherAssert.assertThat(
137+
"the message in the error is correct",
138+
Assertions.assertThrows(
139+
ExAbstract.class,
140+
() -> new Dataized(
141+
new PhWith(
142+
new PhWith(
143+
new PhWith(
144+
new EOmalloc$EOof$EOallocated$EOread(),
145+
Attr.RHO,
146+
new PhWith(
147+
new EOmallocTest.IdDummy(),
148+
"id",
149+
new Data.ToPhi(42)
150+
)
151+
),
152+
"offset",
153+
new Data.ToPhi(42)
154+
),
155+
"length",
156+
new Data.ToPhi(42.42)
157+
)
158+
).take(),
159+
"put double in int attr fails with a proper message that explains what happened"
160+
).getMessage(),
161+
Matchers.equalTo("the 'length' attribute (42.42) must be an integer")
162+
);
163+
}
164+
165+
}

eo-runtime/src/test/java/EOorg/EOeolang/EOmallocTest.java

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,12 @@
3939
import org.eolang.PhDefault;
4040
import org.eolang.PhWith;
4141
import org.eolang.Phi;
42+
import org.hamcrest.MatcherAssert;
43+
import org.hamcrest.Matchers;
4244
import org.junit.jupiter.api.Assertions;
4345
import org.junit.jupiter.api.Test;
46+
import org.junit.jupiter.params.ParameterizedTest;
47+
import org.junit.jupiter.params.provider.ValueSource;
4448

4549
/**
4650
* Test case for {@link EOmalloc}.
@@ -98,6 +102,58 @@ private static Phi allocated(final Phi obj, final Phi dummy) {
98102
return malloc;
99103
}
100104

105+
@ParameterizedTest
106+
@ValueSource(classes = {
107+
EOmalloc$EOof$EOallocated$EOread.class,
108+
})
109+
void throwsCorrectErrorForIdAttrNaN(final Class<?> cls) {
110+
MatcherAssert.assertThat(
111+
"the message in the error is correct",
112+
Assertions.assertThrows(
113+
ExAbstract.class,
114+
() -> new Dataized(
115+
new PhWith(
116+
(Phi) cls.getDeclaredConstructor().newInstance(),
117+
Attr.RHO,
118+
new PhWith(
119+
new EOmallocTest.IdDummy(),
120+
"id",
121+
new Data.ToPhi(true)
122+
)
123+
)
124+
).take(),
125+
"put TRUE in int attr fails with a proper message that explains what happened"
126+
).getMessage(),
127+
Matchers.equalTo("the 'id' attribute must be a number")
128+
);
129+
}
130+
131+
@ParameterizedTest
132+
@ValueSource(classes = {
133+
EOmalloc$EOof$EOallocated$EOread.class,
134+
})
135+
void throwsCorrectErrorForIdAttrNotAnInt(final Class<?> cls) {
136+
MatcherAssert.assertThat(
137+
"the message in the error is correct",
138+
Assertions.assertThrows(
139+
ExAbstract.class,
140+
() -> new Dataized(
141+
new PhWith(
142+
(Phi) cls.getDeclaredConstructor().newInstance(),
143+
Attr.RHO,
144+
new PhWith(
145+
new EOmallocTest.IdDummy(),
146+
"id",
147+
new Data.ToPhi(42.42)
148+
)
149+
)
150+
).take(),
151+
"put double in int attr fails with a proper message that explains what happened"
152+
).getMessage(),
153+
Matchers.equalTo("the 'id' attribute (42.42) must be an integer")
154+
);
155+
}
156+
101157
/**
102158
* Dummy.
103159
* @since 0.37.0
@@ -129,6 +185,19 @@ private static class Dummy extends PhDefault {
129185
}
130186
}
131187

188+
/**
189+
* Dummy with id attr.
190+
* @since 0.51.2
191+
*/
192+
static class IdDummy extends PhDefault {
193+
/**
194+
* Ctor.
195+
*/
196+
IdDummy() {
197+
this.add("id", new AtVoid("id"));
198+
}
199+
}
200+
132201
/**
133202
* Dummy that throws an exception.
134203
* @since 0.36.0
@@ -164,4 +233,5 @@ private static class ErrorDummy extends PhDefault {
164233
);
165234
}
166235
}
236+
167237
}

0 commit comments

Comments
 (0)