File: Tester.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 (165 lines) | stat: -rw-r--r-- 4,308 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
use Test2::V0;
use Test2::Tools::Tester qw/event_groups filter_events facets/;
use Scalar::Util qw/blessed/;

my $funky = sub {
    my $ctx = context();

    $ctx->send_event(
        Generic => (
            facet_data => {
                funk1 => {details => 'funk1'},
                funk2 => [{details => 'funk2'}, {details => 'more funk2'}],
            },
        ),
    );
    $ctx->release;
};

subtest event_groups => sub {
    my $anon = sub {
        my $ctx = context();

        $ctx->pass_and_release('foo');
    };

    my $events = intercept {
        plan 11;

        pass('pass');
        ok(1, 'pass');

        is(1, 1, "pass");
        like(1, 1, "pass");

        $anon->();
        $anon->();

        $funky->();
    };

    my $groups = event_groups($events);

    is(
        $groups,
        {
            '__NA__'              => [$events->[-1]],
            'Test2::Tools::Basic' => {
                '__ALL__' => [@{$events}[0, 1, 2]],
                'plan'    => [$events->[0]],
                'pass'    => [$events->[1]],
                'ok'      => [$events->[2]],
            },
            'Test2::Tools::Compare' => {
                '__ALL__' => [@{$events}[3, 4]],
                'is'      => [$events->[3]],
                'like'    => [$events->[4]],
            },
            'main' => {
                '__ALL__'  => [@{$events}[5, 6]],
                '__ANON__' => [@{$events}[5, 6]],
            },
        },
        "Events were grouped properly"
    );
};

subtest filter_events => sub {
    my $events = intercept {
        ok(1, "pass");
        ok(0, "fail");

        is(1, 1, "pass");
        is(1, 2, "fail");
    };

    my $basic   = filter_events $events => 'Test2::Tools::Basic';
    my $compare = filter_events $events => 'Test2::Tools::Compare';

    is(@$basic, 3, "First 2 events (and a diag) are from vasic tools");
    is(@$compare, @$events - @$basic, "Other events are from compare");

    is(
        $basic,
        [@{$events}[0, 1, 2]],
        "Verify the correct events are in the basic group"
    );

    my $basic2 = filter_events $events => qr/ok$/;
    is($basic2, $basic, "Can use a regex for a filter");
};

subtest facets => sub {
    my $events = intercept {
        ok(1, "pass");
        ok(0, "fail");
        diag "xxx";
        note "yyy";

        $funky->();

        my $it = sub {
            my $ctx = context();
            $ctx->send_event(
                Generic => (
                    facet_data => {
                        errors => [
                            {fatal => 1, details => "a fatal error", tag => 'error'},
                            {fatal => 0, details => "just an error", tag => 'error'},
                        ]
                    }
                )
            );
            $ctx->release;
        };
        $it->();
    };

    my $a_facets  = facets assert => $events;
    my $i_facets  = facets info   => $events;
    my $e1_facets = facets error  => $events;
    my $e2_facets = facets errors => $events;
    my $funk1     = facets funk1  => $events;
    my $funk2     = facets funk2  => $events;

    like(
        $a_facets,
        array {
            item { details => 'pass', pass => 1 };
            item { details => 'fail', pass => 0 };
            end;
        },
        "Got both assertions"
    );

    isa_ok($a_facets->[0], ['Test2::EventFacet::Assert'], "Blessed the facet");

    like(
        $i_facets,
        array {
            item {details => qr/Failed test/, tag => 'DIAG'};
            item {details => 'xxx',     tag => 'DIAG'};
            item {details => 'yyy',     tag => 'NOTE'};
            end;
        },
        "Got the info facets"
    );

    like(
        $e1_facets,
        array {
            item {fatal => 1, details => "a fatal error", tag => 'error'};
            item {fatal => 0, details => "just an error", tag => 'error'};
            end;
        },
        "Got error facets"
    );

    is($e1_facets, $e2_facets, "Can get facet by either the name or the key");

    is($funk1, [{details => 'funk1'}], "Can use unknown facet type");
    is($funk2, [{details => 'funk2'}, {details => 'more funk2'}], "Can use unknown list facet type");
    ok(!blessed($funk1->[0]), "Did not bless the unknown type");
};

done_testing;