From 9cfc7a4d916181d350644f12d6e07de5dd3275e3 Mon Sep 17 00:00:00 2001 From: Dan Sanduleac Date: Mon, 25 Nov 2013 12:09:07 +0000 Subject: [PATCH] Added test case for derived settings --- .../src/test/scala/SettingsTest.scala | 30 +++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/util/collection/src/test/scala/SettingsTest.scala b/util/collection/src/test/scala/SettingsTest.scala index 9ff703526..a00f77cf7 100644 --- a/util/collection/src/test/scala/SettingsTest.scala +++ b/util/collection/src/test/scala/SettingsTest.scala @@ -21,7 +21,7 @@ object SettingsTest extends Properties("settings") singleIntTest( chainBind(value(abs)), 0 ) } - property("Allows references to completed settings") = forAllNoShrink(30) { allowedReference _ } + property("Allows references to completed settings") = forAllNoShrink(30) { allowedReference } final def allowedReference(intermediate: Int): Prop = { val top = value(intermediate) @@ -36,6 +36,32 @@ object SettingsTest extends Properties("settings") catch { case e: java.lang.Exception => ("Unexpected exception: " + e) |: false } } + property("Derived setting chain depending on (prev derived, normal setting)") = forAllNoShrink(Gen.choose(1, 100)) { derivedSettings } + final def derivedSettings(nr: Int): Prop = + { + val alphaStr = Gen.alphaStr + val genScopedKeys = { + val attrKeys = for { + list <- Gen.listOfN(nr, alphaStr) suchThat (l => l.size == l.distinct.size) + item <- list + } yield AttributeKey[Int](item) + attrKeys map (_ map (ak => ScopedKey(Scope(0), ak))) + } + forAll(genScopedKeys) { scopedKeys => + val last = scopedKeys.last + val derivedSettings: Seq[Setting[Int]] = ( + for { + List(scoped0, scoped1) <- chk :: scopedKeys sliding 2 + nextInit = if (scoped0 == chk) chk + else (scoped0 zipWith chk) { (p, _) => p + 1 } + } yield derive(setting(scoped1, nextInit)) + ).toSeq + + { checkKey(last, Some(nr-1), evaluate(setting(chk, value(0)) +: derivedSettings)) :| "Not derived?" } && + { checkKey( last, None, evaluate(derivedSettings)) :| "Should not be derived" } + } + } + // Circular (dynamic) references currently loop infinitely. // This is the expected behavior (detecting dynamic cycles is expensive), // but it may be necessary to provide an option to detect them (with a performance hit) @@ -95,4 +121,4 @@ final class CCR(intermediate: Int) else iterate(value(t - 1), t-1) } -} \ No newline at end of file +}