stas 01/12/23 07:29:43
Modified: perl-framework/Apache-Test/lib/Apache Test.pm
Log:
the implementation of skip_unless function, which provides a few different
ways to check whether the test should be skipped. To be run before plan().
Revision Changes Path
1.41 +44 -16 httpd-test/perl-framework/Apache-Test/lib/Apache/Test.pm
Index: Test.pm
===================================================================
RCS file: /home/cvs/httpd-test/perl-framework/Apache-Test/lib/Apache/Test.pm,v
retrieving revision 1.40
retrieving revision 1.41
diff -u -r1.40 -r1.41
--- Test.pm 2001/12/18 00:47:03 1.40
+++ Test.pm 2001/12/23 15:29:43 1.41
@@ -139,16 +139,32 @@
}
sub skip_unless {
- my $condition = shift;
- my $reason = shift || "no reason given";
-
- if (ref $condition eq 'CODE' and $condition->()) {
- return 1;
+ my $should_skip = 0;
+ for my $cond (@_) {
+ if (ref $cond eq 'HASH') {
+ while (my($code, $reason) = each %$cond) {
+ $reason = "no reason given" unless defined $reason;
+ if (ref $code eq 'CODE' and $code->()) {
+ next;
+ }
+ else {
+ push @SkipReasons, $reason;
+ $should_skip++;
+ }
+ }
+ }
+ else {
+ $should_skip++ unless have_module($cond);
+ }
}
- else {
- push @SkipReasons, $reason;
- return 0;
+
+ if ($should_skip) {
+ my $reason = join ', ',
+ @SkipReasons ? @SkipReasons : "no reason given";
+ print "1..0 # skipped: $reason\n";
+ exit; #XXX: Apache->exit
}
+ @SkipReasons = (); # reset
}
sub have_module {
@@ -321,9 +337,10 @@
plan tests => 5, 0;
But this won't hint the reason for skipping therefore it's better to
-use skip_unless():
+use skip_unless()
- plan tests => 5, skip_unless(sub { $a == $b }, "$a != $b");
+ skip_unless({sub { $a == $b } => "$a != $b"}, 'LWP');
+ plan tests => 5;
see skip_unless() for more info.
@@ -366,14 +383,25 @@
Same as I<Test::skip>, see I<Test.pm> documentation.
=item skip_unless
-
- skip_unless($cond_sub, $reason);
-skip_unless() is used with plan(), it executes C<$cond_sub> code
-reference and if it returns a false value C<$reason> gets printed as a
-reason for test skipping.
+ skip_unless({sub {$a==$b} => "$a != $b!"
+ sub {$a==1} => "$a != 1!"},
+ 'LWP',
+ 'cgi_d',
+ {sub {0} => "forced to be skipped"},
+ );
+
+skip_unless() can be called before plan(), to decide whether to skip the
+whole test or not. plan() won't be reached if skip_unless decides to skip
+the test.
+
+skip_unless's argument is a list of things to test. The list can include
+scalars, which are passed to have_module(), and hash references. The
+hash references have condition code ref as a key and the reason for
+failure as a value. The condition code is run and if it fails the
+reason is used to explain the failure.
-see plan().
+Also see plan().
=item test_pm_refresh