[go: nahoru, domu]

Skip to content

Commit

Permalink
Add ComparableSubject#isAtLeast to Kruth
Browse files Browse the repository at this point in the history
  • Loading branch information
veyndan committed Apr 14, 2023
1 parent beccf74 commit 222d549
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,17 @@ open class ComparableSubject<T : Comparable<T>> constructor(actual: T?) : Subjec
fail("Expected to be less than $other, but was $actual")
}
}

/**
* Checks that the subject is greater than or equal to [other].
*
* @throws NullPointerException if [actual] or [other] is `null`.
*/
fun isAtLeast(other: T?) {
if (actual == null || other == null) {
throw NullPointerException("Expected to be at least $other, but was $actual")
} else if (actual < other) {
fail("Expected to be at least $other, but was $actual")
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ class ComparableSubjectTest {
assertFailsWith<NullPointerException> {
assertThat(6).isLessThan(null)
}
assertFailsWith<NullPointerException> {
assertThat(6).isAtLeast(null)
}
}

@Test
Expand All @@ -52,16 +55,31 @@ class ComparableSubjectTest {
}
}

@Test
fun isAtLeast() {
assertThat(4).isAtLeast(3)
assertThat(4).isAtLeast(4)
assertFailsWith<AssertionError> {
assertThat(4).isAtLeast(5)
}
}

// Brief tests with other comparable types (no negative test cases)

@Test
fun longs() {
assertThat(4L).isLessThan(5L)

assertThat(4L).isAtLeast(4L)
assertThat(4L).isAtLeast(3L)
}

@Test
fun strings() {
assertThat("gak").isLessThan("kak")

assertThat("kak").isAtLeast("kak")
assertThat("kak").isAtLeast("gak")
}

@Test
Expand Down

0 comments on commit 222d549

Please sign in to comment.