Created
November 6, 2016 21:28
-
-
Save exodist/42732aa8e63c492d9d20ca1465d84880 to your computer and use it in GitHub Desktop.
Data from subtest into parent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Test2::Bundle::Extended; | |
use Test2::Tools::AsyncSubtest qw/fork_subtest/; | |
use Test2::API qw/test2_stack/; | |
# This is a custom event meant to transport our data. | |
{ | |
package Test2::Event::MyData; | |
use base 'Test2::Event'; | |
use Test2::Util::HashBase qw/mydata/; | |
sub no_display { 1 } | |
} | |
# Get the main hub | |
my $main_hub = test2_stack->top; | |
# Add a listener that will grab our data, then remove itself. | |
my $from_child; | |
$main_hub->listen(sub { | |
my ($hub, $event) = @_; | |
# Only look for the events we care about | |
return unless $event->isa('Test2::Event::MyData'); | |
# Get the data | |
$from_child = $event->mydata; | |
# Remove the listener, it is done. | |
$hub->unlisten($_->{code}); | |
}); | |
# Do the subtest | |
fork_subtest(foo => sub { | |
ok(1, "blah"); | |
my $child_hub = test2_stack->top; | |
$main_hub->send( | |
Test2::Event::MyData->new( | |
mydata => {any => 'data', that => 'you want'} | |
), | |
); | |
})->finish; | |
# Need to make sure the event it picked up. | |
$main_hub->cull; | |
# This tests confirms we got the data from the child. | |
is($from_child, {any => 'data', that => 'you want'}, "Got the data from the subtest"); | |
done_testing; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment