summaryrefslogtreecommitdiffabout
path: root/t
Side-by-side diff
Diffstat (limited to 't') (more/less context) (ignore whitespace changes)
-rw-r--r--t/ConfigSpec.pm25
-rw-r--r--t/TestConfig.pm130
-rw-r--r--t/conf00.t22
-rw-r--r--t/conf01.t17
-rw-r--r--t/conf02.t13
-rw-r--r--t/conf03.t16
-rw-r--r--t/conf04.t17
-rw-r--r--t/conf05.t14
8 files changed, 254 insertions, 0 deletions
diff --git a/t/ConfigSpec.pm b/t/ConfigSpec.pm
new file mode 100644
index 0000000..3963d83
--- a/dev/null
+++ b/t/ConfigSpec.pm
@@ -0,0 +1,25 @@
+package ConfigSpec;
+use parent 'TestConfig';
+
+sub _check_abs_name {
+ my ($self, $valref, $prev_value, $locus) = @_;
+ unless ($$valref =~ m{^/}) {
+ $self->error("not an absolute pathname", locus => $locus);
+ return 0;
+ }
+ 1;
+}
+
+1;
+__DATA__
+[core]
+ base = STRING :mandatory null
+ number = NUMBER :array
+ size = STRING :re='\d+(?:(?i) *[kmg])'
+[load]
+ file = STRING :check=_check_abs_name :mandatory
+[load ANY param:mandatory]
+ mode = OCTAL
+ owner = STRING
+
+
diff --git a/t/TestConfig.pm b/t/TestConfig.pm
new file mode 100644
index 0000000..886e66b
--- a/dev/null
+++ b/t/TestConfig.pm
@@ -0,0 +1,130 @@
+package TestConfig;
+
+use strict;
+use warnings;
+use Carp;
+
+use Config::Tree qw(:sort);
+use parent 'Config::Parser::Ini';
+use Data::Dumper;
+use File::Temp;
+
+=head1 CONSTRUCTOR
+
+ $obj = new TestConfig(KW => VAL, ...)
+
+Key arguments:
+
+=over 4
+
+=item B<text>
+
+ Text of the configuration file
+
+=item expect
+
+ Reference to the list of expected errors
+
+=back
+
+=cut
+
+sub new {
+ my $class = shift;
+ local %_ = @_;
+
+ my $file = new File::Temp(UNLINK => 1);
+ if (defined(my $text = delete $_{text})) {
+ print $file $text;
+ } else {
+ while (<main::DATA>) {
+ print $file $_;
+ }
+ }
+ close $file;
+
+ my $exp = delete $_{expect};
+ # FIXME: Filter out fh and line keywords?
+ my $self = $class->SUPER::new(%_);
+ $self->{_expected_errors} = $exp if $exp;
+ $self->parse($file->filename);
+ $self->{_status} = $self->commit;
+
+ 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 %_ = @_;
+ carp "unknown parameters: " . join(', ', keys(%_)) if (keys(%_));
+ return join $_{delim} // " ", map {
+ join('.', @{$_->[0]})
+ . "="
+ . Data::Dumper->new([$_->[1]->value])
+ ->Useqq(1)
+ ->Terse(1)
+ ->Indent(0)
+ ->Dump
+ } $self->flatten(sort => SORT_PATH);
+}
+
+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 = shift;
+ my $err = shift;
+ local %_ = @_;
+ push @{$self->{_errors}}, { message => $err };
+ unless ($self->expected_error($err)) {
+ print STDERR "$_{locus}: " if $_{locus};
+ print STDERR "$err\n";
+ }
+}
+
+sub errors {
+ my $self = shift;
+ return 0+@{$self->{_errors}};
+}
+
+sub report {
+ my ($self, $err) = @_;
+ print STDERR "$err\n"
+}
+
+sub lint {
+ my $self = shift;
+ my $synt = shift;
+ local %_ = @_;
+ my $exp = $self->{_expected_errors} = delete $_{expect};
+ carp "unknown parameters: " . join(', ', keys(%_)) if (keys(%_));
+
+ my $ret = $self->SUPER::lint($synt);
+
+ if ($exp && @{$self->{_expected_errors}}) {
+ $self->{_status} = 0;
+ $self->report("not all expected errors reported: @{$self->{_expected_errors}}");
+ }
+ return $ret;
+}
+
+1;
diff --git a/t/conf00.t b/t/conf00.t
new file mode 100644
index 0000000..b497c86
--- a/dev/null
+++ b/t/conf00.t
@@ -0,0 +1,22 @@
+# -*- perl -*-
+use lib qw(t lib);
+use strict;
+use Test;
+use ConfigSpec;
+
+plan(tests => 1);
+
+my $c = new ConfigSpec;
+ok($c->canonical,
+ q{core.base=4 core.number=[5,10] core.size="10 k" load.file="/etc/passwd" load.foobar="baz"});
+
+__DATA__
+[core]
+ number = 5
+ base = 4
+ size = 10 k
+ number = 10
+[load]
+ file = /etc/passwd
+ foobar = baz
+
diff --git a/t/conf01.t b/t/conf01.t
new file mode 100644
index 0000000..36b1f65
--- a/dev/null
+++ b/t/conf01.t
@@ -0,0 +1,17 @@
+# -*- perl -*-
+use lib qw(t lib);
+use strict;
+use Test;
+use ConfigSpec;
+
+plan(tests => 1);
+
+my $c = new ConfigSpec(expect => ['keyword "output" is unknown']);
+ok($c->errors() == 1);
+
+__DATA__
+[core]
+ number = 5
+ output = file;
+[load]
+ file = /etc/passwd
diff --git a/t/conf02.t b/t/conf02.t
new file mode 100644
index 0000000..da0dc07
--- a/dev/null
+++ b/t/conf02.t
@@ -0,0 +1,13 @@
+# -*- perl -*-
+use lib qw(t lib);
+use strict;
+use Test;
+use ConfigSpec;
+
+plan(tests => 1);
+
+my $c = new ConfigSpec(expect => ['mandatory variable "load.file" not set']);
+ok($c->errors() == 1);
+__DATA__
+[core]
+ number = 5
diff --git a/t/conf03.t b/t/conf03.t
new file mode 100644
index 0000000..5b49347
--- a/dev/null
+++ b/t/conf03.t
@@ -0,0 +1,16 @@
+# -*- perl -*-
+use lib qw(t lib);
+use strict;
+use Test;
+use ConfigSpec;
+
+plan(tests => 1);
+
+my $c = new ConfigSpec(expect => ['invalid value for size']);
+ok($c->errors() == 1);
+
+__DATA__
+[core]
+ size = 11
+[load]
+ file = /etc/passwd
diff --git a/t/conf04.t b/t/conf04.t
new file mode 100644
index 0000000..ab4c12a
--- a/dev/null
+++ b/t/conf04.t
@@ -0,0 +1,17 @@
+# -*- perl -*-
+use lib qw(t lib);
+use strict;
+use Test;
+use ConfigSpec;
+
+plan(tests => 1);
+
+my $c = new ConfigSpec(expect => ['not an absolute pathname',
+ 'mandatory variable "load.file" not set']);
+ok($c->errors() == 2);
+
+__DATA__
+[core]
+
+[load]
+ file = test
diff --git a/t/conf05.t b/t/conf05.t
new file mode 100644
index 0000000..aaec270
--- a/dev/null
+++ b/t/conf05.t
@@ -0,0 +1,14 @@
+# -*- perl -*-
+use lib qw(t lib);
+use strict;
+use Test;
+use ConfigSpec;
+
+plan(tests => 1);
+
+my $c = new ConfigSpec;
+ok($c->canonical, q{core.base="null" load.file="/test"});
+
+__DATA__
+[load]
+ file = /test

Return to:

Send suggestions and report system problems to the System administrator.