diff --git a/eo-runtime/src/test/java/EOorg/EOeolang/EOnumberTest.java b/eo-runtime/src/test/java/EOorg/EOeolang/EOnumberTest.java index 038ba939de..c128c37f87 100644 --- a/eo-runtime/src/test/java/EOorg/EOeolang/EOnumberTest.java +++ b/eo-runtime/src/test/java/EOorg/EOeolang/EOnumberTest.java @@ -28,16 +28,14 @@ */ package EOorg.EOeolang; // NOPMD +import matchers.ExpectNumberMatcher; import org.eolang.AtCompositeTest; import org.eolang.Attr; import org.eolang.Data; -import org.eolang.Dataized; -import org.eolang.ExAbstract; import org.eolang.PhWith; import org.eolang.Phi; import org.hamcrest.MatcherAssert; import org.hamcrest.Matchers; -import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.ValueSource; @@ -85,25 +83,19 @@ void hasDifferentHash() { EOnumber$EOplus.class, EOnumber$EOtimes.class }) - void throwsCorrectErrorForXAttr(final Class cls) { + void throwsCorrectErrorForXAttr(final Class cls) throws Exception { MatcherAssert.assertThat( - "the message in the error is correct", - Assertions.assertThrows( - ExAbstract.class, - () -> new Dataized( - new PhWith( - new PhWith( - (Phi) cls.getDeclaredConstructor().newInstance(), - Attr.RHO, - new Data.ToPhi(42) - ), - "x", - new Data.ToPhi(true) - ) - ).take(), - "operation with TRUE fails with a proper message that explains what happened" - ).getMessage(), - Matchers.equalTo("the 'x' attribute must be a number") + "attr use Expect.Number", + new PhWith( + new PhWith( + (Phi) cls.getDeclaredConstructor().newInstance(), + Attr.RHO, + new Data.ToPhi(42) + ), + "x", + new Data.ToPhi(true) + ), + new ExpectNumberMatcher() ); } @@ -116,21 +108,15 @@ void throwsCorrectErrorForXAttr(final Class cls) { EOnumber$EOas_i64.class, EOnumber$EOfloor.class }) - void throwsCorrectErrorForRhoAttr(final Class cls) { + void throwsCorrectErrorForRhoAttr(final Class cls) throws Exception { MatcherAssert.assertThat( - "the message in the error is correct", - Assertions.assertThrows( - ExAbstract.class, - () -> new Dataized( - new PhWith( - (Phi) cls.getDeclaredConstructor().newInstance(), - Attr.RHO, - new Data.ToPhi(true) - ) - ).take(), - "EOnumber must be a number" - ).getMessage(), - Matchers.equalTo("the 'ρ' attribute must be a number") + "attr use Expect.Number", + new PhWith( + (Phi) cls.getDeclaredConstructor().newInstance(), + Attr.RHO, + new Data.ToPhi(true) + ), + new ExpectNumberMatcher() ); } } diff --git a/eo-runtime/src/test/java/matchers/ExpectIntMatcher.java b/eo-runtime/src/test/java/matchers/ExpectIntMatcher.java new file mode 100644 index 0000000000..8b7588639a --- /dev/null +++ b/eo-runtime/src/test/java/matchers/ExpectIntMatcher.java @@ -0,0 +1,63 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2025 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/* + * @checkstyle PackageNameCheck (10 lines) + * @checkstyle TrailingCommentCheck (3 lines) + */ +package matchers; + +import org.eolang.Dataized; +import org.eolang.ExAbstract; +import org.eolang.Phi; +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; + +/** + * Matcher to check if dataizing a Phi throws an error + * with a message indicating that an attribute must be an integer. + * + * @since 0.52 + */ +public final class ExpectIntMatcher extends BaseMatcher { + + @Override + public boolean matches(final Object item) { + boolean matches = false; + try { + new Dataized((Phi) item).take(); + } catch (final ExAbstract ex) { + matches = ex + .getMessage() + .matches(".*the '.*' attribute \\(.*\\) must be an integer.*"); + } + return matches; + } + + @Override + public void describeTo(final Description description) { + description.appendText("Transform Phi to Int fails with correct error message"); + } + +} diff --git a/eo-runtime/src/test/java/matchers/ExpectNaturalMatcher.java b/eo-runtime/src/test/java/matchers/ExpectNaturalMatcher.java new file mode 100644 index 0000000000..71e5088e9c --- /dev/null +++ b/eo-runtime/src/test/java/matchers/ExpectNaturalMatcher.java @@ -0,0 +1,63 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2025 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/* + * @checkstyle PackageNameCheck (10 lines) + * @checkstyle TrailingCommentCheck (3 lines) + */ +package matchers; + +import org.eolang.Dataized; +import org.eolang.ExAbstract; +import org.eolang.Phi; +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; + +/** + * Matcher to check if dataizing a Phi throws an error + * with a message indicating that an attribute must be a natural. + * + * @since 0.52 + */ +public final class ExpectNaturalMatcher extends BaseMatcher { + + @Override + public boolean matches(final Object item) { + boolean matches = false; + try { + new Dataized((Phi) item).take(); + } catch (final ExAbstract ex) { + matches = ex + .getMessage() + .matches(".*the '.*' attribute \\(.*\\) must be greater or equal to zero.*"); + } + return matches; + } + + @Override + public void describeTo(final Description description) { + description.appendText("Transform Phi to Natural fails with correct error message"); + } + +} diff --git a/eo-runtime/src/test/java/matchers/ExpectNumberMatcher.java b/eo-runtime/src/test/java/matchers/ExpectNumberMatcher.java new file mode 100644 index 0000000000..98601b396d --- /dev/null +++ b/eo-runtime/src/test/java/matchers/ExpectNumberMatcher.java @@ -0,0 +1,63 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2025 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/* + * @checkstyle PackageNameCheck (10 lines) + * @checkstyle TrailingCommentCheck (3 lines) + */ +package matchers; + +import org.eolang.Dataized; +import org.eolang.ExAbstract; +import org.eolang.Phi; +import org.hamcrest.BaseMatcher; +import org.hamcrest.Description; + +/** + * Matcher to check if dataizing a Phi throws an error + * with a message indicating that an attribute must be a number. + * + * @since 0.52 + */ +public final class ExpectNumberMatcher extends BaseMatcher { + + @Override + public boolean matches(final Object item) { + boolean matches = false; + try { + new Dataized((Phi) item).take(); + } catch (final ExAbstract ex) { + matches = ex + .getMessage() + .matches(".*the '.*' attribute must be a number.*"); + } + return matches; + } + + @Override + public void describeTo(final Description description) { + description.appendText("Transform Phi to Number fails with correct error message"); + } + +} diff --git a/eo-runtime/src/test/java/matchers/package-info.java b/eo-runtime/src/test/java/matchers/package-info.java new file mode 100644 index 0000000000..483b70c898 --- /dev/null +++ b/eo-runtime/src/test/java/matchers/package-info.java @@ -0,0 +1,30 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2016-2025 Objectionary.com + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +/** + * Matchers. + * + * @since 0.52 + */ +package matchers;