]> git.vpit.fr Git - perl/modules/Test-Leaner.git/blobdiff - lib/Test/Leaner.pm
Turn autoflush on as soon as the module is loaded
[perl/modules/Test-Leaner.git] / lib / Test / Leaner.pm
index d88ecf6779afe7397af9342eff79ebd8aa0d0562..1efe7a295183bf1f285b059b82cc57fe635c9b24 100644 (file)
@@ -28,6 +28,12 @@ BEGIN {
 my $TAP_STREAM  = *STDOUT;
 my $DIAG_STREAM = *STDERR;
 
+for ($TAP_STREAM, $DIAG_STREAM) {
+ my $fh = select $_;
+ $|++;
+ select $fh;
+}
+
 my ($plan, $test, $failed, $no_diag, $done_testing);
 
 sub NO_PLAN  () { -1 }
@@ -35,7 +41,7 @@ sub SKIP_ALL () { -2 }
 
 BEGIN {
  if (THREADSAFE) {
-  threads::shared::share($_) for $plan, $test, $failed, $no_diag;
+  threads::shared::share($_) for $plan, $test, $failed, $no_diag, $done_testing;
  }
 
  lock $plan if THREADSAFE;
@@ -96,12 +102,6 @@ sub plan {
   croak("plan() doesn't understand @args");
  }
 
- {
-  my $fh = select $TAP_STREAM;
-  $|++;
-  select $fh;
- }
-
  if (defined $plan_str) {
   local $\;
   print $TAP_STREAM "$plan_str\n";
@@ -143,6 +143,7 @@ sub import {
     push @imports, @{ $_[$i+1] };
     $splice  = 2;
    } elsif ($item eq 'no_diag') {
+    lock $plan if THREADSAFE;
     $no_diag = 1;
     $splice  = 1;
    }
@@ -183,6 +184,8 @@ sub skip {
  }
 
  for (1 .. $count) {
+  ++$test;
+
   my $skip_str = "ok $test # skip";
   if (defined $reason) {
    sanitize_comment($reason);
@@ -191,8 +194,6 @@ sub skip {
 
   local $\;
   print $TAP_STREAM "$skip_str\n";
-
-  $test++;
  }
 
  no warnings 'exiting';
@@ -259,51 +260,6 @@ sub fail (;$) {
  goto &ok;
 }
 
-my %binops;
-BEGIN {
- %binops = (
-  'or'  => 'or',
-  'and' => 'and',
-  'xor' => 'xor',
-
-  '||'  => 'hor',
-  '&&'  => 'hand',
-
-  'lt'  => 'lt',
-  'le'  => 'le',
-  'gt'  => 'gt',
-  'ge'  => 'ge',
-  'eq'  => 'eq',
-  'ne'  => 'ne',
-  'cmp' => 'cmp',
-
-  '<'   => 'nlt',
-  '<='  => 'nle',
-  '>'   => 'ngt',
-  '>='  => 'nge',
-  '=='  => 'neq',
-  '!='  => 'nne',
-  '<=>' => 'ncmp',
-
-  '=~'  => 'like',
-  '!~'  => 'unlike',
-  '~~'  => 'smartmatch',
- );
-
- for my $op (sort keys %binops) {
-  my $name = $binops{$op};
-  local $@;
-  eval <<"IS_BINOP";
-sub is_$name (\$\$;\$) {
- my (\$got, \$expected, \$desc) = \@_;
- \@_ = ((\$got $op \$expected), \$desc);
- goto &ok;
-}
-IS_BINOP
-  die $@ if $@;
- }
-}
-
 sub is ($$;$) {
  my ($got, $expected, $desc) = @_;
  no warnings 'uninitialized';
@@ -324,19 +280,73 @@ sub isnt ($$;$) {
  goto &ok;
 }
 
+my %binops = (
+ 'or'  => 'or',
+ 'and' => 'and',
+ 'xor' => 'xor',
+
+ '||'  => 'hor',
+ '&&'  => 'hand',
+
+ 'lt'  => 'lt',
+ 'le'  => 'le',
+ 'gt'  => 'gt',
+ 'ge'  => 'ge',
+ 'eq'  => 'eq',
+ 'ne'  => 'ne',
+ 'cmp' => 'cmp',
+
+ '<'   => 'nlt',
+ '<='  => 'nle',
+ '>'   => 'ngt',
+ '>='  => 'nge',
+ '=='  => 'neq',
+ '!='  => 'nne',
+ '<=>' => 'ncmp',
+
+ '=~'  => 'like',
+ '!~'  => 'unlike',
+ '~~'  => 'smartmatch',
+);
+
+my %binop_handlers;
+
+sub _create_binop_handler {
+ my ($op) = @_;
+ my $name = $binops{$op};
+ croak("Operator $op not supported") unless defined $name;
+ {
+  local $@;
+  eval <<"IS_BINOP";
+sub is_$name (\$\$;\$) {
+ my (\$got, \$expected, \$desc) = \@_;
+ \@_ = ((\$got $op \$expected), \$desc);
+ goto &ok;
+}
+IS_BINOP
+  die $@ if $@;
+ }
+ $binop_handlers{$op} = do {
+  no strict 'refs';
+  \&{__PACKAGE__."::is_$name"};
+ }
+}
+
 {
  no warnings 'once';
- *like   = \&is_like;
- *unlike = \&is_unlike;
+ *like   = _create_binop_handler('=~');
+ *unlike = _create_binop_handler('!~');
 }
 
 sub cmp_ok ($$$;$) {
  my ($got, $op, $expected, $desc) = @_;
- my $name = $binops{$op};
- croak("Operator $op not supported") unless defined $name;
+ my $handler = $binop_handlers{$op};
+ unless ($handler) {
+  local $Test::More::Level = ($Test::More::Level || 0) + 1;
+  $handler = _create_binop_handler($op);
+ }
  @_ = ($got, $expected, $desc);
- no strict 'refs';
- goto &{__PACKAGE__."::is_$name"};
+ goto $handler;
 }
 
 sub _diag_fh {