Last active
June 17, 2020 13:26
-
-
Save j1n3l0/b4aeb86f6791afa805546da10a562ae1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use Test2::V0; | |
package Controller { | |
use 5.030; | |
use Moo; | |
use Type::Params qw< Invocant compile >; | |
use Type::Utils qw< declare as where >; | |
use Types::Standard qw< :types >; | |
my $Controller = declare as Object, | |
where { $_->param('jwt') && $_->param('client_ref_key') }; | |
has params => ( required => 1, is => 'ro', isa => HashRef ); | |
sub param { | |
state $check = compile( Invocant, Str ); | |
my ( $self, $param ) = $check->(@_); | |
$self->params->{$param}; | |
} | |
sub launch { | |
state $check = compile($Controller); | |
my ($self) = $check->(@_); | |
$self->params; | |
} | |
}; | |
subtest 'Defining a custom type for the context object' => sub { | |
my $good_params = { jwt => 'abcdef', client_ref_key => 12345 }; | |
ok( lives { Controller->new( params => $good_params )->launch }, | |
'should not raise any exceptions with good params', | |
) || diag $@; | |
my $bad_params = {}; | |
like( | |
dies { Controller->new( params => $bad_params )->launch }, | |
qr/did not pass type constraint/, | |
'should raise exception with bad params', | |
); | |
}; | |
done_testing; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment