From 37c546a76ee96740102890adcc909c4777f131de Mon Sep 17 00:00:00 2001 From: Toby Date: Mon, 30 Apr 2018 21:11:13 +0100 Subject: [PATCH] #57 adding some tests --- .../resources/bad-example-missing-fields.cfg | 1 + .../resources/bad-example-missing-values.cfg | 2 + src/test/resources/bad-example.cfg | 1 + src/test/resources/example-empty.cfg | 1 + src/test/resources/example.cfg | 3 + .../temperature/config/ConfigFileTest.scala | 58 +++++++++++++++++++ 6 files changed, 66 insertions(+) create mode 100644 src/test/resources/bad-example-missing-fields.cfg create mode 100644 src/test/resources/bad-example-missing-values.cfg create mode 100644 src/test/resources/bad-example.cfg create mode 100644 src/test/resources/example-empty.cfg create mode 100644 src/test/resources/example.cfg create mode 100644 src/test/scala/bad/robot/temperature/config/ConfigFileTest.scala diff --git a/src/test/resources/bad-example-missing-fields.cfg b/src/test/resources/bad-example-missing-fields.cfg new file mode 100644 index 0000000..4722364 --- /dev/null +++ b/src/test/resources/bad-example-missing-fields.cfg @@ -0,0 +1 @@ +hosts = ["one", "two"] diff --git a/src/test/resources/bad-example-missing-values.cfg b/src/test/resources/bad-example-missing-values.cfg new file mode 100644 index 0000000..ae0bc73 --- /dev/null +++ b/src/test/resources/bad-example-missing-values.cfg @@ -0,0 +1,2 @@ +mode = +hosts = ["one", "two"] diff --git a/src/test/resources/bad-example.cfg b/src/test/resources/bad-example.cfg new file mode 100644 index 0000000..7507d80 --- /dev/null +++ b/src/test/resources/bad-example.cfg @@ -0,0 +1 @@ +I'm nonsense \ No newline at end of file diff --git a/src/test/resources/example-empty.cfg b/src/test/resources/example-empty.cfg new file mode 100644 index 0000000..317ea57 --- /dev/null +++ b/src/test/resources/example-empty.cfg @@ -0,0 +1 @@ +mode = "client" \ No newline at end of file diff --git a/src/test/resources/example.cfg b/src/test/resources/example.cfg new file mode 100644 index 0000000..ff60ae3 --- /dev/null +++ b/src/test/resources/example.cfg @@ -0,0 +1,3 @@ +# comments are ok in my book +mode = "server" +hosts = ["one", "two"] \ No newline at end of file diff --git a/src/test/scala/bad/robot/temperature/config/ConfigFileTest.scala b/src/test/scala/bad/robot/temperature/config/ConfigFileTest.scala new file mode 100644 index 0000000..c6ce219 --- /dev/null +++ b/src/test/scala/bad/robot/temperature/config/ConfigFileTest.scala @@ -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") + } + } + +}