diff options
Diffstat (limited to 't')
-rw-r--r-- | t/TestConfig.pm | 102 | ||||
-rw-r--r-- | t/conf01.t | 20 | ||||
-rw-r--r-- | t/conf02.t | 45 | ||||
-rw-r--r-- | t/conf03.t | 31 | ||||
-rw-r--r-- | t/conf04.t | 39 | ||||
-rw-r--r-- | t/config.t | 12 | ||||
-rw-r--r-- | t/f.conf | 6 |
7 files changed, 237 insertions, 18 deletions
diff --git a/t/TestConfig.pm b/t/TestConfig.pm new file mode 100644 index 0000000..ef5ce1d --- /dev/null +++ b/t/TestConfig.pm @@ -0,0 +1,102 @@ +package TestConfig; + +use strict; +use Carp; +use File::Temp; + +require App::Beam::Config; +our @ISA = qw(App::Beam::Config); + +sub new { + my $class = shift; + my $text; + local %_ = @_; + + my $file = new File::Temp(UNLINK => 1); + if (defined($text = delete $_{text})) { + print $file $text; + } else { + while (<main::DATA>) { + print $file $_; + } + } + close $file; + + my $exp = delete $_{expect}; + my $self = $class->SUPER::new($file->filename, %_); + $self->{expected_errors} = $exp if $exp; + $self->{status} = $self->parse(); + if ($exp && @{$self->{expected_errors}}) { + $self->{status} = 0; + $self->error("not all expected errors reported"); + } + return $self; +} + +sub success { + my ($self) = @_; + return $self->{status}; +} + +sub canonical { + my $self = shift; + local %_ = @_; + my $delim; + unless (defined($delim = delete $_{delim})) { + $delim = " "; + } + carp "unknown parameters: " . join(', ', keys(%_)) if (keys(%_)); + return undef unless $self->success; + + return join $delim, map { + local $Data::Dumper::Useqq = 1; + local $Data::Dumper::Terse = 1; + local $Data::Dumper::Indent = 0; + $_->[0] . "=" . Data::Dumper->Dump([$_->[1]]); + } $self->flatten(); +} + +sub expected_error { + my ($self, $msg) = @_; + if (exists($self->{expected_errors})) { + my ($i) = grep { ${$self->{expected_errors}}[$_] eq $msg } + 0..$#{$self->{expected_errors}}; + if (defined($i)) { + splice(@{$self->{expected_errors}}, $i, 1); + return 1; + } + } +} + +sub error { + my ($self, $err) = @_; + if ($err =~ /^(.+?):(.+?): (.+)/) { + unless (exists($self->{first_line})) { + if (open(my $fd, '<', $0)) { + $self->{first_line} = 0; + while (<$fd>) { + $self->{first_line}++; + chomp; + last if /^__DATA__$/; + } + close $fd; + } + } + my $line = $2 + $self->{first_line}; + push @{$self->{errors}}, { file => $0, + orig => $1, + line => $line, + message => $3 }; + print STDERR "$0:$line: $3\n" + unless $self->expected_error($3); + } else { + push @{$self->{errors}}, { message => $err }; + print STDERR "$err\n" + } +} + +sub errors { + my $self = shift; + return undef if $self->success; + return @{$self->{errors}}; +} diff --git a/t/conf01.t b/t/conf01.t new file mode 100644 index 0000000..f1d1476 --- /dev/null +++ b/t/conf01.t @@ -0,0 +1,20 @@ +# -*- perl -*- +use lib 't'; +use strict; +use Test; +use TestConfig; + +plan(tests => 1); + +my $cfg = new TestConfig; +ok($cfg->canonical, 'backend.foo.file="a" core.retain-interval=10 core.tempdir="/tmp"'); + +__DATA__ +# This is a sample configuration file +[core] + retain-interval = 10 + tempdir = /tmp +[backend foo] + file = a + + diff --git a/t/conf02.t b/t/conf02.t new file mode 100644 index 0000000..9447060 --- /dev/null +++ b/t/conf02.t @@ -0,0 +1,45 @@ +# -*- perl -*- +use lib 't'; +use strict; +use Test; +use TestConfig; + +plan(tests => 7); + +my %keywords = ( + core => { + section => { + 'retain-interval' => { mandatory => 1 }, + 'tempdir' => 1, + 'verbose' => 1, + } + }, + backend => { + section => { + file => 1 + } + } +); + +my $cfg = new TestConfig(parameters => \%keywords); +ok($cfg->isset('backend','foo','file')); +ok($cfg->isscalar('backend','foo','file')); +ok($cfg->get('backend','foo','file'), 'foo'); + +ok($cfg->isset('core', 'verbose') == 0); + +ok($cfg->issection('backend','foo')); + +$cfg->set('core','verbose','On'); +ok($cfg->get('core','verbose'),'On'); + +$cfg->unset('core','tmpdir'); +ok($cfg->isset('core','tmpdir') == 0); + +__DATA__ +# This is a sample configuration file +[core] + retain-interval = 10 + tempdir = /tmp +[backend foo] + file = foo diff --git a/t/conf03.t b/t/conf03.t new file mode 100644 index 0000000..5aa8313 --- /dev/null +++ b/t/conf03.t @@ -0,0 +1,31 @@ +# -*- perl -*- + +use lib 't'; +use strict; +use Test; +use TestConfig; + +plan(tests => 1); + +my %keywords = ( + core => { + section => { + 'tempdir' => 1, + 'verbose' => 1, + } + }, + backend => { + section => { + file => 1 + } + } +); +my $cfg = new TestConfig(parameters => \%keywords, + expect => [ 'keyword "output" is unknown' ]); +ok($cfg->errors() == 1); +__DATA__ +# This is a sample configuration file +[core] + tempdir = /tmp + output = file + diff --git a/t/conf04.t b/t/conf04.t new file mode 100644 index 0000000..2f1d8d1 --- /dev/null +++ b/t/conf04.t @@ -0,0 +1,39 @@ +# -*- perl -*- +use lib 't'; +use strict; +use Test; +use TestConfig; + +plan(tests => 1); + +my %keywords = ( + core => { + section => { + 'retain-interval' => { mandatory => 1 }, + 'tempdir' => 1, + 'verbose' => 1, + } + }, + backend => { + section => { + file => { + section => { + name => { mandatory => 1 }, + local => 1 + } + } + } + } +); + +my $cfg = new TestConfig(parameters => \%keywords, + expect => [ 'mandatory variable "core.retain-interval" not set', + 'mandatory variable "backend.file.name" not set' ]); +ok($cfg->errors()==2); + +__DATA__ +# This is a sample configuration file +[core] + tempdir = /tmp +[backend file] + local = 1 diff --git a/t/config.t b/t/config.t deleted file mode 100644 index d965844..0000000 --- a/t/config.t +++ /dev/null @@ -1,12 +0,0 @@ -# -*- perl -*- -use lib 't'; -use strict; -use App::Beam::Config; -use Test; - -plan(tests => 2); - -my $cfg = new App::Beam::Config('t/f.conf'); -ok($cfg->parse() && 1); -my $s = join " ", map { $_->[0] . "=" . $_->[1] } $cfg->flatten(); -ok($s, 'backend.foo.file=a core.retain-interval=10 core.tempdir=/tmp'); diff --git a/t/f.conf b/t/f.conf deleted file mode 100644 index 17234a1..0000000 --- a/t/f.conf +++ /dev/null @@ -1,6 +0,0 @@ -[core] - retain-interval = 10 - tempdir = /tmp -[backend foo] - file = a - |