1use strict;
2use warnings;
3
4use Test2::Tools::Tiny;
5use Test2::Event::Plan;
6use Test2::EventFacet::Trace;
7
8my $plan = Test2::Event::Plan->new(
9    trace => Test2::EventFacet::Trace->new(frame => [__PACKAGE__, __FILE__, __LINE__]),
10    max => 100,
11);
12
13is($plan->summary, "Plan is 100 assertions", "simple summary");
14is_deeply( [$plan->sets_plan], [100, '', undef], "Got plan details");
15
16ok(!$plan->global, "regular plan is not a global event");
17is($plan->terminate, undef, "No terminate for normal plan");
18
19$plan->set_max(0);
20$plan->set_directive('SKIP');
21$plan->set_reason('foo');
22is($plan->terminate, 0, "Terminate 0 on skip_all");
23
24is($plan->summary, "Plan is 'SKIP', foo", "skip summary");
25is_deeply( [$plan->sets_plan], [0, 'SKIP', 'foo'], "Got skip details");
26
27$plan->set_max(0);
28$plan->set_directive('NO PLAN');
29$plan->set_reason(undef);
30is($plan->summary, "Plan is 'NO PLAN'", "NO PLAN summary");
31is_deeply( [$plan->sets_plan], [0, 'NO PLAN', undef], "Got 'NO PLAN' details");
32is($plan->terminate, undef, "No terminate for no_plan");
33$plan->set_max(100);
34$plan->set_directive(undef);
35
36$plan = Test2::Event::Plan->new(
37    trace => Test2::EventFacet::Trace->new(frame => [__PACKAGE__, __FILE__, __LINE__]),
38    max => 0,
39    directive => 'skip_all',
40);
41is($plan->directive, 'SKIP', "Change skip_all to SKIP");
42
43$plan = Test2::Event::Plan->new(
44    trace => Test2::EventFacet::Trace->new(frame => [__PACKAGE__, __FILE__, __LINE__]),
45    max => 0,
46    directive => 'no_plan',
47);
48is($plan->directive, 'NO PLAN', "Change no_plan to 'NO PLAN'");
49ok(!$plan->global, "NO PLAN is not global");
50
51like(
52    exception {
53        $plan = Test2::Event::Plan->new(
54            trace     => Test2::EventFacet::Trace->new(frame => [__PACKAGE__, __FILE__, __LINE__]),
55            max       => 0,
56            directive => 'foo',
57        );
58    },
59    qr/'foo' is not a valid plan directive/,
60    "Invalid Directive"
61);
62
63like(
64    exception {
65        $plan = Test2::Event::Plan->new(
66            trace  => Test2::EventFacet::Trace->new(frame => [__PACKAGE__, __FILE__, __LINE__]),
67            max    => 0,
68            reason => 'foo',
69        );
70    },
71    qr/Cannot have a reason without a directive!/,
72    "Reason without directive"
73);
74
75like(
76    exception {
77        $plan = Test2::Event::Plan->new(
78            trace  => Test2::EventFacet::Trace->new(frame => [__PACKAGE__, __FILE__, __LINE__]),
79        );
80    },
81    qr/No number of tests specified/,
82    "Nothing to do"
83);
84
85like(
86    exception {
87        $plan = Test2::Event::Plan->new(
88            trace  => Test2::EventFacet::Trace->new(frame => [__PACKAGE__, __FILE__, __LINE__]),
89            max => 'skip',
90        );
91    },
92    qr/Plan test count 'skip' does not appear to be a valid positive integer/,
93    "Max must be an integer"
94);
95
96$plan = Test2::Event::Plan->new(
97    trace => Test2::EventFacet::Trace->new(frame => [__PACKAGE__, __FILE__, __LINE__]),
98    max => 100,
99);
100
101my $facet_data = $plan->facet_data;
102ok($facet_data->{about}, "Got common facet data");
103is($facet_data->{control}->{terminate}, undef, "no termination defined");
104is_deeply(
105    $facet_data->{plan},
106    {count => 100},
107    "Set the count"
108);
109
110$plan = Test2::Event::Plan->new(
111    trace => Test2::EventFacet::Trace->new(frame => [__PACKAGE__, __FILE__, __LINE__]),
112    max => 0,
113    directive => 'NO PLAN',
114);
115
116$facet_data = $plan->facet_data;
117ok($facet_data->{about}, "Got common facet data");
118is($facet_data->{control}->{terminate}, undef, "no termination defined");
119is_deeply(
120    $facet_data->{plan},
121    {count => 0, none => 1},
122    "No plan"
123);
124
125$plan = Test2::Event::Plan->new(
126    trace => Test2::EventFacet::Trace->new(frame => [__PACKAGE__, __FILE__, __LINE__]),
127    max => 0,
128    directive => 'SKIP',
129);
130
131$facet_data = $plan->facet_data;
132ok($facet_data->{about}, "Got common facet data");
133is($facet_data->{control}->{terminate}, 0, "terminate with 0");
134is_deeply(
135    $facet_data->{plan},
136    {count => 0, skip => 1},
137    "Skip, no reason"
138);
139
140$plan = Test2::Event::Plan->new(
141    trace => Test2::EventFacet::Trace->new(frame => [__PACKAGE__, __FILE__, __LINE__]),
142    max => 0,
143    directive => 'SKIP',
144    reason => 'because',
145);
146
147$facet_data = $plan->facet_data;
148ok($facet_data->{about}, "Got common facet data");
149is($facet_data->{control}->{terminate}, 0, "terminate with 0");
150is_deeply(
151    $facet_data->{plan},
152    {count => 0, skip => 1, details => 'because'},
153    "Skip, no reason"
154);
155
156done_testing;
157