X-Git-Url: https://git.pterodactylus.net/?a=blobdiff_plain;f=src%2Ftest%2Fkotlin%2Fnet%2Fpterodactylus%2Fsone%2Ftest%2FMatchers.kt;h=9d215573069a6aac03000f32153998d82e456d4c;hb=63d91fb521ea8f126efbccb6152ccc1a872479d1;hp=ecb20492e2d4e0da7fc60b9b2f08ff7eed61e324;hpb=a72774c302cdb2c35508380fb2ec445be2705efe;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 ecb2049..9d21557 100644 --- a/src/test/kotlin/net/pterodactylus/sone/test/Matchers.kt +++ b/src/test/kotlin/net/pterodactylus/sone/test/Matchers.kt @@ -9,6 +9,12 @@ 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() { @@ -38,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) } @@ -96,9 +106,7 @@ fun isOwnIdentity(id: String, nickname: String, requestUri: String, insertUri: S 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) - } + handleMatcher(valueMatcher, item.get(name), mismatchDescription) override fun describeTo(description: Description) { description