Last active
September 7, 2018 09:25
Example of Type::Tiny Coercions
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 v5.26.0; | |
use Test::Most; | |
use Types::Standard qw< Enum Bool Str >; | |
use Type::Utils qw< enum >; | |
my $YesNoString = enum( [qw/ yes no /] )->plus_coercions( | |
Bool, sub { $_ ? 'yes' : 'no' }, | |
Str, sub { lc }, | |
); | |
{ | |
package Temp; | |
use Moose; | |
use Types::Standard qw( Bool Join Str ); | |
has delete => ( coerce => 1, is => 'ro', isa => $YesNoString ); | |
has tags => ( coerce => 1, is => 'ro', isa => Str->plus_coercions(Join[' ']) ); | |
} | |
{ | |
cmp_methods( | |
new_ok( Temp => [ delete => 1, tags => [qw(foo bar)] ] ), | |
[ | |
delete => 'yes', | |
tags => 'foo bar', | |
], | |
'attributes are coerced as expected', | |
); | |
# # breakdown of cmp_methods() and new_ok() | |
# my @args = ( | |
# delete => 1, | |
# tags => [qw(foo bar)], | |
# ); | |
# my $obj = Temp->new(@args); | |
# isa_ok $obj, 'Temp', 'object isa okaya'; | |
# | |
# is($obj->delete, 'yes', "delete() = yes"); | |
# is($obj->tags, "foo bar", "tags() = foo bar"); | |
} | |
done_testing; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment