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   1028 use strict;
  109         180  
  109         2563  
3 109     109   533 use warnings;
  109         174  
  109         2737  
4              
5 109     109   535 use Carp qw/confess/;
  109         164  
  109         5834  
6              
7             use Test::Stream::HashBase(
8 109         1002 accessors => [qw{count failed ended bailed_out _passing _plan skip_reason}],
9 109     109   561 );
  109         174  
10              
11             sub init {
12 531     531 0 929 my $self = shift;
13              
14 531 50       2518 $self->{+COUNT} = 0 unless defined $self->{+COUNT};
15 531 50       1919 $self->{+FAILED} = 0 unless defined $self->{+FAILED};
16 531 50       2582 $self->{+_PASSING} = 1 unless defined $self->{+_PASSING};
17             }
18              
19             sub is_passing {
20 814     814 1 2449 my $self = shift;
21              
22 814 100       2045 ($self->{+_PASSING}) = @_ if @_;
23              
24             # If we already failed just return 0.
25 814   100     2507 my $pass = $self->{+_PASSING} || return 0;
26 737 100       1885 return $self->{+_PASSING} = 0 if $self->{+FAILED};
27              
28 736         1191 my $count = $self->{+COUNT};
29 736         1122 my $ended = $self->{+ENDED};
30 736         1179 my $plan = $self->{+_PLAN};
31              
32 736 100 100     2319 return $pass if !$count && $plan && $plan =~ m/^SKIP$/;
      66        
33              
34 724 100 100     5121 return $self->{+_PASSING} = 0
      100        
35             if $ended && (!$count || !$plan);
36              
37 721 100 66     4749 return $pass unless $plan && $plan =~ m/^\d+$/;
38              
39 696 50       1523 if ($ended) {
40 696 50       1731 return $self->{+_PASSING} = 0 if $count != $plan;
41             }
42             else {
43 0 0       0 return $self->{+_PASSING} = 0 if $count > $plan;
44             }
45              
46 696         3446 return $pass;
47             }
48              
49             sub bump {
50 3187     3187 1 4017 my $self = shift;
51 3187         4420 my ($pass) = @_;
52              
53             confess "Cannot change test count after test has ended"
54 3187 50       7391 if $self->{+ENDED};
55              
56 3187         4521 $self->{+COUNT}++;
57 3187 100       11929 return if $pass;
58              
59 185         266 $self->{+FAILED}++;
60 185         487 $self->{+_PASSING} = 0;
61             }
62              
63             sub bump_fail {
64 24     24 1 69 my $self = shift;
65 24         50 $self->{+FAILED}++;
66 24         70 $self->{+_PASSING} = 0;
67             }
68              
69             sub plan {
70 1456     1456 1 2276 my $self = shift;
71              
72 1456 100       5865 return $self->{+_PLAN} unless @_;
73              
74 355         992 my ($plan) = @_;
75              
76 355 50       1409 confess "You cannot unset the plan"
77             unless defined $plan;
78              
79             confess "You cannot change the plan"
80 355 100 100     1478 if $self->{+_PLAN} && $self->{+_PLAN} !~ m/^NO PLAN$/;
81              
82 354 100       1993 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 353         1178 $self->{+_PLAN} = $plan;
86             }
87              
88             sub finish {
89 337     337 1 1668 my $self = shift;
90 337         945 my ($frame) = @_;
91              
92 337 100       1248 if($self->{+ENDED}) {
93 3         6 my (undef, $ffile, $fline) = @{$self->{+ENDED}};
  3         11  
94 3         9 my (undef, $sfile, $sline) = @$frame;
95              
96 3         26 die <<" EOT"
97             Test already ended!
98             First End: $ffile line $fline
99             Second End: $sfile line $sline
100             EOT
101             }
102              
103 334         725 $self->{+ENDED} = $frame;
104 334         1136 $self->is_passing(); # Generate the final boolean.
105             }
106              
107             1;
108              
109             __END__