Skip to content

Commit

Permalink
refactor Expect
Browse files Browse the repository at this point in the history
  • Loading branch information
asmirnov-backend committed Jan 12, 2025
1 parent e6582e2 commit 154eae9
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
16 changes: 8 additions & 8 deletions eo-runtime/src/main/java/org/eolang/Expect.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public <R> Expect<R> that(final Function<T, R> fun) {
try {
return fun.apply(this.sup.get());
} catch (final ExFailure ex) {
throw new ExpectFailureInThat(ex.getMessage(), ex);
throw new ExThat(ex.getMessage(), ex);
}
}
);
Expand All @@ -102,7 +102,7 @@ public Expect<T> otherwise(final String message) {
() -> {
try {
return this.sup.get();
} catch (final ExpectFailureInMust ex) {
} catch (final ExMust ex) {
throw new ExFailure(
String.format(
"%s %s %s",
Expand All @@ -112,7 +112,7 @@ public Expect<T> otherwise(final String message) {
),
ex
);
} catch (final ExpectFailureInThat ex) {
} catch (final ExThat ex) {
throw new ExFailure(
message,
ex
Expand All @@ -133,7 +133,7 @@ public Expect<T> must(final Function<T, Boolean> fun) {
() -> {
final T ret = this.sup.get();
if (!fun.apply(ret)) {
throw new ExpectFailureInMust(
throw new ExMust(
String.format("(%s)", ret)
);
}
Expand All @@ -157,13 +157,13 @@ public T it() {
*
* @since 0.51
*/
private static class ExpectFailureInMust extends ExFailure {
private static class ExMust extends ExFailure {
/**
* Ctor.
* @param cause Exception cause
* @param args Arguments for {@link String#format(String, Object...)}
*/
ExpectFailureInMust(final String cause, final Object... args) {
ExMust(final String cause, final Object... args) {
super(String.format(cause, args));
}
}
Expand All @@ -174,13 +174,13 @@ private static class ExpectFailureInMust extends ExFailure {
*
* @since 0.51
*/
private static class ExpectFailureInThat extends ExFailure {
private static class ExThat extends ExFailure {
/**
* Ctor.
* @param cause Exception cause
* @param args Arguments for {@link String#format(String, Object...)}
*/
ExpectFailureInThat(final String cause, final Object... args) {
ExThat(final String cause, final Object... args) {
super(String.format(cause, args));
}
}
Expand Down
18 changes: 7 additions & 11 deletions eo-runtime/src/test/java/org/eolang/ExpectTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,6 @@
* @since 0.1.0
*/
final class ExpectTest {
/**
* Subject for Expect.
*/
private static final String SUBJ = "a number";

@Test
void buildsAndChecksWithoutErrors() {
Expand All @@ -59,13 +55,13 @@ void failsWithCorrectTraceWithOneError() {
"Throw error in first 'must'. Error message is correct",
Assertions.assertThrows(
ExFailure.class,
() -> new Expect<>(ExpectTest.SUBJ, () -> 42)
() -> new Expect<>("a number", () -> 42)
.must(i -> i < 0)
.otherwise("must be negative")
.it(),
"fails on check"
).getMessage(),
Matchers.equalTo(String.format("%s (42) must be negative", ExpectTest.SUBJ))
Matchers.equalTo("a number (42) must be negative")
);
}

Expand All @@ -75,15 +71,15 @@ void failsWithCorrectTraceWithTwoErrors() {
"Throw error in first 'must'. Not add error about second 'must'",
Assertions.assertThrows(
ExFailure.class,
() -> new Expect<>(ExpectTest.SUBJ, () -> 42.2)
() -> new Expect<>("a number", () -> 42.2)
.must(i -> i < 0)
.otherwise("must be negative")
.must(i -> i % 1 == 0)
.otherwise("must be an integer")
.it(),
"fails only for first 'must'"
).getMessage(),
Matchers.equalTo(String.format("%s (42.2) must be negative", ExpectTest.SUBJ))
Matchers.equalTo("a number (42.2) must be negative")
);
}

Expand All @@ -93,15 +89,15 @@ void failsWithCorrectTraceWithOneOkAndOneError() {
"Throw error in second 'must'. First 'must' passes check",
Assertions.assertThrows(
ExFailure.class,
() -> new Expect<>(ExpectTest.SUBJ, () -> 42.2)
() -> new Expect<>("a number", () -> 42.2)
.must(i -> i > 0)
.otherwise("must be positive")
.must(i -> i % 1 == 0)
.otherwise("must be an integer")
.it(),
"fails on checking integer"
).getMessage(),
Matchers.equalTo(String.format("%s (42.2) must be an integer", ExpectTest.SUBJ))
Matchers.equalTo("a number (42.2) must be an integer")
);
}

Expand All @@ -111,7 +107,7 @@ void failsWithCorrectTraceWithExFailureInThat() {
"Take error message from 'otherwise', not from original error",
Assertions.assertThrows(
ExFailure.class,
() -> new Expect<>(ExpectTest.SUBJ, () -> 42.2)
() -> new Expect<>("something", () -> 42.2)
.must(i -> i > 0)
.otherwise("must be positive")
.that(
Expand Down

0 comments on commit 154eae9

Please sign in to comment.