Skip to content

Instantly share code, notes, and snippets.

@patriknw
Created October 24, 2018 13:55
Show Gist options
  • Save patriknw/5f0c54f5748d25d7928389165098b89e to your computer and use it in GitHub Desktop.
Save patriknw/5f0c54f5748d25d7928389165098b89e to your computer and use it in GitHub Desktop.
FSM testing
import akka.actor.testkit.typed.scaladsl.BehaviorTestKit
import akka.actor.typed.Behavior
import akka.actor.typed.scaladsl.AbstractBehavior
import org.scalatest.Matchers
import org.scalatest.WordSpec
object SyncTestingFSMExampleSpec {
sealed trait Command
final case class Type(c: Char) extends Command
final case class CapsLock(on: Boolean) extends Command
final case object Enter extends Command
final case class NormalMode(text: String) extends AbstractBehavior[Command] {
override def onMessage(msg: Command): Behavior[Command] = {
msg match {
case Type(c) ⇒
NormalMode(text + c)
case CapsLock(on) ⇒
if (on) CapsMode(text)
else this
case Enter ⇒
println(text)
NormalMode("")
}
}
}
final case class CapsMode(text: String) extends AbstractBehavior[Command] {
override def onMessage(msg: Command): Behavior[Command] = {
msg match {
case Type(c) ⇒
CapsMode(text + c.toUpper)
case CapsLock(on) ⇒
if (on) this
else NormalMode(text)
case Enter ⇒
println(text)
CapsMode("")
}
}
}
}
class SyncTestingFSMExampleSpec extends WordSpec with Matchers {
import SyncTestingFSMExampleSpec._
"FSM testing" must {
"support support inspecting state" in {
val testKit = BehaviorTestKit(NormalMode(""))
testKit.run(Type('a'))
testKit.currentBehavior should ===(NormalMode("a"))
testKit.run(CapsLock(on = true))
testKit.currentBehavior should ===(CapsMode("a"))
testKit.run(Type('b'))
testKit.currentBehavior should ===(CapsMode("aB"))
}
}
}
import akka.actor.testkit.typed.scaladsl.BehaviorTestKit
import akka.actor.typed.Behavior
import akka.actor.typed.scaladsl.Behaviors
import org.mockito.Mockito._
import org.scalatest.Matchers
import org.scalatest.WordSpec
import org.scalatest.mockito.MockitoSugar
object SyncTestingFSMMockitoExampleSpec {
sealed trait Command
final case class Type(c: Char) extends Command
final case class CapsLock(on: Boolean) extends Command
final case object Enter extends Command
class TestFSM {
def normalMode(text: String): Behavior[Command] =
Behaviors.receiveMessage {
case Type(c) ⇒
normalMode(text + c)
case CapsLock(on) ⇒
if (on) capsMode(text)
else Behaviors.same
case Enter ⇒
println(text)
normalMode("")
}
def capsMode(text: String): Behavior[Command] =
Behaviors.receiveMessage {
case Type(c) ⇒
capsMode(text + c.toUpper)
case CapsLock(on) ⇒
if (on) Behaviors.same
else normalMode(text)
case Enter ⇒
println(text)
capsMode("")
}
}
}
class SyncTestingFSMMockitoExampleSpec extends WordSpec with Matchers with MockitoSugar {
import SyncTestingFSMMockitoExampleSpec._
"FSM testing" must {
"support support inspecting state" in {
val testFSM = spy(new TestFSM)
val testKit = BehaviorTestKit(testFSM.normalMode(""))
verify(testFSM).normalMode("")
testKit.run(Type('a'))
verify(testFSM).normalMode("a")
testKit.run(CapsLock(on = true))
verify(testFSM).capsMode("a")
testKit.run(Type('b'))
verify(testFSM).capsMode("aB")
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment