File Coverage

blib/lib/Test/Alien/Run.pm
Criterion Covered Total %
statement 85 98 86.7
branch 22 28 78.5
condition 10 23 43.4
subroutine 17 18 94.4
pod 13 13 100.0
total 147 180 81.6


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