Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ->terminate and ->finalize methods to the Formatter
By default, these do nothing, but they're necessary to implement any sort of document-oriented format. Otherwise there's no chance for the formatter to "close" the output. For example, if you're generating XML you need a chance to close all open tags.
- Loading branch information
Showing
5 changed files
with
129 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
use strict; | ||
use warnings; | ||
|
||
BEGIN { require "t/tools.pl" } | ||
|
||
use Test2::API qw/intercept run_subtest test2_stack/; | ||
use Test2::Event::Bail; | ||
|
||
{ | ||
|
||
package Formatter::Subclass; | ||
use parent 'Test2::Formatter'; | ||
use Test2::Util::HashBase qw{f t}; | ||
|
||
sub init { | ||
my $self = shift; | ||
$self->{+F} = []; | ||
$self->{+T} = []; | ||
} | ||
|
||
sub write { } | ||
sub hide_buffered { 1 } | ||
|
||
sub terminate { | ||
my $s = shift; | ||
push @{$s->{+T}}, [@_]; | ||
} | ||
|
||
sub finalize { | ||
my $s = shift; | ||
push @{$s->{+F}}, [@_]; | ||
} | ||
} | ||
|
||
{ | ||
my $f = Formatter::Subclass->new; | ||
intercept { | ||
my $hub = test2_stack->top; | ||
$hub->format($f); | ||
is(1, 1, 'test event 1'); | ||
is(2, 2, 'test event 2'); | ||
is(3, 2, 'test event 3'); | ||
done_testing; | ||
}; | ||
|
||
is(scalar @{$f->f}, 1, 'finalize method was called on formatter'); | ||
is_deeply( | ||
$f->f->[0], | ||
[3, 3, 1, 0], | ||
'finalize method received expected arguments' | ||
); | ||
|
||
ok(!@{$f->t}, 'terminate method was not called on formatter'); | ||
} | ||
|
||
{ | ||
my $f = Formatter::Subclass->new; | ||
|
||
intercept { | ||
my $hub = test2_stack->top; | ||
$hub->format($f); | ||
$hub->send(Test2::Event::Bail->new(reason => 'everything is terrible')); | ||
done_testing; | ||
}; | ||
|
||
is(scalar @{$f->t}, 1, 'terminate method was called because of bail event'); | ||
ok(!@{$f->f}, 'finalize method was not called on formatter'); | ||
} | ||
|
||
done_testing; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters