✅ Add matchers for IdentityTrust objects
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 19 Dec 2025 15:19:46 +0000 (16:19 +0100)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Fri, 19 Dec 2025 15:19:46 +0000 (16:19 +0100)
src/test/java/net/pterodactylus/fcp/test/IdentityTrustMatchers.java [new file with mode: 0644]
src/test/java/net/pterodactylus/fcp/test/IdentityTrustMatchersTest.java [new file with mode: 0644]

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 (file)
index 0000000..2babdcf
--- /dev/null
@@ -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<IdentityTrust> hasTrust(int trust) {
+               return new TypeSafeDiagnosingMatcher<IdentityTrust>() {
+                       @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<IdentityTrust> hasComment(String comment) {
+               return new TypeSafeDiagnosingMatcher<IdentityTrust>() {
+                       @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 (file)
index 0000000..9138f8c
--- /dev/null
@@ -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();
+
+}