🚧 Add matcher for identities
[Sone.git] / src / test / kotlin / net / pterodactylus / sone / test / Matchers.kt
index c084d35..644f192 100644 (file)
@@ -1,7 +1,10 @@
 package net.pterodactylus.sone.test
 
+import net.pterodactylus.sone.freenet.wot.*
+import net.pterodactylus.sone.utils.*
 import net.pterodactylus.util.web.*
 import org.hamcrest.*
+import org.hamcrest.Matchers.*
 
 fun hasHeader(name: String, value: String) = object : TypeSafeDiagnosingMatcher<Header>() {
        override fun matchesSafely(item: Header, mismatchDescription: Description) =
@@ -19,3 +22,46 @@ fun <T : Any> compare(value: T, comparison: (T) -> Boolean, onError: (T) -> Unit
                false.takeUnless { comparison(value) }
                                ?.also { onError(value) }
 
+fun <K, V> isEmptyMap() = object : TypeSafeDiagnosingMatcher<Map<K, V>>() {
+       override fun describeTo(description: Description) {
+               description.appendText("empty map")
+       }
+
+       override fun matchesSafely(item: Map<K, V>, mismatchDescription: Description) =
+                       item.isEmpty().onFalse {
+                               mismatchDescription.appendText("was ").appendValue(item)
+                       }
+}
+
+fun isTrust(trust: Int?, score: Int?, rank: Int?) =
+               AttributeMatcher<Trust>("trust")
+                               .addAttribute("trust", trust, Trust::explicit)
+                               .addAttribute("score", score, Trust::implicit)
+                               .addAttribute("rank", rank, Trust::distance)
+
+fun isTrusted(ownIdentity: OwnIdentity, trust: Matcher<Trust>) = object : TypeSafeDiagnosingMatcher<Identity>() {
+       override fun matchesSafely(item: Identity, mismatchDescription: Description) =
+                       item.getTrust(ownIdentity)?.let { foundTrust ->
+                               trust.matches(foundTrust).onFalse {
+                                       trust.describeMismatch(foundTrust, mismatchDescription)
+                               }
+                       } ?: {
+                               mismatchDescription.appendText("not trusted")
+                               false
+                       }()
+
+       override fun describeTo(description: Description) {
+               description
+                               .appendText("trusted by ").appendValue(ownIdentity)
+                               .appendText(" with ").appendValue(trust)
+       }
+}
+
+fun isIdentity(id: String, nickname: String, requestUri: String, contexts: Matcher<out Iterable<String>>, properties: Matcher<out Map<out String, String>>) =
+               AttributeMatcher<Identity>("identity")
+                               .addAttribute("id", id, Identity::getId)
+                               .addAttribute("nickname", nickname, Identity::getNickname)
+                               .addAttribute("requestUri", requestUri, Identity::getRequestUri)
+                               .addAttribute("contexts", Identity::getContexts, contexts)
+                               .addAttribute("properties", Identity::getProperties, properties)
+