Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 6, 2023 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/bb0f2bf12a3b5f3845c377a4a80b9900 to your computer and use it in GitHub Desktop.
Save dacr/bb0f2bf12a3b5f3845c377a4a80b9900 to your computer and use it in GitHub Desktop.
ZIO learning - testing / published by https://github.com/dacr/code-examples-manager #da808f22-e652-495f-85fe-756b14d6aca5/ec45f6675bb74b9ee7cf52272e402ea405158a1e
// summary : ZIO learning - testing
// keywords : scala, zio, learning, pure-functional, @testable
// publish : gist
// authors : David Crosson
// license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2)
// id : da808f22-e652-495f-85fe-756b14d6aca5
// created-on : 2021-04-02T16:22:22+02:00
// managed-by : https://github.com/dacr/code-examples-manager
// run-with : scala-cli $file
// ---------------------
//> using scala "3.2.2"
//> using dep "dev.zio::zio:2.0.13"
//> using dep "dev.zio::zio-test:2.0.13"
//> using dep "fr.janalyse::zio-worksheet:2.0.13.0"
// ---------------------
import zio.*, zio.worksheet.*
import zio.Duration.*
import zio.test.*
import zio.test.Assertion.*
object Encapsulated extends ZIOSpecDefault { // Work around for some threading issue when used directly
// -------------------------------------------------------------
// The logic we want to test
val alarmLogic = for {
_ <- Console.printLine("how many seconds to wait for ?")
duration <- Console.readLine.map(_.toInt)
_ <- Clock.sleep(duration.seconds)
_ <- Console.printLine("Wake up !")
} yield ()
// -------------------------------------------------------------
// the test logic - just provide custom environments :)
val testLogic = suite("Alarm logic tests")(
zio.test.test("alarm behavior") {
for {
_ <- TestConsole.feedLines("5")
x <- alarmLogic.fork
_ <- TestClock.adjust(6.seconds)
_ <- x.join
output <- TestConsole.output
} yield assertTrue(output.contains("Wake up !\n"))
}
)
// -------------------------------------------------------------
def spec = testLogic
}
Encapsulated.main(Array.empty)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment