Last active
January 22, 2021 16:52
-
-
Save j1n3l0/ade86d924127a4e9f1538d09e2f5e2c0 to your computer and use it in GitHub Desktop.
Coerce an attribute from a Str to a URI object
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
package Service; | |
use Moo; | |
use Type::Utils qw( class_type ); | |
use Types::Standard qw( Str ); | |
use URI; | |
has endpoint => ( | |
coerce => 1, | |
default => 'https://example.com/service', | |
is => 'ro', | |
isa => class_type('URI')->plus_constructors(Str, 'new'), | |
); | |
1; |
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 -target => 'Service'; | |
use URI; | |
subtest 'A Service class with an endpoint attribute of type URI' => sub { | |
is( $CLASS->new(), | |
object { | |
call endpoint => object { prop blessed => match(qr/URI/) }; | |
}, | |
'should accept a Str as the default endpoint', | |
); | |
is( $CLASS->new( endpoint => 'https://example.com/service' ), | |
object { | |
call endpoint => object { prop blessed => match(qr/URI/) }; | |
}, | |
'should accept a Str as the endpoint', | |
); | |
is( $CLASS->new( endpoint => URI->new('https://example.com/service') ), | |
object { | |
call endpoint => object { prop blessed => match(qr/URI/) }; | |
}, | |
'should accept a URI object as the endpoint', | |
); | |
}; | |
done_testing(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment