]> git.vpit.fr Git - perl/modules/Sub-Op.git/commitdiff
Test replacing an existing sub
authorVincent Pit <vince@profvince.com>
Sat, 2 Jan 2010 14:15:46 +0000 (15:15 +0100)
committerVincent Pit <vince@profvince.com>
Sat, 2 Jan 2010 14:15:46 +0000 (15:15 +0100)
MANIFEST
t/11-existing.t [new file with mode: 0644]

index 8f9a60426171a11e3f99b8cfb9ba656070ef6764..09fa5cef45e3a1ead9d3fbfca32ff9d8023871df 100644 (file)
--- a/MANIFEST
+++ b/MANIFEST
@@ -10,6 +10,7 @@ ptable.h
 sub_op.h
 samples/try.pl
 t/10-base.t
 sub_op.h
 samples/try.pl
 t/10-base.t
+t/11-existing.t
 t/Sub-Op-Test/Makefile.PL
 t/Sub-Op-Test/Test.xs
 t/Sub-Op-Test/lib/Sub/Op/Test.pm
 t/Sub-Op-Test/Makefile.PL
 t/Sub-Op-Test/Test.xs
 t/Sub-Op-Test/lib/Sub/Op/Test.pm
diff --git a/t/11-existing.t b/t/11-existing.t
new file mode 100644 (file)
index 0000000..fae36fc
--- /dev/null
@@ -0,0 +1,111 @@
+#!perl
+
+use strict;
+use warnings;
+
+use blib 't/Sub-Op-Test';
+
+use Test::More tests => (4 + 2 * 4) + (2 * 5);
+
+our $call_foo;
+sub foo { ok $call_foo, 'the preexistent foo was called' }
+
+our $called;
+
+{
+ local $/ = "####\n";
+ while (<DATA>) {
+  my ($code, $params)           = split /----\s*/, $_;
+  my ($names, $ret, $exp, $seq) = split /\s*#\s*/, $params;
+
+  my @names = split /\s*,\s*/, $names;
+
+  my @exp = eval $exp;
+  if ($@) {
+   fail "@names: unable to get expected values: $@";
+   next;
+  }
+  my $calls = @exp;
+
+  my @seq;
+  if ($seq) {
+   s/^\s*//, s/\s*$//  for $seq;
+   @seq = split /\s*,\s*/, $seq;
+   die "calls and seq length mismatch" unless @seq == $calls;
+  } else {
+   @seq = ($names[0]) x $calls;
+  }
+
+  my $test = "{\n";
+  for my $name (@names) {
+   $test .= <<"   INIT"
+    use Sub::Op::Test $name => sub {
+     ++\$called;
+     my \$exp = shift \@exp;
+     is_deeply \\\@_, \$exp,   '$name: arguments are correct';
+     my \$seq = shift \@seq;
+     is        \$seq, '$name', '$name: sequence is correct';
+     $ret;
+    };
+   INIT
+  }
+  $test .= "{\n$code\n}\n";
+  $test .= "}\n";
+
+  local $called = 0;
+  eval $test;
+  if ($@) {
+   fail "@names: unable to evaluate test case: $@";
+   diag $test;
+  }
+
+  is $called, $calls, "@names: the hook was called the right number of times";
+  if ($called < $calls) {
+   fail for $called + 1 .. $calls;
+  }
+ }
+}
+
+__DATA__
+foo();
+----
+foo # () # [ ]
+####
+foo;
+----
+foo # () # [ ]
+####
+foo(1);
+----
+foo # () # [ 1 ]
+####
+foo 2;
+----
+foo # () # [ 2 ]
+####
+local $call_foo = 1;
+&foo();
+----
+foo # () #
+####
+local $call_foo = 1;
+&foo;
+----
+foo # () #
+####
+local $call_foo = 1;
+&foo(3);
+----
+foo # () #
+####
+local $call_foo = 1;
+my $foo = \&foo;
+$foo->();
+----
+foo # () #
+####
+local $call_foo = 1;
+my $foo = \&foo;
+&$foo;
+----
+foo # () #