1 package net.pterodactylus.sone.test
3 import org.hamcrest.Description
4 import org.hamcrest.TypeSafeDiagnosingMatcher
6 class OneByOneMatcher<A> : TypeSafeDiagnosingMatcher<A>() {
7 private data class Matcher<in A, out V>(val expected: V, val actual: (A) -> V, val description: String)
9 private val matchers = mutableListOf<Matcher<A, *>>()
11 fun <V> expect(description: String, expected: V, actual: (A) -> V) {
12 matchers += Matcher<A, V>(expected, actual, description)
15 override fun describeTo(description: Description) {
16 matchers.forEachIndexed { index, matcher ->
18 description.appendText(", ")
20 description.appendText("${matcher.description} is ").appendValue(matcher.expected)
24 override fun matchesSafely(item: A, mismatchDescription: Description) =
26 if (it.expected != it.actual(item)) {
27 mismatchDescription.appendText("${it.description} is ").appendValue(it.actual(item))