X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;ds=sidebyside;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ftest%2FMatchers.kt;h=9d215573069a6aac03000f32153998d82e456d4c;hb=23e09dfc13a0cc6dfc7d7b8cc09c78acc6984ec7;hp=21d225c6fcb1eea5fceca9a3e9b5854d6b0ad8a4;hpb=d8b8330c6f0a28a59586970648dd166ca48fe2ee;p=Sone.git diff --git a/src/test/kotlin/net/pterodactylus/sone/test/Matchers.kt b/src/test/kotlin/net/pterodactylus/sone/test/Matchers.kt index 21d225c..9d21557 100644 --- a/src/test/kotlin/net/pterodactylus/sone/test/Matchers.kt +++ b/src/test/kotlin/net/pterodactylus/sone/test/Matchers.kt @@ -1,11 +1,37 @@ package net.pterodactylus.sone.test +import freenet.support.* +import net.pterodactylus.sone.data.Post import net.pterodactylus.sone.freenet.wot.* import net.pterodactylus.sone.utils.* import net.pterodactylus.util.web.* import org.hamcrest.* import org.hamcrest.Matchers.* +/** + * A kotlin-ified version of Hamcrest’s [anything()][anything]. It matches + * everything and has the right type, too! + */ +inline fun everything(): Matcher = any(T::class.java) + +/** + * 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) } @@ -18,6 +44,10 @@ fun hasHeader(name: String, value: String) = object : TypeSafeDiagnosingMatcher< } } +fun handleMatcher(matcher: Matcher, item: T, mismatchDescription: Description) = + matcher.matches(item) + .onFalse { matcher.describeMismatch(item, mismatchDescription) } + fun compare(value: T, comparison: (T) -> Boolean, onError: (T) -> Unit) = false.takeUnless { comparison(value) } ?.also { onError(value) } @@ -57,7 +87,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) @@ -74,6 +104,20 @@ fun isOwnIdentity(id: String, nickname: String, requestUri: String, insertUri: S .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) = + handleMatcher(valueMatcher, item.get(name), mismatchDescription) + + override fun describeTo(description: Description) { + description + .appendText("simple field set with key ").appendValue(name) + .appendText(", value ").appendValue(valueMatcher) + } +} + +fun isPost(isRecipientId: Matcher = any(String::class.java)) = AttributeMatcher("post") + .addAttribute("recipient ID", { it.recipientId.orNull() }, isRecipientId) + /** * [TypeSafeDiagnosingMatcher] implementation that aims to cut down boilerplate on verifying the attributes * of typical container objects.