File Coverage

blib/lib/Test/Able/Runner/Role/Meta/Class.pm
Criterion Covered Total %
statement 32 38 84.2
branch 7 18 38.8
condition n/a
subroutine 7 7 100.0
pod 3 3 100.0
total 49 66 74.2


line stmt bran cond sub pod time code
1             package Test::Able::Runner::Role::Meta::Class;
2             {
3             $Test::Able::Runner::Role::Meta::Class::VERSION = '1.002';
4             }
5 2     2   6142 use Moose::Role;
  2         4  
  2         17  
6 2     2   8896 use Class::Load;
  2         5  
  2         563  
7              
8             =head1 NAME
9              
10             Test::Able::Runner::Role::Meta::Class - metaclass role for test runners
11              
12             =head1 VERSION
13              
14             version 1.002
15              
16             =head1 DESCRIPTION
17              
18             This class provides the real guts for loading the test objects to run. However, you probably don't need to use it directly unless you are doing something fancy. See L<Test::Able::Runner> for the usual cases.
19              
20             =head1 ATTRIBUTES
21              
22             =head2 base_package
23              
24             This is set by the C<< -base_package >> option sent to C<< use_test_packages >>. A C<< has_base_package >> predicate will tell you if this has been set.
25              
26             =cut
27              
28             has base_package => (
29             is => 'rw',
30             isa => 'ArrayRef[Str] | Str',
31             predicate => 'has_base_package',
32             );
33              
34             =head2 test_packages
35              
36             This is set by the C<< -test_packages >> option sent to C<< use_test_packages >>. A C<< has_test_packages >> predicate will tell you if this has been set.
37              
38             =cut
39              
40             has test_packages => (
41             is => 'rw',
42             isa => 'ArrayRef[Str]',
43             predicate => 'has_test_packages',
44             );
45              
46             =head2 test_path
47              
48             This is set by teh C<< -test_path >> option sent to C<< use_test_packages >>.
49              
50             =cut
51              
52             has test_path => (
53             is => 'rw',
54             isa => 'ArrayRef[Str] | Str | Undef ',
55             default => sub { 't/lib' },
56             );
57              
58             =head1 METHODS
59              
60             =head2 test_classes
61              
62             This returns all the packages that will be loaded for testing. This does not filter classes out that have C<< $NOT_A_TEST >> set.
63              
64             This will search for test classes if L</base_package> has been set or it return the contents of L</test_packages>.
65              
66             =cut
67              
68             sub test_classes {
69 3     3 1 2361 my $meta = shift;
70              
71             # Use Module::Pluggable to find the test classes
72 3 50       134 if ($meta->has_base_package) {
    0          
73 3         100 my $base_package = $meta->base_package;
74 3 50       17 $meta->search_path(
75             new => (ref $base_package ? @$base_package : $base_package)
76             );
77 3         42 return $meta->test_modules;
78             }
79              
80             # Use the exact list given
81             elsif ($meta->has_test_packages) {
82 0         0 return @{ $meta->test_packages };
  0         0  
83             }
84              
85             # Probably shouldn't happen...
86 0         0 return ();
87             }
88              
89             =head2 build_test_objects
90              
91             This method returns all the test objects that should be run by this runner. It works by doing the following:
92              
93             =over
94              
95             =item 1
96              
97             It retrieves a list of potential test classes using L</test_classes>.
98              
99             =item 2
100              
101             It checks each package and throws away those with a package global variable named C<< $NOT_A_TEST >> that has been set to a true value.
102              
103             =item 3
104              
105             It instantiates the test classes and returns an arrayref of those test objects.
106              
107             =back
108              
109             =cut
110              
111             sub build_test_objects {
112 2     2 1 3 my $meta = shift;
113              
114             # Insert our test paths into the front of the @INC search path
115 2 50       73 if (defined $meta->test_path) {
116 2         69 my $test_path = $meta->test_path;
117 2 50       11 unshift @INC, (ref $test_path ? @$test_path : $test_path);
118             }
119              
120             # Load all the test objects
121 2         3 my @test_objects;
122 2         23 PACKAGE: for my $test_class ($meta->name, $meta->test_classes) {
123              
124             # Attempt to load the classes
125 2 50       547 unless (Class::Load::load_class($test_class)) {
126 0 0       0 warn $@ if $@;
127 0         0 warn "FAILED TO LOAD $test_class. Skipping.";
128 0         0 next PACKAGE;
129             }
130              
131             # Only Test::Able::Objects are tests we want
132 2 50       88 next PACKAGE unless $test_class->isa('Test::Able::Object');
133              
134             # Make sure this test has not been excluded
135             {
136 2     2   13 no strict 'refs';
  2         13  
  2         224  
  2         3  
137 2 50       2 next PACKAGE if ${$test_class."::NOT_A_TEST"};
  2         18  
138             }
139              
140             # Instantiate and add the test to the list
141 2         7 push @test_objects, $test_class->new;
142             }
143              
144             # Return the tests
145 2         1298 return \@test_objects;
146             }
147              
148             =head2 setup_test_objects
149              
150             Calls L</build_test_objects> and sets the C<test_objects> accessor from L<Test::Able::Role::Meta::Class>.
151              
152             =cut
153              
154             sub setup_test_objects {
155 2     2 1 29 my $meta = shift;
156 2         8 $meta->test_objects($meta->build_test_objects);
157             };
158              
159             # Use Module::Pluggable to find tests for us
160 2     2   991 use Module::Pluggable sub_name => 'test_modules';
  2         13471  
  2         13  
161             __PACKAGE__->meta->add_method(search_path => \&search_path);
162             __PACKAGE__->meta->add_method(test_modules => \&test_modules);
163              
164             =head1 AUTHOR
165              
166             Andrew Sterling Hanenkamp C<< <hanenkamp@cpan.org> >>
167              
168             =head1 COPYRIGHT AND LICENSE
169              
170             Copyright 2010 Qubling Software LLC.
171              
172             This library is free software. You can redistribute it and/or modify
173             it under the same terms as Perl itself.
174              
175             =cut
176              
177             1;