Skip to content

Commit

Permalink
Add methods for Exception test, throw_ok, throw and catch
Browse files Browse the repository at this point in the history
  • Loading branch information
bayashi committed Feb 23, 2020
1 parent 86d3a09 commit e6fdcde
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.pod
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ This is Perl module B<Test::Arrow>. It's Object-Oriented testing library.
# matches '(?^:b)'
# matched at line: 1, offset: 2

$arr->throw(sub { die 'Baz' })->catch(qr/^Ba/);


=head1 INSTALLATION

Expand Down
75 changes: 75 additions & 0 deletions lib/Test/Arrow.pm
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,54 @@ sub _get_isa_diag_name {
return($diag, $name);
}

sub throw_ok {
my $self = shift;

eval { shift->() };

$KLASS->builder->ok(!!$@, $self->_specific('_name', $_[0]));
}

sub throw {
my $self = shift;
my $code = shift;

die 'The `throw` method expects code ref.' unless ref $code eq 'CODE';

eval { $code->() };

if (my $e = $@) {
if (defined $_[0]) {
$KLASS->builder->like($e, $_[0], $_[1] || 'Thrown correctly');
$self->_reset;
}
else {
$self->got($e);
}
}
else {
local $Test::Builder::Level = 2;
$self->fail('Not thrown');
}

$self;
}

sub catch {
my $self = shift;
my $regex = shift;

$KLASS->builder->like(
$self->_specific('_got', undef),
$regex,
$_[0] || 'Thrown correctly',
);

$self->_reset;

$self;
}

1;

__END__
Expand Down Expand Up @@ -376,6 +424,8 @@ Test::Arrow - Object-Oriented testing library
# matches '(?^:b)'
# matched at line: 1, offset: 2
$arr->throw(sub { die 'Baz' })->catch(qr/^Ba/);
=head1 DESCRIPTION
Expand Down Expand Up @@ -505,6 +555,31 @@ It works on references, too:
$arr->got($array_ref)->expected('ARRAY')->isa_ok;
=head2 EXCEPTION TEST
=head3 throw_ok($code_ref)
It makes sure that $code_ref gets an exception.
$arr->throw_ok(sub { die 'oops' });
=head3 throw($code_ref)
=head3 catch($regex)
The C<throw> method invokes $code_ref, and if it's certenly thrown an exception, then an exception message will be set as $got and the $regex in C<catch> method will be evaluated to $got.
$arr->throw(sub { die 'Baz' })->catch(qr/^Ba/);
Above test is equivalent to below
$arr->throw(sub { die 'Baz' })->expected(qr/^Ba/)->like;
Actually, you can execute a test even only C<throw> method
$arr->throw(sub { die 'Baz' }, qr/^Ba/);
=head2 UTILITIES
You can call below utilities methods even without an instance.
Expand Down
18 changes: 18 additions & 0 deletions t/06_throw.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use strict;
use warnings;

use Test::Arrow;

my $arr = Test::Arrow->new;

$arr->throw_ok(sub { die 'foo' });
$arr->throw_ok(sub { die 'bar' }, 'die bar');
$arr->name('die baz')->throw_ok(sub { die 'baz' });
$arr->throw(sub { die 'bar' })->catch(qr/^ba/);
$arr->name('die bar')->throw(sub { die 'bar' })->catch(qr/^ba/);
$arr->throw(sub { die 'baz' })->catch(qr/^ba/, 'die baz');
$arr->throw(sub { die 'bar' })->expect(qr/^ba/)->like;
$arr->throw(sub { die 'bar' }, qr/^ba/);
$arr->throw(sub { die 'bar' }, qr/^ba/, 'die bar');

$arr->done_testing;

0 comments on commit e6fdcde

Please sign in to comment.