Skip to content

Commit

Permalink
Add isIn(Iterable<T>) and isNotIn(Iterable<T>)
Browse files Browse the repository at this point in the history
  • Loading branch information
HaydenMeloche authored and evant committed Nov 13, 2024
1 parent 161b334 commit 9b5b093
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
- renamed `suspendCall` to `having` as part of effort to unify API naming, old name is deprecated.
- added `doesNotExist` assertions to `Path`.
- the receiver types for `isTrue()`, `isFalse()`, and `isInstanceOf()` have been widened to operate on nullable values.
- added assertions `isIn(Iterable<T>)` and `isNotIn(Iterable<T>)`

## [0.28.1] 2024-04-17

Expand Down
18 changes: 18 additions & 0 deletions assertk/src/commonMain/kotlin/assertk/assertions/any.kt
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ fun <T> Assert<T>.isIn(vararg values: T) = given { actual ->
expected(":${show(values)} to contain:${show(actual)}")
}

/**
* Asserts the value is in the expected values, using `in`.
* @see [isNotIn]
*/
fun <T> Assert<T>.isIn(values: Iterable<T>) = given { actual ->
if (actual in values) return
expected(":${show(values)} to contain:${show(actual)}")
}

/**
* Asserts the value is not in the expected values, using `!in`.
* @see [isIn]
Expand All @@ -106,6 +115,15 @@ fun <T> Assert<T>.isNotIn(vararg values: T) = given { actual ->
expected(":${show(values)} to not contain:${show(actual)}")
}

/**
* Asserts the value is not in the expected values, using `!in`.
* @see [isIn]
*/
fun <T> Assert<T>.isNotIn(values: Iterable<T>) = given { actual ->
if (actual !in values) return
expected(":${show(values)} to not contain:${show(actual)}")
}

/**
* Asserts the value has the expected string from it's [toString].
*/
Expand Down
60 changes: 60 additions & 0 deletions assertk/src/commonTest/kotlin/test/assertk/assertions/AnyTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,36 @@ class AnyTest {
assertEquals("expected:<[not test1, not test2]> to contain:<test>", error.message)
}

@Test
fun isIn_iterable_one_equal_item_passes() {
val list = listOf(BasicObject("test"))
assertThat(subject).isIn(list)
}

@Test
fun isIn_iterable_one_non_equal_item_fails() {
val list = listOf(BasicObject("not test1"))
val error = assertFailsWith<AssertionError> {
assertThat(subject).isIn(list)
}
assertEquals("expected:<[not test1]> to contain:<test>", error.message)
}

@Test
fun isIn_iterable_one_equal_item_in_may_passes() {
val list = listOf(BasicObject("not test1"), BasicObject("test"), BasicObject("not test2"))
assertThat(subject).isIn(list)
}

@Test
fun isIn_iterable_no_equal_items_in_may_fails() {
val list = listOf(BasicObject("not test1"), BasicObject("not test2"))
val error = assertFailsWith<AssertionError> {
assertThat(subject).isIn(list)
}
assertEquals("expected:<[not test1, not test2]> to contain:<test>", error.message)
}

@Test
fun isNotIn_one_non_equal_item_passes() {
val isOut1 = BasicObject("not test1")
Expand Down Expand Up @@ -186,6 +216,36 @@ class AnyTest {
assertEquals("expected:<[not test1, test, not test2]> to not contain:<test>", error.message)
}

@Test
fun isNotIn_iterable_one_non_equal_item_passes() {
val list = listOf(BasicObject("not test1"))
assertThat(subject).isNotIn(list)
}

@Test
fun isNotIn_iterable_one_equal_item_fails() {
val list = listOf(BasicObject("test"))
val error = assertFailsWith<AssertionError> {
assertThat(subject).isNotIn(list)
}
assertEquals("expected:<[test]> to not contain:<test>", error.message)
}

@Test
fun isNotIn_iterable_no_equal_items_in_many_passes() {
val list = listOf(BasicObject("not test1"), BasicObject("not test2"))
assertThat(subject).isNotIn(list)
}

@Test
fun isNotIn_iterable_one_equal_item_in_many_fails() {
val list = listOf(BasicObject("not test1"), BasicObject("test"), BasicObject("not test2"))
val error = assertFailsWith<AssertionError> {
assertThat(subject).isNotIn(list)
}
assertEquals("expected:<[not test1, test, not test2]> to not contain:<test>", error.message)
}

@Test
fun hasToString_equal_string_passes() {
assertThat(subject).hasToString("test")
Expand Down

0 comments on commit 9b5b093

Please sign in to comment.