Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow passing in context information to assert methods #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/Specio/Constraint/Role/Interface.pm
Original file line number Diff line number Diff line change
Expand Up @@ -133,13 +133,15 @@ sub has_coercions { scalar keys %{ $_[0]->{_coercions} } }
sub validate_or_die {
my $self = shift;
my $value = shift;
my $context = shift;

return if $self->value_is_valid($value);

Specio::Exception->throw(
message => $self->_message_generator->($value),
type => $self,
value => $value,
($context ? (context => $context) : ()),
);
}

Expand Down Expand Up @@ -381,13 +383,17 @@ sub inline_coercion_and_check {

sub inline_assert {
my $self = shift;
my $context = shift;

my $type_var_name = '$_Specio_Constraint_Interface_type' . $counter;
my $message_generator_var_name
= '$_Specio_Constraint_Interface_message_generator' . $counter;
my $context_var_name
= '$_Specio_Constraint_Interface_context' . $counter;
my %env = (
$type_var_name => \$self,
$message_generator_var_name => \( $self->_message_generator ),
($context ? ($context_var_name => \$context) : ()),
%{ $self->inline_environment },
);

Expand All @@ -396,7 +402,8 @@ sub inline_coercion_and_check {
$source .= $self->_inline_throw_exception(
$_[0],
$message_generator_var_name,
$type_var_name
$type_var_name,
($context ? $context_var_name : ()),
);
$source .= ';';

Expand Down Expand Up @@ -445,12 +452,15 @@ sub _inline_throw_exception {
my $value_var = shift;
my $message_generator_var_name = shift;
my $type_var_name = shift;
my $context_var_name = shift;

#<<<
return 'Specio::Exception->throw( '
. ' message => ' . $message_generator_var_name . '->(' . $value_var . '),'
. ' type => ' . $type_var_name . ','
. ' value => ' . $value_var . ' )';
. ' value => ' . $value_var . ','
. ($context_var_name ? ' context => ' . $context_var_name . ',' : '')
. ')';
#>>>
}

Expand Down
9 changes: 9 additions & 0 deletions lib/Specio/Exception.pm
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ use Specio::OO;
stack_trace => {
init_arg => undef,
},
context => {
isa => 'HashRef',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if this really needs to be a HashRef. Is there any reason not to allow any defined value? I guess this would make including it in the as_string output harder, but as it stands this output won't be very helpful if the hashref contains any references of its own.

},
};

## no critic (Subroutines::ProhibitUnusedPrivateSubroutines)
Expand All @@ -50,6 +53,12 @@ sub as_string {
my $self = shift;

my $str = $self->message;
if ($self->context and my %context = %{$self->context}) {
$str
.= ' ('
. join(', ', map { "$_: $context{$_}" } sort keys %context)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could just use Specio::PartialDump here, which would make even more sense if this wasn't required to be a hashref.

. ')';
}
$str .= "\n\n" . $self->stack_trace->as_string;

return $str;
Expand Down
10 changes: 10 additions & 0 deletions t/exception.t
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ use Specio::Library::Builtins;
qr/Validation failed for type named Str .+ with value \[\s*\]/,
'exception contains expected error'
);

$e = exception {
$str->validate_or_die( [], { attribute => 'foo' } );
};

like(
$e->as_string,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It'd be good to also test that $e->context contains what we expect.

qr/Validation failed for type named Str .+ with value \[\s*\] \(attribute: foo\)/,
'exception contains expected error'
);
}

done_testing();