Skip to content

Commit

Permalink
Allow clues to be added to timeouts #2230
Browse files Browse the repository at this point in the history
  • Loading branch information
sksamuel committed Sep 19, 2021
1 parent 4ae2869 commit 113e617
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
Expand Up @@ -11,6 +11,8 @@ import io.kotest.matchers.nulls.shouldBeNull
import io.kotest.matchers.shouldBe
import io.kotest.matchers.string.shouldContain
import io.kotest.matchers.string.shouldStartWith
import kotlinx.coroutines.delay
import kotlinx.coroutines.withTimeout
import java.util.*

class ClueTest : FreeSpec({
Expand Down Expand Up @@ -127,7 +129,7 @@ class ClueTest : FreeSpec({
shouldThrow<AssertionError> {inner.a shouldBe 10}.message shouldBe "HttpResponse(status=404, body=not found)\nMyData(a=20, b=nest it)\nexpected:<10> but was:<20>"
}
//after nesting, everything looks as before
shouldThrow<AssertionError> {it.status shouldBe 200}.message shouldBe "HttpResponse(status=404, body=not found)\nexpected:<200> but was:<404>"
shouldThrow<AssertionError> { it.status shouldBe 200 }.message shouldBe "HttpResponse(status=404, body=not found)\nexpected:<200> but was:<404>"
}
}

Expand All @@ -137,6 +139,16 @@ class ClueTest : FreeSpec({
}
ex.message shouldBe "null\nexpected:<2> but was:<1>"
}

"clue should work for withTimeout" {
shouldThrow<AssertionError> {
withClue("timey timey") {
withTimeout(2) {
delay(1000)
}
}
}.message shouldBe "timey timey\nTimed out waiting for 2 ms"
}
}

})
@@ -1,5 +1,7 @@
package io.kotest.assertions

import kotlinx.coroutines.TimeoutCancellationException

/**
* Add [clue] as additional info to the assertion error message in case an assertion fails.
* Can be nested, the error message will contain all available clues.
Expand All @@ -22,6 +24,9 @@ inline fun <R> withClue(clue: Lazy<Any?>, thunk: () -> R): R {
try {
errorCollector.pushClue { clue.value.toString() }
return thunk()
// this is a special control exception used by coroutines
} catch (t: TimeoutCancellationException) {
throw Exceptions.createAssertionError(clueContextAsString() + (t.message ?: ""), t)
} finally {
errorCollector.popClue()
}
Expand Down

0 comments on commit 113e617

Please sign in to comment.