Skip to content

Commit 4a996c2

Browse files
committed
#5295: reject out-of-int-range numbers in Expect.Int and Expect.Natural
1 parent c5d218e commit 4a996c2

2 files changed

Lines changed: 50 additions & 0 deletions

File tree

eo-runtime/src/main/java/org/eolang/Expect.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,8 @@ public Integer it() {
262262
.otherwise("must be a number")
263263
.must(number -> number % 1 == 0)
264264
.otherwise("must be an integer")
265+
.must(number -> number >= Integer.MIN_VALUE && number <= Integer.MAX_VALUE)
266+
.otherwise("must fit into int range")
265267
.that(Double::intValue)
266268
.it();
267269
}
@@ -394,6 +396,8 @@ public Integer it() {
394396
.otherwise("must be a number")
395397
.must(number -> number % 1 == 0)
396398
.otherwise("must be an integer")
399+
.must(number -> number >= Integer.MIN_VALUE && number <= Integer.MAX_VALUE)
400+
.otherwise("must fit into int range")
397401
.that(Double::intValue)
398402
.must(integer -> integer >= 0)
399403
.otherwise("must be greater or equal to zero")

eo-runtime/src/test/java/org/eolang/ExpectTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,4 +273,50 @@ void failsInTransformingToNonNegativeIntegerForNegative() {
273273
Matchers.equalTo("the 'ρ' attribute (-42) must be greater or equal to zero")
274274
);
275275
}
276+
277+
@Test
278+
void failsInTransformingToIntegerForTooLarge() {
279+
MatcherAssert.assertThat(
280+
"inner class Integer throws error for a number outside int range, "
281+
+ "instead of silently saturating via Double.intValue()",
282+
Assertions.assertThrows(
283+
ExFailure.class,
284+
() -> new Expect.Int(
285+
Expect.at(
286+
new PhApplication(
287+
new PhDefault(),
288+
Phi.RHO,
289+
new Data.ToPhi(1.0e15)
290+
),
291+
Phi.RHO
292+
)
293+
).it(),
294+
"fails with correct error message while transform Phi to Integer"
295+
).getMessage(),
296+
Matchers.equalTo("the 'ρ' attribute (1.0E15) must fit into int range")
297+
);
298+
}
299+
300+
@Test
301+
void failsInTransformingToNonNegativeIntegerForTooLarge() {
302+
MatcherAssert.assertThat(
303+
"inner class NonNegativeInteger throws error for a number outside int range, "
304+
+ "instead of silently saturating via Double.intValue()",
305+
Assertions.assertThrows(
306+
ExFailure.class,
307+
() -> new Expect.Natural(
308+
Expect.at(
309+
new PhApplication(
310+
new PhDefault(),
311+
Phi.RHO,
312+
new Data.ToPhi(1.0e15)
313+
),
314+
Phi.RHO
315+
)
316+
).it(),
317+
"fails with correct error message while transform Phi to NonNegativeInteger"
318+
).getMessage(),
319+
Matchers.equalTo("the 'ρ' attribute (1.0E15) must fit into int range")
320+
);
321+
}
276322
}

0 commit comments

Comments
 (0)