Skip to content
This repository has been archived by the owner on Mar 11, 2020. It is now read-only.

Commit

Permalink
Merge pull request #38 from ceedubs/configured-finite-duration
Browse files Browse the repository at this point in the history
Add Configured[FiniteDuration]
  • Loading branch information
timperrett authored Oct 21, 2016
2 parents 1eb8e93 + 9595943 commit 0ab9a33
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 3 deletions.
8 changes: 7 additions & 1 deletion core/src/main/scala/knobs/Configured.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import scalaz.syntax.applicative._
import scalaz.std.list._
import scalaz.std.option._
import scalaz.{Monad,\/}
import concurrent.duration.Duration
import concurrent.duration.{Duration, FiniteDuration}

/**
* The class of types that can be automatically and safely
Expand Down Expand Up @@ -55,6 +55,12 @@ object Configured {
}
}

implicit val configuredFiniteDuration: Configured[FiniteDuration] = new Configured[FiniteDuration]{
def apply(a: CfgValue) = configuredDuration(a) collect {
case d: FiniteDuration => d
}
}

implicit val configuredValue: Configured[CfgValue] = new Configured[CfgValue] {
def apply(a: CfgValue) = Some(a)
}
Expand Down
21 changes: 20 additions & 1 deletion core/src/test/scala/knobs/Test.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ import compatibility._

object Test extends Properties("Knobs") {

// TODO remove when available in Scalacheck
// https://github.com/rickynils/scalacheck/pull/284
private implicit val arbFiniteDuration: Arbitrary[FiniteDuration] = Arbitrary(
Gen.chooseNum(Long.MinValue + 1, Long.MaxValue).map(Duration.fromNanos))

def withLoad[A](files: List[KnobsResource])(
t: MutableConfig => Task[A]): Task[A] = for {
mb <- load(files)
Expand All @@ -41,6 +46,7 @@ object Test extends Properties("Knobs") {
af <- cfg.lookup[(Int, Int)]("af")
db <- cfg.lookup[Boolean]("ag.q-e.i_u9.a")
du <- cfg.lookup[Duration]("dur")
fdu <- cfg.lookup[FiniteDuration]("dur")
} yield (aa == Some(1)) :| "int property" &&
(ab == Some("foo")) :| "string property" &&
(acx == Some(1)) :| "nested int" &&
Expand All @@ -49,7 +55,9 @@ object Test extends Properties("Knobs") {
(ae == Some(1)) :| "simple int 2" &&
(af == Some((2, 3))) :| "list property" &&
(db == Some(false)) :| "deep bool" &&
(du == Some(5.seconds)) :| "duration property"
(du == Some(5.seconds)) :| "duration property" &&
(fdu == Some(5.seconds)) :| "finite duration property"

}

lazy val interpTest: Task[Prop] =
Expand Down Expand Up @@ -122,5 +130,16 @@ object Test extends Properties("Knobs") {

property("classloader") = classLoaderTest.unsafePerformSync

property("inifinite duration as finite") = {
val g = Gen.oneOf(Duration.Inf, Duration.MinusInf, Duration.Undefined)
forAll(g){ d =>
Configured[FiniteDuration].apply(CfgDuration(d)) == None
}
}

property("finite duration matches duration") = forAll { d: FiniteDuration =>
val cfg = CfgDuration(d)
(Configured[FiniteDuration].apply(cfg): Option[Duration]) ?= Configured[Duration].apply(cfg)
}

}
2 changes: 1 addition & 1 deletion version.sbt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version in ThisBuild := "3.10.0-SNAPSHOT"
version in ThisBuild := "3.11.0-SNAPSHOT"

0 comments on commit 0ab9a33

Please sign in to comment.