From 10016df2a700e747ae7c2c6ef6c7c11fab76dc0a Mon Sep 17 00:00:00 2001 From: Peter Vlugter Date: Wed, 14 Apr 2010 09:27:45 +0800 Subject: [PATCH] tests for TransactionalRef in for comprehensions --- .../src/test/scala/TransactionalRefSpec.scala | 79 +++++++++++++++++-- 1 file changed, 74 insertions(+), 5 deletions(-) diff --git a/akka-core/src/test/scala/TransactionalRefSpec.scala b/akka-core/src/test/scala/TransactionalRefSpec.scala index b4e9c0e67a..07c36ebdcf 100644 --- a/akka-core/src/test/scala/TransactionalRefSpec.scala +++ b/akka-core/src/test/scala/TransactionalRefSpec.scala @@ -59,10 +59,79 @@ class TransactionalRefSpec extends Spec with ShouldMatchers { evaluating { increment } should produce [RuntimeException] } - it("should be able to be mapped") (pending) - it("should be able to be used in a 'foreach' for comprehension") (pending) - it("should be able to be used in a 'map' for comprehension") (pending) - it("should be able to be used in a 'flatMap' for comprehension") (pending) - it("should be able to be used in a 'filter' for comprehension") (pending) + it("should be able to be mapped") { + val ref1 = Ref(1) + + val ref2 = atomic { + ref1 map (_ + 1) + } + + val value1 = atomic { ref1.get.get } + val value2 = atomic { ref2.get.get } + + value1 should be(1) + value2 should be(2) + } + + it("should be able to be used in a 'foreach' for comprehension") { + val ref = Ref(3) + + var result = 0 + + atomic { + for (value <- ref) { + result += value + } + } + + result should be(3) + } + + it("should be able to be used in a 'map' for comprehension") { + val ref1 = Ref(1) + + val ref2 = atomic { + for (value <- ref1) yield value + 2 + } + + val value2 = atomic { ref2.get.get } + + value2 should be(3) + } + + it("should be able to be used in a 'flatMap' for comprehension") { + val ref1 = Ref(1) + val ref2 = Ref(2) + + val ref3 = atomic { + for { + value1 <- ref1 + value2 <- ref2 + } yield value1 + value2 + } + + val value3 = atomic { ref3.get.get } + + value3 should be(3) + } + + it("should be able to be used in a 'filter' for comprehension") { + val ref1 = Ref(1) + + val refLess2 = atomic { + for (value <- ref1 if value < 2) yield value + } + + val optLess2 = atomic { refLess2.get } + + val refGreater2 = atomic { + for (value <- ref1 if value > 2) yield value + } + + val optGreater2 = atomic { refGreater2.get } + + optLess2 should be(Some(1)) + optGreater2 should be(None) + } } }