Last active
May 25, 2024 10:19
-
-
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/e02ad6d092cd820866eb5b6c78209da5907d9e4f
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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.4.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