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