lib/Test/Assertions/TestScript.pm

filter: Statementstmt 92.1% 35 / 38 Branchbran 50.0% 5 / 10 Conditioncond 40.0% 2 / 5 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 100% 8 / 8 Podpod n/a totaltotal 78.1% 50 / 64 CC 9 SCAR 16.2
CC
9
Cov
79%
INIT
9.7
import
4.2
BEGIN
1.0
CRAP 5.0
« lib/Test/Assertions.pm ↑ Summary t/Test-Assertion_style.pl »
1 package Test::Assertions::TestScript;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use Getopt::Long qw(:config pass_through bundling);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use Log::Trace;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Test::Assertions qw( test );;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use vars qw( $VERSION $SAVE_OUTPUT $TRACE $TRACE_DEEP @TRACE_MODULE );
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 $VERSION = sprintf"%d.%03d", q$Revision: 1.18 $ =~ /: (\d+)\.(\d+)/;
11  
12 1 ▶sub import {
Subroutine import: called CC=4 SCAR=14.3
13 1 my $package = shift;
14 1 my %options = @_;
15
16 1 ▶ if ($0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($0) { } 1 0 1
17 1 my $t_directory = dirname ( $0 );
18 1 ▶ chdir ( $t_directory ) or die "Could not chdir to unit test directory '$t_directory'\n";
Branch: 1 branch
BranchTrueFalseTotal
chdir $t_directory or die "Could not chdir to unit test directory '${t_directory}'\n" 1 0 1
19 } else { # this should never happen
20 0 die "Could not find script location\n";
21 }
22 1 unshift @INC, "./lib", "../lib";
23
24 1 ▶ my $additional_options = $options{options} || {};
Condition50%1/2
$options{'options'} || {}
l!l
10
MC/DC0%0/1
$options{'options'} || {}
$options{'options'}
Truth table50%1/2
A
A= $options{'options'}
Aresult
11
00
25 1 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 plan tests => $options{tests};
34
35 {
36 1 package main; # Cheating to import into the right place
37 1 import Test::Assertions qw( test );
38 }
39
40 }
41  
42 1 ▶INIT {
Subroutine INIT: called CC=6 SCAR=22.7
43 package main; # Cheating to import into the right place
44  
45 1 ▶ import Log::Trace unless ($Test::Assertions::TestScript::TRACE || $Test::Assertions::TestScript::TRACE_DEEP); # still import the stubs
Branch: 1 branch
BranchTrueFalseTotal
unless $Test::Assertions::TestScript::TRACE or $Test::Assertions::TestScript::TRACE_DEEP 1 0 1
Condition33%1/3
$Test::Assertions::TestScript::TRACE or $Test::Assertions::TestScript::TRACE_DEEP
l!l&&r!l&&!r
001
MC/DC0%0/2
$Test::Assertions::TestScript::TRACE or $Test::Assertions::TestScript::TRACE_DEEP
$Test::Assertions::TestScript::TRACE $Test::Assertions::TestScript::TRACE_DEEP
Truth table33%1/3
A or B
A= $Test::Assertions::TestScript::TRACEB= $Test::Assertions::TestScript::TRACE_DEEP
ABresult
1X1
011
000
46 1 ▶ import Log::Trace 'print' => { Deep => 0 } if $Test::Assertions::TestScript::TRACE;
Branch: 1 branch
BranchTrueFalseTotal
if $Test::Assertions::TestScript::TRACE 0 1 1
47 1 ▶ import Log::Trace 'print' => { Deep => 1 } if $Test::Assertions::TestScript::TRACE_DEEP;
Branch: 1 branch
BranchTrueFalseTotal
if $Test::Assertions::TestScript::TRACE_DEEP 0 1 1
48 1 foreach (@Test::Assertions::TestScript::TRACE_MODULE) {
49 0 eval "require $_";
50 0 import Log::Trace print => {Match => $_, Deep => 1};
51 }
52 }
53  
54 2 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<COMMAND-LINE OPTIONS>)
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 C<use>d B<before> 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<use> or C<import>. 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<plan tests> 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 C<print>ed and AutoImport is turned on.
150  
151 =item -T
152  
153 Deep tracing. Traces are C<print>ed 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<options> import parameter is used to capture them.
169  
170 =head1 VERSION
171  
172 $Revision: 1.18 $
173  
174 =head1 AUTHOR
175  
176 Colin Robertson <cpan _at_ bbc _dot_ co _dot_ uk>
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
« lib/Test/Assertions.pm ↑ Summary t/Test-Assertion_style.pl »