Don't use $_ as loop variable when calling arbitrary code (RT#81072)
[gitmo/Moo.git] / t / sub-quote.t
CommitLineData
a165a07f 1use strictures 1;
2use Test::More;
3use Test::Fatal;
4
5use Sub::Quote;
6
7our %EVALED;
8
9my $one = quote_sub q{
10 BEGIN { $::EVALED{'one'} = 1 }
11 42
12};
13
14my $two = quote_sub q{
15 BEGIN { $::EVALED{'two'} = 1 }
16 3 + $x++
17} => { '$x' => \do { my $x = 0 } };
18
19ok(!keys %EVALED, 'Nothing evaled yet');
20
21my $u_one = unquote_sub $one;
22
23is_deeply(
55d91f64 24 [ sort keys %EVALED ], [ qw(one) ],
25 'subs one evaled'
a165a07f 26);
27
28is($one->(), 42, 'One (quoted version)');
29
30is($u_one->(), 42, 'One (unquoted version)');
31
32is($two->(), 3, 'Two (quoted version)');
33is(unquote_sub($two)->(), 4, 'Two (unquoted version)');
34is($two->(), 5, 'Two (quoted version again)');
35
36my $three = quote_sub 'Foo::three' => q{
37 $x = $_[1] if $_[1];
38 die +(caller(0))[3] if @_ > 2;
39 return $x;
40} => { '$x' => \do { my $x = 'spoon' } };
41
42is(Foo->three, 'spoon', 'get ok (named method)');
43is(Foo->three('fork'), 'fork', 'set ok (named method)');
44is(Foo->three, 'fork', 're-get ok (named method)');
45like(
46 exception { Foo->three(qw(full cutlery set)) }, qr/Foo::three/,
47 'exception contains correct name'
48);
49
50done_testing;