On Tue, 4 Dec 2001, Doug MacEachern wrote:

> On Thu, 22 Nov 2001, Stas Bekman wrote:
>
> > I can extend it to engulf the plan() extension that we have added and then
> > the only function you will ever call with plan() is skip_unless. I
> > think this:
> >
> >   plan ..., skip_unless('cgi', 'lwp');
>
> there's no point in overloading plan anymore then.  we should just change
> it to:
>
> skip_unless(...);
>
> plan tests => $tests;
>
> and have skip_unless() print "1..0\n..."; exit; if conditions are not met.
>
> personally, i would rather keep the the current plan shorthand and change
> skip_unless to be called as it is above.

OK, here it is: I've finally called it skip_all() as it's a standalone
function now.

Index: Apache-Test/lib/Apache/Test.pm
===================================================================
RCS file: /home/cvs/httpd-test/perl-framework/Apache-Test/lib/Apache/Test.pm,v
retrieving revision 1.40
diff -u -r1.40 Test.pm
--- Apache-Test/lib/Apache/Test.pm      2001/12/18 00:47:03     1.40
+++ Apache-Test/lib/Apache/Test.pm      2001/12/21 09:34:43
@@ -11,7 +11,7 @@
 use vars qw(@ISA @EXPORT $VERSION %SubTests @SkipReasons);

 @ISA = qw(Exporter);
[EMAIL PROTECTED] = qw(ok skip sok plan skip_unless have_lwp have_http11
[EMAIL PROTECTED] = qw(ok skip sok plan skip_all have_lwp have_http11
              have_cgi have_module have_apache have_perl);
 $VERSION = '0.01';

@@ -138,17 +138,33 @@
     Test::plan(@_);
 }

-sub skip_unless {
-    my $condition = shift;
-    my $reason = shift || "no reason given";
-
-    if (ref $condition eq 'CODE' and $condition->()) {
-        return 1;
+sub skip_all {
+    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,11 +337,12 @@
   plan tests => 5, 0;

 But this won't hint the reason for skipping therefore it's better to
-use skip_unless():
+use skip_all()

-  plan tests => 5, skip_unless(sub { $a == $b }, "$a != $b");
+  skip_all({sub { $a == $b } => "$a != $b"}, 'LWP');
+  plan tests => 5;

-see skip_unless() for more info.
+see skip_all() for more info.

 =item * an C<ARRAY> reference

@@ -364,16 +381,27 @@
 =item skip

 Same as I<Test::skip>, see I<Test.pm> documentation.
-
-=item skip_unless

-  skip_unless($cond_sub, $reason);
+=item skip_all

-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_all({sub {$a==$b} => "$a != $b!"
+               sub {$a==1}  => "$a != 1!"},
+              'LWP',
+              'cgi_d',
+               {sub {0} => "forced to be skipped"},
+             );
+
+skip_all() can be called before plan(), to decide whether to skip the
+whole test or not. plan() won't be reached if skip_all decides to skip
+the test.
+
+skip_all'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


_____________________________________________________________________
Stas Bekman             JAm_pH      --   Just Another mod_perl Hacker
http://stason.org/      mod_perl Guide   http://perl.apache.org/guide
mailto:[EMAIL PROTECTED]  http://ticketmaster.com http://apacheweek.com
http://singlesheaven.com http://perl.apache.org http://perlmonth.com/

Reply via email to