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 {
};
}
+ /**
+ * 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<? extends Identity> 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<? extends Identity> hasId(Matcher<? super String> idMatcher) {
+ return new TypeSafeDiagnosingMatcher<Identity>() {
+ @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<? extends Identity> 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<? extends Identity> hasNickname(Matcher<? super String> nicknameMatcher) {
+ return new TypeSafeDiagnosingMatcher<Identity>() {
+ @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);
+ }
+ };
+ }
+
}
--- /dev/null
+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();
+
+}