Skip to content

Instantly share code, notes, and snippets.

@j1n3l0
Last active January 22, 2021 16:52
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/ade86d924127a4e9f1538d09e2f5e2c0 to your computer and use it in GitHub Desktop.
Save j1n3l0/ade86d924127a4e9f1538d09e2f5e2c0 to your computer and use it in GitHub Desktop.
Coerce an attribute from a Str to a URI object
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;
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