App-MaMGal
view release on metacpan or search on metacpan
lib/App/MaMGal/Exceptions.pm view on Meta::CPAN
package App::MaMGal::SystemException;
use strict;
use warnings;
use Carp;
sub _initialize
{
my $self = shift;
$self->SUPER::_initialize(@_);
croak "This exception requires a message argument" unless $self->message;
# zero-width negative look-ahead assertion: a percent not followed by percent
my $placeholder_count = () = $self->message =~ /%(?!%)/g;
my $object_count = $self->objects ? scalar @{$self->objects} : 0;
croak "Message with $placeholder_count placeholders must be followed by this many arguments, not $object_count" unless $placeholder_count == $object_count;
}
sub interpolated_message
{
my $self = shift;
my @args = $self->objects ? @{$self->objects} : ();
sprintf($self->message, @args);
t/010_unit_system_exception.t view on Meta::CPAN
$e
}
my $e;
$e = exception_instantiation_ok('exception creation succeeds with unnamed argument and no placeholders', 'boom', 'boom');
is($e->interpolated_message, 'boom');
dies_ok(sub { App::MaMGal::SystemException->new('boom %s boom') }, 'exception creation dies with unnamed argument containing a placeholder');
dies_ok(sub { App::MaMGal::SystemException->new(message => 'boom %s %s boom', objects => [qw(asdf)]) }, 'exception creation dies with message containing more placeholders than there are objects');
dies_ok(sub { App::MaMGal::SystemException->new(message => 'boom %s boom', objects => [qw(asdf ghjk)]) }, 'exception creation dies with message containing less placeholders than there are objects');
dies_ok(sub { App::MaMGal::SystemException->new(message => 'boom %% boom', objects => [qw(asdf ghjk)]) }, 'exception creation dies with message containing a single quoted percent character and two objects passed');
$e = exception_instantiation_ok('exception creation succeeds with unnamed argument and no placeholders', 'bim %s bom', message => 'bim %s bom', objects => ['bam']);
is($e->interpolated_message, 'bim bam bom');
( run in 0.372 second using v1.01-cache-2.11-cpan-05162d3a2b1 )