Update threads usage in Sub::Quote/Defer tests
[gitmo/Moo.git] / t / sub-quote-threads.t
1 use Config;
2 BEGIN {
3   unless ($Config{useithreads}) {
4     print "1..0 # SKIP your perl does not support ithreads\n";
5     exit 0;
6   }
7 }
8 use threads;
9 use strictures 1;
10 use Test::More;
11
12 use Sub::Quote;
13
14 my $one = quote_sub my $one_code = q{
15     BEGIN { $::EVALED{'one'} = 1 }
16     42
17 };
18
19 my $two = quote_sub q{
20     BEGIN { $::EVALED{'two'} = 1 }
21     3 + $x++
22 } => { '$x' => \do { my $x = 0 } };
23
24 is(threads->create(sub {
25   my $quoted = quoted_from_sub($one);
26   $quoted && $quoted->[1];
27 })->join, $one_code, 'able to retrieve quoted sub in thread');
28
29 my $u_one = unquote_sub $one;
30
31 is(threads->create(sub { $one->() })->join, 42, 'One (quoted version)');
32
33 is(threads->create(sub { $u_one->() })->join, 42, 'One (unquoted version)');
34
35 my $r = threads->create(sub {
36   my @r;
37   push @r, $two->();
38   push @r, unquote_sub($two)->();
39   push @r, $two->();
40   \@r;
41 })->join;
42
43 is($r->[0], 3, 'Two in thread (quoted version)');
44 is($r->[1], 4, 'Two in thread (unquoted version)');
45 is($r->[2], 5, 'Two in thread (quoted version again)');
46
47 done_testing;