From c40e6ae736f82f133d89b9b60cd6ff23c7690c6a Mon Sep 17 00:00:00 2001 From: Patrick Spek Date: Thu, 27 Apr 2017 15:01:23 +0200 Subject: Update method sigs, type checking and tests --- META6.json | 4 +-- lib/Config.pm6 | 28 +++++++------------ lib/Config/Exception/UnknownTypeException.pm6 | 13 --------- lib/Config/Type.pm6 | 8 ------ t/01-reading.t | 3 +-- t/06-deduce-parser.t | 39 +++++++++++++++++++++++++++ 6 files changed, 50 insertions(+), 45 deletions(-) delete mode 100644 lib/Config/Exception/UnknownTypeException.pm6 delete mode 100644 lib/Config/Type.pm6 create mode 100644 t/06-deduce-parser.t diff --git a/META6.json b/META6.json index 303955b..29890c7 100644 --- a/META6.json +++ b/META6.json @@ -1,7 +1,7 @@ { "perl": "6", "name": "Config", - "version": "1.1.0", + "version": "1.1.1", "auth": "github:scriptkitties", "description": "Extensible library for reading and writing configuration files.", "license": "GPL-3.0", @@ -10,12 +10,10 @@ ], "provides": { "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", "Config::Exception::MissingParserException": "lib/Config/Exception/MissingParserException.pm6" }, "authors": [ diff --git a/lib/Config.pm6 b/lib/Config.pm6 index a13f589..ad350bf 100644 --- a/lib/Config.pm6 +++ b/lib/Config.pm6 @@ -5,9 +5,7 @@ use v6.c; use Hash::Merge; use Config::Exception::MissingParserException; -use Config::Exception::UnknownTypeException; use Config::Exception::FileNotFoundException; -use Config::Type; use Config::Parser; class Config is export @@ -54,30 +52,22 @@ class Config is export #| Get the name of the parser module to use for the #| given path. - method get-parser(Str $path, Str $parser = "") + method get-parser(Str $path, Str $parser = "" --> Str) { - if ($parser ne "") { - return $parser; - } - - if ($!parser ne "") { - return $!parser; - } + return $parser if $parser ne ""; + return $!parser if $!parser ne ""; + say $!parser; my $type = self.get-parser-type($path); - Config::Exception::UnknownTypeException.new( - file => $path - ).throw() if $type eq Config::Type::unknown; - "Config::Parser::" ~ $type; } #| Get the type of parser required for the given path. - method get-parser-type(Str $path) + method get-parser-type(Str $path --> Str) { given ($path) { - when .ends-with(".yml") { return Config::Type::yaml; }; + when .ends-with(".yml") { return "yaml"; }; } my $file = $path; @@ -87,10 +77,10 @@ class Config is export } if (defined($file.index("."))) { - return $file.split(".")[*-1]; + return $file.split(".")[*-1].lc; } - return Config::Type::unknown; + return ""; } #| Check wether a given key exists. @@ -139,7 +129,7 @@ class Config is export CATCH { when X::CompUnit::UnsatisfiedDependency { Config::Exception::MissingParserException.new( - parser => $parser + parser => $!parser ).throw(); } } diff --git a/lib/Config/Exception/UnknownTypeException.pm6 b/lib/Config/Exception/UnknownTypeException.pm6 deleted file mode 100644 index 095572b..0000000 --- a/lib/Config/Exception/UnknownTypeException.pm6 +++ /dev/null @@ -1,13 +0,0 @@ -#! /usr/bin/env false - -use v6.c; - -class Config::Exception::UnknownTypeException is Exception -{ - has Str $.file; - - method message() - { - "Could not deduce loader type for $!file." - } -} diff --git a/lib/Config/Type.pm6 b/lib/Config/Type.pm6 deleted file mode 100644 index f348985..0000000 --- a/lib/Config/Type.pm6 +++ /dev/null @@ -1,8 +0,0 @@ -#! /usr/bin/env false - -use v6.c; - -enum Config::Type < - unknown - yaml ->; diff --git a/t/01-reading.t b/t/01-reading.t index 0792188..f834226 100644 --- a/t/01-reading.t +++ b/t/01-reading.t @@ -4,14 +4,13 @@ use v6.c; use Test; use lib "lib"; -plan 5; +plan 4; use Config; my $config = Config.new(); 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 = { diff --git a/t/06-deduce-parser.t b/t/06-deduce-parser.t new file mode 100644 index 0000000..2811d19 --- /dev/null +++ b/t/06-deduce-parser.t @@ -0,0 +1,39 @@ +#! /usr/bin/env perl6 + +use v6.c; +use Test; +use lib "lib"; + +use Config; + +plan 4; + +my $config = Config.new; + +subtest "Unknown parser type" => { + plan 1; + + is $config.get-parser-type("config"), "", "Type for plain file without extension"; +}; + +subtest "Check parser type by file extension" => { + plan 2; + + is $config.get-parser-type("config.yaml"), "yaml", "Should return extension"; + is $config.get-parser-type("config.TOML"), "toml", "Should return lower-cased extension"; +}; + +subtest "Check parser type for edge-cases defined in get-parser-type" => { + plan 1; + + is $config.get-parser-type("config.yml"), "yaml", "yml --> yaml"; +}; + +subtest "Returns correct fully qualified module name" => { + plan 4; + + is $config.get-parser("config"), "Config::Parser::", "Empty parser on unknown type"; + is $config.get-parser("config.yaml"), "Config::Parser::yaml", "Extension when available"; + is $config.get-parser("config.TOML"), "Config::Parser::toml", "Lowercased extension"; + is $config.get-parser("config", "Config::Parser::NULL"), "Config::Parser::NULL", "Given string"; +}; -- cgit v1.1