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=d941c01e44716fc9a74aee3d1cd46a2b6651bba9;hb=HEAD;hpb=1ccf70bc878f6cdba8affdbe6d34c4a4f811e81a diff --git a/src/test/kotlin/net/pterodactylus/sone/test/Matchers.kt b/src/test/kotlin/net/pterodactylus/sone/test/Matchers.kt index d941c01..1f479f6 100644 --- a/src/test/kotlin/net/pterodactylus/sone/test/Matchers.kt +++ b/src/test/kotlin/net/pterodactylus/sone/test/Matchers.kt @@ -1,11 +1,30 @@ 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) = compare(item.name, { it.equals(name, ignoreCase = true) }) { mismatchDescription.appendText("name is ").appendValue(it) } @@ -57,7 +76,7 @@ fun isTrusted(ownIdentity: OwnIdentity, trust: Matcher) = object : TypeSa } } -fun isIdentity(id: String, nickname: String, requestUri: String, contexts: Matcher>, properties: Matcher>) = +fun isIdentity(id: String, nickname: String?, requestUri: String, contexts: Matcher>, properties: Matcher>) = AttributeMatcher("identity") .addAttribute("id", id, Identity::getId) .addAttribute("nickname", nickname, Identity::getNickname) @@ -65,6 +84,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>, 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.