X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ftest%2FMatchers.kt;h=ecb20492e2d4e0da7fc60b9b2f08ff7eed61e324;hb=a72774c302cdb2c35508380fb2ec445be2705efe;hp=e7588dba2fef8a019ebeb021ccb8a487619616e6;hpb=f1bfb86be7bacf05949c722d80ec8c7a8a996ed9;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 e7588db..ecb2049 100644 --- a/src/test/kotlin/net/pterodactylus/sone/test/Matchers.kt +++ b/src/test/kotlin/net/pterodactylus/sone/test/Matchers.kt @@ -1,12 +1,31 @@ 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.* +/** + * 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) } @@ -58,7 +77,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) @@ -88,6 +107,9 @@ fun hasField(name: String, valueMatcher: Matcher) = object : TypeSafeDia } } +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.