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   1013 use strict;
  109         184  
  109         2604  
3 109     109   511 use warnings;
  109         162  
  109         2708  
4              
5 109     109   502 use Carp qw/confess/;
  109         167  
  109         5756  
6              
7             use Test::Stream::HashBase(
8 109         920 accessors => [qw{count failed ended bailed_out _passing _plan skip_reason}],
9 109     109   583 );
  109         186  
10              
11             sub init {
12 531     531 0 940 my $self = shift;
13              
14 531 50       2604 $self->{+COUNT} = 0 unless defined $self->{+COUNT};
15 531 50       1983 $self->{+FAILED} = 0 unless defined $self->{+FAILED};
16 531 50       2503 $self->{+_PASSING} = 1 unless defined $self->{+_PASSING};
17             }
18              
19             sub is_passing {
20 814     814 1 2641 my $self = shift;
21              
22 814 100       2152 ($self->{+_PASSING}) = @_ if @_;
23              
24             # If we already failed just return 0.
25 814   100     2539 my $pass = $self->{+_PASSING} || return 0;
26 737 100       1823 return $self->{+_PASSING} = 0 if $self->{+FAILED};
27              
28 736         1232 my $count = $self->{+COUNT};
29 736         1159 my $ended = $self->{+ENDED};
30 736         1207 my $plan = $self->{+_PLAN};
31              
32 736 100 100     2442 return $pass if !$count && $plan && $plan =~ m/^SKIP$/;
      66        
33              
34 724 100 100     5444 return $self->{+_PASSING} = 0
      100        
35             if $ended && (!$count || !$plan);
36              
37 721 100 66     4975 return $pass unless $plan && $plan =~ m/^\d+$/;
38              
39 696 50       1595 if ($ended) {
40 696 50       1908 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         3748 return $pass;
47             }
48              
49             sub bump {
50 3187     3187 1 4104 my $self = shift;
51 3187         4351 my ($pass) = @_;
52              
53             confess "Cannot change test count after test has ended"
54 3187 50       7438 if $self->{+ENDED};
55              
56 3187         4602 $self->{+COUNT}++;
57 3187 100       12128 return if $pass;
58              
59 185         286 $self->{+FAILED}++;
60 185         519 $self->{+_PASSING} = 0;
61             }
62              
63             sub bump_fail {
64 24     24 1 70 my $self = shift;
65 24         58 $self->{+FAILED}++;
66 24         79 $self->{+_PASSING} = 0;
67             }
68              
69             sub plan {
70 1456     1456 1 2288 my $self = shift;
71              
72 1456 100       5869 return $self->{+_PLAN} unless @_;
73              
74 355         966 my ($plan) = @_;
75              
76 355 50       1523 confess "You cannot unset the plan"
77             unless defined $plan;
78              
79             confess "You cannot change the plan"
80 355 100 100     1476 if $self->{+_PLAN} && $self->{+_PLAN} !~ m/^NO PLAN$/;
81              
82 354 100       2034 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         1282 $self->{+_PLAN} = $plan;
86             }
87              
88             sub finish {
89 337     337 1 1771 my $self = shift;
90 337         1024 my ($frame) = @_;
91              
92 337 100       1151 if($self->{+ENDED}) {
93 3         6 my (undef, $ffile, $fline) = @{$self->{+ENDED}};
  3         9  
94 3         8 my (undef, $sfile, $sline) = @$frame;
95              
96 3         28 die <<" EOT"
97             Test already ended!
98             First End: $ffile line $fline
99             Second End: $sfile line $sline
100             EOT
101             }
102              
103 334         826 $self->{+ENDED} = $frame;
104 334         1074 $self->is_passing(); # Generate the final boolean.
105             }
106              
107             1;
108              
109             __END__