File Coverage

blib/lib/Test/Unit/Loader.pm
Criterion Covered Total %
statement 69 101 68.3
branch 14 36 38.8
condition 3 9 33.3
subroutine 15 20 75.0
pod 0 12 0.0
total 101 178 56.7


line stmt bran cond sub pod time code
1             package Test::Unit::Loader;
2              
3 2     2   12 use strict;
  2         3  
  2         149  
4              
5 2     2   2394 use FileHandle;
  2         30682  
  2         11  
6 2     2   847 use Test::Unit::Debug qw(debug);
  2         5  
  2         85  
7 2     2   1942 use Test::Unit::TestSuite;
  2         11  
  2         100  
8 2     2   15 use Test::Unit::TestCase;
  2         3  
  2         41  
9 2     2   1822 use Test::Unit::UnitHarness;
  2         5  
  2         204  
10 2     2   12 use Test::Unit::Warning;
  2         2  
  2         512  
11              
12             # should really do something in here about a local @INC.
13 0     0 0 0 sub obj_load { shift; load(@_) }
  0         0  
14              
15             # Compiles a target. Returns the package if successful.
16             sub compile {
17 16     16 0 45 my $target = shift;
18 16         72 debug("Test::Unit::Loader::compile($target) called\n");
19              
20 16 50       135 if ($target =~ /^\w+(::\w+)*$/) {
    0          
21 16         49 compile_class($target);
22 14         46 return $target;
23             }
24             elsif ($target =~ /\.pm$/) {
25 0         0 compile_file($target);
26             # In this case I need to figure out what the class was I just loaded!
27 0         0 return get_package_name_from_file($target);
28             }
29             else {
30 0         0 return undef;
31             }
32             }
33              
34             sub compile_class {
35 38     38 0 79 my $classname = shift;
36 38         158 debug(" Test::Unit::Loader::compile_class($classname) called\n");
37             # Check if the package exists already.
38             {
39 2     2   21 no strict 'refs';
  2         4  
  2         2269  
  38         47  
40 38 100       56 if (my @keys = grep { ! /::$/ } keys %{"$classname\::"}) {
  344         644  
  38         319  
41 21         155 debug(" package $classname already exists (@keys); not compiling.\n");
42 21         71 return;
43             }
44             }
45             # No? Try 'require'ing it
46 17         1221 eval "require $classname";
47 17 100       35377 die $@ if $@;
48 15         99 debug(" $classname compiled OK as class name\n");
49             }
50              
51             sub compile_file {
52 0     0 0 0 my $file = shift;
53 0         0 debug(" Test::Unit::Loader::compile_file($file) called\n");
54 0         0 eval qq{require "$file"};
55 0 0       0 die $@ if $@;
56 0         0 debug(" $file compiled OK as filename\n");
57             }
58              
59             sub load {
60 9     9 0 18 my $target = shift;
61 9         50 debug("Test::Unit::Loader::load($target) called\n");
62              
63 9   33     30 my $suite = load_test($target)
64             || load_test_harness_test($target)
65             || load_test_dir($target);
66 7 50       50 return $suite if $suite;
67              
68 0         0 die "Couldn't load $target in any of the supported ways";
69             }
70              
71             sub load_test {
72 16     16 0 25 my $target = shift;
73 16         68 debug("Test::Unit::Loader::load_test($target) called\n");
74 16         52 my $package = compile($target);
75 14 50       51 return unless $package;
76 14         67 debug(" compile returned $package\n");
77 14   66     52 my $suite = load_test_suite($package) || load_test_case($package);
78 14 50       60 die "`$target' was not a valid Test::Unit::Test\n" unless $suite;
79 14         66 return $suite;
80             }
81              
82             sub load_test_suite {
83 14     14 0 26 my $package = shift;
84 14         64 debug(" Test::Unit::Loader::load_test_suite($package) called\n");
85 14 100       256 if ($package->can("suite")) {
86 3         19 debug(" $package has a suite() method\n");
87 3         15 return $package->suite();
88             }
89             }
90              
91             sub load_test_case {
92 11     11 0 22 my $package = shift;
93 11         47 debug(" Test::Unit::Loader::load_test_case($package) called\n");
94 11 50       98 if ($package->isa("Test::Unit::TestCase")) {
95 11         41 debug(" $package isa Test::Unit::TestCase\n");
96 11         71 return Test::Unit::TestSuite->new($package);
97             }
98             }
99              
100             sub extract_testcases {
101 21     21 0 39 my $classname = shift;
102              
103 21         43 my @testcases = ();
104              
105 21         172 foreach my $method ($classname->list_tests()) {
106 67 50       266 if ( my $a_class_instance = $classname->new($method) ) {
107 67         177 push @testcases, $a_class_instance;
108             }
109             else {
110 0         0 push @testcases, Test::Unit::Warning->new(
111             "extract_testcases: Couldn't create a $classname object"
112             );
113             }
114             }
115              
116 21 100       88 push @testcases, Test::Unit::Warning->new("No tests found in $classname")
117             unless @testcases;
118              
119 21         89 return @testcases;
120             }
121              
122             sub load_test_harness_test {
123 0     0 0   my $target = shift;
124              
125 0           foreach my $file ("$target", "$target.t", "t/$target", "t/$target.t" ) {
126 0 0         if (-r $file) {
127             # are the next 3 lines really necessary?
128 0 0         open(FH, $file) or next;
129 0           my $first = ;
130 0 0         close(FH) or next;
131 0           return Test::Unit::UnitHarness->new($file);
132             }
133             }
134 0           return undef;
135             }
136              
137             sub load_test_dir {
138 0     0 0   my $test_dir = shift;
139 0 0         if (-d $test_dir) {
140 0           die "This is a test directory. I haven't implemented that.\n";
141 0           return Test::Unit::UnitHarness::new_dir($test_dir);
142             }
143             }
144              
145             # The next bit of code is a helper function which attempts
146             # to identify the class we are trying to use from a '.pm'
147             # file. If we've reached this point, we managed to 'require'
148             # the file already, but we dont know the file the package was
149             # loaded from. Somehow I feel this information is in perl
150             # somwhere but if it is I dont know where...
151             sub get_package_name_from_file {
152 0     0 0   my $filename = shift;
153 0           my $real_path = $INC{$filename};
154 0 0 0       die "Can't find $filename in @INC: $!"
155             unless $real_path && open(FH, $real_path);
156 0           while () {
157 0 0         if (/^\s*package\s+([\w:]+)/) {
158 0           close(FH);
159 0           return $1;
160             }
161             }
162 0           die "Can't find a package in $filename";
163             }
164              
165             1;
166             __END__