Skip to content

Commit

Permalink
Add Expect.NonNegativeInteger
Browse files Browse the repository at this point in the history
  • Loading branch information
asmirnov-backend committed Jan 17, 2025
1 parent e6ecd46 commit 4dc93e8
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 7 deletions.
65 changes: 58 additions & 7 deletions eo-runtime/src/main/java/org/eolang/Expect.java
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ public T it() {
*
* @since 0.51
*/
private static class ExMust extends RuntimeException {
private static final class ExMust extends RuntimeException {
/**
* Ctor.
* @param cause Exception cause
Expand All @@ -182,7 +182,7 @@ private static class ExMust extends RuntimeException {
*
* @since 0.51
*/
private static class ExThat extends RuntimeException {
private static final class ExThat extends RuntimeException {
/**
* Ctor.
* @param cause Exception cause
Expand All @@ -199,7 +199,7 @@ private static class ExThat extends RuntimeException {
*
* @since 0.51
*/
private static class ExOtherwise extends RuntimeException {
private static final class ExOtherwise extends RuntimeException {
/**
* Ctor.
* @param cause Exception cause
Expand All @@ -211,11 +211,12 @@ private static class ExOtherwise extends RuntimeException {
}

/**
* Transform Expect to Number
* Transform Expect to Number.
*
* @since 0.51
*/
public static class Number {
@SuppressWarnings("PMD.ShortMethodName")
public static final class Number {

/**
* Expect.
Expand All @@ -230,6 +231,11 @@ public Number(final Expect<Phi> expect) {
this.expect = expect;
}

/**
* Return it.
* @return The token
* @checkstyle MethodNameCheck (5 lines)
*/
public Double it() {
return this.expect
.that(phi -> new Dataized(phi).asNumber())
Expand All @@ -239,11 +245,12 @@ public Double it() {
}

/**
* Transform Expect to Integer
* Transform Expect to Integer.
*
* @since 0.51
*/
public static class Integer {
@SuppressWarnings({"PMD.ShortMethodName", "PMD.UnnecessaryFullyQualifiedName"})
public static final class Integer {

/**
* Expect.
Expand All @@ -258,13 +265,57 @@ public Integer(final Expect<Phi> expect) {
this.expect = expect;
}

/**
* Return it.
* @return The token
* @checkstyle MethodNameCheck (5 lines)
*/
public java.lang.Integer it() {
return this.expect
.that(phi -> new Dataized(phi).asNumber())
.otherwise("must be a number")
.must(number -> number % 1 == 0)
.otherwise("must be an integer")
.that(Double::intValue)
.it();
}
}

/**
* Transform Expect to NonNegativeInteger.
*
* @since 0.51
*/
@SuppressWarnings({"PMD.ShortMethodName", "PMD.UnnecessaryFullyQualifiedName"})
public static final class NonNegativeInteger {

/**
* Expect.
*/
private final Expect<Phi> expect;

/**
* Ctor.
* @param expect Expect
*/
public NonNegativeInteger(final Expect<Phi> expect) {
this.expect = expect;
}

/**
* Return it.
* @return The token
* @checkstyle MethodNameCheck (5 lines)
*/
public java.lang.Integer it() {
return this.expect
.that(phi -> new Dataized(phi).asNumber())
.otherwise("must be a number")
.must(number -> number % 1 == 0)
.otherwise("must be an integer")
.that(Double::intValue)
.must(integer -> integer >= 0)
.otherwise("must be greater or equal to zero")
.it();
}
}
Expand Down
67 changes: 67 additions & 0 deletions eo-runtime/src/test/java/org/eolang/ExpectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
*
* @since 0.1.0
*/
@SuppressWarnings("PMD.TooManyMethods")
final class ExpectTest {

@Test
Expand Down Expand Up @@ -229,4 +230,70 @@ void failsInTransformingToIntegerForNotInteger() {
);
}

@Test
void failsInTransformingToNonNegativeIntegerForNotNumber() {
MatcherAssert.assertThat(
"inner class NonNegativeInteger throws error for not a number",
Assertions.assertThrows(
ExFailure.class,
() -> new Expect.NonNegativeInteger(
Expect.at(
new PhWith(
new PhDefault(),
Attr.RHO,
new Data.ToPhi(true)
),
Attr.RHO
)
).it(),
"fails with correct error message while transform Phi to NonNegativeInteger"
).getMessage(),
Matchers.equalTo("the 'ρ' attribute must be a number")
);
}

@Test
void failsInTransformingToNonNegativeIntegerForNotInteger() {
MatcherAssert.assertThat(
"inner class NonNegativeInteger throws error for not an integer number",
Assertions.assertThrows(
ExFailure.class,
() -> new Expect.NonNegativeInteger(
Expect.at(
new PhWith(
new PhDefault(),
Attr.RHO,
new Data.ToPhi(42.23)
),
Attr.RHO
)
).it(),
"fails with correct error message while transform Phi to NonNegativeInteger"
).getMessage(),
Matchers.equalTo("the 'ρ' attribute (42.23) must be an integer")
);
}

@Test
void failsInTransformingToNonNegativeIntegerForNegative() {
MatcherAssert.assertThat(
"inner class NonNegativeInteger throws error for a negative integer",
Assertions.assertThrows(
ExFailure.class,
() -> new Expect.NonNegativeInteger(
Expect.at(
new PhWith(
new PhDefault(),
Attr.RHO,
new Data.ToPhi(-42)
),
Attr.RHO
)
).it(),
"fails with correct error message while transform Phi to NonNegativeInteger"
).getMessage(),
Matchers.equalTo("the 'ρ' attribute (-42) must be greater or equal to zero")
);
}

}

0 comments on commit 4dc93e8

Please sign in to comment.