File: Devel-Caller.t

package info (click to toggle)
libdevel-caller-perl 2.07-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 92 kB
  • sloc: perl: 344; makefile: 3
file content (196 lines) | stat: -rw-r--r-- 6,157 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
#!perl -w
use strict;
use Test::More tests => 72;

BEGIN { use_ok( 'Devel::Caller', qw( caller_cv caller_args caller_vars called_with called_as_method ) ) }

package CV;
use Test::More;

my $cv;
$cv = sub {
    is( ::caller_cv(0), $cv, "caller_cv" );
};
$cv->();

sub foo { bar(my $bar) }
sub bar { baz(my $baz) }
sub baz { check(my $check) }
sub check {
    my $i = 0;
    for (qw( check baz bar foo )) {
        is(         ::caller_cv($i),    \&{"CV::$_"}, "caller_cv $i is $_" );
        is_deeply( [::called_with($i,1)], [ "\$$_" ], "called_with $i is \$$_" );
        ++$i;
    }
}
foo(my $foo);

package main;

my (@foo, %foo);
sub called_lex {
    my @called = called_with(0);
    is( scalar @called, 3, "right count");
    is( $called[0], \$foo, "with lexical \$foo" );
    is( $called[1], \@foo, "with lexical \@foo" );
    is( $called[2], \%foo, "with lexical \%foo" );
}
called_lex($foo, @foo, %foo);

sub called_lex_names {
    my @called = called_with(0, 1);
    is( @called, 3, "right count");
    is( $called[0], '$foo', "with lexical name \$foo" );
    is( $called[1], '@foo', "with lexical name \@foo" );
    is( $called[2], '%foo', "with lexical name \%foo" );
}
called_lex_names($foo, @foo, %foo);

# called_with muddied with assignments
my @expect;
my $what;
sub called_assign {
    is_deeply([ called_with(0, 1) ], \@expect,
              "$what called_assign(".join(', ', map { $_ || "undef"} @expect).")");
}

$what = 'constant';
{
    my $foo;
    @expect = undef;                called_assign('foo');
    @expect = (undef, '$foo');      called_assign('foo', $foo);
    @expect = (undef, '$foo');      called_assign(['foo'], $foo);
}

$what = 'lexical create';
{ # test scalars
    @expect = qw( $bar );           called_assign(my $bar = q(some value));
    @expect = qw( $baz );           called_assign(my $baz = $foo);
    @expect = qw( $quux $bar );     called_assign(my $quux = $foo, $bar);
}
{ # same again for arrays
    @expect = qw( @bar );           called_assign(my @bar = qw(some values));
    @expect = qw( @baz );           called_assign(my @baz = @foo);
    @expect = qw( @quux @bar );     called_assign(my @quux = @foo, @bar);
    @expect = qw( @flange );        called_assign(my @flange = (@foo, @bar));
}
{ # and again for hashes
    @expect = qw( %bar );           called_assign(my %bar = qw(some values));
    @expect = qw( %baz );           called_assign(my %baz = %foo);
    @expect = qw( %quux %bar );     called_assign(my %quux = %foo, %bar);
    @expect = qw( %flange );        called_assign(my %flange = (%foo, %bar));
}

$what = 'lexical prexist';
{ # test scalars
    my ($bar, $baz, $quux);
    @expect = qw( $bar );           called_assign($bar = q(some value));
    @expect = qw( $baz );           called_assign($baz = $foo);
    @expect = qw( $quux $bar );     called_assign($quux = $foo, $bar);
}
{ # same again for arrays
    my (@bar, @baz, @quux, @flange);
    @expect = qw( @bar );           called_assign(@bar = qw(some values));
    @expect = qw( @baz );           called_assign(@baz = @foo);
    @expect = qw( @quux @bar );     called_assign(@quux = @foo, @bar);
    @expect = qw( @flange );        called_assign(@flange = (@foo, @bar));
}
{ # and again for hashes
    my (%bar, %baz, %quux, %flange);
    @expect = qw( %bar );           called_assign(%bar = qw(some values));
    @expect = qw( %baz );           called_assign(%baz = %foo);
    @expect = qw( %quux %bar );     called_assign(%quux = %foo, %bar);
    @expect = qw( %flange );        called_assign(%flange = (%foo, %bar));
}

use vars qw( $quux @quux %quux );
sub called {
    my @called = caller_vars(0);
    is( scalar @called, 3, "right count");
    is( $called[0], \$quux, "with \$quux" );
    is( $called[1], \@quux, "with \@quux" );
    is( $called[2], \%quux, "with \%quux" );
}
called($quux, @quux, %quux);


sub called_names {
    my @called = called_with(0, 1);
    is( scalar @called, 3, "right count");
    is( $called[0], '$main::quux', "with name 0" );
    is( $called[1], '@main::quux', "with name 1" );
    is( $called[2], '%main::quux', "with name 2" );
}

called_names($quux, @quux, %quux);
sub called_globs {
    my @called = called_with(0, 1);
    is( scalar @called, 3, "right count");
    is( $called[0], '*main::STDIN',  "with name 0" );
    is( $called[1], '*main::STDOUT', "with name 1" );
    is( $called[2], '*main::STDERR', "with name 2" );
}

called_globs(*STDIN, *STDOUT, *STDERR);

package T;
$what = 'package';
*called_assign = \&::called_assign;

{ # test scalars
    use vars qw( $bar $baz $quux );
    @expect = qw( $T::bar );           called_assign($bar = q(a value));
    @expect = qw( $T::baz );           called_assign($baz = $foo);
    @expect = qw( $T::quux $T::bar );  called_assign($quux = $foo, $bar);
}
{ # same again for arrays
    use vars qw( @bar @baz @quux @flange );
    {
        local $::TODO = "splitops under 5.00503"
          if $] < 5.006;
        @expect = qw( @T::bar );       called_assign(@bar = qw(some values));
    }
    @expect = qw( @T::baz );           called_assign(@baz = @foo);
    @expect = qw( @T::quux @T::bar );  called_assign(@quux = @foo, @bar);
    @expect = qw( @T::flange );        called_assign(@flange = (@foo, @bar));
}
{ # and again for hashes
    use vars qw( %bar %baz %quux %flange );
    @expect = qw( %T::bar );           called_assign(%bar = qw(1 2));
    @expect = qw( %T::baz );           called_assign(%baz = %foo);
    @expect = qw( %T::quux %T::bar );  called_assign(%quux = %foo, %bar);
    @expect = qw( %T::flange );        called_assign(%flange = (%foo, %bar));
}


package main;
# were we called as a method or a sub
my $called;
sub maybe_method {
    is( called_as_method(), $called, "called_as_method" );
}
maybe_method();
$called = 1;
main->maybe_method();
my $name = 'maybe_method';
main->$name();


sub args {
    is_deeply( \@_, [ caller_args(0) ] );
}

args('foo', 'bar');

# rt.cpan.org 2878
my $coy = rand 6;
print "# cunning coy tests\n";
real( $coy, $coy );
print "# concat\n";

print "# print ", real( $coy, $coy ), "\n";

sub real {
    is_deeply( [ called_with(0,1) ], [qw( $coy $coy )], 'real( $coy, $coy )' );
}