From 6f9d0d7e0a3c5dd6f0ad813cd576c923a58e514b Mon Sep 17 00:00:00 2001 From: Andrew Smirnov Date: Fri, 17 Jan 2025 18:23:46 +0300 Subject: [PATCH] Fix wrong exception handling in Expect --- .../src/main/java/org/eolang/Expect.java | 31 ++++++++++++++++--- .../src/test/java/org/eolang/ExpectTest.java | 19 ++++++++++++ 2 files changed, 45 insertions(+), 5 deletions(-) diff --git a/eo-runtime/src/main/java/org/eolang/Expect.java b/eo-runtime/src/main/java/org/eolang/Expect.java index 3f19a85cff..e412b8c263 100644 --- a/eo-runtime/src/main/java/org/eolang/Expect.java +++ b/eo-runtime/src/main/java/org/eolang/Expect.java @@ -103,7 +103,7 @@ public Expect otherwise(final String message) { try { return this.sup.get(); } catch (final ExMust ex) { - throw new ExFailure( + throw new ExOtherwise( String.format( "%s %s %s", this.subject, @@ -113,7 +113,7 @@ public Expect otherwise(final String message) { ex ); } catch (final ExThat ex) { - throw new ExFailure( + throw new ExOtherwise( String.format( "%s %s", this.subject, @@ -152,7 +152,11 @@ public Expect must(final Function fun) { * @checkstyle MethodNameCheck (5 lines) */ public T it() { - return this.sup.get(); + try { + return this.sup.get(); + } catch (final ExOtherwise ex) { + throw new ExFailure(ex.getMessage(), ex); + } } /** @@ -161,7 +165,7 @@ public T it() { * * @since 0.51 */ - private static class ExMust extends ExFailure { + private static class ExMust extends RuntimeException { /** * Ctor. * @param cause Exception cause @@ -178,7 +182,7 @@ private static class ExMust extends ExFailure { * * @since 0.51 */ - private static class ExThat extends ExFailure { + private static class ExThat extends RuntimeException { /** * Ctor. * @param cause Exception cause @@ -189,4 +193,21 @@ private static class ExThat extends ExFailure { } } + /** + * This exception is used to enhance the error message + * in the {@link Expect#it()} method. + * + * @since 0.51 + */ + private static class ExOtherwise extends RuntimeException { + /** + * Ctor. + * @param cause Exception cause + * @param args Arguments for {@link String#format(String, Object...)} + */ + ExOtherwise(final String cause, final Object... args) { + super(String.format(cause, args)); + } + } + } diff --git a/eo-runtime/src/test/java/org/eolang/ExpectTest.java b/eo-runtime/src/test/java/org/eolang/ExpectTest.java index e5dcc8233f..9c08dc78b5 100644 --- a/eo-runtime/src/test/java/org/eolang/ExpectTest.java +++ b/eo-runtime/src/test/java/org/eolang/ExpectTest.java @@ -144,4 +144,23 @@ void failsWithCorrectTraceWithExFailureInThatForParsing() { Matchers.equalTo("attr must be an integer") ); } + + @Test + void failsWithCorrectTraceForMustAndThat() { + MatcherAssert.assertThat( + "Take error message from must", + Assertions.assertThrows( + ExFailure.class, + () -> new Expect<>("attr", () -> "string") + .must(i -> false) + .otherwise("in must") + .that(i -> i) + .otherwise("in that") + .it(), + "fails on 'must'" + ).getMessage(), + Matchers.equalTo("attr (string) in must") + ); + } + }