Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add containsExactlyInAnyOrder(expected: Iterable<T>) support #476

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions assertk/src/commonMain/kotlin/assertk/assertions/iterable.kt
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,27 @@ fun Assert<Iterable<*>>.containsExactlyInAnyOrder(vararg elements: Any?) = given
}.toString())
}

inline fun <reified T> Assert<Iterable<T>>.containsExactlyInAnyOrder(expected: Iterable<T>) = given { actual ->
val actualMap = actual.groupingBy { it }.eachCount()
val expectedMap = expected.groupingBy { it }.eachCount()

if (actualMap == expectedMap) {
return
}

val notInActual = expectedMap.filter { (key, value) -> (actualMap[key] ?: 0) < value }.map { it.key }
val notInExpected = actualMap.filter { (key, value) -> (expectedMap[key] ?: 0) < value }.map { it.key }

expected(StringBuilder("to contain exactly in any order:${show(expected)} but was:${show(actual)}").apply {
if (notInActual.isNotEmpty()) {
append("\n elements not found:${show(notInActual)}")
}
if (notInExpected.isNotEmpty()) {
append("\n extra elements found:${show(notInExpected)}")
}
}.toString())
}

internal fun MutableList<*>.removeFirst(value: Any?) {
val index = indexOf(value)
if (index > -1) removeAt(index)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,72 @@ class IterableTest {
error.message
)
}

@Test fun containsExactlyInAnyOrder_only_elements_of_another_iterable_passes() {
assertThat(iterableOf(1, 2)).containsExactlyInAnyOrder(iterableOf(2, 1))
}

@Test fun containsExactlyInAnyOrder_only_elements_of_another_iterable_passes2() {
assertThat(iterableOf(1, 2, 1)).containsExactlyInAnyOrder(iterableOf(2, 1, 1))
}

@Test fun containsExactlyInAnyOrder_duplicate_elements_of_another_iterable_fails() {
val error = assertFailsWith<AssertionError> {
assertThat(iterableOf(1, 2, 2)).containsExactlyInAnyOrder(iterableOf(2, 1))
}
assertEquals(
"""expected to contain exactly in any order:<[2, 1]> but was:<[1, 2, 2]>
| extra elements found:<[2]>
""".trimMargin(), error.message
)
}

@Test fun containsExactlyInAnyOrder_of_another_iterable_duplicate_elements_fails2() {
val error = assertFailsWith<AssertionError> {
assertThat(iterableOf(1, 2)).containsExactlyInAnyOrder(iterableOf(2, 2, 1))
}
assertEquals(
"""expected to contain exactly in any order:<[2, 2, 1]> but was:<[1, 2]>
| elements not found:<[2]>
""".trimMargin(), error.message
)
}

@Test fun containsExactlyInAnyOrder__of_another_iterable_more_elements_fails() {
val error = assertFailsWith<AssertionError> {
assertThat(iterableOf(1, 2, 3)).containsExactlyInAnyOrder(iterableOf(2, 1))
}
assertEquals(
"""expected to contain exactly in any order:<[2, 1]> but was:<[1, 2, 3]>
| extra elements found:<[3]>
""".trimMargin(), error.message
)
}

@Test fun containsExactlyInAnyOrder_of_another_iterable_less_elements_fails() {
val error = assertFailsWith<AssertionError> {
assertThat(iterableOf(1, 2, 3)).containsExactlyInAnyOrder(iterableOf(2, 1, 3, 4))
}
assertEquals(
"""expected to contain exactly in any order:<[2, 1, 3, 4]> but was:<[1, 2, 3]>
| elements not found:<[4]>
""".trimMargin(),
error.message
)
}

@Test fun containsExactlyInAnyOrder_of_another_iterable_different_elements_fails() {
val error = assertFailsWith<AssertionError> {
assertThat(iterableOf(1)).containsExactlyInAnyOrder(iterableOf(2))
}
assertEquals(
"""expected to contain exactly in any order:<[2]> but was:<[1]>
| elements not found:<[2]>
| extra elements found:<[1]>
""".trimMargin(),
error.message
)
}
//endregion

//region each
Expand Down