From 1a4377f4a7c66590ef48ea6b9e5993c2f184ea2f Mon Sep 17 00:00:00 2001 From: =?utf8?q?David=20=E2=80=98Bombe=E2=80=99=20Roden?= Date: Fri, 19 Dec 2025 16:19:46 +0100 Subject: [PATCH] =?utf8?q?=E2=9C=85=20Add=20matchers=20for=20IdentityTrust?= =?utf8?q?=20objects?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- .../fcp/test/IdentityTrustMatchers.java | 63 ++++++++++++++++++++++ .../fcp/test/IdentityTrustMatchersTest.java | 61 +++++++++++++++++++++ 2 files changed, 124 insertions(+) create mode 100644 src/test/java/net/pterodactylus/fcp/test/IdentityTrustMatchers.java create mode 100644 src/test/java/net/pterodactylus/fcp/test/IdentityTrustMatchersTest.java diff --git a/src/test/java/net/pterodactylus/fcp/test/IdentityTrustMatchers.java b/src/test/java/net/pterodactylus/fcp/test/IdentityTrustMatchers.java new file mode 100644 index 0000000..2babdcf --- /dev/null +++ b/src/test/java/net/pterodactylus/fcp/test/IdentityTrustMatchers.java @@ -0,0 +1,63 @@ +package net.pterodactylus.fcp.test; + +import java.util.Objects; +import net.pterodactylus.fcp.plugin.IdentityTrust; +import org.hamcrest.Description; +import org.hamcrest.Matcher; +import org.hamcrest.TypeSafeDiagnosingMatcher; + +public class IdentityTrustMatchers { + + /** + * Creates a {@link Matcher} for {@link IdentityTrust} objects which + * verifies its {@link IdentityTrust#getTrust() trust} value against the + * given trust value. + * + * @param trust The value to verify the trust against + * @return A {@link Matcher} for {@link IdentityTrust} objects + */ + public static Matcher hasTrust(int trust) { + return new TypeSafeDiagnosingMatcher() { + @Override + protected boolean matchesSafely(IdentityTrust identityTrust, Description mismatchDescription) { + if (identityTrust.getTrust() != trust) { + mismatchDescription.appendText("trust was ").appendValue(identityTrust.getTrust()); + return false; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("has trust ").appendValue(trust); + } + }; + } + + /** + * Creates a {@link Matcher} for {@link IdentityTrust} objects which + * verifies its {@link IdentityTrust#getComment() comment} against the + * given comment. + * + * @param comment The value to verify the comment against + * @return A {@link Matcher} for {@link IdentityTrust} objects + */ + public static Matcher hasComment(String comment) { + return new TypeSafeDiagnosingMatcher() { + @Override + protected boolean matchesSafely(IdentityTrust identityTrust, Description mismatchDescription) { + if (!Objects.equals(identityTrust.getComment(), comment)) { + mismatchDescription.appendText("comment was ").appendValue(identityTrust.getComment()); + return false; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("has comment ").appendValue(comment); + } + }; + } + +} diff --git a/src/test/java/net/pterodactylus/fcp/test/IdentityTrustMatchersTest.java b/src/test/java/net/pterodactylus/fcp/test/IdentityTrustMatchersTest.java new file mode 100644 index 0000000..9138f8c --- /dev/null +++ b/src/test/java/net/pterodactylus/fcp/test/IdentityTrustMatchersTest.java @@ -0,0 +1,61 @@ +package net.pterodactylus.fcp.test; + +import net.pterodactylus.fcp.plugin.IdentityTrust; +import org.hamcrest.Description; +import org.hamcrest.StringDescription; +import org.junit.Test; + +import static net.pterodactylus.fcp.test.IdentityTrustMatchers.hasComment; +import static net.pterodactylus.fcp.test.IdentityTrustMatchers.hasTrust; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; + +public class IdentityTrustMatchersTest { + + @Test + public void hasTrustMatcherDoesNotMatchIfTrustIsDifferent() { + assertThat(hasTrust(123).matches(new IdentityTrust((byte) 99, "")), equalTo(false)); + } + + @Test + public void hasTrustMatcherDescribesMismatchCorrectly() { + hasTrust(123).describeMismatch(new IdentityTrust((byte) 99, ""), description); + assertThat(description.toString(), equalTo("trust was <99b>")); + } + + @Test + public void hasTrustMatcherMatchesIfTrustIsTheSame() { + assertThat(hasTrust(123).matches(new IdentityTrust((byte) 123, "")), equalTo(true)); + } + + @Test + public void hasTrustMatcherDescribesItselfCorrectly() { + hasTrust(123).describeTo(description); + assertThat(description.toString(), equalTo("has trust <123>")); + } + + @Test + public void hasCommentMatcherDoesNotMatchIfCommentIsDifferent() { + assertThat(hasComment("bar").matches(new IdentityTrust((byte) 123, "foo")), equalTo(false)); + } + + @Test + public void hasCommentMatcherDescribesMismatchCorrectly() { + hasComment("bar").describeMismatch(new IdentityTrust((byte) 123, "foo"), description); + assertThat(description.toString(), equalTo("comment was \"foo\"")); + } + + @Test + public void hasCommentMatcherMatchesIfCommentIsTheSame() { + assertThat(hasComment("foo").matches(new IdentityTrust((byte) 123, "foo")), equalTo(true)); + } + + @Test + public void hasCommentMatcherDescribesItselfCorrectly() { + hasComment("foo").describeTo(description); + assertThat(description.toString(), equalTo("has comment \"foo\"")); + } + + private final Description description = new StringDescription(); + +} -- 2.7.4