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

Add matchers for better tests attrs which uses Expect #3930

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
56 changes: 21 additions & 35 deletions eo-runtime/src/test/java/EOorg/EOeolang/EOnumberTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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()
);
}

Expand All @@ -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()
);
}
}
63 changes: 63 additions & 0 deletions eo-runtime/src/test/java/matchers/ExpectIntMatcher.java
Original file line number Diff line number Diff line change
@@ -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<Phi> {

@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");
}

}
63 changes: 63 additions & 0 deletions eo-runtime/src/test/java/matchers/ExpectNaturalMatcher.java
Original file line number Diff line number Diff line change
@@ -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<Phi> {

@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");
}

}
63 changes: 63 additions & 0 deletions eo-runtime/src/test/java/matchers/ExpectNumberMatcher.java
Original file line number Diff line number Diff line change
@@ -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<Phi> {

@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");
}

}
30 changes: 30 additions & 0 deletions eo-runtime/src/test/java/matchers/package-info.java
Original file line number Diff line number Diff line change
@@ -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;
Loading