1use strict;
2use warnings;
3
4use Test2::Tools::Tiny;
5
6use Test2::API qw/intercept run_subtest test2_stack/;
7use Test2::Event::Bail;
8
9{
10
11	package Formatter::Subclass;
12	use base 'Test2::Formatter';
13	use Test2::Util::HashBase qw{f t};
14
15    sub init {
16        my $self = shift;
17        $self->{+F} = [];
18        $self->{+T} = [];
19    }
20
21	sub write         { }
22	sub hide_buffered { 1 }
23
24	sub terminate {
25		my $s = shift;
26		push @{$s->{+T}}, [@_];
27	}
28
29	sub finalize {
30		my $s = shift;
31		push @{$s->{+F}}, [@_];
32	}
33}
34
35{
36	my $f = Formatter::Subclass->new;
37	intercept {
38		my $hub = test2_stack->top;
39		$hub->format($f);
40		is(1, 1, 'test event 1');
41		is(2, 2, 'test event 2');
42		is(3, 2, 'test event 3');
43		done_testing;
44	};
45
46	is(scalar @{$f->f}, 1, 'finalize method was called on formatter');
47	is_deeply(
48		$f->f->[0],
49		[3, 3, 1, 0, 0],
50		'finalize method received expected arguments'
51	);
52
53	ok(!@{$f->t}, 'terminate method was not called on formatter');
54}
55
56{
57	my $f = Formatter::Subclass->new;
58
59	intercept {
60		my $hub = test2_stack->top;
61		$hub->format($f);
62		$hub->send(Test2::Event::Bail->new(reason => 'everything is terrible'));
63		done_testing;
64	};
65
66	is(scalar @{$f->t}, 1, 'terminate method was called because of bail event');
67	ok(!@{$f->f}, 'finalize method was not called on formatter');
68}
69
70{
71	my $f = Formatter::Subclass->new;
72
73	intercept {
74		my $hub = test2_stack->top;
75		$hub->format($f);
76		$hub->send(Test2::Event::Plan->new(directive => 'skip_all', reason => 'Skipping all the tests'));
77		done_testing;
78	};
79
80	is(scalar @{$f->t}, 1, 'terminate method was called because of plan skip_all event');
81	ok(!@{$f->f}, 'finalize method was not called on formatter');
82}
83
84done_testing;
85