File Coverage

blib/lib/Test/Stream/State.pm
Criterion Covered Total %
statement 55 56 98.2
branch 27 36 75.0
condition 18 20 90.0
subroutine 10 10 100.0
pod 5 6 83.3
total 115 128 89.8


line stmt bran cond sub pod time code
1             package Test::Stream::State;
2 109     109   672 use strict;
  109         108  
  109         2377  
3 109     109   295 use warnings;
  109         100  
  109         2183  
4              
5 109     109   326 use Carp qw/confess/;
  109         98  
  109         4933  
6              
7             use Test::Stream::HashBase(
8 109         689 accessors => [qw{count failed ended bailed_out _passing _plan skip_reason}],
9 109     109   381 );
  109         102  
10              
11             sub init {
12 535     535 0 572 my $self = shift;
13              
14 535 50       1858 $self->{+COUNT} = 0 unless defined $self->{+COUNT};
15 535 50       1271 $self->{+FAILED} = 0 unless defined $self->{+FAILED};
16 535 50       1648 $self->{+_PASSING} = 1 unless defined $self->{+_PASSING};
17             }
18              
19             sub is_passing {
20 817     817 1 1561 my $self = shift;
21              
22 817 100       1369 ($self->{+_PASSING}) = @_ if @_;
23              
24             # If we already failed just return 0.
25 817   100     1590 my $pass = $self->{+_PASSING} || return 0;
26 740 100       1233 return $self->{+_PASSING} = 0 if $self->{+FAILED};
27              
28 739         678 my $count = $self->{+COUNT};
29 739         643 my $ended = $self->{+ENDED};
30 739         690 my $plan = $self->{+_PLAN};
31              
32 739 100 100     1586 return $pass if !$count && $plan && $plan =~ m/^SKIP$/;
      66        
33              
34 727 100 100     3505 return $self->{+_PASSING} = 0
      100        
35             if $ended && (!$count || !$plan);
36              
37 724 100 66     3651 return $pass unless $plan && $plan =~ m/^\d+$/;
38              
39 699 50       1076 if ($ended) {
40 699 50       1171 return $self->{+_PASSING} = 0 if $count != $plan;
41             }
42             else {
43 0 0       0 return $self->{+_PASSING} = 0 if $count > $plan;
44             }
45              
46 699         2484 return $pass;
47             }
48              
49             sub bump {
50 3220     3220 1 2723 my $self = shift;
51 3220         2620 my ($pass) = @_;
52              
53             confess "Cannot change test count after test has ended"
54 3220 50       5342 if $self->{+ENDED};
55              
56 3220         2849 $self->{+COUNT}++;
57 3220 100       6521 return if $pass;
58              
59 194         198 $self->{+FAILED}++;
60 194         343 $self->{+_PASSING} = 0;
61             }
62              
63             sub bump_fail {
64 24     24 1 49 my $self = shift;
65 24         41 $self->{+FAILED}++;
66 24         43 $self->{+_PASSING} = 0;
67             }
68              
69             sub plan {
70 1460     1460 1 1367 my $self = shift;
71              
72 1460 100       3729 return $self->{+_PLAN} unless @_;
73              
74 356         414 my ($plan) = @_;
75              
76 356 50       769 confess "You cannot unset the plan"
77             unless defined $plan;
78              
79             confess "You cannot change the plan"
80 356 100 100     1053 if $self->{+_PLAN} && $self->{+_PLAN} !~ m/^NO PLAN$/;
81              
82 355 100       1512 confess "'$plan' is not a valid plan! Plan must be an integer greater than 0, 'NO PLAN', or 'SKIP'"
83             unless $plan =~ m/^(\d+|NO PLAN|SKIP)$/;
84              
85 354         815 $self->{+_PLAN} = $plan;
86             }
87              
88             sub finish {
89 338     338 1 1084 my $self = shift;
90 338         403 my ($frame) = @_;
91              
92 338 100       725 if($self->{+ENDED}) {
93 3         4 my (undef, $ffile, $fline) = @{$self->{+ENDED}};
  3         8  
94 3         4 my (undef, $sfile, $sline) = @$frame;
95              
96 3         23 die <<" EOT"
97             Test already ended!
98             First End: $ffile line $fline
99             Second End: $sfile line $sline
100             EOT
101             }
102              
103 335         485 $self->{+ENDED} = $frame;
104 335         705 $self->is_passing(); # Generate the final boolean.
105             }
106              
107             1;
108              
109             __END__