| 1 | package Test::Proto::Formatter::TestBuilder; | ||||||||||||||
| 2 | 13 | ▶ | use 5.008; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 3 | 13 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 13 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 13 | ▶ | use Moo; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 13 | extends 'Test::Builder::Module'; | |||||||||||||
| 7 | 13 | my $CLASS = __PACKAGE__; | |||||||||||||
| 8 | |||||||||||||||
| 9 | =pod | ||||||||||||||
| 10 | |||||||||||||||
| 11 | =head1 NAME | ||||||||||||||
| 12 | |||||||||||||||
| 13 | Test::Proto::Formatter::TestBuilder - formats RunnerEvents as TestBuilder events. | ||||||||||||||
| 14 | |||||||||||||||
| 15 | =head1 SYNOPSIS | ||||||||||||||
| 16 | |||||||||||||||
| 17 | my $formatter = Test::Proto::Formatter->new(); | ||||||||||||||
| 18 | $formatter->event($testRunner, 'new'); | ||||||||||||||
| 19 | $formatter->event($testRunner, 'done'); | ||||||||||||||
| 20 | |||||||||||||||
| 21 | This formatter is only used by the L<Test::Proto::TestRunner> class, and will be created when you use a prototype's C<ok>. | ||||||||||||||
| 22 | |||||||||||||||
| 23 | |||||||||||||||
| 24 | =head1 METHODS | ||||||||||||||
| 25 | |||||||||||||||
| 26 | =cut | ||||||||||||||
| 27 | |||||||||||||||
| 28 | =head3 event | ||||||||||||||
| 29 | |||||||||||||||
| 30 | $formatter->event($testRunner, 'new'); | ||||||||||||||
| 31 | $formatter->event($testRunner, 'done'); | ||||||||||||||
| 32 | |||||||||||||||
| 33 | Used in Test::Proto::TestRunner to inform the formatter of progress. Event types 'new' and 'done' are supported. | ||||||||||||||
| 34 | |||||||||||||||
| 35 | =cut | ||||||||||||||
| 36 | |||||||||||||||
| 37 | has '_object_id_register', | ||||||||||||||
| 38 | is => 'rw', | ||||||||||||||
| 39 | 3 | ▶ | default => sub { {} }; | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 40 | |||||||||||||||
| 41 | 66 | ▶ | sub _explain_value { | ||||||||||||
|
Subroutine
_explain_value: called CC=5 SCAR=16.2
| |||||||||||||||
| 42 | 66 | my $v = shift; | |||||||||||||
| 43 | 66 | ▶ | return 'undef' unless defined $v; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 44 | 66 | ▶ | return $v unless ref $v; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | 33 | ▶ | return 'Arrayref with ' . ( $#$v + 1 ) . ' values' if ref $v eq 'ARRAY'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | 13 | ▶ | return 'Hashref with ' . ( scalar keys %$v ) . ' keys' if ref $v eq 'HASH'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 47 | 13 | return ref $v; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | |||||||||||||||
| 50 | 110 | ▶ | sub _explain_test_case { | ||||||||||||
|
Subroutine
_explain_test_case: called CC=6 SCAR=17.9
| |||||||||||||||
| 51 | 110 | my $self = shift; | |||||||||||||
| 52 | 110 | my $test_case = shift; | |||||||||||||
| 53 | 110 | ▶ | if ( ref $test_case ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 54 | 106 | ▶ | if ( $test_case->isa('Test::Proto::TestCase') ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | 66 | my $report = ''; | |||||||||||||
| 56 | 66 | $report .= $test_case->name; | |||||||||||||
| 57 | 66 | ▶ | $report .= "\nexpected: " . _explain_value( $test_case->data->{expected} ) if defined( $test_case->data->{expected} ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 58 | 66 | ▶ | if ( scalar keys %{ $test_case->data } > 1 ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | 9 | $report .= "\nOther data:"; | |||||||||||||
| 60 | 9 | foreach my $key ( grep { 'expected' ne $_ } keys %{ $test_case->data } ) { | |||||||||||||
| 61 | 9 | $report .= "\n $key: " . $test_case->data->{$key}; | |||||||||||||
| 62 | } | ||||||||||||||
| 63 | } | ||||||||||||||
| 64 | 66 | return $report; | |||||||||||||
| 65 | } | ||||||||||||||
| 66 | 40 | return '[not a TestCase]'; | |||||||||||||
| 67 | } | ||||||||||||||
| 68 | else { | ||||||||||||||
| 69 | 4 | return '[not a TestCase or any other object]'; | |||||||||||||
| 70 | } | ||||||||||||||
| 71 | } | ||||||||||||||
| 72 | |||||||||||||||
| 73 | 220 | ▶ | sub event { | ||||||||||||
|
Subroutine
event: called CC=9 SCAR=22.2
Pod
documented
| |||||||||||||||
| 74 | 220 | my $self = shift; | |||||||||||||
| 75 | 220 | my $runner = shift; | |||||||||||||
| 76 | 220 | my $eventType = shift; | |||||||||||||
| 77 | 220 | ▶ | if ( 'new' eq $eventType ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 78 | 110 | ▶ | my $name = | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 79 | defined( $runner->test_case ) | ||||||||||||||
| 80 | ? $runner->test_case->can('name') | ||||||||||||||
| 81 | ? $runner->test_case->name | ||||||||||||||
| 82 | : ref $runner->test_case | ||||||||||||||
| 83 | : undef; | ||||||||||||||
| 84 | 110 | ▶ | if ( defined $runner->parent ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 85 | 107 | $self->_object_id_register->{ $runner->object_id } = $self->_object_id_register->{ $runner->parent->object_id }->child($name); | |||||||||||||
| 86 | } | ||||||||||||||
| 87 | else { | ||||||||||||||
| 88 | 3 | $self->_object_id_register->{ $runner->object_id } = $CLASS->builder->child($name); | |||||||||||||
| 89 | } | ||||||||||||||
| 90 | } | ||||||||||||||
| 91 | elsif ( 'done' eq $eventType ) { | ||||||||||||||
| 92 | 110 | ▶ | if ( my $tb = $self->_object_id_register->{ $runner->object_id } ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 93 | 110 | ▶ | $tb->ok( $runner, $runner->status . " - got: " . ( defined $runner->subject ? $runner->subject : '[undefined]' ) . "\n" . $self->_explain_test_case( $runner->test_case ) . ( defined $runner->status_message ? "\n" . $runner->status_message : '' ) ); | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 94 | 110 | $tb->done_testing; | |||||||||||||
| 95 | 110 | $tb->finalize; | |||||||||||||
| 96 | } | ||||||||||||||
| 97 | else { | ||||||||||||||
| 98 | 0 | die( 'Have not registered object ' . $runner->object_id ); | |||||||||||||
| 99 | } | ||||||||||||||
| 100 | } | ||||||||||||||
| 101 | 220 | return $self; | |||||||||||||
| 102 | } | ||||||||||||||
| 103 | |||||||||||||||
| 104 | =head3 format | ||||||||||||||
| 105 | |||||||||||||||
| 106 | $formatter->format($runner); | ||||||||||||||
| 107 | |||||||||||||||
| 108 | Outputs information from a test runner that is already complete but did not expect to be outputting to Test::Builder. | ||||||||||||||
| 109 | |||||||||||||||
| 110 | =cut | ||||||||||||||
| 111 | |||||||||||||||
| 112 | 1 | ▶ | sub format { | ||||||||||||
|
Subroutine
format: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 113 | 1 | my $self = shift; | |||||||||||||
| 114 | 1 | my $runner = shift; | |||||||||||||
| 115 | 1 | $self->event( $runner, 'new' ); | |||||||||||||
| 116 | 1 | $self->event( $runner, 'done' ); | |||||||||||||
| 117 | 1 | return $self; | |||||||||||||
| 118 | } | ||||||||||||||
| 119 | |||||||||||||||
| 120 | 13 | 1; | |||||||||||||
| 121 | |||||||||||||||
| 122 | =head1 OTHER INFORMATION | ||||||||||||||
| 123 | |||||||||||||||
| 124 | For author, version, bug reports, support, etc, please see L<Test::Proto>. | ||||||||||||||
| 125 | |||||||||||||||
| 126 | =cut | ||||||||||||||
| 127 | |||||||||||||||