File Coverage

blib/lib/Test/Unit/Loader.pm
Criterion Covered Total %
statement 70 102 68.6
branch 14 36 38.8
condition 3 9 33.3
subroutine 16 21 76.1
pod 0 12 0.0
total 103 180 57.2


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