Skip to content

Commit

Permalink
Implement fails-like routine in Test.pm6
Browse files Browse the repository at this point in the history
Same as throws-like, except checks the code returns an armed Failure.

Failures are a ubiquitous feature of the language, yet there's no
convenient way to test for them:

- isa-ok: only tests for type, not the exception contained within,
    and doesn't mark Failures as handled causing warnings
    when those Failures are GCed
- throws-ok: explodes Failures, making it impossible to differentiate
    between code that throws and code that fails (we already had a bug
    in core that wasn't caught by roast for this reason)
- fails-like helper routine is already used extensively in roast,
    showing there is real need for this routine
  • Loading branch information
zoffixznet committed Mar 28, 2018
1 parent 29b7f46 commit 4abfd4c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
19 changes: 19 additions & 0 deletions lib/Test.pm6
Expand Up @@ -628,6 +628,25 @@ sub throws-like($code, $ex_type, $reason?, *%matcher) is export {
}, $reason // "did we throws-like $ex_type.^name()?";
}

sub fails-like (
\test where Callable:D|Str:D, $ex-type, $reason?, *%matcher
) is export {
subtest sub {
plan 2;
CATCH { default {
with "expected code to fail but it threw {.^name} instead" {
.&flunk;
.&skip;
return False;
}
}}
my $res = test ~~ Callable ?? test.() !! test.EVAL;
isa-ok $res, Failure, 'code returned a Failure';
throws-like { $res.sink }, $ex-type,
'Failure threw when sunk', |%matcher,
}, $reason // "did we fails-like $ex-type.^name()?"
}

sub _is_deeply(Mu $got, Mu $expected) {
$got eqv $expected;
}
Expand Down
1 change: 1 addition & 0 deletions t/spectest.data
Expand Up @@ -919,6 +919,7 @@ S24-testing/11-plan-skip-all-subtests.t
S24-testing/12-subtest-todo.t
S24-testing/13-cmp-ok.t
S24-testing/14-like-unlike.t
S24-testing/fails-like.t
S24-testing/line-numbers.t
S26-documentation/01-delimited.t
S26-documentation/02-paragraph.t
Expand Down

0 comments on commit 4abfd4c

Please sign in to comment.