File Coverage

blib/lib/Test/Alien/Run.pm
Criterion Covered Total %
statement 83 96 86.4
branch 22 28 78.5
condition 10 23 43.4
subroutine 16 17 94.1
pod 13 13 100.0
total 144 177 81.3


line stmt bran cond sub pod time code
1             package Test::Alien::Run;
2              
3 2     2   720 use strict;
  2         14  
  2         50  
4 2     2   9 use warnings;
  2         4  
  2         45  
5 2     2   8 use Test2::API qw( context );
  2         4  
  2         1848  
6              
7             # ABSTRACT: Run object
8             our $VERSION = '0.15'; # VERSION
9              
10              
11 7     7 1 10767 sub out { shift->{out} }
12 7     7 1 39 sub err { shift->{err} }
13 27     27 1 112 sub exit { shift->{exit} }
14 14     14 1 56 sub signal { shift->{sig} }
15              
16              
17             sub success
18             {
19 5     5 1 957 my($self, $message) = @_;
20 5   50     35 $message ||= 'command succeeded';
21 5   100     11 my $ok = $self->exit == 0 && $self->signal == 0;
22 5 100       19 $ok = 0 if $self->{fail};
23              
24 5         13 my $ctx = context();
25 5         352 $ctx->ok($ok, $message);
26 5 100       731 unless($ok)
27             {
28 4 100       14 $ctx->diag(" command exited with @{[ $self->exit ]}") if $self->exit;
  1         5  
29 4 100       72 $ctx->diag(" command killed with @{[ $self->signal ]}") if $self->signal;
  1         6  
30 4 100       78 $ctx->diag(" @{[ $self->{fail} ]}") if $self->{fail};
  2         13  
31             }
32 5         133 $ctx->release;
33 5         114 $self;
34             }
35              
36              
37             sub exit_is
38             {
39 4     4 1 7739 my($self, $exit, $message) = @_;
40            
41 4   33     34 $message ||= "command exited with value $exit";
42 4         20 my $ok = $self->exit == $exit;
43            
44 4         13 my $ctx = context();
45 4         300 $ctx->ok($ok, $message);
46 4 100       429 $ctx->diag(" actual exit value was: @{[ $self->exit ]}") unless $ok;
  2         5  
47 4         131 $ctx->release;
48 4         107 $self;
49             }
50              
51              
52             sub exit_isnt
53             {
54 4     4 1 10586 my($self, $exit, $message) = @_;
55            
56 4   33     62 $message ||= "command exited with value not $exit";
57 4         14 my $ok = $self->exit != $exit;
58            
59 4         12 my $ctx = context();
60 4         384 $ctx->ok($ok, $message);
61 4 100       617 $ctx->diag(" actual exit value was: @{[ $self->exit ]}") unless $ok;
  2         10  
62 4         232 $ctx->release;
63 4         105 $self;
64             }
65              
66              
67             sub _like
68             {
69 6     6   15 my($self, $regex, $source, $not, $message) = @_;
70            
71 6         38 my $ok = $self->{$source} =~ $regex;
72 6 100       18 $ok = !$ok if $not;
73            
74 6         40 my $ctx = context();
75 6         481 $ctx->ok($ok, $message);
76 6 100       556 unless($ok)
77             {
78 2         9 $ctx->diag(" $source:");
79 2         134 $ctx->diag(" $_") for split /\r?\n/, $self->{$source};
80 2 100       127 $ctx->diag($not ? ' matches:' : ' does not match:');
81 2         123 $ctx->diag(" $regex");
82             }
83 6         132 $ctx->release;
84            
85 6         136 $self;
86             }
87              
88             sub out_like
89             {
90 2     2 1 2860 my($self, $regex, $message) = @_;
91 2   33     14 $message ||= "output matches $regex";
92 2         8 $self->_like($regex, 'out', 0, $message);
93             }
94              
95              
96             sub out_unlike
97             {
98 2     2 1 8259 my($self, $regex, $message) = @_;
99 2   33     17 $message ||= "output does not match $regex";
100 2         5 $self->_like($regex, 'out', 1, $message);
101             }
102              
103              
104             sub err_like
105             {
106 1     1 1 1379 my($self, $regex, $message) = @_;
107 1   33     8 $message ||= "standard error matches $regex";
108 1         3 $self->_like($regex, 'err', 0, $message);
109             }
110              
111              
112             sub err_unlike
113             {
114 1     1 1 1389 my($self, $regex, $message) = @_;
115 1   33     9 $message ||= "standard error does not match $regex";
116 1         4 $self->_like($regex, 'err', 1, $message);
117             }
118              
119              
120             sub note
121             {
122 1     1 1 3106 my($self) = @_;
123 1         5 my $ctx = context();
124 1         91 $ctx->note("[cmd]");
125 1         100 $ctx->note(" @{$self->{cmd}}");
  1         10  
126 1 50       85 if($self->out ne '')
127             {
128 1         6 $ctx->note("[out]");
129 1         82 $ctx->note(" $_") for split /\r?\n/, $self->out;
130             }
131 1 50       84 if($self->err ne '')
132             {
133 1         4 $ctx->note("[err]");
134 1         81 $ctx->note(" $_") for split /\r?\n/, $self->err;
135             }
136 1         83 $ctx->release;
137 1         25 $self;
138             }
139              
140              
141             sub diag
142             {
143 0     0 1   my($self) = @_;
144 0           my $ctx = context();
145 0           $ctx->diag("[cmd]");
146 0           $ctx->diag(" @{$self->{cmd}}");
  0            
147 0 0         if($self->out ne '')
148             {
149 0           $ctx->diag("[out]");
150 0           $ctx->diag(" $_") for split /\r?\n/, $self->out;
151             }
152 0 0         if($self->err ne '')
153             {
154 0           $ctx->diag("[err]");
155 0           $ctx->diag(" $_") for split /\r?\n/, $self->err;
156             }
157 0           $ctx->release;
158 0           $self;
159             }
160              
161             1;
162              
163             __END__