xref: /openbsd-src/gnu/usr.bin/perl/cpan/Test-Simple/t/Test2/modules/API/InterceptResult.t (revision 5486feefcc8cb79b19e014ab332cc5dfd05b3b33)
1256a93a4Safresh1use strict;
2256a93a4Safresh1use warnings;
3256a93a4Safresh1
4256a93a4Safresh1use Test::Builder;
5256a93a4Safresh1use Test2::Tools::Tiny;
6256a93a4Safresh1use Test2::API::InterceptResult;
7256a93a4Safresh1use Scalar::Util qw/reftype/;
8256a93a4Safresh1use Test2::API qw/intercept context/;
9256a93a4Safresh1
10256a93a4Safresh1my $CLASS = 'Test2::API::InterceptResult';
11256a93a4Safresh1
12256a93a4Safresh1tests construction => sub {
13256a93a4Safresh1    my $one = $CLASS->new('a');
14256a93a4Safresh1    ok($one->isa($CLASS), "Got an instance");
15256a93a4Safresh1    is(reftype($one), 'ARRAY', "Blessed arrayref");
16256a93a4Safresh1    is_deeply($one, ['a'], "Ref looks good.");
17256a93a4Safresh1
18256a93a4Safresh1    my $two = $CLASS->new_from_ref(['a']);
19256a93a4Safresh1    ok($two->isa($CLASS), "Got an instance");
20256a93a4Safresh1    is(reftype($two), 'ARRAY', "Blessed arrayref");
21256a93a4Safresh1    is_deeply($two, ['a'], "Ref looks good.");
22256a93a4Safresh1
23256a93a4Safresh1    my $three = $two->clone;
24256a93a4Safresh1    ok($three->isa($CLASS), "Got an instance");
25256a93a4Safresh1    is(reftype($three), 'ARRAY', "Blessed arrayref");
26256a93a4Safresh1    is_deeply($three, ['a'], "Ref looks good.");
27256a93a4Safresh1
28256a93a4Safresh1    push @$two => 'b';
29256a93a4Safresh1    is_deeply($two, ['a', 'b'], "Modified two");
30256a93a4Safresh1    is_deeply($three, ['a'], "three was not changed");
31256a93a4Safresh1
32256a93a4Safresh1    my $four = intercept {
33256a93a4Safresh1        ok(1, "Pass");
34256a93a4Safresh1    };
35256a93a4Safresh1
36256a93a4Safresh1    ok($four->isa($CLASS), "Intercept returns an instance");
37256a93a4Safresh1};
38256a93a4Safresh1
39256a93a4Safresh1tests event_list => sub {
40256a93a4Safresh1    my $one = $CLASS->new('a', 'b');
41256a93a4Safresh1    is_deeply([$one->event_list], ['a', 'b'], "event_list is essentially \@{\$self}");
42256a93a4Safresh1};
43256a93a4Safresh1
44256a93a4Safresh1tests _upgrade => sub {
45256a93a4Safresh1    require Test2::Event::Pass;
46256a93a4Safresh1    my $event = Test2::Event::Pass->new(name => 'soup for you', trace => {frame => ['foo', 'foo.pl', 42]});
47256a93a4Safresh1    ok($event->isa('Test2::Event'), "Start with an event");
48256a93a4Safresh1
49256a93a4Safresh1    my $one = $CLASS->new;
50256a93a4Safresh1    my $up = $one->_upgrade($event);
51256a93a4Safresh1    ok($up->isa('Test2::API::InterceptResult::Event'), "Upgraded the event");
52256a93a4Safresh1    is($up->result_class, $CLASS, "set the result class");
53256a93a4Safresh1
54256a93a4Safresh1    is_deeply($event->facet_data, $up->facet_data, "Facet data is identical");
55256a93a4Safresh1
56256a93a4Safresh1    $up->facet_data->{trace}->{frame}->[2] = 43;
57256a93a4Safresh1    is($up->trace_line, 43, "Modified the facet data in the upgraded clone");
58*5486feefSafresh1    is($event->facet_data->{trace}->{frame}->[2], 42, "Did not modify the original");
59256a93a4Safresh1
60256a93a4Safresh1    my $up2 = $one->_upgrade($up);
61256a93a4Safresh1    is("$up2", "$up", "Returned the ref unmodified because it is already an upgraded item");
62256a93a4Safresh1
63256a93a4Safresh1    require Test2::Event::V2;
64256a93a4Safresh1    my $subtest = 'Test2::Event::V2'->new(
65256a93a4Safresh1        trace => {frame => ['foo', 'foo.pl', 42]},
66256a93a4Safresh1        assert => {pass => 1, details => 'pass'},
67256a93a4Safresh1        parent => {
68256a93a4Safresh1            hid => 1,
69256a93a4Safresh1            children => [ $event ],
70256a93a4Safresh1        },
71256a93a4Safresh1    );
72256a93a4Safresh1
73256a93a4Safresh1    my $subup = $one->_upgrade($subtest);
74256a93a4Safresh1    ok($subup->the_subtest->{children}->isa($CLASS), "Blessed subtest subevents");
75256a93a4Safresh1    ok(
76256a93a4Safresh1        $subup->the_subtest->{children}->[0]->isa('Test2::API::InterceptResult::Event'),
77256a93a4Safresh1        "Upgraded the children"
78256a93a4Safresh1    );
79256a93a4Safresh1};
80256a93a4Safresh1
81256a93a4Safresh1tests hub => sub {
82256a93a4Safresh1    my $one = intercept {
83256a93a4Safresh1        ok(1, "pass");
84256a93a4Safresh1        ok(0, "fail");
85256a93a4Safresh1        plan 2;
86256a93a4Safresh1    };
87256a93a4Safresh1
88256a93a4Safresh1    my $hub = $one->hub;
89256a93a4Safresh1    ok($hub->isa('Test2::Hub'), "Hub is a proper instance");
90256a93a4Safresh1    ok($hub->check_plan, "Had a plan and followed it");
91256a93a4Safresh1    is($hub->count, 2, "saw both events");
92256a93a4Safresh1    is($hub->failed, 1, "saw a failure");
93256a93a4Safresh1    ok($hub->ended, "Hub ended");
94256a93a4Safresh1
95256a93a4Safresh1    is_deeply(
96256a93a4Safresh1        $one->state,
97256a93a4Safresh1        {
98256a93a4Safresh1            count => 2,
99256a93a4Safresh1            failed => 1,
100256a93a4Safresh1            is_passing => 0,
101256a93a4Safresh1            plan => 2,
102256a93a4Safresh1            bailed_out => undef,
103256a93a4Safresh1            skip_reason => undef,
104256a93a4Safresh1            follows_plan => 1,
105256a93a4Safresh1        },
106256a93a4Safresh1        "Got the hub state"
107256a93a4Safresh1    );
108256a93a4Safresh1};
109256a93a4Safresh1
110256a93a4Safresh1tests upgrade => sub {
111256a93a4Safresh1    my $one = intercept {
112256a93a4Safresh1        require Test::More;
113256a93a4Safresh1        Test::More::ok(1, "pass");
114256a93a4Safresh1        Test::More::ok(1, "pass");
115256a93a4Safresh1    };
116256a93a4Safresh1
117256a93a4Safresh1    ok($one->[0]->isa('Test2::Event::Ok'), "Original event is not upgraded 0");
118256a93a4Safresh1    ok($one->[1]->isa('Test2::Event::Ok'), "Original event is not upgraded 1");
119256a93a4Safresh1
120256a93a4Safresh1    my $two = $one->upgrade;
121256a93a4Safresh1    ok($one->[0]->isa('Test2::Event::Ok'), "Did not modify original 0");
122256a93a4Safresh1    ok($one->[0]->isa('Test2::Event::Ok'), "Did not modify original 1");
123256a93a4Safresh1    ok($two->[0]->isa('Test2::API::InterceptResult::Event'), "Upgraded copy 0");
124256a93a4Safresh1    ok($two->[1]->isa('Test2::API::InterceptResult::Event'), "Upgraded copy 1");
125256a93a4Safresh1
126256a93a4Safresh1    my $three = $two->upgrade;
127256a93a4Safresh1    ok("$two->[0]" ne "$three->[0]", "Upgrade on an already upgraded instance returns copies of the events, not originals");
128256a93a4Safresh1
129256a93a4Safresh1    like(
130256a93a4Safresh1        exception { $one->upgrade() },
131256a93a4Safresh1        qr/Called a method that creates a new instance in void context/,
132256a93a4Safresh1        "Calling upgrade() without keeping the result is a bug"
133256a93a4Safresh1    );
134256a93a4Safresh1
135256a93a4Safresh1    $one->upgrade(in_place => 1);
136256a93a4Safresh1    ok($one->[0]->isa('Test2::API::InterceptResult::Event'), "Upgraded in place 0");
137256a93a4Safresh1    ok($one->[1]->isa('Test2::API::InterceptResult::Event'), "Upgraded in place 1");
138256a93a4Safresh1};
139256a93a4Safresh1
140256a93a4Safresh1tests squash_info => sub {
141256a93a4Safresh1    my $one = intercept {
142256a93a4Safresh1        diag "isolated 1";
143256a93a4Safresh1        note "isolated 2";
144256a93a4Safresh1        sub {
145256a93a4Safresh1            my $ctx = context();
146256a93a4Safresh1            diag "inline 1";
147256a93a4Safresh1            note "inline 2";
148256a93a4Safresh1            $ctx->fail;
149256a93a4Safresh1            diag "inline 3";
150256a93a4Safresh1            note "inline 4";
151256a93a4Safresh1            $ctx->release;
152256a93a4Safresh1        }->();
153256a93a4Safresh1        diag "isolated 3";
154256a93a4Safresh1        note "isolated 4";
155256a93a4Safresh1    };
156256a93a4Safresh1
157256a93a4Safresh1    my $new = $one->squash_info;
158256a93a4Safresh1    $one->squash_info(in_place => 1);
159256a93a4Safresh1    is_deeply(
160256a93a4Safresh1        $new,
161256a93a4Safresh1        $one,
162256a93a4Safresh1        "Squash and squash in place produce the same result"
163256a93a4Safresh1    );
164256a93a4Safresh1
165256a93a4Safresh1    is(@$one, 5, "5 events after squash");
166256a93a4Safresh1    is_deeply([$one->[0]->info_messages], ['isolated 1'], "First event not modified");
167256a93a4Safresh1    is_deeply([$one->[1]->info_messages], ['isolated 2'], "Second event not modified");
168256a93a4Safresh1    is_deeply([$one->[3]->info_messages], ['isolated 3'], "second to last event not modified");
169256a93a4Safresh1    is_deeply([$one->[4]->info_messages], ['isolated 4'], "last event not modified");
170256a93a4Safresh1    is_deeply(
171256a93a4Safresh1        [$one->[2]->info_messages],
172256a93a4Safresh1        [
173256a93a4Safresh1            'inline 1',
174256a93a4Safresh1            'inline 2',
175256a93a4Safresh1            'inline 3',
176256a93a4Safresh1            'inline 4',
177256a93a4Safresh1        ],
178256a93a4Safresh1        "Assertion collected info generated in the same context"
179256a93a4Safresh1    );
180256a93a4Safresh1    ok($one->[2]->has_assert, "Assertion is still an assertion");
181256a93a4Safresh1
182256a93a4Safresh1
183256a93a4Safresh1    my $two = intercept {
184256a93a4Safresh1
185256a93a4Safresh1    };
186256a93a4Safresh1};
187256a93a4Safresh1
188256a93a4Safresh1tests messages => sub {
189256a93a4Safresh1    my $one = intercept {
190256a93a4Safresh1        note "foo";
191256a93a4Safresh1        diag "bar";
192256a93a4Safresh1
193256a93a4Safresh1        ok(1);
194256a93a4Safresh1
195256a93a4Safresh1        sub {
196256a93a4Safresh1            my $ctx = context();
197256a93a4Safresh1
198256a93a4Safresh1            $ctx->send_ev2(
199256a93a4Safresh1                errors => [
200256a93a4Safresh1                    {tag => 'error', details => "Error 1" },
201256a93a4Safresh1                    {tag => 'error', details => "Error 2" },
202256a93a4Safresh1                ],
203256a93a4Safresh1                info => [
204256a93a4Safresh1                    {tag => 'DIAG', details => 'Diag 1'},
205256a93a4Safresh1                    {tag => 'DIAG', details => 'Diag 2'},
206256a93a4Safresh1                    {tag => 'NOTE', details => 'Note 1'},
207256a93a4Safresh1                    {tag => 'NOTE', details => 'Note 2'},
208256a93a4Safresh1                ],
209256a93a4Safresh1            );
210256a93a4Safresh1
211256a93a4Safresh1            $ctx->release;
212256a93a4Safresh1        }->();
213256a93a4Safresh1
214256a93a4Safresh1        note "baz";
215256a93a4Safresh1        diag "bat";
216256a93a4Safresh1    };
217256a93a4Safresh1
218256a93a4Safresh1    is_deeply(
219256a93a4Safresh1        $one->diag_messages,
220256a93a4Safresh1        ['bar', 'Diag 1', 'Diag 2', 'bat'],
221256a93a4Safresh1        "Got diags"
222256a93a4Safresh1    );
223256a93a4Safresh1
224256a93a4Safresh1    is_deeply(
225256a93a4Safresh1        $one->note_messages,
226256a93a4Safresh1        ['foo', 'Note 1', 'Note 2', 'baz'],
227256a93a4Safresh1        "Got Notes"
228256a93a4Safresh1    );
229256a93a4Safresh1
230256a93a4Safresh1    is_deeply(
231256a93a4Safresh1        $one->error_messages,
232256a93a4Safresh1        ['Error 1', 'Error 2'],
233256a93a4Safresh1        "Got errors"
234256a93a4Safresh1    );
235256a93a4Safresh1};
236256a93a4Safresh1
237256a93a4Safresh1tests grep => sub {
238256a93a4Safresh1    my $one = intercept {
239256a93a4Safresh1        ok(1),                          # 0
240256a93a4Safresh1        note "A Note";                  # 1
241256a93a4Safresh1        diag "A Diag";                  # 2
242256a93a4Safresh1        tests foo => sub { ok(1) };   # 3
243256a93a4Safresh1
244256a93a4Safresh1        sub {                           # 4
245256a93a4Safresh1            my $ctx = context();
246256a93a4Safresh1            $ctx->send_ev2(errors => [{tag => 'error', details => "Error 1"}]);
247256a93a4Safresh1            $ctx->release;
248256a93a4Safresh1        }->();                          # 4
249256a93a4Safresh1
250256a93a4Safresh1        plan 2;                         # 5
251256a93a4Safresh1    };
252256a93a4Safresh1
253256a93a4Safresh1    $one->upgrade(in_place => 1);
254256a93a4Safresh1
255256a93a4Safresh1    is_deeply($one->asserts, [$one->[0], $one->[3]], "Got the asserts");
256256a93a4Safresh1    is_deeply($one->subtests, [$one->[3]], "Got the subtests");
257256a93a4Safresh1    is_deeply($one->diags, [$one->[2]], "Got the diags");
258256a93a4Safresh1    is_deeply($one->notes, [$one->[1]], "Got the notes");
259256a93a4Safresh1    is_deeply($one->errors, [$one->[4]], "Got the errors");
260256a93a4Safresh1    is_deeply($one->plans, [$one->[5]], "Got the plans");
261256a93a4Safresh1
262256a93a4Safresh1    $one->asserts(in_place => 1);
263256a93a4Safresh1    is(@$one, 2, "2 events");
264256a93a4Safresh1    ok($_->has_assert, "Is an assert") for @$one;
265256a93a4Safresh1};
266256a93a4Safresh1
267256a93a4Safresh1tests map => sub {
268256a93a4Safresh1    my $one = intercept { ok(1); ok(2) };
269256a93a4Safresh1    $one->upgrade(in_place => 1);
270256a93a4Safresh1
271256a93a4Safresh1    is_deeply(
272256a93a4Safresh1        $one->flatten,
273256a93a4Safresh1        [ $one->[0]->flatten, $one->[1]->flatten ],
274256a93a4Safresh1        "Flattened both events"
275256a93a4Safresh1    );
276256a93a4Safresh1
277256a93a4Safresh1    is_deeply(
278256a93a4Safresh1        $one->briefs,
279256a93a4Safresh1        [ $one->[0]->brief, $one->[1]->brief ],
280256a93a4Safresh1        "Brief of both events"
281256a93a4Safresh1    );
282256a93a4Safresh1
283256a93a4Safresh1    is_deeply(
284256a93a4Safresh1        $one->summaries,
285256a93a4Safresh1        [ $one->[0]->summary, $one->[1]->summary ],
286256a93a4Safresh1        "Summaries of both events"
287256a93a4Safresh1    );
288256a93a4Safresh1
289256a93a4Safresh1    my $two = intercept {
290256a93a4Safresh1        tests foo => sub { ok(1) };
291256a93a4Safresh1        ok(1);
292256a93a4Safresh1        tests bar => sub { ok(1) };
293256a93a4Safresh1    }->upgrade;
294256a93a4Safresh1
295256a93a4Safresh1    is_deeply(
296256a93a4Safresh1        $two->subtest_results,
297256a93a4Safresh1        [ $two->[0]->subtest_result, $two->[2]->subtest_result ],
298256a93a4Safresh1        "Got subtest results"
299256a93a4Safresh1    );
300256a93a4Safresh1};
301256a93a4Safresh1
302256a93a4Safresh1done_testing;
303