File: AsyncSubtest.t

package info (click to toggle)
perl 5.42.0-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 128,392 kB
  • sloc: perl: 534,963; ansic: 240,563; sh: 72,042; pascal: 6,934; xml: 2,428; yacc: 1,360; makefile: 1,197; cpp: 208; lisp: 1
file content (237 lines) | stat: -rw-r--r-- 6,007 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
use Test2::Bundle::Extended -target => 'Test2::AsyncSubtest';
use Test2::AsyncSubtest;
use Test2::Util qw/get_tid CAN_THREAD CAN_REALLY_FORK/;
use Test2::API qw/intercept/;

ok($INC{'Test2/IPC.pm'}, "Loaded Test2::IPC");

# Preserve the API
can_ok $CLASS => qw{
    name hub trace send_to events finished active stack id children pid tid

    context cleave attach detach ready pending run start stop finish wait fork
    run_fork run_thread
};

my $file = __FILE__;
my $line;
like(
    dies { $line = __LINE__; $CLASS->new },
    qr/'name' is a required attribute at \Q$file\E line $line/,
    "Must provide name"
);

my ($one, $two, $three, $hub);
my %lines;
intercept {
    $lines{one} = __LINE__ + 1;
    $one = $CLASS->new(name => 'one');
    $hub = Test2::API::test2_stack()->top;

    $one->run(sub {
        $lines{two} = __LINE__ + 1;
        $two = $CLASS->new(name => 'two');
        $two->run(sub {
            $lines{three} = __LINE__ + 1;
            $three = $CLASS->new(name => 'three');
        });
    });
};
isa_ok($one, $CLASS);

is($one->hub->ast, exact_ref($one), "Can retrieve AST fromthe hub");

like(
    $one,
    {
        name     => 'one',
        send_to  => exact_ref($hub),
        trace    => {frame => [__PACKAGE__, __FILE__, $lines{one}]},
        stack    => [],
        _in_use  => 2,
        tid      => get_tid,
        pid      => $$,
        finished => 0,
        id       => 1,
        active   => 0,
        children => [],
        hub => meta { prop blessed => 'Test2::AsyncSubtest::Hub' },
        events => array {},
    },
    "Got expected properties from construction part 1"
);

like(
    $two,
    {
        name     => 'two',
        send_to  => exact_ref($one->hub),
        trace    => {frame => [__PACKAGE__, __FILE__, $lines{two}]},
        stack    => [exact_ref($one)],
        _in_use  => 1,
        tid      => get_tid,
        pid      => $$,
        finished => 0,
        id       => 1,
        active   => 0,
        children => [],
        hub => meta { prop blessed => 'Test2::AsyncSubtest::Hub' },
        events => array {},
    },
    "Got expected properties from construction part 2"
);

like(
    $three,
    {
        name     => 'three',
        send_to  => exact_ref($two->hub),
        trace    => {frame => [__PACKAGE__, __FILE__, $lines{three}]},
        stack    => [exact_ref($one), exact_ref($two)],
        _in_use  => 0,
        tid      => get_tid,
        pid      => $$,
        finished => 0,
        id       => 1,
        active   => 0,
        children => [],
        hub => meta { prop blessed => 'Test2::AsyncSubtest::Hub' },
        events => array {},
    },
    "Got expected properties from construction part 3"
);

$_->finish for $three, $two, $one;

is(
    intercept {
        my $st = Test2::AsyncSubtest->new(name => 'collapse: empty');
        $st->finish(collapse => 1);
    },
    array {
        event Ok => {
            pass => 1,
            name => 'collapse: empty',
        };
        end;
    },
    "Got 1 ok event for collapsed/empty subtest"
);

is(
    intercept {
        my $st = Test2::AsyncSubtest->new(name => 'collapse: note only');
        $st->run(sub { note "inside" });
        $st->finish(collapse => 1);
    },
    array {
        event Subtest => sub {
            call pass => 1;
            call name => 'collapse: note only';
            call subevents => array {
                event Note => { message => "inside" };
                event Plan => { max => 0, directive => 'SKIP' };
                end;
            };
        };
        end;
    },
    "Got subtest event containing only the note and a 0 plan"
);

is(
    intercept {
        my $st = Test2::AsyncSubtest->new(name => 'collapse: full');
        $st->run(sub { ok(1, "test") });
        $st->finish(collapse => 1);
    },
    array {
        event Subtest => sub {
            call pass => 1;
            call name => 'collapse: full';
            call subevents => array {
                event Ok => { pass => 1 };
                event Plan => { max => 1 };
                end;
            };
        };
        end;
    },
    "Got full subtest"
);

is(
    intercept {
        my $st = Test2::AsyncSubtest->new(name => 'collapse: no assert, but fail');
        $st->hub->set_failed(1);
        $st->finish(collapse => 1);
    },
    array {
        fail_events Ok => sub {
            call pass => 0;
            call name => 'collapse: no assert, but fail';
        };
        end;
    },
    "Failure with no assertion (no test count)"
);


is(
    intercept {
        my $st = Test2::AsyncSubtest->new(name => 'skip test');
        $st->finish(skip => "foo bar");
    },
    array {
        event Skip => { name => 'skip test', reason => 'foo bar' };
        end;
    },
    "Can skip"
);

my $events = intercept {
    my $control = mock 'Test2::Hub' => (
        override => [ is_local => sub { 0 } ],
    );

    my $st = Test2::AsyncSubtest->new(name => 'early');
    $st->run(sub { diag("We want to see this message or people die!") });
    $control = undef;
    $st->finish();
};

is(
    $events->[0]->{subevents}->[0]->{message},
    "We want to see this message or people die!",
    "Can send non-local non-attached events"
);

# TODO Make this into an actual test, we want it to cause an explosion, but
# intercept is not string enough to contain that explosion...
#$events = intercept {
#    my $control = mock 'Test2::Hub' => (
#        override => [ is_local => sub { 0 } ],
#    );
#
#    my $st = Test2::AsyncSubtest->new(name => 'early');
#
#    local $SIG{PIPE} = 'IGNORE';
#    pipe(my $rh, my $wh) or die "Could not pipe";
#    my $pid = fork();
#    if ($pid) {
#        $st->run(sub{ ok(1) });
#        $control = undef;
#        $st->finish();
#        print $wh "ready\n";
#        $wh->flush;
#        close($wh);
#        waitpid($pid, 0);
#    }
#    else {
#        my $ready = <$rh>;
#        $st->run(sub{ diag "Too Late" });
#        exit 0;
#    }
#};

done_testing;