Skip to content

Commit 6d4e245

Browse files
use Expect for EOmalloc$EOof$EOallocated$EOread
1 parent a828139 commit 6d4e245

File tree

7 files changed

+238
-20
lines changed

7 files changed

+238
-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: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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+
* @checkstyle TypeNameCheck (5 lines)
47+
*/
48+
@SuppressWarnings("PMD.AvoidDollarSigns")
49+
final class EOmalloc$EOof$EOallocated$EOreadTest {
50+
51+
@Test
52+
void throwsCorrectErrorForOffsetAttrNaN() {
53+
MatcherAssert.assertThat(
54+
"the message in the error is correct",
55+
Assertions.assertThrows(
56+
ExAbstract.class,
57+
() -> new Dataized(
58+
new PhWith(
59+
new Dummy().it(),
60+
"offset",
61+
new Data.ToPhi(true)
62+
)
63+
).take(),
64+
"put TRUE in int attr fails with a proper message that explains what happened"
65+
).getMessage(),
66+
Matchers.equalTo("the 'offset' attribute must be a number")
67+
);
68+
}
69+
70+
@Test
71+
void throwsCorrectErrorForOffsetAttrNotAnInt() {
72+
MatcherAssert.assertThat(
73+
"the message in the error is correct",
74+
Assertions.assertThrows(
75+
ExAbstract.class,
76+
() -> new Dataized(
77+
new PhWith(
78+
new Dummy().it(),
79+
"offset",
80+
new Data.ToPhi(42.42)
81+
)
82+
).take(),
83+
"put double in int attr fails with a proper message that explains what happened"
84+
).getMessage(),
85+
Matchers.equalTo("the 'offset' attribute (42.42) must be an integer")
86+
);
87+
}
88+
89+
@Test
90+
void throwsCorrectErrorForLengthAttrNaN() {
91+
MatcherAssert.assertThat(
92+
"the message in the error is correct",
93+
Assertions.assertThrows(
94+
ExAbstract.class,
95+
() -> new Dataized(
96+
new PhWith(
97+
new PhWith(
98+
new Dummy().it(),
99+
"offset",
100+
new Data.ToPhi(42)
101+
),
102+
"length",
103+
new Data.ToPhi(true)
104+
)
105+
).take(),
106+
"put TRUE in int attr fails with a proper message that explains what happened"
107+
).getMessage(),
108+
Matchers.equalTo("the 'length' attribute must be a number")
109+
);
110+
}
111+
112+
@Test
113+
void throwsCorrectErrorForLengthAttrNotAnInt() {
114+
MatcherAssert.assertThat(
115+
"the message in the error is correct",
116+
Assertions.assertThrows(
117+
ExAbstract.class,
118+
() -> new Dataized(
119+
new PhWith(
120+
new PhWith(
121+
new Dummy().it(),
122+
"offset",
123+
new Data.ToPhi(42)
124+
),
125+
"length",
126+
new Data.ToPhi(42.42)
127+
)
128+
).take(),
129+
"put double in int attr fails with a proper message that explains what happened"
130+
).getMessage(),
131+
Matchers.equalTo("the 'length' attribute (42.42) must be an integer")
132+
);
133+
}
134+
135+
/**
136+
* Dummy with correct id attr.
137+
* @since 0.51.2
138+
*/
139+
@SuppressWarnings("PMD.MethodNameCheck")
140+
private static final class Dummy {
141+
/**
142+
* Produce EOmalloc$EOof$EOallocated$EOread with id attr.
143+
* @checkstyle TypeNameCheck (5 lines)
144+
*/
145+
private Phi it() {
146+
return new PhWith(
147+
new EOmalloc$EOof$EOallocated$EOread(),
148+
Attr.RHO,
149+
new PhWith(
150+
new EOmallocTest.IdDummy(),
151+
"id",
152+
new Data.ToPhi(42)
153+
)
154+
);
155+
}
156+
}
157+
158+
}

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)