Skip to content

Instantly share code, notes, and snippets.

@willsheppard
Last active September 7, 2018 09:25
Example of Type::Tiny Coercions
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