Skip to content

Instantly share code, notes, and snippets.

@j1n3l0
Last active June 17, 2020 13:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save j1n3l0/b4aeb86f6791afa805546da10a562ae1 to your computer and use it in GitHub Desktop.
Save j1n3l0/b4aeb86f6791afa805546da10a562ae1 to your computer and use it in GitHub Desktop.
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