-
Notifications
You must be signed in to change notification settings - Fork 8
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -29,6 +29,9 @@ use Specio::OO; | |
stack_trace => { | ||
init_arg => undef, | ||
}, | ||
context => { | ||
isa => 'HashRef', | ||
}, | ||
}; | ||
|
||
## no critic (Subroutines::ProhibitUnusedPrivateSubroutines) | ||
|
@@ -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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could just use |
||
. ')'; | ||
} | ||
$str .= "\n\n" . $self->stack_trace->as_string; | ||
|
||
return $str; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It'd be good to also test that |
||
qr/Validation failed for type named Str .+ with value \[\s*\] \(attribute: foo\)/, | ||
'exception contains expected error' | ||
); | ||
} | ||
|
||
done_testing(); |
There was a problem hiding this comment.
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.