added simple test for DNStools::Config hs12
authorHeiko Schlittermann <hs@schlittermann.de>
Fri, 14 Jan 2011 22:12:24 +0100
branchhs12
changeset 80 fc2156761c29
parent 79 ac04894f45c1
child 81 0bb6d585cd49
added simple test for DNStools::Config
.hgignore
Build.PL
lib/DNStools/Config.pm
t/00-config.t
--- a/.hgignore	Thu Jan 13 00:27:30 2011 +0100
+++ b/.hgignore	Fri Jan 14 22:12:24 2011 +0100
@@ -2,3 +2,4 @@
 blib
 Build
 _build
+cover_db
--- a/Build.PL	Thu Jan 13 00:27:30 2011 +0100
+++ b/Build.PL	Fri Jan 14 22:12:24 2011 +0100
@@ -1,9 +1,16 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
 use Module::Build;
+
 my $build = Module::Build->new(
-    module_name  => "dnstools",
-    dist_version => "0.1",
-    license      => "perl",
-    requires     => { perl => "5.10.0", },
+    module_name    => "dnstools",
+    author         => "schlittermann.de",
+    dist_version   => "0.1",
+    license        => "perl",
+    requires       => { perl => "5.10.0", },
+    build_requires => { "Pod::Coverage" => 0, },
 );
 
 $build->create_build_script;
--- a/lib/DNStools/Config.pm	Thu Jan 13 00:27:30 2011 +0100
+++ b/lib/DNStools/Config.pm	Fri Jan 14 22:12:24 2011 +0100
@@ -1,4 +1,5 @@
 package DNStools::Config;
+
 use strict;
 use warnings;
 
@@ -15,6 +16,8 @@
 
     # the first config FILE
     my ($_) = grep { -f } @configs;
+
+    die "no config file found, searched for @configs\n" if not $_;
     open(my $cf, $_) or die "Can't open $_: $!\n";
 
     while (<$cf>) {
@@ -34,3 +37,38 @@
 }
 
 1;
+
+__END__
+
+=head1 NAME
+
+    DNStools::Config - config parser
+
+=head1 SYNOPSIS
+
+    use DNStools::Config qw(get_config);
+    %config = get_config($file1, $file2, ...);
+
+=head1 DESCRIPTION
+
+Simple config file parser. The format is simple:
+
+    key = value
+
+All spaces are ignored.
+
+=head1 FUNCTIONS
+
+=over
+
+=item B<get_config>(I<list of config files>)
+
+Read the first file of the list (or dies if none of the files is found).
+Returns a hash with the config keys and values.
+
+=back
+
+=cut
+
+
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/t/00-config.t	Fri Jan 14 22:12:24 2011 +0100
@@ -0,0 +1,38 @@
+#! /usr/bin/perl
+
+use strict;
+use warnings;
+use Test::More;
+use Pod::Coverage;
+use File::Temp;
+
+BEGIN {
+    use_ok "DNStools::Config", "get_config";
+}
+
+can_ok("DNStools::Config" => "get_config");
+
+# should die if there is no config
+eval { get_config() };
+ok($@, "dies on missing file names");
+
+eval { get_config("xxx|xxx", "yyy|yyy") };
+ok($@, "dies on missing config");
+
+# prepare some simple sample config
+my $tmp = File::Temp->new();
+print {$tmp} <<__EOF;
+# comment
+abc = xyz
+other =    value with space
+__EOF
+close($tmp);
+
+my %cf = get_config($tmp->filename);
+ok(%cf, "got config");
+
+is($cf{abc} => "xyz", "simple value");
+is($cf{other} => "valuewithspace", "spaced value");
+
+
+done_testing();