File Coverage

blib/lib/Test/Run/Base.pm
Criterion Covered Total %
statement 70 70 100.0
branch 4 4 100.0
condition 4 4 100.0
subroutine 19 19 100.0
pod 2 2 100.0
total 99 99 100.0


line stmt bran cond sub pod time code
1             package Test::Run::Base;
2              
3 24     24   1973944 use strict;
  24         125  
  24         1108  
4 24     24   139 use warnings;
  24         44  
  24         1523  
5              
6 24     24   1379 use MRO::Compat;
  24         4967  
  24         840  
7              
8              
9             =head1 NAME
10              
11             Test::Run::Base - base class for all of Test::Run.
12              
13             =head1 DESCRIPTION
14              
15             This is the base class for all Test::Run classes. It inherits from
16             L<Class::Accessor> and provides some goodies of its own.
17              
18             =head1 METHODS
19              
20             =cut
21              
22 24     24   1623 use Moose;
  24         1270533  
  24         189  
23              
24 24     24   219251 use Text::Sprintf::Named;
  24         28105  
  24         1557  
25 24     24   13621 use Test::Run::Sprintf::Named::FromAccessors;
  24         107  
  24         2093  
26              
27 24     24   14543 use Test::Run::Class::Hierarchy (qw(hierarchy_of rev_hierarchy_of));
  24         104  
  24         2528  
28              
29 24     24   205 use Carp ();
  24         89  
  24         22521  
30              
31             has '_formatters' => (is => "rw", isa => "HashRef", default => sub { +{} },);
32              
33             =head2 $package->new({%args})
34              
35             The default constructor. Do not over-ride it. Instead, define a
36             L<BUILD()> method.
37              
38             =cut
39              
40             =head2 $dest->copy_from($source, [@fields])
41              
42             Assigns the fields C<@fields> using their accessors based on their values
43             in C<$source>.
44              
45             =cut
46              
47             sub copy_from
48             {
49 57     57 1 220 my ($dest, $source, $fields) = @_;
50              
51 57         359 foreach my $f (@$fields)
52             {
53 228         9436 $dest->$f($source->$f());
54             }
55              
56 57         205 return;
57             }
58              
59             sub _get_formatter
60             {
61 476     476   852 my ($self, $fmt) = @_;
62              
63             return
64 476         2043 Text::Sprintf::Named->new(
65             { fmt => $fmt, },
66             );
67             }
68              
69             sub _register_formatter
70             {
71 468     468   930 my ($self, $name, $fmt) = @_;
72              
73 468         1081 $self->_formatters->{$name} = $self->_get_formatter($fmt);
74              
75 468         2589 return;
76             }
77              
78             sub _get_obj_formatter
79             {
80 685     685   1530 my ($self, $fmt) = @_;
81              
82             return
83 685         5228 Test::Run::Sprintf::Named::FromAccessors->new(
84             { fmt => $fmt, },
85             );
86             }
87              
88             sub _register_obj_formatter
89             {
90 671     671   1473 my ($self, $args) = @_;
91              
92 671         1391 my $name = $args->{name};
93 671         1407 my $fmt = $args->{format};
94              
95 671         2312 $self->_formatters->{$name} = $self->_get_obj_formatter($fmt);
96              
97 671         2720 return;
98             }
99              
100             sub _format
101             {
102 121     121   444 my ($self, $format, $args) = @_;
103              
104 121 100       419 if (ref($format) eq "")
105             {
106 113         4349 return $self->_formatters->{$format}->format({ args => $args});
107             }
108             else
109             {
110 8         35 return $self->_get_formatter(${$format})->format({ args => $args});
  8         76  
111             }
112             }
113              
114             sub _format_self
115             {
116 43     43   603 my ($self, $format, $args) = @_;
117              
118 43   100     287 $args ||= {};
119              
120 43         82 return $self->_format($format, { obj => $self, %{$args}});
  43         324  
121             }
122              
123             =head2 $self->accum_array({ method => $method_name })
124              
125             This is a more simplistic version of the :CUMULATIVE functionality
126             in Class::Std. It was done to make sure that one can collect all the
127             members of array refs out of methods defined in each class into one big
128             array ref, that can later be used.
129              
130             =cut
131              
132             sub accum_array
133             {
134 204     204 1 3480 my ($self, $args) = @_;
135              
136 204         571 my $method_name = $args->{method};
137              
138             # my $class = ((ref($self) eq "") ? $self : ref($self));
139              
140 204         453 my @results;
141 204         3652 foreach my $isa_class (
142             $self->meta->find_all_methods_by_name($method_name)
143             )
144             {
145 7         1595 my $body = $isa_class->{code}->body();
146 7         13 push @results, @{ $self->$body() };
  7         23  
147             }
148              
149 204         83086 return \@results;
150             }
151              
152             sub _list_pluralize
153             {
154 12     12   106 my ($self, $noun, $list) = @_;
155              
156 12         160 return $self->_pluralize($noun, scalar(@$list));
157             }
158              
159             sub _pluralize
160             {
161 21     21   79 my ($self, $noun, $count) = @_;
162              
163 21 100       323 return sprintf("%s%s",
164             $noun,
165             (($count > 1) ? "s" : "")
166             );
167             }
168              
169             =head2 $self->_run_sequence(\@params)
170              
171             Runs the sequence of commands specified using
172             C<_calc__${calling_sub}__callbacks> while passing @params to
173             each one. Generates a list of all the callbacks return values.
174              
175             =cut
176              
177             sub _run_sequence
178             {
179 543     543   1526 my $self = shift;
180 543   100     7465 my $params = shift || [];
181              
182 543         12914 my $sub = (caller(1))[3];
183              
184 543         6612 $sub =~ s{::_?([^:]+)$}{};
185              
186 543         2797 my $calc_cbs_sub = "_calc__${1}__callbacks";
187              
188             return
189             [
190 1740         4278 map { my $cb = $_; $self->$cb(@$params); }
  1740         21084  
191 543         1630 @{$self->$calc_cbs_sub(@$params)}
  543         3658  
192             ];
193             }
194              
195             1;
196              
197             __END__
198              
199             =head1 LICENSE
200              
201             This file is licensed under the MIT X11 License:
202              
203             http://www.opensource.org/licenses/mit-license.php
204              
205             =cut
206