kotest / kotest-extensions-arrow

Kotest extensions for Arrow
Apache License 2.0
39 stars 13 forks source link

arrow fx proposal and add Either.rethrow #143

Closed i-walker closed 2 years ago

i-walker commented 2 years ago

inspired by https://github.com/nomisRev/ktor-arrow-example/blob/main/src/test/kotlin/io/github/nomisrev/resource.kt and arrow-fx-coroutines-test

nomisRev commented 2 years ago

The goal of this PR is to introduce similar functionality to autoClose but for Arrow Fx Coroutines Resource.

In an ideal world the API would be the folllowing:

class XXXSpec : StringSpec({
   val a by resource(resourceA) // A & B *have to be independent*
   val b by resource(resourceB)

   "test {
      // usage
   }
}

If there is no relationship between A & B they can lazily be initialised by beforeSpec which has support for suspension. Which can then be safely used inside the test.

if A & B do depend on each-other the user should do:

class XXXSpec : StringSpec({
   val (a, b) by resource(resourceA.flatMap { a -> resourceB(a) })
}

The following is incorrect, and requires runBlocking

class XXXSpec : StringSpec({
   val a by resource(resourceA)
   val b by resource(resourceB(a)) <-- relying on directly on `a` which requires it to be evaluated with `runBlocking`.
}

We can of course try to avoid runBlocking on a best-effort basis, based on the requirements that are described above. PS: reason I used by instead of = in the snippets above is that is required if you want to make it (best-effort) lazy.

i-walker commented 2 years ago

CI is failing, bc kotest.common.runBlocking isn't available in Js

sksamuel commented 2 years ago

In kotest proper I added a helper function that runs runBlocking on jvm or just inline on js.

nomisRev commented 2 years ago

@sksamuel I couldn't find the snippet. Could you link it, please? Or just the js actual fun would be great.

nomisRev commented 2 years ago

@i-walker updated in my example project latest Arrow, and avoids runBlocking as much as possible.

https://raw.githubusercontent.com/nomisRev/ktor-arrow-example/e498a5bd221f9240002f8adedc20a20e6d9e794e/src/test/kotlin/io/github/nomisrev/resource.kt

sksamuel commented 2 years ago

@nomisRev https://github.com/kotest/kotest/blob/f433e975b61f186e98ad361f84fcc74741f2dc09/kotest-common/src/commonMain/kotlin/io/kotest/common/runBlocking.kt

i-walker commented 2 years ago

Thanks @nomisRev I will go for your encoding in https://raw.githubusercontent.com/nomisRev/ktor-arrow-example/e498a5bd221f9240002f8adedc20a20e6d9e794e/src/test/kotlin/io/github/nomisrev/resource.kt

sksamuel commented 2 years ago

Lgtm

i-walker commented 2 years ago

Thanks everyone 🙌🏾 I am prepping the release and a release post for 1.3.0 this week and hope to get this out by next week or so

nomisRev commented 2 years ago

Awesome @i-walker! Let me know if I can help with anything.

sksamuel commented 2 years ago

Loving this.

On Tue, May 17, 2022, 4:32 AM Imran Malic Settuba @.***> wrote:

Merged #143 https://github.com/kotest/kotest-extensions-arrow/pull/143 into master.

— Reply to this email directly, view it on GitHub https://github.com/kotest/kotest-extensions-arrow/pull/143#event-6622830353, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFVSGWZEN3N35XLQ224H4TVKNRUJANCNFSM5PN65WOQ . You are receiving this because you were mentioned.Message ID: @.*** com>