From e6ecd46e4d6469204111ad1c04ee27f01ca27ad4 Mon Sep 17 00:00:00 2001 From: Andrew Smirnov Date: Fri, 17 Jan 2025 17:53:33 +0300 Subject: [PATCH 1/4] Add inner Integer and Number to Expect --- .../src/main/java/org/eolang/Expect.java | 59 +++++++++++++++++ .../src/test/java/org/eolang/ExpectTest.java | 66 +++++++++++++++++++ 2 files changed, 125 insertions(+) diff --git a/eo-runtime/src/main/java/org/eolang/Expect.java b/eo-runtime/src/main/java/org/eolang/Expect.java index e412b8c263..f7a6837ef7 100644 --- a/eo-runtime/src/main/java/org/eolang/Expect.java +++ b/eo-runtime/src/main/java/org/eolang/Expect.java @@ -210,4 +210,63 @@ private static class ExOtherwise extends RuntimeException { } } + /** + * Transform Expect to Number + * + * @since 0.51 + */ + public static class Number { + + /** + * Expect. + */ + private final Expect expect; + + /** + * Ctor. + * @param expect Expect + */ + public Number(final Expect expect) { + this.expect = expect; + } + + 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 class Integer { + + /** + * Expect. + */ + private final Expect expect; + + /** + * Ctor. + * @param expect Expect + */ + public Integer(final Expect expect) { + this.expect = expect; + } + + 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(); + } + } + } diff --git a/eo-runtime/src/test/java/org/eolang/ExpectTest.java b/eo-runtime/src/test/java/org/eolang/ExpectTest.java index 9c08dc78b5..eb2d0508d7 100644 --- a/eo-runtime/src/test/java/org/eolang/ExpectTest.java +++ b/eo-runtime/src/test/java/org/eolang/ExpectTest.java @@ -163,4 +163,70 @@ 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.Integer( + 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.Integer( + 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") + ); + } + } From 4dc93e8f55d795621dc714b7be9fc02ca72605dd Mon Sep 17 00:00:00 2001 From: Andrew Smirnov Date: Fri, 17 Jan 2025 22:37:42 +0300 Subject: [PATCH 2/4] Add Expect.NonNegativeInteger --- .../src/main/java/org/eolang/Expect.java | 65 ++++++++++++++++-- .../src/test/java/org/eolang/ExpectTest.java | 67 +++++++++++++++++++ 2 files changed, 125 insertions(+), 7 deletions(-) diff --git a/eo-runtime/src/main/java/org/eolang/Expect.java b/eo-runtime/src/main/java/org/eolang/Expect.java index f7a6837ef7..0a8249c7e1 100644 --- a/eo-runtime/src/main/java/org/eolang/Expect.java +++ b/eo-runtime/src/main/java/org/eolang/Expect.java @@ -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 @@ -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 @@ -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 @@ -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. @@ -230,6 +231,11 @@ public Number(final Expect 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()) @@ -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. @@ -258,6 +265,48 @@ public Integer(final Expect 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 expect; + + /** + * Ctor. + * @param expect Expect + */ + public NonNegativeInteger(final Expect 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()) @@ -265,6 +314,8 @@ public java.lang.Integer it() { .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(); } } diff --git a/eo-runtime/src/test/java/org/eolang/ExpectTest.java b/eo-runtime/src/test/java/org/eolang/ExpectTest.java index eb2d0508d7..a37014a8c5 100644 --- a/eo-runtime/src/test/java/org/eolang/ExpectTest.java +++ b/eo-runtime/src/test/java/org/eolang/ExpectTest.java @@ -33,6 +33,7 @@ * * @since 0.1.0 */ +@SuppressWarnings("PMD.TooManyMethods") final class ExpectTest { @Test @@ -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") + ); + } + } From 10943e24622aaecf7a4a5570cf36155e8ce6f2e2 Mon Sep 17 00:00:00 2001 From: Andrew Smirnov Date: Fri, 17 Jan 2025 22:54:46 +0300 Subject: [PATCH 3/4] remove useless SuppressWarnings --- eo-runtime/src/main/java/org/eolang/Expect.java | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/eo-runtime/src/main/java/org/eolang/Expect.java b/eo-runtime/src/main/java/org/eolang/Expect.java index 0a8249c7e1..423102f49a 100644 --- a/eo-runtime/src/main/java/org/eolang/Expect.java +++ b/eo-runtime/src/main/java/org/eolang/Expect.java @@ -34,7 +34,7 @@ * @param The type of result * @since 0.41.0 */ -@SuppressWarnings("PMD.ShortMethodName") +@SuppressWarnings({"PMD.ShortMethodName", "PMD.UnnecessaryFullyQualifiedName"}) public class Expect { /** @@ -215,7 +215,6 @@ private static final class ExOtherwise extends RuntimeException { * * @since 0.51 */ - @SuppressWarnings("PMD.ShortMethodName") public static final class Number { /** @@ -249,7 +248,6 @@ public Double it() { * * @since 0.51 */ - @SuppressWarnings({"PMD.ShortMethodName", "PMD.UnnecessaryFullyQualifiedName"}) public static final class Integer { /** @@ -286,7 +284,6 @@ public java.lang.Integer it() { * * @since 0.51 */ - @SuppressWarnings({"PMD.ShortMethodName", "PMD.UnnecessaryFullyQualifiedName"}) public static final class NonNegativeInteger { /** From 47058c52339a97fcea0038b6ce288016f6af2775 Mon Sep 17 00:00:00 2001 From: Andrew Smirnov Date: Sat, 18 Jan 2025 10:02:01 +0300 Subject: [PATCH 4/4] fix PMD errors --- eo-runtime/src/main/java/org/eolang/Expect.java | 17 +++++++++-------- .../src/test/java/org/eolang/ExpectTest.java | 10 +++++----- 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/eo-runtime/src/main/java/org/eolang/Expect.java b/eo-runtime/src/main/java/org/eolang/Expect.java index 423102f49a..dc266506e0 100644 --- a/eo-runtime/src/main/java/org/eolang/Expect.java +++ b/eo-runtime/src/main/java/org/eolang/Expect.java @@ -34,7 +34,7 @@ * @param The type of result * @since 0.41.0 */ -@SuppressWarnings({"PMD.ShortMethodName", "PMD.UnnecessaryFullyQualifiedName"}) +@SuppressWarnings("PMD.ShortMethodName") public class Expect { /** @@ -248,7 +248,7 @@ public Double it() { * * @since 0.51 */ - public static final class Integer { + public static final class Int { /** * Expect. @@ -259,7 +259,7 @@ public static final class Integer { * Ctor. * @param expect Expect */ - public Integer(final Expect expect) { + public Int(final Expect expect) { this.expect = expect; } @@ -268,7 +268,7 @@ public Integer(final Expect expect) { * @return The token * @checkstyle MethodNameCheck (5 lines) */ - public java.lang.Integer it() { + public Integer it() { return this.expect .that(phi -> new Dataized(phi).asNumber()) .otherwise("must be a number") @@ -280,11 +280,12 @@ public java.lang.Integer it() { } /** - * Transform Expect to NonNegativeInteger. + * Transform Expect to Natural number. + * Natural number is integer greater or equal to zero. * * @since 0.51 */ - public static final class NonNegativeInteger { + public static final class Natural { /** * Expect. @@ -295,7 +296,7 @@ public static final class NonNegativeInteger { * Ctor. * @param expect Expect */ - public NonNegativeInteger(final Expect expect) { + public Natural(final Expect expect) { this.expect = expect; } @@ -304,7 +305,7 @@ public NonNegativeInteger(final Expect expect) { * @return The token * @checkstyle MethodNameCheck (5 lines) */ - public java.lang.Integer it() { + public Integer it() { return this.expect .that(phi -> new Dataized(phi).asNumber()) .otherwise("must be a number") diff --git a/eo-runtime/src/test/java/org/eolang/ExpectTest.java b/eo-runtime/src/test/java/org/eolang/ExpectTest.java index a37014a8c5..6b1fa81880 100644 --- a/eo-runtime/src/test/java/org/eolang/ExpectTest.java +++ b/eo-runtime/src/test/java/org/eolang/ExpectTest.java @@ -192,7 +192,7 @@ void failsInTransformingToIntegerForNotNumber() { "inner class Integer throws error for not a number", Assertions.assertThrows( ExFailure.class, - () -> new Expect.Integer( + () -> new Expect.Int( Expect.at( new PhWith( new PhDefault(), @@ -214,7 +214,7 @@ void failsInTransformingToIntegerForNotInteger() { "inner class Integer throws error for not an integer number", Assertions.assertThrows( ExFailure.class, - () -> new Expect.Integer( + () -> new Expect.Int( Expect.at( new PhWith( new PhDefault(), @@ -236,7 +236,7 @@ void failsInTransformingToNonNegativeIntegerForNotNumber() { "inner class NonNegativeInteger throws error for not a number", Assertions.assertThrows( ExFailure.class, - () -> new Expect.NonNegativeInteger( + () -> new Expect.Natural( Expect.at( new PhWith( new PhDefault(), @@ -258,7 +258,7 @@ void failsInTransformingToNonNegativeIntegerForNotInteger() { "inner class NonNegativeInteger throws error for not an integer number", Assertions.assertThrows( ExFailure.class, - () -> new Expect.NonNegativeInteger( + () -> new Expect.Natural( Expect.at( new PhWith( new PhDefault(), @@ -280,7 +280,7 @@ void failsInTransformingToNonNegativeIntegerForNegative() { "inner class NonNegativeInteger throws error for a negative integer", Assertions.assertThrows( ExFailure.class, - () -> new Expect.NonNegativeInteger( + () -> new Expect.Natural( Expect.at( new PhWith( new PhDefault(),