Subject: | [TEST ATTACHED] Wierd behavoir with is_X and to_X on subtyping... |
Can't figure this stuff out, lets try again...
Subject: | wierd_behavoir_with_base_type.t |
#!/usr/bin/env perl
# Awkward behavoir with is_X and to_X
# Evan Carroll <me@evancarroll.com>
package Class;
use Moose;
use MooseX::Types -declare => [qw( okStr okInt okBool)];
use MooseX::Types::Moose qw(:all);
## These tests also fail if you try to coerce from Any (MooseX::Types::Base type)
subtype okStr , as Str , where \&is_Str;
coerce okStr, from Str , via \&to_Str;
subtype okInt , as Int , where \&is_Int;
coerce okInt, from Int , via \&to_Str;
subtype okBool , as Bool , where \&is_Bool;
coerce okBool, from Bool , via \&to_Str;
has 'str' => ( isa => 'okStr', is => 'rw' );
has 'bool' => ( isa => 'okBool', is => 'rw' );
has 'int' => ( isa => 'okInt', is => 'rw' );
has 'cstr' => ( isa => 'okStr', is => 'rw' , coerce => 1 );
has 'cbool' => ( isa => 'okBool', is => 'rw' , coerce => 1 );
has 'cint' => ( isa => 'okInt', is => 'rw' , coerce => 1 );
package main;
use Test::More tests => 8;
{
eval {
Class->new({
'bool' => 1
, 'str' => 'foobar'
, 'int' => 5
});
};
ok ( !$@, "Using subtypes without coercions in constructor [error:$@]" );
my $o = Class->new;
eval { $o->int(5) };
ok ( !$@, "Setter for int on subtype wo/ coercion [error:$@]" );
eval { $o->str('foobar') };
ok ( !$@, "Setter for str on subtype wo/ coercion [error:$@]" );
eval { $o->bool(1) };
ok ( !$@, "Setter for bool on subtype wo/ coercion [error:$@]" );
}
{
eval {
Class->new({
'obool' => 1
, 'ostr' => 'foobar'
, 'oint' => 5
});
};
ok ( !$@, "Using subtypes without coercions in constructor [error:$@]" );
my $o = Class->new;
eval { $o->cint(5) };
ok ( !$@, "Setter for int on subtype w/ coercion [error:$@]" );
eval { $o->cstr('foobar') };
ok ( !$@, "Setter for str on subtype w/ coercion [error:$@]" );
eval { $o->cbool(1) };
ok ( !$@, "Setter for bool on subtype w/ coercion [error:$@]" );
}