This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Make tests for gmagic on sub exit count fetches
[perl5.git] / t / op / gmagic.t
CommitLineData
8d6d96c1
HS
1#!./perl -w
2
3BEGIN {
8d6d96c1
HS
4 chdir 't' if -d 't';
5 @INC = '../lib';
ead3e279 6 require './test.pl';
8d6d96c1
HS
7}
8
ead3e279 9use strict;
8d6d96c1 10
8d6d96c1
HS
11tie my $c => 'Tie::Monitor';
12
ead3e279 13sub expected_tie_calls {
40914f83 14 my ($obj, $rexp, $wexp, $tn) = @_;
ead3e279
NC
15 local $::Level = $::Level + 1;
16 my ($rgot, $wgot) = $obj->init();
40914f83
FC
17 is ($rgot, $rexp, $tn ? "number of fetches when $tn" : ());
18 is ($wgot, $wexp, $tn ? "number of stores when $tn" : ());
8d6d96c1
HS
19}
20
ead3e279 21# Use ok() instead of is(), cmp_ok() etc, to strictly control number of accesses
8d6d96c1 22my($r, $s);
ead3e279
NC
23ok($r = $c + 0 == 0, 'the thing itself');
24expected_tie_calls(tied $c, 1, 0);
25ok($r = "$c" eq '0', 'the thing itself');
26expected_tie_calls(tied $c, 1, 0);
27
28ok($c . 'x' eq '0x', 'concat');
29expected_tie_calls(tied $c, 1, 0);
30ok('x' . $c eq 'x0', 'concat');
31expected_tie_calls(tied $c, 1, 0);
8d6d96c1 32$s = $c . $c;
ead3e279
NC
33ok($s eq '00', 'concat');
34expected_tie_calls(tied $c, 2, 0);
8d6d96c1
HS
35$r = 'x';
36$s = $c = $r . 'y';
ead3e279
NC
37ok($s eq 'xy', 'concat');
38expected_tie_calls(tied $c, 1, 1);
8d6d96c1 39$s = $c = $c . 'x';
ead3e279
NC
40ok($s eq '0x', 'concat');
41expected_tie_calls(tied $c, 2, 1);
8d6d96c1 42$s = $c = 'x' . $c;
ead3e279
NC
43ok($s eq 'x0', 'concat');
44expected_tie_calls(tied $c, 2, 1);
8d6d96c1 45$s = $c = $c . $c;
ead3e279
NC
46ok($s eq '00', 'concat');
47expected_tie_calls(tied $c, 3, 1);
8d6d96c1 48
1e968d83 49$s = chop($c);
ead3e279
NC
50ok($s eq '0', 'multiple magic in core functions');
51expected_tie_calls(tied $c, 1, 1);
1e968d83 52
5cf4b255
FC
53$c = *strat;
54$s = $c;
ead3e279
NC
55ok($s eq *strat,
56 'Assignment should not ignore magic when the last thing assigned was a glob');
074ededa 57expected_tie_calls(tied $c, 1, 1);
5cf4b255 58
f64c9ac5
FC
59# A plain *foo should not call get-magic on *foo.
60# This method of scalar-tying an immutable glob relies on details of the
61# current implementation that are subject to change. This test may need to
62# be rewritten if they do change.
63my $tyre = tie $::{gelp} => 'Tie::Monitor';
64# Compilation of this eval autovivifies the *gelp glob.
65eval '$tyre->init(0); () = \*gelp';
66my($rgot, $wgot) = $tyre->init(0);
ead3e279
NC
67ok($rgot == 0, 'a plain *foo causes no get-magic');
68ok($wgot == 0, 'a plain *foo causes no set-magic');
f64c9ac5 69
767eda44
FC
70# get-magic when exiting a non-lvalue sub in potentially autovivify-
71# ing context
40914f83
FC
72{
73 no strict;
74
75 my $tied_to = tie $_{elem}, "Tie::Monitor";
76 () = sub { delete $_{elem} }->()->[3];
77 expected_tie_calls $tied_to, 1, 0,
78 'mortal magic var is implicitly returned in autoviv context';
79
80 $tied_to = tie $_{elem}, "Tie::Monitor";
81 () = sub { return delete $_{elem} }->()->[3];
82 expected_tie_calls $tied_to, 1, 0,
83 'mortal magic var is explicitly returned in autoviv context';
84}
767eda44 85
ead3e279 86done_testing();
5cf4b255 87
8d6d96c1
HS
88# adapted from Tie::Counter by Abigail
89package Tie::Monitor;
90
91sub TIESCALAR {
92 my($class, $value) = @_;
93 bless {
94 read => 0,
95 write => 0,
96 values => [ 0 ],
97 };
98}
99
100sub FETCH {
101 my $self = shift;
102 ++$self->{read};
103 $self->{values}[$#{ $self->{values} }];
104}
105
106sub STORE {
107 my($self, $value) = @_;
108 ++$self->{write};
109 push @{ $self->{values} }, $value;
110}
111
112sub init {
113 my $self = shift;
114 my @results = ($self->{read}, $self->{write});
115 $self->{read} = $self->{write} = 0;
116 $self->{values} = [ 0 ];
117 @results;
118}