blib/lib/Test/Unit/Loader.pm

filter: Statementstmt 84.6% 11 / 13 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 5 / 5 Podpod n/a totaltotal 88.8% 16 / 18 CC 1 SCAR 0.6
CC
1
Cov
85%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.1
« examples/patch100132-2 ↑ Summary blib/lib/Test/Unit/TestSuite.pm »
1 package Test::Unit::Loader;
2 BEGIN {
3 3 ▶ $Test::Unit::Loader::VERSION = '0.25_1325'; # added by dist-tools/SetVersion.pl
Subroutine BEGIN: called CC=1 SCAR=0.0
4 }
5  
6 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 3 ▶use FileHandle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Test::Unit::Debug qw(debug);
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Test::Unit::TestSuite;
Subroutine BEGIN: called CC=1 SCAR=2.6
11 use Test::Unit::TestCase;
12 use Test::Unit::UnitHarness;
13 use Test::Unit::Warning;
14  
15 # should really do something in here about a local @INC.
16 sub obj_load { shift; load(@_) }
17  
18 # Compiles a target. Returns the package if successful.
19 sub compile {
20 my $target = shift;
21 debug("Test::Unit::Loader::compile($target) called\n");
22  
23 if ($target =~ /^\w+(::\w+)*$/) {
24 compile_class($target);
25 return $target;
26 }
27 elsif ($target =~ /\.pm$/) {
28 compile_file($target);
29 # In this case I need to figure out what the class was I just loaded!
30 return get_package_name_from_file($target);
31 }
32 else {
33 return undef;
34 }
35 }
36  
37 sub compile_class {
38 my $classname = shift;
39 debug(" Test::Unit::Loader::compile_class($classname) called\n");
40 # Check if the package exists already.
41 {
42 no strict 'refs';
43 if (my @keys = grep { ! /::$/ } keys %{"$classname\::"}) {
44 debug(" package $classname already exists (@keys); not compiling.\n");
45 return;
46 }
47 }
48 # No? Try 'require'ing it
49 eval "require $classname";
50 die $@ if $@;
51 debug(" $classname compiled OK as class name\n");
52 }
53  
54 sub compile_file {
55 my $file = shift;
56 debug(" Test::Unit::Loader::compile_file($file) called\n");
57 eval qq{require "$file"};
58 die $@ if $@;
59 debug(" $file compiled OK as filename\n");
60 }
61  
62 sub load {
63 my $target = shift;
64 debug("Test::Unit::Loader::load($target) called\n");
65  
66 my $suite = load_test($target)
67 || load_test_harness_test($target)
68 || load_test_dir($target);
69 return $suite if $suite;
70  
71 die "Couldn't load $target in any of the supported ways";
72 }
73  
74 sub load_test {
75 my $target = shift;
76 debug("Test::Unit::Loader::load_test($target) called\n");
77 my $package = compile($target);
78 return unless $package;
79 debug(" compile returned $package\n");
80 my $suite = load_test_suite($package) || load_test_case($package);
81 die "`$target' was not a valid Test::Unit::Test\n" unless $suite;
82 return $suite;
83 }
84  
85 sub load_test_suite {
86 my $package = shift;
87 debug(" Test::Unit::Loader::load_test_suite($package) called\n");
88 if ($package->can("suite")) {
89 debug(" $package has a suite() method\n");
90 return $package->suite();
91 }
92 }
93  
94 sub load_test_case {
95 my $package = shift;
96 debug(" Test::Unit::Loader::load_test_case($package) called\n");
97 if ($package->isa("Test::Unit::TestCase")) {
98 debug(" $package isa Test::Unit::TestCase\n");
99 return Test::Unit::TestSuite->new($package);
100 }
101 }
102  
103 sub extract_testcases {
104 my $classname = shift;
105  
106 my @testcases = ();
107  
108 foreach my $method ($classname->list_tests()) {
109 if ( my $a_class_instance = $classname->new($method) ) {
110 push @testcases, $a_class_instance;
111 }
112 else {
113 push @testcases, Test::Unit::Warning->new(
114 "extract_testcases: Couldn't create a $classname object"
115 );
116 }
117 }
118  
119 push @testcases, Test::Unit::Warning->new("No tests found in $classname")
120 unless @testcases;
121  
122 return @testcases;
123 }
124  
125 sub load_test_harness_test {
126 my $target = shift;
127  
128 foreach my $file ("$target", "$target.t", "t/$target", "t/$target.t" ) {
129 if (-r $file) {
130 # are the next 3 lines really necessary?
131 open(FH, $file) or next;
132 my $first = <FH>;
133 close(FH) or next;
134 return Test::Unit::UnitHarness->new($file);
135 }
136 }
137 return undef;
138 }
139  
140 sub load_test_dir {
141 my $test_dir = shift;
142 if (-d $test_dir) {
143 die "This is a test directory. I haven't implemented that.\n";
144 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 my $filename = shift;
156 my $real_path = $INC{$filename};
157 die "Can't find $filename in @INC: $!"
158 unless $real_path && open(FH, $real_path);
159 while (<FH>) {
160 if (/^\s*package\s+([\w:]+)/) {
161 close(FH);
162 return $1;
163 }
164 }
165 die "Can't find a package in $filename";
166 }
167  
168 1;
169 __END__
« examples/patch100132-2 ↑ Summary blib/lib/Test/Unit/TestSuite.pm »