diff options
-rwxr-xr-x | t/Test/Execute.pm | 64 |
1 files changed, 48 insertions, 16 deletions
diff --git a/t/Test/Execute.pm b/t/Test/Execute.pm index 0d4798f..ab3fb11 100755 --- a/t/Test/Execute.pm +++ b/t/Test/Execute.pm @@ -4,7 +4,12 @@ use 5.16.0; use strict; use warnings FATAL => 'all'; -use Test::More; +use Test2::API qw/ context release run_subtest no_context /; +use Test2::Compare qw/ compare /; +use Test2::Compare::Number; +use Test2::Compare::Pattern; +use Test2::Compare::String; + use Capture::Tiny qw/ capture_merged /; use Exporter 'import'; use Carp 'croak'; @@ -31,8 +36,9 @@ sub run { @_ ); + my $ctx = $args{ctx} // context(); my @cmd = @{ $args{cmd} }; - return undef unless @cmd; # no command to run + return release($ctx, undef) unless @cmd; # no command to run my ($exit, $input, $test, $expected, $name, $note) = @args{qw/ exit input test expected name note /}; @@ -51,44 +57,69 @@ sub run { }; if ($note) { - note sprintf "Exit value: %s", $return // '<undef>'; - note "Output: $output"; + $ctx->note(sprintf "Exit value: %s", $return // '<undef>'); + $ctx->note("Output: $output"); } if (not $test) { if (defined $expected and ref $expected eq 'Regexp') { + $ctx->release(); return $output =~ $expected; } elsif (defined $expected and ref $expected eq 'CODE') { + $ctx->release(); local $_ = $output; return $expected->($output); } - return $return; + return release($ctx, $return); } $name //= "Testing run of @cmd"; - local $Test::Builder::Level = $Test::Builder::Level + 2; - subtest $name => sub { - plan tests => 2; + run_subtest($name => sub { no_context { + my $sub = context(); + $sub->plan(2); # 1: Test exit value if (not defined $exit) { - SKIP: { skip "Expected exit value undefined", 1 } + $sub->skip("$name - exit value", "Expected exit value undefined"); } else { - is ($return, $exit, "$name - exit value"); + my $delta = compare($return, $exit, sub { Test2::Compare::Number->new(input => shift()); }); + if ($delta) { + $sub->ok(0, "$name - exit value", [$delta->table]); + } else { + $sub->ok(1, "$name - exit value"); + } } # 2: Test output if (not defined $expected) { - SKIP: { skip "Expected output undefined", 1 } + $sub->skip("$name - output", "Expected output undefined"); } elsif (ref $expected eq 'Regexp') { - like ($output, $expected, "$name - output"); + my $delta = compare($output, $expected, sub { Test2::Compare::Pattern->new(pattern => shift(), stringify_got => 1); }); + if ($delta) { + $sub->ok(0, "$name - output", [$delta->table]); + } else { + $sub->ok(1, "$name - output"); + } } elsif (ref $expected eq 'CODE') { local $_ = $output; - ok ($expected->($output), "$name - output") or note "Output: $output"; + my $delta = ! $expected->($output); + if ($delta) { + $sub->ok(0, "$name - output", [ "Output: $output" ]); + } else { + $sub->ok(1, "$name - output"); + } } else { - is ($output, $expected, "$name - output"); + my $delta = compare($output, $expected, sub { Test2::Compare::String->new(input => shift()); }); + if ($delta) { + $sub->ok(0, "$name - output", [$delta->table]); + } else { + $sub->ok(1, "$name - output"); + } } - }; + + $sub->release(); + }}, 1); + $ctx->release(); } @@ -112,7 +143,8 @@ sub script { @cmd = ($^X, @lib, "$path$cmd", @cmd); $args{cmd} = \@cmd; - return run(%args); + my $ctx = context(); + return run(%args, ctx => $ctx); } 1; |