File Coverage

lib/Test/Assertions/TestScript.pm
Criterion Covered Total %
statement 33 36 91.6
branch 5 10 50.0
condition 2 5 40.0
subroutine 8 8 100.0
pod n/a
total 48 59 81.3


line stmt bran cond sub pod time code
1             package Test::Assertions::TestScript;
2              
3 1     1   2754 use strict;
  1         1  
  1         31  
4 1     1   1362 use Getopt::Long qw(:config pass_through bundling);
  1         75780  
  1         9  
5 1     1   2926 use Log::Trace;
  1         8860  
  1         10  
6 1     1   1938 use Test::Assertions qw( test );;
  1         15  
  1         7  
7 1     1   6 use File::Basename;
  1         2  
  1         139  
8 1     1   6 use vars qw( $VERSION $SAVE_OUTPUT $TRACE $TRACE_DEEP @TRACE_MODULE );
  1         2  
  1         478  
9              
10             $VERSION = sprintf"%d.%03d", q$Revision: 1.18 $ =~ /: (\d+)\.(\d+)/;
11              
12             sub import {
13 1     1   13 my $package = shift;
14 1         5 my %options = @_;
15            
16 1 50       4 if ($0) {
17 1         77 my $t_directory = dirname ( $0 );
18 1 50       23 chdir ( $t_directory ) or die "Could not chdir to unit test directory '$t_directory'\n";
19             } else { # this should never happen
20 0         0 die "Could not find script location\n";
21             }
22 1         3 unshift @INC, "./lib", "../lib";
23            
24 1   50     5 my $additional_options = $options{options} || {};
25 1         9 GetOptions(
26             't' => \$TRACE,
27             'T' => \$TRACE_DEEP,
28             's' => \$SAVE_OUTPUT,
29             'trace-module=s@' => \@TRACE_MODULE,
30             %$additional_options
31             );
32            
33 1         354 plan tests => $options{tests};
34            
35             {
36 1         3 package main; # Cheating to import into the right place
37 1         7 import Test::Assertions qw( test );
38             }
39            
40             }
41              
42             INIT {
43             package main; # Cheating to import into the right place
44              
45 1 50 33 1   21 import Log::Trace unless ($Test::Assertions::TestScript::TRACE || $Test::Assertions::TestScript::TRACE_DEEP); # still import the stubs
46 1 50       512 import Log::Trace 'print' => { Deep => 0 } if $Test::Assertions::TestScript::TRACE;
47 1 50       4 import Log::Trace 'print' => { Deep => 1 } if $Test::Assertions::TestScript::TRACE_DEEP;
48 1         6 foreach (@Test::Assertions::TestScript::TRACE_MODULE) {
49 0         0 eval "require $_";
50 0         0 import Log::Trace print => {Match => $_, Deep => 1};
51             }
52             }
53              
54             1;
55              
56             =head1 NAME
57              
58             Test::Assertions::TestScript - Base for test scripts
59              
60             =head1 SYNOPSIS
61              
62             use Test::Assertions::TestScript;
63             use Module::To::Test qw( frobnicate );
64            
65             ASSERT(frobnicate(),"Frobnicate returns true");
66              
67             =head1 DESCRIPTION
68              
69             Test::Assertions::TestScript provides a base for writing test scripts. It performs some
70             common actions such as setting up the @INC path and parsing command-line options, specifically:
71              
72             =over
73              
74             =item *
75              
76             The lib and t/lib directories are added to @INC.
77              
78             =item *
79              
80             The current directory is changed to the directory the script is in.
81              
82             =item *
83              
84             Test script command-line options are parsed. (See L)
85              
86             =item *
87              
88             The test set of functions from Test::Assertions are imported into your test
89             script.
90              
91             =back
92              
93             Test::Assertions::TestScript makes certain assumptions about the filesystem layout of
94             your project:
95              
96             =over 4
97              
98             =item *
99              
100             Modules that you are testing are in the lib directory of your project.
101              
102             =item *
103              
104             Test scripts are in the t directory.
105              
106             =item *
107              
108             There may also be a t/lib directory for any modules written for the test process.
109              
110             =back
111              
112             Test::Assertions::TestScript should be Cd B any modules that you intend to test.
113              
114             =head1 OPTIONS
115              
116             Options can be supplied to the import function. These should be placed after
117             the C or C. For example
118              
119             use Test::Assertions::TestScript( tests => 10, options => { 'b', \$opt_b })
120              
121             The following options are defined:
122              
123             =over
124              
125             =item tests
126              
127             The number of tests to pass to C from Test::Assertions. For example to tell Test::Assertions::TestScript that the script contains 42 tests:
128              
129             use Test::Assertions::TestScript tests => 42;
130              
131             =item options
132              
133             A hashref of additional options to capture via Getopt::Long. The "options" import parameter is passed
134             verbatim to GetOptions, so something along the following lines is required in order to capture the "-b" command line option:
135              
136             use Test::Assertions::TestScript( options => { 'b' => \$opt_b } );
137              
138             =back
139              
140             =head1 COMMAND-LINE OPTIONS
141              
142             A script based on Test::Assertions::TestScript will detect the following
143             command line options.
144              
145             =over
146              
147             =item -t
148              
149             Shallow tracing. Traces are Ced and AutoImport is turned on.
150              
151             =item -T
152              
153             Deep tracing. Traces are Ced and AutoImport is turned on.
154              
155             =item --trace-module=MODULE
156              
157             Imports tracing into MODULE specifically. Can be specified multiple times.
158              
159             =item -s
160              
161             Save generated output. You will need to write the actual code to do this in
162             your testscript, but you can inspect $Test::Assertions::TestScript::SAVE_OUTPUT
163             to see whether this argument was given.
164              
165             =back
166              
167             Be aware that all other command line options will be disregarded unless the
168             C import parameter is used to capture them.
169              
170             =head1 VERSION
171              
172             $Revision: 1.18 $
173              
174             =head1 AUTHOR
175              
176             Colin Robertson
177              
178             =head1 COPYRIGHT
179              
180             (c) BBC 2005-6. This program is free software; you can redistribute it and/or modify it under the GNU GPL.
181              
182             See the file COPYING in this distribution, or http://www.gnu.org/licenses/gpl.txt
183              
184             =cut