2009-10-10 16:21:31 +00:00
|
|
|
# -*-perl-*-
|
2009-10-10 16:11:25 +00:00
|
|
|
# testscript for Config::General Classes by Thomas Linden
|
|
|
|
|
#
|
2009-10-10 16:07:21 +00:00
|
|
|
# needs to be invoked using the command "make test" from
|
2009-10-10 16:11:25 +00:00
|
|
|
# the Config::General source directory.
|
|
|
|
|
#
|
|
|
|
|
# Under normal circumstances every test should succeed.
|
2009-10-10 16:07:21 +00:00
|
|
|
|
1.36 - simplified new() parameter parsing, should be now a little
bit better to understand.
- added new parameter -DefaultConfig, which can hold a hashref
or a string, which will be used to pre-define values
of the resulting hash after parsing a config.
Thanks to Mark Hampton <mark.hampton@qualis.com> for the
suggestion.
- added new parameter -MergeDuplicateOptions, which allows
one to overwrite duplicate options, which is required,
if you turn on -DefaultConfig, because otherwise a
array would be created, which is probably not what you
wanted.
- added patch by Danial Pearce <danial@infoxchange.net.au>
to Config::General::Extended::keys(), which allows to
retrieve the keys of the object itself (which was not
directly possible before)
- added patch by Danial Pearce <danial@infoxchange.net.au>
to Config::General::Extended::value(), which allows to
set a value to a (perlish-) nontrue value. This was a
bug.
- added patch by Danial Pearce <danial@infoxchange.net.au>
to Config::General::_parse_value, which fixes a bug in
this method, which in prior versions caused values of
"0" (zero digit) to be wiped out of the config.
- added tests in t/run.t for the new default config feature.
git-svn-id: http://dev.catalyst.perl.org/repos/Config-General/trunk@25 be1acefe-a474-0410-9a34-9b3221f2030f
2009-10-10 16:19:00 +00:00
|
|
|
BEGIN { $| = 1; print "1..18\n";}
|
2009-10-10 16:07:21 +00:00
|
|
|
use lib "blib/lib";
|
|
|
|
|
use Config::General;
|
2009-10-10 16:10:12 +00:00
|
|
|
use Data::Dumper;
|
2009-10-10 16:21:31 +00:00
|
|
|
|
2009-10-10 16:07:21 +00:00
|
|
|
print "ok\n";
|
2009-10-10 16:21:31 +00:00
|
|
|
print STDERR " .. ok # loading Config::General\n";
|
|
|
|
|
|
2009-10-10 16:07:21 +00:00
|
|
|
|
2009-10-10 16:10:12 +00:00
|
|
|
foreach (2..7) {
|
2009-10-10 16:07:21 +00:00
|
|
|
&p("t/cfg." . $_, $_);
|
|
|
|
|
}
|
|
|
|
|
|
2009-10-10 16:10:12 +00:00
|
|
|
my $conf = new Config::General("t/cfg.8");
|
2009-10-10 16:07:21 +00:00
|
|
|
my %hash = $conf->getall;
|
2009-10-10 16:21:31 +00:00
|
|
|
$conf->save_file("t/cfg.out");
|
2009-10-10 16:07:21 +00:00
|
|
|
|
|
|
|
|
my $copy = new Config::General("t/cfg.out");
|
|
|
|
|
my %copyhash = $copy->getall;
|
|
|
|
|
|
|
|
|
|
my $a = \%hash;
|
|
|
|
|
my $b = \%copyhash;
|
|
|
|
|
|
|
|
|
|
# now see if the saved hash is still the same as the
|
2009-10-10 16:10:12 +00:00
|
|
|
# one we got from cfg.8
|
2009-10-10 16:07:21 +00:00
|
|
|
if (&comp($a,$b)) {
|
|
|
|
|
print "ok\n";
|
2009-10-10 16:17:24 +00:00
|
|
|
print STDERR " .. ok # Writing Config Hash to disk and compare with original\n";
|
2009-10-10 16:07:21 +00:00
|
|
|
}
|
|
|
|
|
else {
|
2009-10-10 16:10:12 +00:00
|
|
|
print "8 not ok\n";
|
|
|
|
|
print STDERR "8 .. not ok\n";
|
2009-10-10 16:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-10-10 16:11:25 +00:00
|
|
|
############## Extended Tests #################
|
|
|
|
|
|
2009-10-10 16:21:31 +00:00
|
|
|
$conf = new Config::General(
|
|
|
|
|
-ExtendedAccess => 1,
|
|
|
|
|
-ConfigFile => "t/test.rc");
|
2009-10-10 16:11:25 +00:00
|
|
|
print "ok\n";
|
2009-10-10 16:17:24 +00:00
|
|
|
print STDERR " .. ok # Creating a new object from config file\n";
|
2009-10-10 16:11:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# now test the new notation of new()
|
2009-10-10 16:21:31 +00:00
|
|
|
my $conf2 = new Config::General(
|
|
|
|
|
-ExtendedAccess => 1,
|
|
|
|
|
-ConfigFile => "t/test.rc",
|
|
|
|
|
-AllowMultiOptions => "yes"
|
|
|
|
|
);
|
2009-10-10 16:11:25 +00:00
|
|
|
print "ok\n";
|
2009-10-10 16:17:24 +00:00
|
|
|
print STDERR " .. ok # Creating a new object using the hash parameter way\n";
|
2009-10-10 16:11:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
my $domain = $conf->obj("domain");
|
|
|
|
|
print "ok\n";
|
2009-10-10 16:17:24 +00:00
|
|
|
print STDERR " .. ok # Creating a new object from a block\n";
|
2009-10-10 16:11:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
my $addr = $domain->obj("bar.de");
|
|
|
|
|
print "ok\n";
|
2009-10-10 16:17:24 +00:00
|
|
|
print STDERR " .. ok # Creating a new object from a sub block\n";
|
2009-10-10 16:11:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
my @keys = $conf->keys("domain");
|
|
|
|
|
print "ok\n";
|
2009-10-10 16:17:24 +00:00
|
|
|
print STDERR " .. ok # Getting values from the object\n";
|
2009-10-10 16:11:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# test various OO methods
|
|
|
|
|
if ($conf->is_hash("domain")) {
|
|
|
|
|
my $domains = $conf->obj("domain");
|
|
|
|
|
foreach my $domain ($conf->keys("domain")) {
|
|
|
|
|
my $domain_obj = $domains->obj($domain);
|
|
|
|
|
foreach my $address ($domains->keys($domain)) {
|
|
|
|
|
my $blah = $domain_obj->value($address);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
print "ok\n";
|
2009-10-10 16:17:24 +00:00
|
|
|
print STDERR " .. ok # Using keys() and values() \n";
|
2009-10-10 16:11:25 +00:00
|
|
|
|
2009-10-10 16:21:31 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-10 16:11:25 +00:00
|
|
|
# test AUTOLOAD methods
|
2009-10-10 16:21:31 +00:00
|
|
|
my $conf3 = new Config::General(
|
|
|
|
|
-ExtendedAccess => 1,
|
|
|
|
|
-ConfigHash => { name => "Moser", prename => "Hannes"}
|
|
|
|
|
);
|
2009-10-10 16:11:25 +00:00
|
|
|
my $n = $conf3->name;
|
|
|
|
|
my $p = $conf3->prename;
|
|
|
|
|
$conf3->name("Meier");
|
|
|
|
|
$conf3->prename("Max");
|
2009-10-10 16:21:31 +00:00
|
|
|
$conf3->save_file("t/test.cfg");
|
2009-10-10 16:11:25 +00:00
|
|
|
|
|
|
|
|
print "ok\n";
|
2009-10-10 16:17:24 +00:00
|
|
|
print STDERR " .. ok # Using AUTOLOAD methods\n";
|
2009-10-10 16:11:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2009-10-10 16:17:24 +00:00
|
|
|
# testing variable interpolation
|
2009-10-10 16:21:31 +00:00
|
|
|
my $conf16 = new Config::General(-ConfigFile => "t/cfg.16", -InterPolateVars => 1);
|
2009-10-10 16:17:24 +00:00
|
|
|
my %h16 = $conf16->getall();
|
|
|
|
|
if($h16{etc}->{log} eq "/usr/local/log/logfile") {
|
|
|
|
|
print "ok\n";
|
|
|
|
|
print STDERR " .. ok # Testing variable interpolation\n";
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
print "16 not ok\n";
|
|
|
|
|
print STDERR "16 not ok\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1.36 - simplified new() parameter parsing, should be now a little
bit better to understand.
- added new parameter -DefaultConfig, which can hold a hashref
or a string, which will be used to pre-define values
of the resulting hash after parsing a config.
Thanks to Mark Hampton <mark.hampton@qualis.com> for the
suggestion.
- added new parameter -MergeDuplicateOptions, which allows
one to overwrite duplicate options, which is required,
if you turn on -DefaultConfig, because otherwise a
array would be created, which is probably not what you
wanted.
- added patch by Danial Pearce <danial@infoxchange.net.au>
to Config::General::Extended::keys(), which allows to
retrieve the keys of the object itself (which was not
directly possible before)
- added patch by Danial Pearce <danial@infoxchange.net.au>
to Config::General::Extended::value(), which allows to
set a value to a (perlish-) nontrue value. This was a
bug.
- added patch by Danial Pearce <danial@infoxchange.net.au>
to Config::General::_parse_value, which fixes a bug in
this method, which in prior versions caused values of
"0" (zero digit) to be wiped out of the config.
- added tests in t/run.t for the new default config feature.
git-svn-id: http://dev.catalyst.perl.org/repos/Config-General/trunk@25 be1acefe-a474-0410-9a34-9b3221f2030f
2009-10-10 16:19:00 +00:00
|
|
|
# testing value pre-setting using a hash
|
|
|
|
|
my $conf17 = new Config::General(
|
|
|
|
|
-file => "t/cfg.17",
|
|
|
|
|
-DefaultConfig => { home => "/exports/home", logs => "/var/backlog" },
|
|
|
|
|
-MergeDuplicateOptions => 1,
|
|
|
|
|
-MergeDuplicateBlocks => 1
|
|
|
|
|
);
|
|
|
|
|
my %h17 = $conf17->getall();
|
|
|
|
|
if ($h17{home} eq "/home/users") {
|
|
|
|
|
print "ok\n";
|
|
|
|
|
print STDERR " .. ok # Testing value pre-setting using a hash\n";
|
|
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
print "17 not ok\n";
|
|
|
|
|
print STDERR "17 not ok\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# testing value pre-setting using a string
|
|
|
|
|
my $conf18 = new Config::General(
|
|
|
|
|
-file => "t/cfg.17", # reuse the file
|
|
|
|
|
-DefaultConfig => "home = /exports/home\nlogs = /var/backlog",
|
|
|
|
|
-MergeDuplicateOptions => 1,
|
|
|
|
|
-MergeDuplicateBlocks => 1
|
|
|
|
|
);
|
|
|
|
|
my %h18 = $conf18->getall();
|
|
|
|
|
if ($h18{home} eq "/home/users") {
|
|
|
|
|
print "ok\n";
|
2009-10-10 16:21:31 +00:00
|
|
|
print STDERR " .. ok # Testing value pre-setting using a string\n";
|
1.36 - simplified new() parameter parsing, should be now a little
bit better to understand.
- added new parameter -DefaultConfig, which can hold a hashref
or a string, which will be used to pre-define values
of the resulting hash after parsing a config.
Thanks to Mark Hampton <mark.hampton@qualis.com> for the
suggestion.
- added new parameter -MergeDuplicateOptions, which allows
one to overwrite duplicate options, which is required,
if you turn on -DefaultConfig, because otherwise a
array would be created, which is probably not what you
wanted.
- added patch by Danial Pearce <danial@infoxchange.net.au>
to Config::General::Extended::keys(), which allows to
retrieve the keys of the object itself (which was not
directly possible before)
- added patch by Danial Pearce <danial@infoxchange.net.au>
to Config::General::Extended::value(), which allows to
set a value to a (perlish-) nontrue value. This was a
bug.
- added patch by Danial Pearce <danial@infoxchange.net.au>
to Config::General::_parse_value, which fixes a bug in
this method, which in prior versions caused values of
"0" (zero digit) to be wiped out of the config.
- added tests in t/run.t for the new default config feature.
git-svn-id: http://dev.catalyst.perl.org/repos/Config-General/trunk@25 be1acefe-a474-0410-9a34-9b3221f2030f
2009-10-10 16:19:00 +00:00
|
|
|
}
|
|
|
|
|
else {
|
|
|
|
|
print "18 not ok\n";
|
|
|
|
|
print STDERR "18 not ok\n";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
# all subs here
|
|
|
|
|
|
2009-10-10 16:07:21 +00:00
|
|
|
sub p {
|
|
|
|
|
my($cfg, $t) = @_;
|
|
|
|
|
open T, "<$cfg";
|
|
|
|
|
my @file = <T>;
|
|
|
|
|
close T;
|
|
|
|
|
@file = map { chomp($_); $_} @file;
|
|
|
|
|
my $fst = $file[0];
|
|
|
|
|
my $conf = new Config::General($cfg);
|
|
|
|
|
my %hash = $conf->getall;
|
|
|
|
|
print "ok\n";
|
2009-10-10 16:17:24 +00:00
|
|
|
print STDERR " .. ok $fst\n";
|
2009-10-10 16:07:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sub comp {
|
|
|
|
|
my($a, $b) = @_;
|
|
|
|
|
foreach my $key (keys %{$a}) {
|
|
|
|
|
if(ref($a->{$key}) eq "HASH") {
|
|
|
|
|
&comp($a->{$key},$b->{$key});
|
|
|
|
|
next;
|
|
|
|
|
}
|
|
|
|
|
elsif(ref($a->{$key}) eq "ARRAY") {
|
|
|
|
|
# ignore arrays for simplicity
|
|
|
|
|
next;
|
|
|
|
|
}
|
|
|
|
|
return 0 if($a->{$key} ne $b->{$key});
|
|
|
|
|
}
|
|
|
|
|
return 1;
|
|
|
|
|
}
|