| 1 | package Test::Run::Trap::Obj; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 8 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 8 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | =head1 NAME | ||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | Test::Run::Trap::Obj - wrapper around Test::Trap for trapping errors. | ||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | =head1 SYNPOSIS | ||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | my $got = Test::Run::Trap::Obj->trap_run({ | ||||||||||||||||||||||||||||
| 13 | args => [test_files => ["t/sample-tests/simple"]] | ||||||||||||||||||||||||||||
| 14 | }); | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | $got->field_like("stdout", qr/All tests successful/, | ||||||||||||||||||||||||||||
| 17 | "Everything is OK." | ||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | This class implements a wrapper around L<Test::Trap>. When an | ||||||||||||||||||||||||||||
| 23 | assertion files, the diagnostics prints all the relevant and trapped | ||||||||||||||||||||||||||||
| 24 | fields for easy debugging. | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | =head1 METHODS | ||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | =cut | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 8 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 8 | extends('Test::Run::Base::Struct'); | |||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 8 | ▶ | use Test::More; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 36 | 8 | ▶ | use Data::Dumper (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||
| 38 | 8 | ▶ | use Text::Sprintf::Named; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | 8 | ▶ | use Test::Trap qw( trap $trap :flow:stderr(systemsafe):stdout(systemsafe):warn ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 8 | ▶ | use Test::Run::Obj; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | 8 | my @fields = qw( | |||||||||||||||||||||||||||
| 45 | die | ||||||||||||||||||||||||||||
| 46 | exit | ||||||||||||||||||||||||||||
| 47 | leaveby | ||||||||||||||||||||||||||||
| 48 | return | ||||||||||||||||||||||||||||
| 49 | stderr | ||||||||||||||||||||||||||||
| 50 | stdout | ||||||||||||||||||||||||||||
| 51 | wantarray | ||||||||||||||||||||||||||||
| 52 | warn | ||||||||||||||||||||||||||||
| 53 | run_func | ||||||||||||||||||||||||||||
| 54 | ); | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | 8 | has 'die' => (is => "rw", isa => "Any"); | |||||||||||||||||||||||||||
| 58 | 8 | has 'exit' => (is => "rw", isa => "Any"); | |||||||||||||||||||||||||||
| 59 | 8 | has 'leaveby' => (is => "rw", isa => "Str"); | |||||||||||||||||||||||||||
| 60 | 8 | has 'return' => (is => "rw", isa => "Any"); | |||||||||||||||||||||||||||
| 61 | 8 | has 'stderr' => (is => "rw", isa => "Str"); | |||||||||||||||||||||||||||
| 62 | 8 | has 'stdout' => (is => "rw", isa => "Str"); | |||||||||||||||||||||||||||
| 63 | 8 | has 'wantarray' => (is => "rw", isa => "Bool"); | |||||||||||||||||||||||||||
| 64 | 8 | has 'warn' => (is => "rw", isa => "Any"); | |||||||||||||||||||||||||||
| 65 | 8 | has 'run_func' => (is => "rw", isa => "CodeRef"); | |||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | sub _stringify_value | ||||||||||||||||||||||||||||
| 68 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
_stringify_value: not called CC=4 SCAR=30.0
| |||||||||||||||||||||||||||||
| 69 | 0 | my ($self, $name) = @_; | |||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||
| 71 | 0 | my $value = $self->$name(); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if (($name eq "return") || ($name eq "warn")) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$name eq "return" or $name eq "warn"
MC/DC0%0/2
$name eq "return" or $name eq "warn"
$name eq "return"
$name eq "warn"
Truth table0%0/3
A or B
A= $name eq "return"B= $name eq "warn"
| |||||||||||||||||||||||||||||
| 74 | { | ||||||||||||||||||||||||||||
| 75 | 0 | return Data::Dumper->new([$value])->Dump(); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | else | ||||||||||||||||||||||||||||
| 78 | { | ||||||||||||||||||||||||||||
| 79 | 0 | ▶ | return (defined($value) ? $value : ""); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | =head2 $trapper->diag_all() | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | Calls L<Test::More>'s diag() with all the trapped fields, like stdout, | ||||||||||||||||||||||||||||
| 86 | stderr, etc. | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | =cut | ||||||||||||||||||||||||||||
| 89 | |||||||||||||||||||||||||||||
| 90 | sub diag_all | ||||||||||||||||||||||||||||
| 91 | 0 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
diag_all: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 92 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | diag( | ||||||||||||||||||||||||||||
| 95 | Text::Sprintf::Named->new( | ||||||||||||||||||||||||||||
| 96 | { | ||||||||||||||||||||||||||||
| 97 | fmt => | ||||||||||||||||||||||||||||
| 98 | join( "", | ||||||||||||||||||||||||||||
| 99 | 0 | map { "$_ ===\n{{{{{{\n%($_)s\n}}}}}}\n\n" } | |||||||||||||||||||||||||||
| 100 | (@fields)) | ||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 0 | )->format({args => { map { my $name = $_; | |||||||||||||||||||||||||||
| 103 | 0 | ($name => $self->_stringify_value($name)) } | |||||||||||||||||||||||||||
| 104 | @fields | ||||||||||||||||||||||||||||
| 105 | }}) | ||||||||||||||||||||||||||||
| 106 | ); | ||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | =head2 $trapper->field_like($what, $regex, $message) | ||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||
| 111 | A wrapper for L<Test::More>'s like(), that also emits more diagnostics | ||||||||||||||||||||||||||||
| 112 | on failure. | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | =cut | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | sub field_like | ||||||||||||||||||||||||||||
| 117 | 25 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
field_like: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||
| 118 | 25 | local $Test::Builder::Level = $Test::Builder::Level + 1; | |||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | 25 | my $self = shift; | |||||||||||||||||||||||||||
| 121 | 25 | my ($what, $regex, $name) = @_; | |||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||
| 123 | 25 | ▶ | if (! Test::More::like($self->$what(), $regex, $name)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | { | ||||||||||||||||||||||||||||
| 125 | 0 | $self->diag_all(); | |||||||||||||||||||||||||||
| 126 | } | ||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | =head2 $trapper->field_unlike($what, $regex, $msg) | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | A wrapper for unlike(). | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | =cut | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | sub field_unlike | ||||||||||||||||||||||||||||
| 136 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
field_unlike: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||
| 137 | 1 | local $Test::Builder::Level = $Test::Builder::Level + 1; | |||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 140 | 1 | my ($what, $regex, $name) = @_; | |||||||||||||||||||||||||||
| 141 | |||||||||||||||||||||||||||||
| 142 | 1 | ▶ | if (! Test::More::unlike($self->$what(), $regex, $name)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 143 | { | ||||||||||||||||||||||||||||
| 144 | 0 | $self->diag_all(); | |||||||||||||||||||||||||||
| 145 | } | ||||||||||||||||||||||||||||
| 146 | } | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | =head2 $trapper->field_is($what, $expected, $msg) | ||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||
| 150 | A wrapper for is(). | ||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | =cut | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | sub field_is | ||||||||||||||||||||||||||||
| 155 | 4 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
field_is: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||
| 156 | 4 | local $Test::Builder::Level = $Test::Builder::Level + 1; | |||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 159 | 4 | my ($what, $expected, $name) = @_; | |||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | 4 | ▶ | if (! Test::More::is($self->$what(), $expected, $name)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 162 | { | ||||||||||||||||||||||||||||
| 163 | 0 | $self->diag_all(); | |||||||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||
| 166 | |||||||||||||||||||||||||||||
| 167 | =head2 $trapper->field_is_deeply($what, $expected, $msg) | ||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | A wrapper for is_deeply(). | ||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||
| 171 | =cut | ||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | sub field_is_deeply | ||||||||||||||||||||||||||||
| 174 | 1 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
field_is_deeply: called CC=2 SCAR=7.4
Pod
documented
| |||||||||||||||||||||||||||||
| 175 | 1 | local $Test::Builder::Level = $Test::Builder::Level + 1; | |||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||
| 177 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 178 | 1 | my ($what, $expected, $name) = @_; | |||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | 1 | ▶ | if (! Test::More::is_deeply($self->$what(), $expected, $name)) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 181 | { | ||||||||||||||||||||||||||||
| 182 | 0 | $self->diag_all(); | |||||||||||||||||||||||||||
| 183 | } | ||||||||||||||||||||||||||||
| 184 | } | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | =head2 my $got = Test::Run::Trap::Obj->trap_run({class => $class, args => \@args, run_func => $func}) | ||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||
| 189 | Runs C<$class->$func()> with the arguments @args placed into a hash-ref, | ||||||||||||||||||||||||||||
| 190 | traps the results and returns a results object. | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =cut | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | sub trap_run | ||||||||||||||||||||||||||||
| 195 | 25 | ▶ | { | ||||||||||||||||||||||||||
|
Subroutine
trap_run: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 196 | 25 | my ($class, $args) = @_; | |||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | 25 | ▶ | my $test_run_class = $args->{class} || "Test::Run::Obj"; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args->{'class'} || 'Test::Run::Obj'
MC/DC100%1/1
$args->{'class'} || 'Test::Run::Obj'
$args->{'class'}
Truth table100%2/2
A
A= $args->{'class'}
| |||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 25 | my $test_run_args = $args->{args}; | |||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | 25 | ▶ | my $run_func = $args->{run_func} || "runtests"; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args->{'run_func'} || 'runtests'
MC/DC100%1/1
$args->{'run_func'} || 'runtests'
$args->{'run_func'}
Truth table100%2/2
A
A= $args->{'run_func'}
| |||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | my $tester = $test_run_class->new( | ||||||||||||||||||||||||||||
| 205 | 25 | {@{$test_run_args}}, | |||||||||||||||||||||||||||
| 206 | ); | ||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 25 | ▶ | trap { $tester->$run_func(); }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||
| 210 | return $class->new({ | ||||||||||||||||||||||||||||
| 211 | 25 | ( map { $_ => $trap->$_() } | |||||||||||||||||||||||||||
| 212 | (qw(stdout stderr die leaveby exit return warn wantarray))) | ||||||||||||||||||||||||||||
| 213 | }); | ||||||||||||||||||||||||||||
| 214 | } | ||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | 8 | 1; | |||||||||||||||||||||||||||
| 217 | |||||||||||||||||||||||||||||
| 218 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 219 | |||||||||||||||||||||||||||||
| 220 | Shlomi Fish, L<http://www.shlomifish.org/>. | ||||||||||||||||||||||||||||
| 221 | |||||||||||||||||||||||||||||
| 222 | =head1 LICENSE | ||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||
| 224 | This file is licensed under the MIT X11 License: | ||||||||||||||||||||||||||||
| 225 | |||||||||||||||||||||||||||||
| 226 | http://www.opensource.org/licenses/mit-license.php | ||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 229 | |||||||||||||||||||||||||||||
| 230 | L<Test::Trap> , L<Test::More> . | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | =cut | ||||||||||||||||||||||||||||