Skip to content

Commit 3bfc7f7

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

File tree

7 files changed

+235
-20
lines changed

7 files changed

+235
-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: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
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; // 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.eolang.Phi;
37+
import org.hamcrest.MatcherAssert;
38+
import org.hamcrest.Matchers;
39+
import org.junit.jupiter.api.Assertions;
40+
import org.junit.jupiter.api.Test;
41+
42+
/**
43+
* Test case for {@link EOmalloc$EOof$EOallocated$EOread}.
44+
*
45+
* @since 0.51.2
46+
*/
47+
@SuppressWarnings("PMD.AvoidDollarSigns")
48+
final class EOmalloc$EOof$EOallocated$EOreadTest {
49+
50+
@Test
51+
void throwsCorrectErrorForOffsetAttrNaN() {
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 Dummy().it(),
59+
"offset",
60+
new Data.ToPhi(true)
61+
)
62+
).take(),
63+
"put TRUE in int attr fails with a proper message that explains what happened"
64+
).getMessage(),
65+
Matchers.equalTo("the 'offset' attribute must be a number")
66+
);
67+
}
68+
69+
@Test
70+
void throwsCorrectErrorForOffsetAttrNotAnInt() {
71+
MatcherAssert.assertThat(
72+
"the message in the error is correct",
73+
Assertions.assertThrows(
74+
ExAbstract.class,
75+
() -> new Dataized(
76+
new PhWith(
77+
new Dummy().it(),
78+
"offset",
79+
new Data.ToPhi(42.42)
80+
)
81+
).take(),
82+
"put double in int attr fails with a proper message that explains what happened"
83+
).getMessage(),
84+
Matchers.equalTo("the 'offset' attribute (42.42) must be an integer")
85+
);
86+
}
87+
88+
@Test
89+
void throwsCorrectErrorForLengthAttrNaN() {
90+
MatcherAssert.assertThat(
91+
"the message in the error is correct",
92+
Assertions.assertThrows(
93+
ExAbstract.class,
94+
() -> new Dataized(
95+
new PhWith(
96+
new PhWith(
97+
new Dummy().it(),
98+
"offset",
99+
new Data.ToPhi(42)
100+
),
101+
"length",
102+
new Data.ToPhi(true)
103+
)
104+
).take(),
105+
"put TRUE in int attr fails with a proper message that explains what happened"
106+
).getMessage(),
107+
Matchers.equalTo("the 'length' attribute must be a number")
108+
);
109+
}
110+
111+
@Test
112+
void throwsCorrectErrorForLengthAttrNotAnInt() {
113+
MatcherAssert.assertThat(
114+
"the message in the error is correct",
115+
Assertions.assertThrows(
116+
ExAbstract.class,
117+
() -> new Dataized(
118+
new PhWith(
119+
new PhWith(
120+
new Dummy().it(),
121+
"offset",
122+
new Data.ToPhi(42)
123+
),
124+
"length",
125+
new Data.ToPhi(42.42)
126+
)
127+
).take(),
128+
"put double in int attr fails with a proper message that explains what happened"
129+
).getMessage(),
130+
Matchers.equalTo("the 'length' attribute (42.42) must be an integer")
131+
);
132+
}
133+
134+
/**
135+
* Dummy with correct id attr.
136+
* @since 0.51.2
137+
*/
138+
private static class Dummy {
139+
/**
140+
* @return EOmalloc$EOof$EOallocated$EOread with id attr
141+
*/
142+
private Phi it() {
143+
return 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+
}
153+
}
154+
155+
}

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

Lines changed: 67 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}.
@@ -85,6 +89,54 @@ void freesMemoryIfErrorIsOccurred() {
8589
);
8690
}
8791

92+
@ParameterizedTest
93+
@ValueSource(classes = EOmalloc$EOof$EOallocated$EOread.class)
94+
void throwsCorrectErrorForIdAttrNaN(final Class<?> cls) {
95+
MatcherAssert.assertThat(
96+
"the message in the error is correct",
97+
Assertions.assertThrows(
98+
ExAbstract.class,
99+
() -> new Dataized(
100+
new PhWith(
101+
(Phi) cls.getDeclaredConstructor().newInstance(),
102+
Attr.RHO,
103+
new PhWith(
104+
new EOmallocTest.IdDummy(),
105+
"id",
106+
new Data.ToPhi(true)
107+
)
108+
)
109+
).take(),
110+
"put TRUE in int attr fails with a proper message that explains what happened"
111+
).getMessage(),
112+
Matchers.equalTo("the 'id' attribute must be a number")
113+
);
114+
}
115+
116+
@ParameterizedTest
117+
@ValueSource(classes = EOmalloc$EOof$EOallocated$EOread.class)
118+
void throwsCorrectErrorForIdAttrNotAnInt(final Class<?> cls) {
119+
MatcherAssert.assertThat(
120+
"the message in the error is correct",
121+
Assertions.assertThrows(
122+
ExAbstract.class,
123+
() -> new Dataized(
124+
new PhWith(
125+
(Phi) cls.getDeclaredConstructor().newInstance(),
126+
Attr.RHO,
127+
new PhWith(
128+
new EOmallocTest.IdDummy(),
129+
"id",
130+
new Data.ToPhi(42.42)
131+
)
132+
)
133+
).take(),
134+
"put double in int attr fails with a proper message that explains what happened"
135+
).getMessage(),
136+
Matchers.equalTo("the 'id' attribute (42.42) must be an integer")
137+
);
138+
}
139+
88140
/**
89141
* Allocated data.
90142
* @param obj Init object
@@ -129,6 +181,20 @@ private static class Dummy extends PhDefault {
129181
}
130182
}
131183

184+
/**
185+
* Dummy with id attr.
186+
* @since 0.51.2
187+
*/
188+
static class IdDummy extends PhDefault {
189+
/**
190+
* Ctor.
191+
*/
192+
@SuppressWarnings("PMD.ConstructorOnlyInitializesOrCallOtherConstructors")
193+
IdDummy() {
194+
this.add("id", new AtVoid("id"));
195+
}
196+
}
197+
132198
/**
133199
* Dummy that throws an exception.
134200
* @since 0.36.0
@@ -164,4 +230,5 @@ private static class ErrorDummy extends PhDefault {
164230
);
165231
}
166232
}
233+
167234
}

0 commit comments

Comments
 (0)