🚧 Add matcher for simple field sets
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / test / Matchers.kt
index d941c01..e7588db 100644 (file)
@@ -1,5 +1,6 @@
 package net.pterodactylus.sone.test
 
+import freenet.support.*
 import net.pterodactylus.sone.freenet.wot.*
 import net.pterodactylus.sone.utils.*
 import net.pterodactylus.util.web.*
@@ -65,6 +66,28 @@ fun isIdentity(id: String, nickname: String, requestUri: String, contexts: Match
                                .addAttribute("contexts", Identity::getContexts, contexts)
                                .addAttribute("properties", Identity::getProperties, properties)
 
+fun isOwnIdentity(id: String, nickname: String, requestUri: String, insertUri: String, contexts: Matcher<Iterable<String>>, properties: Matcher<Map<out String, String>>) =
+               AttributeMatcher<OwnIdentity>("own identity")
+                               .addAttribute("id", id, OwnIdentity::getId)
+                               .addAttribute("nickname", nickname, OwnIdentity::getNickname)
+                               .addAttribute("request uri", requestUri, OwnIdentity::getRequestUri)
+                               .addAttribute("insert uri", insertUri, OwnIdentity::getInsertUri)
+                               .addAttribute("contexts", OwnIdentity::getContexts, contexts)
+                               .addAttribute("properties", OwnIdentity::getProperties, properties)
+
+fun hasField(name: String, valueMatcher: Matcher<String>) = object : TypeSafeDiagnosingMatcher<SimpleFieldSet>() {
+       override fun matchesSafely(item: SimpleFieldSet, mismatchDescription: Description) =
+                       valueMatcher.matches(item.get(name)).onFalse {
+                               valueMatcher.describeMismatch(item, mismatchDescription)
+                       }
+
+       override fun describeTo(description: Description) {
+               description
+                               .appendText("simple field set with key ").appendValue(name)
+                               .appendText(", value ").appendValue(valueMatcher)
+       }
+}
+
 /**
  * [TypeSafeDiagnosingMatcher] implementation that aims to cut down boilerplate on verifying the attributes
  * of typical container objects.