File Coverage

blib/lib/App/Spec/Run/Response.pm
Criterion Covered Total %
statement 45 49 91.8
branch 9 14 64.2
condition 2 4 50.0
subroutine 9 9 100.0
pod 4 4 100.0
total 69 80 86.2


line stmt bran cond sub pod time code
1             # ABSTRACT: Response class for App::Spec::Run
2 7     7   264098 use strict;
  7         13  
  7         267  
3 7     7   28 use warnings;
  7         33  
  7         1180  
4             package App::Spec::Run::Response;
5              
6             our $VERSION = 'v0.15.0'; # VERSION
7              
8 7     7   3690 use App::Spec::Run::Output;
  7         22  
  7         318  
9 7     7   51 use Scalar::Util qw/ blessed /;
  7         14  
  7         465  
10              
11 7     7   36 use Moo;
  7         31  
  7         27  
12              
13             has exit => ( is => 'rw', default => 0 );
14             has outputs => ( is => 'rw', default => sub { [] } );
15             has finished => ( is => 'rw' );
16             has halted => ( is => 'rw' );
17             has buffered => ( is => 'rw', default => 0 );
18             has callbacks => ( is => 'rw', default => sub { +{} } );
19              
20             sub add_output {
21 25     25 1 75 my ($self, @out) = @_;
22              
23 25         117 for my $out (@out) {
24 25 50       80 unless (blessed $out) {
25 25 100       919 $out = App::Spec::Run::Output->new(
26             content => $out,
27             ref $out ? (type => 'data') : (),
28             );
29             }
30             }
31              
32 25 100       4614 if ($self->buffered) {
33 23         78 my $outputs = $self->outputs;
34 23         485 push @$outputs, @out;
35             }
36             else {
37 2         7 $self->print_output(@out);
38             }
39             }
40              
41             sub add_error {
42 28     28 1 86 my ($self, @out) = @_;
43              
44 28         82 for my $out (@out) {
45 28 50       80 unless (blessed $out) {
46 28         1049 $out = App::Spec::Run::Output->new(
47             error => 1,
48             content => $out,
49             );
50             }
51             }
52              
53 28 50       825 if ($self->buffered) {
54 28         123 my $outputs = $self->outputs;
55 28         157 push @$outputs, @out;
56             }
57             else {
58 0         0 $self->print_output(@out);
59             }
60             }
61              
62             sub print_output {
63 2     2 1 5 my ($self, @out) = @_;
64 2         6 my $outputs = $self->outputs;
65 2         4 push @$outputs, @out;
66              
67 2   50     10 my $callbacks = $self->callbacks->{print_output} || [];
68 2         4 for my $cb (@$callbacks) {
69 4         8 $cb->();
70             }
71              
72 2         11 while (my $out = shift @$outputs) {
73 2         9 my $content = $out->content;
74 2 50       6 if (ref $content) {
75 0         0 require Data::Dumper;
76 0         0 $content = Data::Dumper->Dump([$content], ['output']);
77             }
78 2 50       8 if ($out->error) {
79 0         0 print STDERR $content;
80             }
81             else {
82 2         106 print $content;
83             }
84             }
85             }
86              
87             sub add_callbacks {
88 66     66 1 200 my ($self, $event, $cb_add) = @_;
89 66         165 my $callbacks = $self->callbacks;
90 66   50     398 my $cb = $callbacks->{ $event } ||= [];
91 66         246 push @$cb, @$cb_add;
92             }
93              
94             1;
95              
96             __END__