Model.pm

filter: Statementstmt 100% 3 / 3 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 1 / 1 Podpod n/a totaltotal 100% 4 / 4 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
CRAP 1.0
« example.pl ↑ Summary Model/Consolidated.pm »
1 #!/usr/bin/perl
2  
3 package Test::TAP::Model;
4 5 ▶use base qw/Test::Harness::Straps/;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 use strict;
7 use warnings;
8  
9 use Test::TAP::Model::File;
10  
11 use List::Util qw/sum/;
12  
13 our $VERSION = "0.10";
14  
15 # callback handlers
16 sub _handle_bailout {
17 my($self, $line, $type, $totals) = @_;
18  
19 $self->log_event(
20 type => 'bailout',
21 ($self->{bailout_reason}
22 ? (reason => $self->{bailout_reason})
23 : ()
24 ),
25 );
26  
27 $self->{meat}{test_files}[-1]{results} = $totals;
28  
29 die "Bailed out"; # catch with an eval { }
30 }
31
32 sub _handle_test {
33 my($self, $line, $type, $totals) = @_;
34 my $curr = $totals->seen || 0;
35  
36 # this is used by pugs' Test.pm, it's rather useful
37 my $pos;
38 if ($line =~ /^(.*?) <pos:(.*)>(\r?$|\s*#.*\r?$)/){
39 $line = $1 . $3;
40 $pos = $2;
41 }
42  
43 my %details = %{ $totals->details->[-1] };
44  
45 $self->log_event(
46 type => 'test',
47 num => $curr,
48 ok => $details{ok},
49 actual_ok => $details{actual_ok},
50 str => $details{ok} # string for people
51 ? "ok $curr/" . $totals->max
52 : "NOK $curr",
53 todo => ($details{type} eq 'todo'),
54 skip => ($details{type} eq 'skip'),
55  
56 reason => $details{reason}, # if at all
57  
58 # pugs aux stuff
59 line => $line,
60 pos => $pos,
61 );
62  
63 if( $curr > $self->{'next'} ) {
64 $self->latest_event->{note} =
65 "Test output counter mismatch [test $curr]\n";
66 }
67 elsif( $curr < $self->{'next'} ) {
68 $self->latest_event->{note} = join("",
69 "Confused test output: test $curr answered after ",
70 "test ", ($self->{'next'}||0) - 1, "\n");
71 }
72 }
73  
74 sub _handle_other {
75 my($self, $line, $type, $totals) = @_;
76  
77 my $last_test = $self->{meat}{test_files}[-1];
78 if (@{ $last_test->{events} ||= [] } > 0) {
79 ($self->latest_event->{diag} ||= "") .= "$line\n";
80 } else {
81 ($last_test->{pre_diag} ||= "") .= "$line\n";
82 }
83 }
84  
85 sub new_with_tests {
86 my $pkg = shift;
87 my @tests = @_;
88  
89 my $self = $pkg->new;
90 $self->run_tests(@tests);
91  
92 $self;
93 }
94  
95 sub new_with_struct {
96 my $pkg = shift;
97 my $meat = shift;
98  
99 my $self = $pkg->new(@_);
100 $self->{meat} = $meat; # FIXME - the whole Test::Harness::Straps model can be figured out from this
101  
102 $self;
103 }
104  
105 sub structure {
106 my $self = shift;
107 $self->{meat};
108 }
109  
110 # just a dispatcher for the above event handlers
111 sub _init {
112 my $s = shift;
113  
114 $s->{callback} = sub {
115 my($self, $line, $type, $totals) = @_;
116  
117 my $meth = "_handle_$type";
118 $self->$meth($line, $type, $totals) if $self->can($meth);
119 };
120  
121 $s->SUPER::_init( @_ );
122 }
123  
124 sub log_time {
125 my $self = shift;
126 $self->{log_time} = shift if @_;
127 $self->{log_time};
128 }
129  
130 sub log_event {
131 my $self = shift;
132 my %event = (($self->log_time ? (time => time) : ()), @_);
133  
134 push @{ $self->{events} }, \%event;
135  
136 \%event;
137 }
138  
139 sub latest_event {
140 my($self) = shift;
141 my %event = @_;
142 $self->{events}[-1] || $self->log_event(%event);
143 }
144  
145 sub run {
146 my $self = shift;
147 $self->run_tests($self->get_tests);
148 }
149  
150 sub get_tests {
151 die 'the method get_tests is a stub. You must implement it yourself if you want $self->run to work.';
152 }
153  
154 sub run_tests {
155 my $self = shift;
156  
157 $self->_init;
158  
159 $self->{meat}{start_time} = time;
160  
161 foreach my $file (@_) {
162 $self->run_test($file);
163 }
164  
165 $self->{meat}{end_time} = time;
166 }
167  
168 sub run_test {
169 my $self = shift;
170 my $file = shift;
171  
172 my $test_file = $self->start_file($file);
173
174 my $results = eval { $self->analyze_file($file) } || Test::Harness::Results->new;
175 $test_file->{results} = $results;
176 $test_file->{results}->details(undef); # we don't need that
177  
178 $test_file;
179 }
180  
181 sub start_file {
182 my $self = shift;
183 my $file = shift;
184  
185 push @{ $self->{meat}{test_files} }, my $test_file = {
186 file => $file,
187 events => ($self->{events} = []),
188 };
189  
190 $test_file;
191 }
192  
193 sub file_class { "Test::TAP::Model::File" }
194  
195 sub test_files {
196 my $self = shift;
197 @{$self->{_test_files_cache} ||= [ $self->get_test_files ]};
198 }
199  
200 sub get_test_files {
201 my $self = shift;
202 map { $self->file_class->new($_) } @{ $self->{meat}{test_files} };
203 }
204  
205 sub ok { $_->ok or return for $_[0]->test_files; 1 }; *passed = \&ok; *passing = \&ok;
206 sub nok { !$_[0]->ok }; *failing = \&nok; *failed = \&nok;
207 sub total_ratio { return $_ ? $_[0]->total_passed / $_ : ($_[0]->ok ? 1 : 0) for $_[0]->total_seen }; *ratio = \&total_ratio;
208 sub total_percentage { sprintf("%.2f%%", 100 * $_[0]->total_ratio) }
209 sub total_seen { sum map { scalar $_->seen } $_[0]->test_files }
210 sub total_todo { sum map { scalar $_->todo_tests } $_[0]->test_files }
211 sub total_skipped { sum map { scalar $_->skipped_tests } $_[0]->test_files }
212 sub total_passed { sum map { scalar $_->ok_tests } $_[0]->test_files }; *total_ok = \&total_passed;
213 sub total_failed { sum map { scalar $_->nok_tests } $_[0]->test_files }; *total_nok = \&total_failed;
214 sub total_unexpectedly_succeeded { sum map { scalar $_->unexpectedly_succeeded_tests } $_[0]->test_files }
215  
216 sub summary {
217 my $self = shift;
218 $self->{_summary} ||=
219 sprintf "%d test cases: %d ok, %d failed, %d todo, "
220 ."%d skipped and %d unexpectedly succeeded",
221 map { my $m = "total_$_"; $self->$m }
222 qw/seen passed failed todo skipped unexpectedly_succeeded/;
223 }
224  
225 __PACKAGE__
226  
227 __END__
« example.pl ↑ Summary Model/Consolidated.pm »