Skip to content

Commit

Permalink
Merge pull request #3833 from asmirnov-backend/feat/inner-classes-for…
Browse files Browse the repository at this point in the history
…-expect

feat: Add inner helpful classes in `Expect`
  • Loading branch information
yegor256 authored Jan 19, 2025
2 parents 37cc4f4 + 47058c5 commit f4aabe2
Show file tree
Hide file tree
Showing 2 changed files with 244 additions and 3 deletions.
114 changes: 111 additions & 3 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 @@ -210,4 +210,112 @@ private static class ExOtherwise extends RuntimeException {
}
}

/**
* Transform Expect to Number.
*
* @since 0.51
*/
public static final class Number {

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

/**
* Ctor.
* @param expect Expect
*/
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())
.otherwise("must be a number")
.it();
}
}

/**
* Transform Expect to Integer.
*
* @since 0.51
*/
public static final class Int {

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

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

/**
* Return it.
* @return The token
* @checkstyle MethodNameCheck (5 lines)
*/
public 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 Natural number.
* Natural number is integer greater or equal to zero.
*
* @since 0.51
*/
public static final class Natural {

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

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

/**
* Return it.
* @return The token
* @checkstyle MethodNameCheck (5 lines)
*/
public 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();
}
}

}
133 changes: 133 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 @@ -163,4 +164,136 @@ void failsWithCorrectTraceForMustAndThat() {
);
}

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

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

@Test
void failsInTransformingToIntegerForNotInteger() {
MatcherAssert.assertThat(
"inner class Integer throws error for not an integer number",
Assertions.assertThrows(
ExFailure.class,
() -> new Expect.Int(
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 Integer"
).getMessage(),
Matchers.equalTo("the 'ρ' attribute (42.23) must be an integer")
);
}

@Test
void failsInTransformingToNonNegativeIntegerForNotNumber() {
MatcherAssert.assertThat(
"inner class NonNegativeInteger throws error for not a number",
Assertions.assertThrows(
ExFailure.class,
() -> new Expect.Natural(
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.Natural(
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.Natural(
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 f4aabe2

Please sign in to comment.