aboutsummaryrefslogtreecommitdiff
path: root/t/TestConfig.pm
blob: 7f8f232e6267cdc2f63f00f9eea22db26d8b4eb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package TestConfig;

use strict;
use Carp;
use File::Temp;

use App::Beam::Config qw(:sort);
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;
	join('.', @{$_->[0]}) . "=" . Data::Dumper->Dump([$_->[1]{-value}]);
    } $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 %_ = @_;

    if (exists($_{locus})) {
	push @{$self->{errors}}, { message => $err };
	print STDERR $_{locus}->format($err)."\n"
	    unless $self->expected_error($err);
    } else {
	push @{$self->{errors}}, { message => $err };
	print STDERR "$err\n"
    }
}

sub errors {
    my $self = shift;
    return undef if $self->success;
    return @{$self->{errors}};
}

sub lint {
    my $self = shift;
    my $synt = shift;
    local %_ = @_;
    my $exp = $self->{expected_errors} = delete $_{expect};
    carp "unknown parameters: " . join(', ', keys(%_)) if (keys(%_));
    
    $self->SUPER::lint($synt);

    if ($exp && @{$self->{expected_errors}}) {
	$self->{status} = 0;
	$self->error("not all expected errors reported");
    }
}

Return to:

Send suggestions and report system problems to the System administrator.