Skip to content

Instantly share code, notes, and snippets.

@j1n3l0
Last active March 23, 2020 18:00
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/49a00a5e2d6e2f0ce078521fee998cc3 to your computer and use it in GitHub Desktop.
Save j1n3l0/49a00a5e2d6e2f0ce078521fee998cc3 to your computer and use it in GitHub Desktop.
use Test2::V0;
use MooseX::Test::Role;
package Temporal {
use Moo::Role;
use Types::Standard ':all';
requires 'created_at';
around created_at => sub {
my ( $orig, $self, @args ) = @_;
my $value = $self->$orig(@args);
if ( my $error = Int->validate($value) ) {
die $error;
}
return $value;
};
};
requires_ok 'Temporal', 'created_at';
subtest 'role requires method and validates the return type' => sub {
like(
dies {
consuming_object(
Temporal => methods => {
created_at => sub {'foo'}
}
)->created_at
},
qr/Value .+ did not pass type constraint .+ at/,
'should die if required method returns the wrong type',
);
ok( lives {
consuming_object(
Temporal => methods => {
created_at => sub {12345}
}
)->created_at
},
'should live if required method returns the right type',
) || diag $@;
};
done_testing;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment