From 404a677955ae54fdfabb8d1001a5ed7a80778044 Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Sun, 5 Nov 2017 08:23:21 -0800 Subject: More `ApplicativeTests` --- core/src/test/scala/forge/ApplicativeTests.scala | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'core/src/test/scala/forge/ApplicativeTests.scala') diff --git a/core/src/test/scala/forge/ApplicativeTests.scala b/core/src/test/scala/forge/ApplicativeTests.scala index 462cb180..000554ef 100644 --- a/core/src/test/scala/forge/ApplicativeTests.scala +++ b/core/src/test/scala/forge/ApplicativeTests.scala @@ -71,10 +71,9 @@ object ApplicativeTests extends TestSuite { assert(res == Some("lol hello")) } 'upstreamAlwaysEvaluated - { - // Validate the assumption that whether or not control-flow reaches the - // Applyable#apply call inside an Opt{...} block, we always evaluate the - // LHS of the Applyable#apply because it gets lifted out of any control - // flow statements + // Whether or not control-flow reaches the Applyable#apply call inside an + // Opt{...} block, we always evaluate the LHS of the Applyable#apply + // because it gets lifted out of any control flow statements val counter = new Counter() def up = Opt{ "lol " + counter() } val down = Opt{ if ("lol".length > 10) up() else "fail" } @@ -83,6 +82,18 @@ object ApplicativeTests extends TestSuite { counter.value == 1 ) } + 'upstreamEvaluatedOnlyOnce - { + // Even if control-flow reaches the Applyable#apply call more than once, + // it only gets evaluated once due to its lifting out of the Opt{...} block + val counter = new Counter() + def up = Opt{ "lol " + counter() } + def runTwice[T](t: => T) = (t, t) + val down = Opt{ runTwice(up()) } + assert( + down == Some(("lol 1", "lol 1")), + counter.value == 1 + ) + } } } -- cgit v1.2.3