Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add inner helpful classes in Expect #3833

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
);
}

}
Loading