6 use Test::More tests => 26;
8 use Variable::Magic qw<wizard cast>;
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');
15 my $n = int rand 1000;
18 my $res = eval { cast $a, $wiz };
19 is($@, '', 'cast doesn\'t croak');
20 ok($res, 'cast is valid');
24 local $SIG{__WARN__} = sub { die };
27 is($@, '', 'callback returning undef doesn\'t warn/croak');
28 is($x, $n, 'callback returning undef fails');
33 my $wiz = eval { wizard get => \'X::wat' };
34 is($@, '', 'wizard with a qualified string callback doesn\'t croak');
36 my $res = eval { cast $b, $wiz };
37 is($@, '', 'cast a wizard with a qualified string callback doesn\'t croak');
40 local $SIG{__WARN__} = sub { die };
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');
50 sub wut { fail 'main::wut was called' }
52 my $wiz = eval { wizard get => \'wut' };
53 is($@, '', 'wizard with a short string callback doesn\'t croak');
55 my $res = eval { cast $b, $wiz };
56 is($@, '', 'cast a wizard with a short string callback doesn\'t croak');
59 local $SIG{__WARN__} = sub { die };
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');
69 my $wiz = eval { wizard get => \undef };
70 is($@, '', 'wizard with a ref-to-undef callback doesn\'t croak');
72 my $res = eval { cast $b, $wiz };
73 is($@, '', 'cast a wizard with a ref-to-undef callback doesn\'t croak');
76 local $SIG{__WARN__} = sub { die };
79 is($@, '', 'ref-to-undef callback doesn\'t warn/croak');
80 is($x, $n, 'ref-to-undef callback returns the right thing');
84 $wiz = wizard get => sub {
87 while (@c = caller $i++) {
88 push @callers, [ @c[0, 1, 2] ];
96 is_deeply(\@callers, [
97 ([ 'main', $0, __LINE__-2 ]) x 2,
98 ], 'caller into callback returns the right thing');
102 is_deeply(\@callers, [
103 ([ 'main', $0, __LINE__-2 ]) x 2,
104 ], 'caller into callback returns the right thing (second time)');
109 is_deeply(\@callers, [
110 ([ 'main', $0, __LINE__-2 ]) x 2,
111 ], 'caller into callback into block returns the right thing');
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');