testlib/PtsTestLib.pm

filter: Statementstmt 66.3% 65 / 98 Branchbran 50.0% 30 / 60 Conditioncond 55.5% 5 / 9 MC/DCmcdc 33.3% 2 / 6 Subroutinesub 71.4% 5 / 7 Podpod 0.0% 0 / 4 totaltotal 58.1% 107 / 184 CC 23 SCAR 27.4
CC
23
Cov
60%
test_file
22.8
test_mod
5.7
BEGIN
2.1
CRAP 15.5
« lib/MIME/AltWords.pm ↑ Summary blib/lib/MIME/AltWords0.pm »
1 #! /bin/sh
2 1 eval '(exit $?0)' && eval 'PERL_BADLANG=x;PATH="$PATH:.";export PERL_BADLANG\
3 1 PATH;exec perl -x -S -- "$0" ${1+"$@"};#'if 0;eval 'setenv PERL_BADLANG x\
4 ;setenv PATH "$PATH":.;exec perl -x -S -- "$0" $argv:q;#'.q
5 #!perl -w
6 +push@INC,'.';$0=~/(.*)/s;do(index($1,"/")<0?"./$1":$1);die$@if$@__END__+if 0
7 ;#Don't touch/remove lines 1--7: http://www.inf.bme.hu/~pts/Magic.Perl.Header
8 #
9 # pts-test-lib.t -- a simple Perl test framework based on Test::More + aspects of Test::Inline
10 # by pts@fazekas.hu at Mon Dec 26 17:27:27 CET 2005
11 #
12 # This program is free software; you can redistribute it and/or modify
13 # it under the terms of the GNU General Public License as published by
14 # the Free Software Foundation; either version 2 of the License, or
15 # (at your option) any later version.
16 #
17 # This program is distributed in the hope that it will be useful,
18 # but WITHOUT ANY WARRANTY; without even the implied warranty of
19 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 # GNU General Public License for more details.
21 #
22 # Dat: perl -MTest::Harness "-eruntests@ARGV" test1.t
23 # test1.t:
24 # use Test::More qw(no_plan);
25 # BEGIN { use_ok( 'integer' ); }
26 # is(1, 1.0, "foo");
27 # isnt(1, '1.0', "bar");
28 # Dat: --harness should display: "pts-test....ok\n"
29 # Imp: plan the # tests for Test::
30 # Imp: skip "=begin" inside strings or otherwise quoted
31 #
32  
33 package PtsTestLib;
34 1 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
35 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 1 ▶BEGIN {
Subroutine BEGIN: called CC=2 SCAR=7.2
37 1 $main::Id='$Id: pts-test.pl,v 1.2 2006/04/26 07:42:33 pts Exp $';
38 1 ▶ $main::VERSION=$1 if $main::Id=~/,v *([\d.]+)/;
Branch: 1 branch
BranchTrueFalseTotal
if $main::Id =~ /,v *([\d.]+)/ 1 0 1
39 }
40 1 my $SELF='pts-test.pl';
41  
42 #** @param $_[0] output filehandle
43 0 ▶sub banner($) {
Subroutine banner: not called CC=1 SCAR=6.9
Pod undocumented
44 0 my $fh=$_[0];
45 0 print $fh "This is $SELF, version $main::VERSION, by <pts\@fazekas.hu>\n"
46 ."The license is GNU GPL >=2.0. It comes without warranty. USE AT YOUR OWN RISK!\n\n";
47 }
48 #** @param $_[0] output filehandle
49 0 ▶sub usage($) {
Subroutine usage: not called CC=1 SCAR=6.9
Pod undocumented
50 0 my $fh=$_[0];
51 0 print $fh "Usage: $0 [<option> ...] [--] { <script.pl> | <script.t> } ...\n",
52 "Options:\n",
53 "--help this help\n",
54 "--harness run whole test through Test::Harness\n";
55 }
56  
57 1 my $do_help=0;
58 1 my $do_harness=0;
59 1 my $do_work=(defined $ENV{PTS_TEST_DO});
60 1 ▶@ARGV=split("\n",$ENV{PTS_TEST_DO}) if !@ARGV and $do_work;
Branch: 1 branch
BranchTrueFalseTotal
if not @ARGV and $do_work 0 1 1
Condition33%1/3
not @ARGV and $do_work
!ll&&!rl&&r
010
MC/DC0%0/2
not @ARGV and $do_work
not @ARGV $do_work
Truth table33%1/3
not(A) and B
A= not @ARGVB= $do_work
ABresult
0X0
100
111
61  
62 1 { my $I;
63 1 ▶ for ($I=0;$I<@ARGV;$I++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($I < @ARGV) 0 1 1
64 0 ▶ if ($ARGV[$I]eq'--') { $I++; last }
Branch: 5 branches
BranchTrueFalseTotal
if ($ARGV[$I] eq '--') { } 0 0 0
elsif (substr($ARGV[$I], 0, 1) ne '-') { } 0 0 0
elsif ($ARGV[$I] eq '--help') { } 0 0 0
elsif ($ARGV[$I] eq '--harness') { } 0 0 0
elsif ($ARGV[$I] eq '--work') { } 0 0 0
65 0 elsif (substr($ARGV[$I],0,1)ne'-') { last }
66 0 elsif ($ARGV[$I]eq'--help') { $do_help++ }
67 0 elsif ($ARGV[$I]eq'--harness') { $do_harness++ }
68 0 elsif ($ARGV[$I]eq'--work') { $do_work++ }
69 0 else { die "$SELF: unknown option: $ARGV[$I]\n" }
70 }
71 1 splice(@ARGV,0,$I);
72 }
73  
74 1 ▶if ($do_help) { #
Branch: 1 branch
BranchTrueFalseTotal
if ($do_help) 0 1 1
75 0 banner(\*STDOUT); usage(\*STDOUT); exit 0;
76 }
77  
78 #my $perl_exe=$^X;
79 #$perl_exe='perl' if $^X!~m@perl[^/]*\Z(?!\n)@;
80 #die "$SELF: perl executable not found: $perl_exe\n" if (!-f $perl_exe);
81 ## ^^^ Imp: test `-e' on UNIX
82  
83 1 ▶if ($do_harness) {
Branch: 1 branch
BranchTrueFalseTotal
if ($do_harness) 0 1 1
84 0 $ENV{PTS_TEST_DO}=join("\n",@ARGV); # Imp: quoting
85 0 require Test::Harness;
86 0 import Test::Harness;
87 0 $Test::Harness::switches=""; # Dat: remove "-w", we have /bin/sh in our shebang
88 0 runtests($0);
89 0 exit 0;
90 }
91  
92 # Imp: maybe in separate processes?
93 # vvv Dat: `use' here, would add a mandatory dependency on `Test::More', but
94 # it also prints "# No tests run!" with --help.
95 1 require Test::More;
96 1 import Test::More qw(no_plan);
97  
98 1 $|=1;
99 1 my $errc=0;
100  
101 #** @param $_[0] filename, usually a Perl script or a .pm Perl module
102 1 ▶sub test_file($) {
Subroutine test_file: called CC=18 SCAR=31.3
Pod undocumented
103 1 my $testfile=$_[0];
104 1 ▶ if ($testfile=~/[\r\n"\\]/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($testfile =~ /[\r\n"\\]/) { } 0 1 1
elsif (not open(F, "< $testfile")) { } 0 1 1
105 0 print STDERR "$SELF: special character in test filename: $testfile\n";
106 0 $errc++; next
107 0 } elsif (!open F, "< $testfile") {
108 0 print STDERR "$SELF: test file missing: $testfile: $!\n";
109 0 $errc++; next
110 0 }
111 1 print "# testing file: $testfile\n"; # Dat: ignored by Test::Harness
112 #** Previous line was whitespace only.
113 1 my $prevws_p=1;
114 #** 0: we are outside a testing block
115 #** 1: we are in a `=begin testing' block
116 #** 2: we are in a `=for testing' block
117 1 my $in_testing=0;
118 1 my $testcode="";
119 1 ▶ while (<F>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline F)) 1003 1 1004
120 1003 ▶ if (!/\S/) {
Branch: 1 branch
BranchTrueFalseTotal
unless (/\S/) 151 852 1003
121 151 ▶ $testcode.=$_ if $in_testing!=0;
Branch: 1 branch
BranchTrueFalseTotal
if $in_testing != 0 24 127 151
122 151 ▶ $in_testing=0 if $in_testing==2; # Dat: paragraph of `=for' has ended
Branch: 1 branch
BranchTrueFalseTotal
if $in_testing == 2 0 151 151
123 151 $prevws_p=1; next
124 151 }
125 852 $prevws_p=0;
126 852 ▶ if (/^=(begin|for)\s+testing\s*$/) {
Branch: 3 branches
BranchTrueFalseTotal
if (/^=(begin|for)\s+testing\s*$/) { } 1 851 852
elsif ($in_testing == 1 and /^=(?:end(?:\s+testing)?|cut)\s*$/) { } 1 850 851
elsif ($in_testing != 0) { } 170 680 850
127 1 my $lineno=$.+1;
128 1 ▶ print STDERR "$SELF: nested testing declaration in $testfile:$.\n" if $in_testing!=0;
Branch: 1 branch
BranchTrueFalseTotal
if $in_testing != 0 0 1 1
129 1 $testcode.="\n\n#line $lineno \"$testfile\"\n";
130 1 ▶ $in_testing=($1 eq "begin") ? 1 : 2;
Branch: 1 branch
BranchTrueFalseTotal
$1 eq 'begin' ? : 1 0 1
131 ▶ } elsif ($in_testing==1 and /^=(?:end(?:\s+testing)?|cut)\s*$/) {
Condition100%3/3
$in_testing == 1 and /^=(?:end(?:\s+testing)?|cut)\s*$/
!ll&&!rl&&r
6801701
MC/DC100%2/2
$in_testing == 1 and /^=(?:end(?:\s+testing)?|cut)\s*$/
$in_testing == 1 /^=(?:end(?:\s+testing)?|cut)\s*$/
Truth table100%3/3
A and B
A= $in_testing == 1B= /^=(?:end(?:\s+testing)?|cut)\s*$/
ABresult
0X0
100
111
132 # Dat: `=end testing' is OK, `=end' isn't, but we're not that picky
133 1 $in_testing=0;
134 } elsif ($in_testing!=0) {
135 170 $testcode.=$_
136 }
137 }
138 1 ▶ die if !close F;
Branch: 1 branch
BranchTrueFalseTotal
unless close F 0 1 1
139 1 ▶ $testfile="./$testfile" if 0>index($testfile,"/"); # Dat: for better do()
Branch: 1 branch
BranchTrueFalseTotal
if index($testfile, "/") == -1 0 1 1
140 1 ▶ if (0==length($testcode)) {
Branch: 1 branch
BranchTrueFalseTotal
if (0 == length $testcode) { } 0 1 1
141 # Dat: no inline POD tests found
142 0 do $testfile;
143 } else {
144 1 do $testfile; # Dat: parse definitions etc.
145 1 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
146 1 eval $testcode;
147 }
148 1 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1 1
149 }
150  
151 # ---
152  
153 #** @param $_[1] Perl package/module name (e.g. MIME::Words)
154 1 ▶sub test_mod($) {
Subroutine test_mod: called CC=5 SCAR=17.4
Pod undocumented
155 1 my $mod=$_[0];
156 1 my $modfn=$mod; $modfn=~s@::@/@g; $modfn.=".pm";
157 1 my $dirmodfn;
158 1 for my $dir (@INC) {
159 1 ▶ if (!ref($dir) and (-f"$dir/$modfn")) { $dirmodfn="$dir/$modfn"; last }
Branch: 1 branch
BranchTrueFalseTotal
if (not ref $dir and -f "$dir/$modfn") 1 0 1
Condition33%1/3
not ref $dir and -f "$dir/$modfn"
!ll&&!rl&&r
001
MC/DC0%0/2
not ref $dir and -f "$dir/$modfn"
not ref $dir -f "$dir/$modfn"
Truth table33%1/3
not(A) and B
A= not ref $dirB= -f "$dir/$modfn"
ABresult
0X0
100
111
160 }
161 1 ▶ if (defined$dirmodfn) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $dirmodfn) { } 1 0 1
162 1 test_file($dirmodfn);
163 } else {
164 0 print STDERR "$0: file to test not found: $modfn\n";
165 0 $errc++;
166 }
167 }
168  
169 1 ▶if ($0 eq __FILE__) {
Branch: 1 branch
BranchTrueFalseTotal
if ($0 eq __FILE__) 0 1 1
170 0 for my $fn (@ARGV) { test_file $fn }
171 0 ▶ exit($errc>99 ? 100 : $errc);
Branch: 1 branch
BranchTrueFalseTotal
$errc > 99 ? : 0 0 0
172 }
173  
174 1 1
« lib/MIME/AltWords.pm ↑ Summary blib/lib/MIME/AltWords0.pm »