]> git.vpit.fr Git - perl/modules/Variable-Magic.git/blobdiff - t/25-copy.t
Add support for copy magic on code prototype clone
[perl/modules/Variable-Magic.git] / t / 25-copy.t
index 0630ae33502d7c659616d1df4de0b9bebb809d9f..46e324141653d2298c32b85ad5a3b4ce948e379e 100644 (file)
@@ -5,13 +5,12 @@ use warnings;
 
 use Test::More;
 
-use Variable::Magic qw/cast dispell MGf_COPY/;
+use lib 't/lib';
+use VPIT::TestHelpers;
 
-if (MGf_COPY) {
- plan tests => 2 + ((2 * 5 + 3) + (2 * 2 + 1)) + (2 * 9 + 6) + 1;
-} else {
- plan skip_all => 'No copy magic for this perl';
-}
+use Variable::Magic qw<wizard cast dispell VMG_COMPAT_CODE_COPY_CLONE>;
+
+plan tests => 2 + ((2 * 5 + 3) + (2 * 2 + 1)) + (2 * 9 + 6) + 3 + 1;
 
 use lib 't/lib';
 use Variable::Magic::TestWatcher;
@@ -20,10 +19,7 @@ use Variable::Magic::TestValue;
 my $wiz = init_watcher 'copy', 'copy';
 
 SKIP: {
- eval "use Tie::Array";
- skip 'Tie::Array required to test copy magic on arrays'
-                                             => (2 * 5 + 3) + (2 * 2 + 1) if $@;
- defined and diag "Using Tie::Array $_" for $Tie::Array::VERSION;
+ load_or_skip('Tie::Array', undef, undef, (2 * 5 + 3) + (2 * 2 + 1));
 
  tie my @a, 'Tie::StdArray';
  @a = (1 .. 10);
@@ -55,9 +51,7 @@ SKIP: {
 }
 
 SKIP: {
- eval "use Tie::Hash";
- skip 'Tie::Hash required to test copy magic on hashes' => 2 * 9 + 6 if $@;
- defined and diag "Using Tie::Hash $_" for $Tie::Hash::VERSION;
+ load_or_skip('Tie::Hash', undef, undef, 2 * 9 + 6);
 
  tie my %h, 'Tie::StdHash';
  %h = (a => 1, b => 2, c => 3);
@@ -79,10 +73,30 @@ SKIP: {
  watch { my ($k, $v) = each %h } { copy => 1 }, 'tied hash each';
 
  my @k = watch { keys %h } { }, 'tied hash keys';
- is_deeply [ sort @k ], [ qw/a c/ ], 'copy: tied hash keys correctly';
+ is_deeply [ sort @k ], [ qw<a c> ], 'copy: tied hash keys correctly';
 
  my @v = watch { values %h } { copy => 2 }, 'tied hash values';
  is_deeply [ sort { $a <=> $b } @v ], [ 1, 3 ], 'copy: tied hash values correctly';
 
  watch { undef %h } { }, 'tied hash undef';
 }
+
+SKIP: {
+ skip 'copy magic not called for cloned prototypes before perl 5.17.0' => 3
+                                              unless VMG_COMPAT_CODE_COPY_CLONE;
+ my $w = wizard copy => sub {
+  is ref($_[0]), 'CODE', 'first arg in copy on clone is a code ref';
+  is $_[2],      undef,  'third arg in copy on clone is undef';
+  is ref($_[3]), 'CODE', 'fourth arg in copy on clone is a code ref';
+ };
+ eval <<'TEST_COPY';
+  package X;
+  sub MODIFY_CODE_ATTRIBUTES {
+   my ($pkg, $sub) = @_;
+   &Variable::Magic::cast($sub, $w);
+   return;
+  }
+  my $i;
+  my $f = sub : Hello { $i };
+TEST_COPY
+}