Add tests for autovivication combined with get-magic, some of which are TODO.
authorGerard Goossen <gerard@ggoossen.net>
Wed, 24 Aug 2011 11:39:55 +0000 (13:39 +0200)
committerFather Chrysostomos <sprout@cpan.org>
Wed, 24 Aug 2011 18:14:42 +0000 (11:14 -0700)
t/op/gmagic.t

index 64e7a2a..083b7ec 100644 (file)
@@ -56,6 +56,54 @@ ok($s eq *strat,
    'Assignment should not ignore magic when the last thing assigned was a glob');
 expected_tie_calls(tied $c, 1, 1);
 
+# autovivication of aelem, helem, of rv2sv combined with get-magic
+{
+    my $true = 1;
+    my $s;
+    tie $$s, "Tie::Monitor";
+    $$s = undef;
+    $$s->[0] = 73;
+    is($$s->[0], 73);
+    expected_tie_calls(tied $$s, 2, 2);
+
+    my @a;
+    tie $a[0], "Tie::Monitor";
+    $a[0] = undef;
+    $a[0][0] = 73;
+    is($a[0][0], 73);
+    expected_tie_calls(tied $a[0], 2, 2);
+
+    my %h;
+    tie $h{foo}, "Tie::Monitor";
+    $h{foo} = undef;
+    $h{foo}{bar} = 73;
+    is($h{foo}{bar}, 73);
+    expected_tie_calls(tied $h{foo}, 2, 2);
+
+    # Similar tests, but with obscured autovivication by using dummy list or "?:" operator
+    {
+        local our $TODO = "Get magic is executed too often";
+        $$s = undef;
+        ${ (), $$s }[0] = 73;
+        is( $$s->[0], 73);
+        expected_tie_calls(tied $$s, 2, 2);
+    }
+
+    $$s = undef;
+    ( ! $true ? undef : $$s )->[0] = 73;
+    is( $$s->[0], 73);
+    expected_tie_calls(tied $$s, 2, 2);
+
+    {
+        local our $TODO = "Get magic is executed too often";
+        $$s = undef;
+        ( $true ? $$s : undef )->[0] = 73;
+        is( $$s->[0], 73);
+        expected_tie_calls(tied $$s, 2, 2);
+    }
+
+}
+
 # A plain *foo should not call get-magic on *foo.
 # This method of scalar-tying an immutable glob relies on details of the
 # current implementation that are subject to change. This test may need to