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   789 use strict;
  3         9  
  3         118  
4 3     3   20 use warnings;
  3         7  
  3         73  
5 3     3   57 use 5.008004;
  3         11  
6 3     3   25 use Test2::API qw( context );
  3         6  
  3         4218  
7              
8             # ABSTRACT: Run object
9             our $VERSION = '2.45'; # VERSION
10              
11              
12 7     7 1 15226 sub out { shift->{out} }
13 7     7 1 50 sub err { shift->{err} }
14 27     27 1 139 sub exit { shift->{exit} }
15 14     14 1 70 sub signal { shift->{sig} }
16              
17              
18             sub success
19             {
20 5     5 1 2404 my($self, $message) = @_;
21 5   50     47 $message ||= 'command succeeded';
22 5   100     15 my $ok = $self->exit == 0 && $self->signal == 0;
23 5 100       25 $ok = 0 if $self->{fail};
24              
25 5         19 my $ctx = context();
26 5         514 $ctx->ok($ok, $message);
27 5 100       1596 unless($ok)
28             {
29 4 100       18 $ctx->diag(" command exited with @{[ $self->exit ]}") if $self->exit;
  1         8  
30 4 100       208 $ctx->diag(" command killed with @{[ $self->signal ]}") if $self->signal;
  1         5  
31 4 100       195 $ctx->diag(" @{[ $self->{fail} ]}") if $self->{fail};
  2         13  
32             }
33 5         378 $ctx->release;
34 5         132 $self;
35             }
36              
37              
38             sub exit_is
39             {
40 4     4 1 10053 my($self, $exit, $message) = @_;
41              
42 4   33     39 $message ||= "command exited with value $exit";
43 4         20 my $ok = $self->exit == $exit;
44              
45 4         13 my $ctx = context();
46 4         371 $ctx->ok($ok, $message);
47 4 100       958 $ctx->diag(" actual exit value was: @{[ $self->exit ]}") unless $ok;
  2         6  
48 4         374 $ctx->release;
49 4         101 $self;
50             }
51              
52              
53             sub exit_isnt
54             {
55 4     4 1 10069 my($self, $exit, $message) = @_;
56              
57 4   33     26 $message ||= "command exited with value not $exit";
58 4         9 my $ok = $self->exit != $exit;
59              
60 4         22 my $ctx = context();
61 4         382 $ctx->ok($ok, $message);
62 4 100       858 $ctx->diag(" actual exit value was: @{[ $self->exit ]}") unless $ok;
  2         7  
63 4         385 $ctx->release;
64 4         103 $self;
65             }
66              
67              
68             sub _like
69             {
70 6     6   19 my($self, $regex, $source, $not, $message) = @_;
71              
72 6         43 my $ok = $self->{$source} =~ $regex;
73 6 100       20 $ok = !$ok if $not;
74              
75 6         18 my $ctx = context();
76 6         569 $ctx->ok($ok, $message);
77 6 100       1025 unless($ok)
78             {
79 2         10 $ctx->diag(" $source:");
80 2         370 $ctx->diag(" $_") for split /\r?\n/, $self->{$source};
81 2 100       377 $ctx->diag($not ? ' matches:' : ' does not match:');
82 2         398 $ctx->diag(" $regex");
83             }
84 6         386 $ctx->release;
85              
86 6         154 $self;
87             }
88              
89             sub out_like
90             {
91 2     2 1 3840 my($self, $regex, $message) = @_;
92 2   33     20 $message ||= "output matches $regex";
93 2         7 $self->_like($regex, 'out', 0, $message);
94             }
95              
96              
97             sub out_unlike
98             {
99 2     2 1 10363 my($self, $regex, $message) = @_;
100 2   33     25 $message ||= "output does not match $regex";
101 2         7 $self->_like($regex, 'out', 1, $message);
102             }
103              
104              
105             sub err_like
106             {
107 1     1 1 1881 my($self, $regex, $message) = @_;
108 1   33     8 $message ||= "standard error matches $regex";
109 1         4 $self->_like($regex, 'err', 0, $message);
110             }
111              
112              
113             sub err_unlike
114             {
115 1     1 1 1890 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 42129 my($self) = @_;
124 1         4 my $ctx = context();
125 1         107 $ctx->note("[cmd]");
126 1         275 $ctx->note(" @{$self->{cmd}}");
  1         65  
127 1 50       239 if($self->out ne '')
128             {
129 1         4 $ctx->note("[out]");
130 1         236 $ctx->note(" $_") for split /\r?\n/, $self->out;
131             }
132 1 50       234 if($self->err ne '')
133             {
134 1         5 $ctx->note("[err]");
135 1         218 $ctx->note(" $_") for split /\r?\n/, $self->err;
136             }
137 1         215 $ctx->release;
138 1         26 $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__