X-Git-Url: https://git.pterodactylus.net/?p=Sone.git;a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ftest%2FMatchers.kt;fp=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ftest%2FMatchers.kt;h=c084d35ffdb9489f80aff75e83fb9187a2e41c09;hp=0000000000000000000000000000000000000000;hb=70ae04e2fd5ffa01c6f000f74ec8ece826ab7b2b;hpb=a73938206e78e061bc63db07391623ee6a218845 diff --git a/src/test/kotlin/net/pterodactylus/sone/test/Matchers.kt b/src/test/kotlin/net/pterodactylus/sone/test/Matchers.kt new file mode 100644 index 0000000..c084d35 --- /dev/null +++ b/src/test/kotlin/net/pterodactylus/sone/test/Matchers.kt @@ -0,0 +1,21 @@ +package net.pterodactylus.sone.test + +import net.pterodactylus.util.web.* +import org.hamcrest.* + +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) } + ?: compare(item.hasValue(value), { it }) { mismatchDescription.appendText("does not have value ").appendValue(value) } + ?: true + + override fun describeTo(description: Description) { + description.appendText("name is ").appendValue(name) + .appendText(", value is ").appendValue(value) + } +} + +fun compare(value: T, comparison: (T) -> Boolean, onError: (T) -> Unit) = + false.takeUnless { comparison(value) } + ?.also { onError(value) } +