Skip to content

Instantly share code, notes, and snippets.

@dariathecracker
Last active June 4, 2020 11:41
Show Gist options
  • Save dariathecracker/f6bdcf8f15cbb40e846bab1ef05c8796 to your computer and use it in GitHub Desktop.
Save dariathecracker/f6bdcf8f15cbb40e846bab1ef05c8796 to your computer and use it in GitHub Desktop.
Program in a loop
package ioleo.tictactoe
import ioleo.tictactoe.app.RunLoop
import ioleo.tictactoe.domain.{ConfirmAction, ConfirmMessage, MenuMessage, State}
import zio.{Managed, ZIO}
import zio.clock.Clock
import zio.duration._
import zio.test.{assertM, suite, testM, DefaultRunnableSpec}
import zio.test.Assertion.{equalTo, isRight, isSome, isUnit}
import zio.test.mock.Expectation.{failure, value}
import TicTacToeSpecUtils._
object TicTacToeSpec extends DefaultRunnableSpec(
suite("TicTacToe")(
suite("program")(
testM("repeats RunLoop.step until interrupted by Unit error") {
val app = TicTacToe.program
val mock = (
(RunLoop.step(equalTo(state0)) returns value(state1) *>
(RunLoop.step(equalTo(state1)) returns value(state2) *>
(RunLoop.step(equalTo(state2)) returns value(state3) *>
(RunLoop.step(equalTo(state3)) returns failure(()))
)
val result = app.either.provideManaged(mock).timeout(500.millis).provide(Clock.Live)
assertM(result, isSome(isRight(isUnit)))
}
)
)
)
object TicTacToeSpecUtils {
val state0 = State.default
val state1 = State.Menu(None, MenuMessage.InvalidCommand)
val state2 = State.Confirm(ConfirmAction.Quit, state0, state1, ConfirmMessage.Empty)
val state3 = State.Confirm(ConfirmAction.Quit, state0, state1, ConfirmMessage.InvalidCommand)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment