xref: /openbsd-src/gnu/usr.bin/perl/cpan/Test-Simple/t/Test2/modules/Util/Facets2Legacy.t (revision f3efcd0145415b7d44d9da97e0ad5c21b186ac61)
1use strict;
2use warnings;
3use Test2::Tools::Tiny;
4
5use Test2::Util::Facets2Legacy ':ALL';
6
7my $CLASS;
8BEGIN {
9    $CLASS = 'Test2::Util::Facets2Legacy';
10
11    # This private function is not exported, but we want to test it anyway
12    *_get_facet_data = $CLASS->can('_get_facet_data');
13}
14
15tests _get_facet_data => sub {
16    my $pass = Test2::Event::Pass->new(name => 'xxx');
17    is_deeply(
18        _get_facet_data($pass),
19        {
20            about  => {package => 'Test2::Event::Pass', details => 'pass', eid => $pass->eid},
21            assert => {pass    => 1,                    details => 'xxx'},
22        },
23        "Got facet data from event"
24    );
25
26    is_deeply(
27        _get_facet_data({assert => {pass => 1}}),
28        {assert => {pass => 1}},
29        "Facet data gets passed through"
30    );
31
32    my $file = __FILE__;
33    my $line;
34    like(
35        exception { $line = __LINE__; _get_facet_data([]) },
36        qr/'ARRAY\(.*\)' Does not appear to be either a Test::Event or an EventFacet hashref at \Q$file\E line $line/,
37        "Must provide sane input data"
38    );
39
40    {
41        package Fake::Event;
42        use base 'Test2::Event';
43        use Test2::Util::Facets2Legacy qw/causes_fail/;
44    }
45
46    my $e = Fake::Event->new();
47    like(
48        exception { $line = __LINE__; $e->causes_fail },
49        qr/Cycle between Facets2Legacy and Fake::Event=HASH\(.*\)->facet_data\(\) \(Did you forget to override the facet_data\(\) method\?\)/,
50        "Cannot depend on legacy facet_data and Facets2Legacy"
51    );
52};
53
54tests causes_fail => sub {
55    is(causes_fail({errors => [{fail => 1}]}), 1, "Fatal errors cause failure");
56
57    is(causes_fail({control => {terminate => 0}}), 0, "defined but 0 termination does not cause failure");
58    is(causes_fail({control => {terminate => 1}}), 1, "non-zero defined termination causes failure");
59    is(causes_fail({control => {halt      => 1}}), 1, "A halt causes failure");
60    is(causes_fail({assert  => {pass      => 0}}), 1, "non-passign assert causes failure");
61
62    is(causes_fail({assert => {pass => 0}, amnesty => [{}]}), 0, "amnesty prevents assertion failure");
63
64    is(causes_fail({}), 0, "Default is no failure");
65};
66
67tests diagnostics => sub {
68    is(diagnostics({}), 0, "Default is no");
69
70    is(diagnostics({errors => [{}]}), 1, "Errors mean diagnostics");
71    is(diagnostics({info   => [{}]}), 0, "Info alone does not make diagnostics");
72
73    is(diagnostics({info => [{debug => 1}]}), 1, "Debug flag makes info diagnostics");
74};
75
76tests global => sub {
77    is(global({}), 0, "not global by default");
78    is(global({control => {global => 0}}), 0, "global not set");
79    is(global({control => {global => 1}}), 1, "global is set");
80};
81
82tests increments_count => sub {
83    is(increments_count({}), 0, "No count bump without an assertion");
84    is(increments_count({assert => {}}), 1, "count bump with assertion");
85};
86
87tests no_display => sub {
88    is(no_display({}), 0, "default is no");
89    is(no_display({about => {no_display => 0}}), 0, "set to off");
90    is(no_display({about => {no_display => 1}}), 1, "set to on");
91};
92
93tests subtest_id => sub {
94    is(subtest_id({}), undef, "none by default");
95    is(subtest_id({parent => {hid => 123}}), 123, "use parent hid when present");
96};
97
98tests summary => sub {
99    is(summary({}), '', "no summary without about->details");
100    is(summary({about => {details => 'foo'}}), 'foo', "got about->details");
101};
102
103tests terminate => sub {
104    is(terminate({}), undef, "undef by default");
105    is(terminate({control => {terminate => undef}}), undef, "undef by choice");
106    is(terminate({control => {terminate => 100}}), 100, "got the terminate value");
107    is(terminate({control => {terminate => 0}}), 0, "0 is passed through");
108};
109
110tests sets_plan => sub {
111    is_deeply( [sets_plan({})], [], "No plan by default");
112
113    is_deeply(
114        [sets_plan({plan => {}})],
115        [0],
116        "Empty plan means count of 0, nothing extra"
117    );
118
119    is_deeply(
120        [sets_plan({plan => {count => 100}})],
121        [100],
122        "Got simple count"
123    );
124
125    is_deeply(
126        [sets_plan({plan => {count => 0, none => 1}})],
127        [0, 'NO PLAN'],
128        "No Plan"
129    );
130
131    is_deeply(
132        [sets_plan({plan => {count => 0, skip => 1}})],
133        [0, 'SKIP'],
134        "Skip"
135    );
136
137    is_deeply(
138        [sets_plan({plan => {count => 0, skip => 1, details => 'foo bar'}})],
139        [0, 'SKIP', 'foo bar'],
140        "Skip with reason"
141    );
142};
143
144done_testing;
145