From: David ‘Bombe’ Roden Date: Fri, 12 Dec 2025 16:50:00 +0000 (+0100) Subject: ✅ Add matcher for an Identity’s ID X-Git-Url: https://git.pterodactylus.net/?a=commitdiff_plain;h=06ee0fe4c9e0d106e9035d896ef708ace90c6d84;p=jFCPlib.git ✅ Add matcher for an Identity’s ID --- diff --git a/src/test/java/net/pterodactylus/fcp/test/IdentityMatchers.java b/src/test/java/net/pterodactylus/fcp/test/IdentityMatchers.java index 982e154..829a50d 100644 --- a/src/test/java/net/pterodactylus/fcp/test/IdentityMatchers.java +++ b/src/test/java/net/pterodactylus/fcp/test/IdentityMatchers.java @@ -1,13 +1,14 @@ package net.pterodactylus.fcp.test; +import java.util.Map; +import java.util.Set; import net.pterodactylus.fcp.plugin.Identity; import net.pterodactylus.fcp.plugin.OwnIdentity; import org.hamcrest.Description; import org.hamcrest.Matcher; import org.hamcrest.TypeSafeDiagnosingMatcher; -import java.util.Map; -import java.util.Set; +import static org.hamcrest.Matchers.equalTo; public class IdentityMatchers { @@ -107,4 +108,80 @@ public class IdentityMatchers { }; } + /** + * Creates a {@link Matcher} for {@link Identity Identities} which + * verifies that the {@link Identity#getIdentifier() ID} of the + * {@link Identity} matches the given ID. + * + * @param id The ID to match + * @return A {@link Matcher} for {@link Identity Identities} + */ + public static Matcher hasId(String id) { + return hasId(equalTo(id)); + } + + /** + * Creates a {@link Matcher} for {@link Identity Identities} which + * verifies the {@link Identity#getIdentifier() ID} of the + * {@link Identity} against the given {@link Matcher}. + * + * @param idMatcher The matcher to verify the ID against + * @return A {@link Matcher} for {@link Identity Identities} + */ + public static Matcher hasId(Matcher idMatcher) { + return new TypeSafeDiagnosingMatcher() { + @Override + protected boolean matchesSafely(Identity identity, Description mismatchDescription) { + if (!idMatcher.matches(identity.getIdentifier())) { + mismatchDescription.appendText("was ").appendValue(identity.getIdentifier()); + return false; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("has ID matching ").appendDescriptionOf(idMatcher); + } + }; + } + + /** + * Creates a {@link Matcher} for {@link Identity Identities} which + * verifies the {@link Identity#getNickname() nickname} of the + * {@link Identity} against the given nickname. + * + * @param nickname The nickname to match + * @return A {@link Matcher} for {@link Identity Identities} + */ + public static Matcher hasNickname(String nickname) { + return hasNickname(equalTo(nickname)); + } + + /** + * Creates a {@link Matcher} for {@link Identity Identities} which + * verifies the {@link Identity#getNickname() nickname} of the + * {@link Identity} against the given {@link Matcher}. + * + * @param nicknameMatcher The matcher to verify the nickname against + * @return A {@link Matcher} for {@link Identity Identities} + */ + public static Matcher hasNickname(Matcher nicknameMatcher) { + return new TypeSafeDiagnosingMatcher() { + @Override + protected boolean matchesSafely(Identity identity, Description mismatchDescription) { + if (!nicknameMatcher.matches(identity.getNickname())) { + mismatchDescription.appendText("was ").appendValue(identity.getNickname()); + return false; + } + return true; + } + + @Override + public void describeTo(Description description) { + description.appendText("has nickname matching ").appendDescriptionOf(nicknameMatcher); + } + }; + } + } diff --git a/src/test/java/net/pterodactylus/fcp/test/IdentityMatchersTest.java b/src/test/java/net/pterodactylus/fcp/test/IdentityMatchersTest.java new file mode 100644 index 0000000..6d6e93d --- /dev/null +++ b/src/test/java/net/pterodactylus/fcp/test/IdentityMatchersTest.java @@ -0,0 +1,131 @@ +package net.pterodactylus.fcp.test; + +import net.pterodactylus.fcp.plugin.Identity; +import net.pterodactylus.fcp.plugin.OwnIdentity; +import org.hamcrest.Description; +import org.hamcrest.StringDescription; +import org.junit.Test; + +import static java.util.Collections.emptyList; +import static java.util.Collections.emptyMap; +import static net.pterodactylus.fcp.test.IdentityMatchers.hasId; +import static net.pterodactylus.fcp.test.IdentityMatchers.hasNickname; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.any; +import static org.hamcrest.Matchers.equalTo; + +public class IdentityMatchersTest { + + @Test + public void hasIdWithMatcherDoesNotMatchIfIdIsDifferent() { + assertThat(hasId(equalTo("abc")).matches(identity), equalTo(false)); + } + + @Test + public void hasIdWithMatcherDescribesMismatchCorrectly() { + hasId(equalTo("abc")).describeMismatch(identity, description); + assertThat(description.toString(), equalTo("was \"id\"")); + } + + @Test + public void hasIdWithMatcherMatchesIfIdMatches() { + assertThat(hasId(equalTo("id")).matches(identity), equalTo(true)); + } + + @Test + public void hasIdWithMatcherDescribesItselfCorrectly() { + hasId(any(String.class)).describeTo(description); + assertThat(description.toString(), equalTo("has ID matching an instance of java.lang.String")); + } + + @Test + public void hasIdWithMatcherMatchesOwnIdentityAsWell() { + assertThat(hasId(equalTo("id")).matches(ownIdentity), equalTo(true)); + } + + @Test + public void hasIdWithStringDoesNotMatchIfIdIsDifferent() { + assertThat(hasId("abc").matches(identity), equalTo(false)); + } + + @Test + public void hasIdWithStringDescribesMismatchCorrectly() { + hasId("abc").describeMismatch(identity, description); + assertThat(description.toString(), equalTo("was \"id\"")); + } + + @Test + public void hasIdWithStringMatchesIfIdMatches() { + assertThat(hasId("id").matches(identity), equalTo(true)); + } + + @Test + public void hasIdWithStringDescribesItselfCorrectly() { + hasId("abc").describeTo(description); + assertThat(description.toString(), equalTo("has ID matching \"abc\"")); + } + + @Test + public void hasIdWithStringMatchesOwnIdentityAsWell() { + assertThat(hasId("id").matches(ownIdentity), equalTo(true)); + } + + @Test + public void hasNicknameWithMatcherDoesNotMatchIfNicknameIsDifferent() { + assertThat(hasNickname(equalTo("foo")).matches(identity), equalTo(false)); + } + + @Test + public void hasNicknameWithMatcherDescribesMismatchCorrectly() { + hasNickname(equalTo("foo")).describeMismatch(identity, description); + assertThat(description.toString(), equalTo("was \"nickname\"")); + } + + @Test + public void hasNicknameWithMatcherMatchesIfNicknameMatches() { + assertThat(hasNickname(equalTo("nickname")).matches(identity), equalTo(true)); + } + + @Test + public void hasNicknameWithMatcherDescribesItselfCorrectly() { + hasNickname(equalTo("nickname")).describeTo(description); + assertThat(description.toString(), equalTo("has nickname matching \"nickname\"")); + } + + @Test + public void hasNicknameWithMatcherMatchesOwnIdentityAsWell() { + assertThat(hasNickname(equalTo("nickname")).matches(ownIdentity), equalTo(true)); + } + + @Test + public void hasNicknameWithStringDoesNotMatchIfNicknameIsDifferent() { + assertThat(hasNickname("foo").matches(identity), equalTo(false)); + } + + @Test + public void hasNicknameWithStringDescribesMismatchCorrectly() { + hasNickname("foo").describeMismatch(identity, description); + assertThat(description.toString(), equalTo("was \"nickname\"")); + } + + @Test + public void hasNicknameWithStringMatchesIfNicknameMatches() { + assertThat(hasNickname("nickname").matches(identity), equalTo(true)); + } + + @Test + public void hasNicknameWithStringDescribesItselfCorrectly() { + hasNickname("nickname").describeTo(description); + assertThat(description.toString(), equalTo("has nickname matching \"nickname\"")); + } + + @Test + public void hasNicknameWithStringMatchesOwnIdentityAsWell() { + assertThat(hasNickname("nickname").matches(ownIdentity), equalTo(true)); + } + + private final Identity identity = new Identity("id", "nickname", "request-uri", emptyList(), emptyMap()); + private final OwnIdentity ownIdentity = new OwnIdentity("id", "nickname", "request-uri", "insert-uri", emptyList(), emptyMap()); + private final Description description = new StringDescription(); + +}