xref: /openbsd-src/gnu/usr.bin/perl/cpan/Test-Simple/t/Test2/modules/API/InterceptResult/Squasher.t (revision 5486feefcc8cb79b19e014ab332cc5dfd05b3b33)
1use strict;
2use warnings;
3
4use Test2::Tools::Tiny;
5use Test2::API::InterceptResult::Squasher;
6use Test2::API::InterceptResult::Event;
7
8my $CLASS = 'Test2::API::InterceptResult::Squasher';
9
10my $trace1 = {pid => $$, tid => 0, cid => 1, frame => ['Foo::Bar', 'Foo/Bar.pm', 42, 'ok']};
11my $trace2 = {pid => $$, tid => 0, cid => 2, frame => ['Foo::Bar', 'Foo/Bar.pm', 43, 'note']};
12my $trace3 = {pid => $$, tid => 0, cid => 3, frame => ['Foo::Bar', 'Foo/Bar.pm', 44, 'subtest']};
13my $trace4 = {pid => $$, tid => 0, cid => 4, frame => ['Foo::Bar', 'Foo/Bar.pm', 45, 'diag']};
14
15my @raw = (
16    # These 4 should merge
17    Test2::API::InterceptResult::Event->new(facet_data => {
18        trace => $trace1,
19        info => [{tag => 'DIAG', details => 'about to fail'}],
20    }),
21    Test2::API::InterceptResult::Event->new(facet_data => {
22        trace => $trace1,
23        assert => { pass => 0, details => 'fail' },
24    }),
25    Test2::API::InterceptResult::Event->new(facet_data => {
26        trace => $trace1,
27        info => [{tag => 'DIAG', details => 'it failed'}],
28    }),
29    Test2::API::InterceptResult::Event->new(facet_data => {
30        trace => $trace1,
31        info => [{tag => 'DIAG', details => 'it failed part 2'}],
32    }),
33
34    # Same trace, but should not merge as it has an assert
35    Test2::API::InterceptResult::Event->new(facet_data => {
36        trace => $trace1,
37        assert => { pass => 0, details => 'fail again' },
38        info => [{tag => 'DIAG', details => 'it failed again'}],
39    }),
40
41    # Stand alone note
42    Test2::API::InterceptResult::Event->new(facet_data => {
43        trace => $trace2,
44        info => [{tag => 'NOTE', details => 'Take Note!'}],
45    }),
46
47    # Subtest, note, assert, diag as 3 events, should be merged
48    Test2::API::InterceptResult::Event->new(facet_data => {
49        trace => $trace3,
50        info => [{tag => 'NOTE', details => 'About to start subtest'}],
51    }),
52    Test2::API::InterceptResult::Event->new(facet_data => {
53        trace => $trace3,
54        assert => { pass => 0, details => 'failed subtest' },
55        parent => { details => 'foo', state => {}, children => [] },
56    }),
57    Test2::API::InterceptResult::Event->new(facet_data => {
58        trace => $trace3,
59        info => [{tag => 'DIAG', details => 'Subtest failed'}],
60    }),
61
62    # Stand alone diag
63    Test2::API::InterceptResult::Event->new(facet_data => {
64        trace => $trace4,
65        info => [{tag => 'DIAG', details => 'Diagnosis: Murder'}],
66    }),
67);
68
69my @events;
70my $squasher = $CLASS->new(events => \@events);
71ok($squasher->isa($CLASS), "Got an instance");
72$squasher->process($_) for @raw;
73$squasher = undef;
74
75is_deeply(
76    [map { $_->facet_data } @events],
77    [
78        {
79            trace  => $trace1,
80            assert => {pass => 0, details => 'fail'},
81            info   => [
82                {tag => 'DIAG', details => 'about to fail'},
83                {tag => 'DIAG', details => 'it failed'},
84                {tag => 'DIAG', details => 'it failed part 2'},
85            ],
86        },
87
88        {
89            trace  => $trace1,
90            assert => {pass => 0, details => 'fail again'},
91            info   => [{tag => 'DIAG', details => 'it failed again'}],
92        },
93
94        {
95            trace => $trace2,
96            info  => [{tag => 'NOTE', details => 'Take Note!'}],
97        },
98
99        {
100            trace  => $trace3,
101            assert => {pass => 0, details => 'failed subtest'},
102            parent => {details => 'foo', state => {}, children => []},
103            info   => [
104                {tag => 'NOTE', details => 'About to start subtest'},
105                {tag => 'DIAG', details => 'Subtest failed'},
106            ],
107        },
108
109        {
110            trace => $trace4,
111            info  => [{tag => 'DIAG', details => 'Diagnosis: Murder'}],
112        },
113    ],
114    "Squashed events as expected"
115);
116
117done_testing;
118