Skip to content

Commit

Permalink
#57 adding some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tobyweston committed May 8, 2018
1 parent 82211bf commit 37c546a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/test/resources/bad-example-missing-fields.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
hosts = ["one", "two"]
2 changes: 2 additions & 0 deletions src/test/resources/bad-example-missing-values.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mode =
hosts = ["one", "two"]
1 change: 1 addition & 0 deletions src/test/resources/bad-example.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
I'm nonsense
1 change: 1 addition & 0 deletions src/test/resources/example-empty.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
mode = "client"
3 changes: 3 additions & 0 deletions src/test/resources/example.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# comments are ok in my book
mode = "server"
hosts = ["one", "two"]
58 changes: 58 additions & 0 deletions src/test/scala/bad/robot/temperature/config/ConfigFileTest.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package bad.robot.temperature.config

import java.io.File

import bad.robot.temperature.ConfigurationError
import bad.robot.temperature.config.ConfigFile.loadOrWarn
import knobs._
import org.specs2.mutable.Specification

class ConfigFileTest extends Specification {

"Can be loaded" >> {
loadOrWarn(Required(ClassPathResource("example.cfg"))).unsafeRunSync() must beRight
}

"Retrieve empty list for hosts" >> {
loadOrWarn(Required(ClassPathResource("example-empty.cfg"))).unsafeRunSync() must beRight.like { case config => {
config.mode must_== "client"
config.hosts must throwA[KeyError]
}}
}

"Retrieve no mode" >> {
val config = loadOrWarn(Required(ClassPathResource("bad-example-missing-fields.cfg"))).unsafeRunSync()
config must beRight.like { case config => {
config.mode must throwA[KeyError]
config.hosts must_== List("one", "two")
}}
}

"Retrieve the values configuration items" >> {
loadOrWarn(Required(ClassPathResource("example.cfg"))).unsafeRunSync() must beRight.like { case config => {
config.mode must_== "server"
config.hosts must_== List("one", "two")
}}
}

"Example failure messages" >> {
import scala.concurrent.ExecutionContext.Implicits.global // todo replace with explicit one

loadOrWarn(Required(FileResource.unwatched(new File("/foo.cfg")))).unsafeRunSync() must beLeft.like {
case error: ConfigurationError => error.details must contain("/foo.cfg (No such file or directory)")
}
loadOrWarn(Required(FileResource.unwatched(new File(sys.props("user.home"))))).unsafeRunSync() must beLeft.like {
case error: ConfigurationError => error.details must contain(s"${sys.props("user.home")} (Is a directory)")
}
loadOrWarn(Required(ClassPathResource("not/present/temperature-machine.cfg"))).unsafeRunSync() must beLeft.like {
case error: ConfigurationError => error.details must contain("not/present/temperature-machine.cfg not found on classpath")
}
loadOrWarn(Required(ClassPathResource("bad-example.cfg"))).unsafeRunSync() must beLeft.like {
case error: ConfigurationError => error.details must contain("expected configuration")
}
loadOrWarn(Required(ClassPathResource("bad-example-missing-values.cfg"))).unsafeRunSync() must beLeft.like {
case error: ConfigurationError => error.details must contain("expected comment, newline, value, or whitespace")
}
}

}

0 comments on commit 37c546a

Please sign in to comment.