tests for TransactionalRef in for comprehensions

This commit is contained in:
Peter Vlugter 2010-04-14 09:27:45 +08:00 committed by Jonas Bonér
parent 9545efacd5
commit 10016df2a7

View file

@ -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)
}
}
}