From b798dfb131b0df6675a392279f87f2c440fd6bb7 Mon Sep 17 00:00:00 2001 From: Christopher Batey Date: Tue, 27 Mar 2018 17:51:54 +0100 Subject: [PATCH] BehaviorTestKit - hasEffects and expectEffectPF --- .../typed/internal/BehaviorTestKitImpl.scala | 11 +++++++ .../typed/javadsl/BehaviorTestKit.scala | 5 ++++ .../typed/scaladsl/BehaviorTestKit.scala | 12 +++++++- .../typed/scaladsl/BehaviorTestKitSpec.scala | 29 ++++++++++++++++++- 4 files changed, 55 insertions(+), 2 deletions(-) diff --git a/akka-testkit-typed/src/main/scala/akka/testkit/typed/internal/BehaviorTestKitImpl.scala b/akka-testkit-typed/src/main/scala/akka/testkit/typed/internal/BehaviorTestKitImpl.scala index cda8ecf7df..ab7098beda 100644 --- a/akka-testkit-typed/src/main/scala/akka/testkit/typed/internal/BehaviorTestKitImpl.scala +++ b/akka-testkit-typed/src/main/scala/akka/testkit/typed/internal/BehaviorTestKitImpl.scala @@ -76,6 +76,16 @@ private[akka] final class BehaviorTestKitImpl[T](_name: String, _initialBehavior } } + def expectEffectPF[R](f: PartialFunction[Effect, R]): R = { + ctx.effectQueue.poll() match { + case null ⇒ throw new AssertionError(s"expected matching effect but no effects were recorded") + case eff if f.isDefinedAt(eff) ⇒ + f.apply(eff) + case other ⇒ + throw new AssertionError(s"expected matching effect but got: $other") + } + } + def expectEffectType[E <: Effect](implicit classTag: ClassTag[E]): E = expectEffectClass(classTag.runtimeClass.asInstanceOf[Class[E]]) @@ -114,4 +124,5 @@ private[akka] final class BehaviorTestKitImpl[T](_name: String, _initialBehavior } catch handleException } + override def hasEffects(): Boolean = !ctx.effectQueue.isEmpty } diff --git a/akka-testkit-typed/src/main/scala/akka/testkit/typed/javadsl/BehaviorTestKit.scala b/akka-testkit-typed/src/main/scala/akka/testkit/typed/javadsl/BehaviorTestKit.scala index 4e6ed7040a..11a7e59a58 100644 --- a/akka-testkit-typed/src/main/scala/akka/testkit/typed/javadsl/BehaviorTestKit.scala +++ b/akka-testkit-typed/src/main/scala/akka/testkit/typed/javadsl/BehaviorTestKit.scala @@ -57,6 +57,11 @@ abstract class BehaviorTestKit[T] { */ def getAllEffects(): java.util.List[Effect] + /** + * Returns if there have been any effects. + */ + def hasEffects(): Boolean + /** * Asserts that the oldest effect is the expectedEffect. Removing it from * further assertions. diff --git a/akka-testkit-typed/src/main/scala/akka/testkit/typed/scaladsl/BehaviorTestKit.scala b/akka-testkit-typed/src/main/scala/akka/testkit/typed/scaladsl/BehaviorTestKit.scala index d78000857d..7755c505f0 100644 --- a/akka-testkit-typed/src/main/scala/akka/testkit/typed/scaladsl/BehaviorTestKit.scala +++ b/akka-testkit-typed/src/main/scala/akka/testkit/typed/scaladsl/BehaviorTestKit.scala @@ -58,6 +58,11 @@ trait BehaviorTestKit[T] { */ def retrieveAllEffects(): immutable.Seq[Effect] + /** + * Returns if there have been any effects. + */ + def hasEffects(): Boolean + /** * Asserts that the oldest effect is the expectedEffect. Removing it from * further assertions. @@ -68,7 +73,12 @@ trait BehaviorTestKit[T] { * Asserts that the oldest effect is of type T. Consumes and returns the concrete effect for * further direct assertions. */ - def expectEffectType[T <: Effect](implicit classTag: ClassTag[T]): T + def expectEffectType[E <: Effect](implicit classTag: ClassTag[E]): E + + /** + * Asserts that the oldest effect matches the given partial function. + */ + def expectEffectPF[R](f: PartialFunction[Effect, R]): R /** * The current behavior, can change any time `run` is called diff --git a/akka-testkit-typed/src/test/scala/akka/testkit/typed/scaladsl/BehaviorTestKitSpec.scala b/akka-testkit-typed/src/test/scala/akka/testkit/typed/scaladsl/BehaviorTestKitSpec.scala index e827d068fe..69cc61af2d 100644 --- a/akka-testkit-typed/src/test/scala/akka/testkit/typed/scaladsl/BehaviorTestKitSpec.scala +++ b/akka-testkit-typed/src/test/scala/akka/testkit/typed/scaladsl/BehaviorTestKitSpec.scala @@ -82,7 +82,7 @@ class BehaviorTestKitSpec extends WordSpec with Matchers { private val props = Props.empty - "BehaviorTeskit" must { + "BehaviorTestKit" must { "allow assertions on effect type" in { val testkit = BehaviorTestKit[Father.Command](Father.init()) @@ -90,6 +90,33 @@ class BehaviorTestKitSpec extends WordSpec with Matchers { val spawnAnonymous = testkit.expectEffectType[Effects.SpawnedAnonymous[_]] spawnAnonymous.props should ===(Props.empty) } + + "return if effects have taken place" in { + val testkit = BehaviorTestKit[Father.Command](Father.init()) + testkit.hasEffects() should ===(false) + testkit.run(SpawnAnonymous(1)) + testkit.hasEffects() should ===(true) + } + + "allow assertions using partial functions - no match" in { + val testkit = BehaviorTestKit[Father.Command](Father.init()) + testkit.run(SpawnChildren(1)) + val ae = intercept[AssertionError] { + testkit.expectEffectPF { + case SpawnedAnonymous(_, _) ⇒ + } + } + ae.getMessage should startWith("expected matching effect but got: ") + } + + "allow assertions using partial functions - match" in { + val testkit = BehaviorTestKit[Father.Command](Father.init()) + testkit.run(SpawnChildren(1)) + val childName = testkit.expectEffectPF { + case Spawned(_, name, _) ⇒ name + } + childName should ===("child0") + } } "BehaviorTestkit's spawn" must {