xref: /openbsd-src/gnu/usr.bin/perl/cpan/Test-Simple/t/Test2/modules/Hub/Subtest.t (revision 5486feefcc8cb79b19e014ab332cc5dfd05b3b33)
1use strict;
2use warnings;
3use Test2::Tools::Tiny;
4
5use Test2::Hub::Subtest;
6use Test2::Util qw/get_tid/;
7use Carp qw/croak/;
8
9my %TODO;
10
11sub def {
12    my ($func, @args) = @_;
13
14    my @caller = caller(0);
15
16    $TODO{$caller[0]} ||= [];
17    push @{$TODO{$caller[0]}} => [$func, \@args, \@caller];
18}
19
20sub do_def {
21    my $for = caller;
22    my $tests = delete $TODO{$for} or croak "No tests to run!";
23
24    for my $test (@$tests) {
25        my ($func, $args, $caller) = @$test;
26
27        my ($pkg, $file, $line) = @$caller;
28
29# Note: The '&' below is to bypass the prototype, which is important here.
30        eval <<"        EOT" or die $@;
31package $pkg;
32# line $line "(eval in DeferredTests) $file"
33\&$func(\@\$args);
341;
35        EOT
36    }
37}
38
39my $ran = 0;
40my $event;
41
42my $one = Test2::Hub::Subtest->new(
43    nested => 3,
44);
45
46ok($one->isa('Test2::Hub'), "inheritance");
47
48{
49    no warnings 'redefine';
50    local *Test2::Hub::process = sub { $ran++; (undef, $event) = @_; 'P!' };
51    use warnings;
52
53    my $ok = Test2::Event::Ok->new(
54        pass => 1,
55        name => 'blah',
56        trace => Test2::EventFacet::Trace->new(frame => [__PACKAGE__, __FILE__, __LINE__, 'xxx']),
57    );
58
59    def is => ($one->process($ok), 'P!', "processed");
60    def is => ($ran, 1, "ran the mocked process");
61    def is => ($event, $ok, "got our event");
62    def is => ($one->bailed_out, undef, "did not bail");
63
64    $ran = 0;
65    $event = undef;
66
67    my $bail = Test2::Event::Bail->new(
68        message => 'blah',
69        trace => Test2::EventFacet::Trace->new(frame => [__PACKAGE__, __FILE__, __LINE__, 'xxx']),
70    );
71
72    def is => ($one->process($bail), 'P!', "processed");
73    def is => ($ran, 1, "ran the mocked process");
74    def is => ($event, $bail, "got our event");
75}
76
77do_def;
78
79my $skip = Test2::Event::Plan->new(
80    trace => Test2::EventFacet::Trace->new(frame => [__PACKAGE__, __FILE__, __LINE__], pid => $$, tid => get_tid),
81    directive => 'SKIP',
82    reason => 'foo',
83);
84
85$ran = 0;
86T2_SUBTEST_WRAPPER: {
87    $ran++;
88    $one->terminate(100, $skip);
89    $ran++;
90}
91is($ran, 1, "did not get past the terminate");
92
93$ran = 0;
94T2_SUBTEST_WRAPPER: {
95    $ran++;
96    $one->send($skip);
97    $ran++;
98}
99is($ran, 1, "did not get past the terminate");
100
101$one->reset_state;
102$one->set_manual_skip_all(1);
103
104$ran = 0;
105T2_SUBTEST_WRAPPER: {
106    $ran++;
107    $one->terminate(100, $skip);
108    $ran++;
109}
110is($ran, 2, "did not automatically abort");
111
112$one->reset_state;
113$ran = 0;
114T2_SUBTEST_WRAPPER: {
115    $ran++;
116    $one->send($skip);
117    $ran++;
118}
119is($ran, 2, "did not automatically abort");
120
121done_testing;
122