Debian Codegraph @

t/14-callbacks.t

raw
1 #!perl -T
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 26;
7
8 use Variable::Magic qw<wizard cast>;
9
10 my $wiz = eval { wizard get => sub { undef } };
11 is($@, '', 'wizard creation doesn\'t croak');
12 ok(defined $wiz, 'wizard is defined');
13 is(ref $wiz, 'SCALAR', 'wizard is a scalar ref');
14
15 my $n = int rand 1000;
16 my $a = $n;
17
18 my $res = eval { cast $a, $wiz };
19 is($@, '', 'cast doesn\'t croak');
20 ok($res, 'cast is valid');
21
22 my $x;
23 eval {
24 local $SIG{__WARN__} = sub { die };
25 $x = $a
26 };
27 is($@, '', 'callback returning undef doesn\'t warn/croak');
28 is($x, $n, 'callback returning undef fails');
29
30 {
31 my $c = 0;
32 sub X::wat { ++$c }
33 my $wiz = eval { wizard get => \'X::wat' };
34 is($@, '', 'wizard with a qualified string callback doesn\'t croak');
35 my $b = $n;
36 my $res = eval { cast $b, $wiz };
37 is($@, '', 'cast a wizard with a qualified string callback doesn\'t croak');
38 my $x;
39 eval {
40 local $SIG{__WARN__} = sub { die };
41 $x = $b;
42 };
43 is($@, '', 'qualified string callback doesn\'t warn/croak');
44 is($c, 1, 'qualified string callback is called');
45 is($x, $n, 'qualified string callback returns the right thing');
46 }
47
48 {
49 my $c = 0;
50 sub wut { fail 'main::wut was called' }
51 sub Y::wut { ++$c }
52 my $wiz = eval { wizard get => \'wut' };
53 is($@, '', 'wizard with a short string callback doesn\'t croak');
54 my $b = $n;
55 my $res = eval { cast $b, $wiz };
56 is($@, '', 'cast a wizard with a short string callback doesn\'t croak');
57 my $x;
58 eval {
59 local $SIG{__WARN__} = sub { die };
60 package Y;
61 $x = $b;
62 };
63 is($@, '', 'short string callback doesn\'t warn/croak');
64 is($c, 1, 'short string callback is called');
65 is($x, $n, 'short string callback returns the right thing');
66 }
67
68 {
69 my $wiz = eval { wizard get => \undef };
70 is($@, '', 'wizard with a ref-to-undef callback doesn\'t croak');
71 my $b = $n;
72 my $res = eval { cast $b, $wiz };
73 is($@, '', 'cast a wizard with a ref-to-undef callback doesn\'t croak');
74 my $x;
75 eval {
76 local $SIG{__WARN__} = sub { die };
77 $x = $b;
78 };
79 is($@, '', 'ref-to-undef callback doesn\'t warn/croak');
80 is($x, $n, 'ref-to-undef callback returns the right thing');
81 }
82
83 my @callers;
84 $wiz = wizard get => sub {
85 my @c;
86 my $i = 0;
87 while (@c = caller $i++) {
88 push @callers, [ @c[0, 1, 2] ];
89 }
90 };
91
92 my $b;
93 cast $b, $wiz;
94
95 my $u = $b;
96 is_deeply(\@callers, [
97 ([ 'main', $0, __LINE__-2 ]) x 2,
98 ], 'caller into callback returns the right thing');
99
100 @callers = ();
101 $u = $b;
102 is_deeply(\@callers, [
103 ([ 'main', $0, __LINE__-2 ]) x 2,
104 ], 'caller into callback returns the right thing (second time)');
105
106 {
107 @callers = ();
108 my $u = $b;
109 is_deeply(\@callers, [
110 ([ 'main', $0, __LINE__-2 ]) x 2,
111 ], 'caller into callback into block returns the right thing');
112 }
113
114 @callers = ();
115 eval { my $u = $b };
116 is($@, '', 'caller into callback doesn\'t croak');
117 is_deeply(\@callers, [
118 ([ 'main', $0, __LINE__-3 ]) x 3,
119 ], 'caller into callback into eval returns the right thing');
120