aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Spek <p.spek@tyil.nl>2017-04-26 23:08:59 +0200
committerPatrick Spek <p.spek@tyil.nl>2017-04-26 23:08:59 +0200
commited219a26574e05ba27264d73e75e5690e478570f (patch)
treefafb21c1c9db6f543fb213c9d658e5f91f027788
parent845c2d891e12b7abe006aa1e36c1201266b29a51 (diff)
Add Config::Parser::NULL for testcases
-rw-r--r--META6.json1
-rw-r--r--lib/Config.pm621
-rw-r--r--lib/Config/Parser.pm66
-rw-r--r--lib/Config/Parser/NULL.pm631
-rw-r--r--t/05-null-parser.t30
-rw-r--r--t/files/config (renamed from t/test-stub)0
-rw-r--r--t/files/config.yaml0
-rw-r--r--t/reading.t6
8 files changed, 85 insertions, 10 deletions
diff --git a/META6.json b/META6.json
index ccf33e7..303955b 100644
--- a/META6.json
+++ b/META6.json
@@ -12,6 +12,7 @@
"Config": "lib/Config.pm6",
"Config::Type": "lib/Config/Type.pm6",
"Config::Parser": "lib/Config/Parser.pm6",
+ "Config::Parser::NULL": "lib/Config/Parser/NULL.pm6",
"Config::Exception::FileNotFoundException": "lib/Config/Exception/FileNotFoundException.pm6",
"Config::Exception::UnimplementedMethodException": "lib/Config/Exception/UnimplementedMethodException.pm6",
"Config::Exception::UnknownTypeException": "lib/Config/Exception/UnknownTypeException.pm6",
diff --git a/lib/Config.pm6 b/lib/Config.pm6
index 40bfa27..a13f589 100644
--- a/lib/Config.pm6
+++ b/lib/Config.pm6
@@ -60,6 +60,10 @@ class Config is export
return $parser;
}
+ if ($!parser ne "") {
+ return $!parser;
+ }
+
my $type = self.get-parser-type($path);
Config::Exception::UnknownTypeException.new(
@@ -117,7 +121,7 @@ class Config is export
return False;
}
- return self.load($!path);
+ return self.read($!path);
}
#| Load a configuration file from the given path. Optionally
@@ -150,9 +154,14 @@ class Config is export
#| Read a list of paths. Will fail on the first file that
#| fails to load for whatever reason.
- multi method read(List $paths, Str $parser = "")
- {
+ multi method read(
+ List $paths,
+ Str $parser = "",
+ Bool :$skip-not-found = False
+ ) {
for $paths.list -> $path {
+ next if $skip-not-found && !$path.IO.f;
+
self.read($path, $parser);
}
@@ -193,9 +202,9 @@ class Config is export
#| was used when loading the configuration.
method write(Str $path, Str $parser = "")
{
- $parser = self.get-parser($path, $parser);
+ my $chosen-parser = self.get-parser($path, $parser);
- require ::($parser);
- return ::($parser).write($path, $!content);
+ require ::($chosen-parser);
+ return ::($chosen-parser).write($path, $!content);
}
}
diff --git a/lib/Config/Parser.pm6 b/lib/Config/Parser.pm6
index 1a99ad6..d067513 100644
--- a/lib/Config/Parser.pm6
+++ b/lib/Config/Parser.pm6
@@ -6,6 +6,8 @@ use Config::Exception::UnimplementedMethodException;
class Config::Parser
{
+ #| Attempt to read the file at a given $path, and returns its
+ #| parsed contents as a Hash.
method read(Str $path --> Hash)
{
Config::Exception::UnimplementedMethodException.new(
@@ -13,7 +15,9 @@ class Config::Parser
).throw();
}
- method write(Str $path, Hash $config --> Hash)
+ #| Attempt to write the $config Hash at a given $path. Returns
+ #| True on success, False on failure.
+ method write(Str $path, Hash $config --> Bool)
{
Config::Exception::UnimplementedMethodException.new(
method => "write"
diff --git a/lib/Config/Parser/NULL.pm6 b/lib/Config/Parser/NULL.pm6
new file mode 100644
index 0000000..ff384b1
--- /dev/null
+++ b/lib/Config/Parser/NULL.pm6
@@ -0,0 +1,31 @@
+#! /usr/bin/env false
+
+use v6.c;
+
+use Config::Parser;
+
+#| The Config::Parser::NULL is a parser to mock with for testing purposes.
+#| It exposes an additional method, set-config, so you can set a config
+#| Hash to return when calling `read`.
+class Config::Parser::NULL is Config::Parser
+{
+ my $mock-config;
+
+ #| Return the mock config, skipping the file entirely.
+ method read(Str $path --> Hash)
+ {
+ $mock-config;
+ }
+
+ #| Set the mock config to return on read.
+ method set-config(Hash $config)
+ {
+ $mock-config = $config;
+ }
+
+ #| Return True, as if writing succeeded.
+ method write(Str $path, Hash $config --> Bool)
+ {
+ True;
+ }
+}
diff --git a/t/05-null-parser.t b/t/05-null-parser.t
new file mode 100644
index 0000000..97e897f
--- /dev/null
+++ b/t/05-null-parser.t
@@ -0,0 +1,30 @@
+#! /usr/bin/env perl6
+
+use v6.c;
+use Test;
+use lib "lib";
+
+use Config;
+use Config::Parser::NULL;
+
+plan 3;
+
+::("Config::Parser::NULL").set-config({
+ "a" => "a",
+ "b" => {
+ "c" => "c"
+ }
+});
+
+my $config = Config.new();
+
+ok $config.read("t/files/config", "Config::Parser::NULL"), "Attempt to read a file with Config::Parser::NULL";
+
+is-deeply $config.get(), {
+ "a" => "a",
+ "b" => {
+ "c" => "c"
+ }
+}, "Check read config from Config::Parser::NULL";
+
+ok $config.write("t/t/t"), "Attempt to write a file with Config::Parser::NULL";
diff --git a/t/test-stub b/t/files/config
index e69de29..e69de29 100644
--- a/t/test-stub
+++ b/t/files/config
diff --git a/t/files/config.yaml b/t/files/config.yaml
new file mode 100644
index 0000000..e69de29
--- /dev/null
+++ b/t/files/config.yaml
diff --git a/t/reading.t b/t/reading.t
index e43252a..0792188 100644
--- a/t/reading.t
+++ b/t/reading.t
@@ -10,9 +10,9 @@ use Config;
my $config = Config.new();
-throws-like { $config.read("nonexistant-config") }, Config::Exception::FileNotFoundException, "Reading nonexisting file";
-throws-like { $config.read("t/test-stub") }, Config::Exception::UnknownTypeException, "Reading file of unknown type";
-throws-like { $config.read("t/test-stub", "Config::Parser:NoSuchParserForTest") }, Config::Exception::MissingParserException, "Using non-existing parser";
+throws-like { $config.read("t/files/none") }, Config::Exception::FileNotFoundException, "Reading nonexisting file";
+throws-like { $config.read("t/files/config") }, Config::Exception::UnknownTypeException, "Reading file of unknown type";
+throws-like { $config.read("t/files/config", "Config::Parser:NoSuchParserForTest") }, Config::Exception::MissingParserException, "Using non-existing parser";
my $hash = {
"a" => "a",