Last active
March 23, 2020 18:00
-
-
Save j1n3l0/49a00a5e2d6e2f0ce078521fee998cc3 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; | |
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