X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ftest%2FMatchers.kt;h=1f479f6ecec350fa76b599bcf43b9767535aee26;hp=c084d35ffdb9489f80aff75e83fb9187a2e41c09;hb=HEAD;hpb=03cec6a6772c2d836d94864adddaf544cbe9d72f diff --git a/src/test/kotlin/net/pterodactylus/sone/test/Matchers.kt b/src/test/kotlin/net/pterodactylus/sone/test/Matchers.kt index c084d35..1f479f6 100644 --- a/src/test/kotlin/net/pterodactylus/sone/test/Matchers.kt +++ b/src/test/kotlin/net/pterodactylus/sone/test/Matchers.kt @@ -1,7 +1,29 @@ package net.pterodactylus.sone.test +import freenet.support.* +import net.pterodactylus.sone.freenet.wot.* +import net.pterodactylus.sone.utils.* import net.pterodactylus.util.web.* import org.hamcrest.* +import org.hamcrest.Matchers.* + +/** + * Returns a [hamcrest matcher][Matcher] constructed from the given predicate. + */ +fun matches(description: String? = null, predicate: (T) -> Boolean) = object : TypeSafeDiagnosingMatcher() { + + override fun matchesSafely(item: T, mismatchDescription: Description) = + predicate(item).onFalse { + mismatchDescription.appendValue(item).appendText(" did not match predicate") + } + + override fun describeTo(description: Description) { + description.appendText("matches predicate ").appendValue(predicate) + } + +}.let { matcher -> + description?.let { describedAs(description, matcher) } ?: matcher +} fun hasHeader(name: String, value: String) = object : TypeSafeDiagnosingMatcher
() { override fun matchesSafely(item: Header, mismatchDescription: Description) = @@ -19,3 +41,123 @@ fun compare(value: T, comparison: (T) -> Boolean, onError: (T) -> Unit false.takeUnless { comparison(value) } ?.also { onError(value) } +fun isEmptyMap() = object : TypeSafeDiagnosingMatcher>() { + override fun describeTo(description: Description) { + description.appendText("empty map") + } + + override fun matchesSafely(item: Map, mismatchDescription: Description) = + item.isEmpty().onFalse { + mismatchDescription.appendText("was ").appendValue(item) + } +} + +fun isTrust(trust: Int?, score: Int?, rank: Int?) = + AttributeMatcher("trust") + .addAttribute("trust", trust, Trust::explicit) + .addAttribute("score", score, Trust::implicit) + .addAttribute("rank", rank, Trust::distance) + +fun isTrusted(ownIdentity: OwnIdentity, trust: Matcher) = object : TypeSafeDiagnosingMatcher() { + 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>, properties: Matcher>) = + AttributeMatcher("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) + +fun isOwnIdentity(id: String, nickname: String, requestUri: String, insertUri: String, contexts: Matcher>, properties: Matcher>) = + AttributeMatcher("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) = object : TypeSafeDiagnosingMatcher() { + 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. + */ +class AttributeMatcher(private val objectName: String) : TypeSafeDiagnosingMatcher() { + + private data class AttributeToMatch( + val name: String, + val getter: (T) -> V, + val matcher: Matcher + ) + + private val attributesToMatch = mutableListOf>() + + /** + * Adds an attribute to check for equality, returning `this`. + */ + fun addAttribute(name: String, expected: V, getter: (T) -> V): AttributeMatcher = apply { + attributesToMatch.add(AttributeToMatch(name, getter, describedAs("$name %0", equalTo(expected), expected))) + } + + /** + * Adds an attribute to check with the given [hamcrest matcher][Matcher]. + */ + fun addAttribute(name: String, getter: (T) -> V, matcher: Matcher) = apply { + attributesToMatch.add(AttributeToMatch(name, getter, matcher)) + } + + override fun describeTo(description: Description) { + attributesToMatch.forEachIndexed { index, attributeToMatch -> + if (index == 0) { + description.appendText("$objectName with ") + } else { + description.appendText(", ") + } + attributeToMatch.matcher.describeTo(description) + } + } + + override fun matchesSafely(item: T, mismatchDescription: Description): Boolean = + attributesToMatch.fold(true) { matches, attributeToMatch -> + if (!matches) { + false + } else { + if (!attributeToMatch.matcher.matches(attributeToMatch.getter(item))) { + mismatchDescription.appendText("but ${attributeToMatch.name} ") + attributeToMatch.matcher.describeMismatch(attributeToMatch.getter(item), mismatchDescription) + false + } else { + true + } + } + } + +}