Skip Menu |

This queue is for tickets about the MooseX-Types CPAN distribution.

Report information
The Basics
Id: 47474
Status: resolved
Priority: 0/
Queue: MooseX-Types

People
Owner: Nobody in particular
Requestors: perl [...] evancarroll.com
Cc:
AdminCc:

Bug Information
Severity: (no value)
Broken in: (no value)
Fixed in: (no value)



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:$@]" ); }
Subject: Re: [rt.cpan.org #47474] [TEST ATTACHED] Wierd behavoir with is_X and to_X on subtyping...
Date: Tue, 30 Jun 2009 20:37:43 -0700
To: Evan Carroll via RT <bug-MooseX-Types [...] rt.cpan.org>
From: Rafael Kitover <rkitover [...] io.com>
A couple things: You have to use: where { is_Str $_ } these functions don't operate on $_ Also to_Foo helpers are only created for types that have coercions, core Moose types have no coercions. On Tue, Jun 30, 2009 at 10:24:37PM -0400, Evan Carroll via RT wrote: Show quoted text
> Tue Jun 30 22:24:36 2009: Request 47474 was acted upon. > Transaction: Ticket created by ECARROLL > Queue: MooseX-Types > Subject: [TEST ATTACHED] Wierd behavoir with is_X and to_X on subtyping... > Broken in: (no value) > Severity: (no value) > Owner: Nobody > Requestors: perl@evancarroll.com > Status: new > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=47474 > > > > Can't figure this stuff out, lets try again...
Show quoted text
> #!/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:$@]" ); > > }
Updated the test, I'm still not getting this...
#!/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 { $_ }; subtype okInt , as Int , where { is_Int $_ }; coerce okInt, from Int , via { $_ }; subtype okBool , as Bool , where { is_Bool $_ }; coerce okBool, from Bool , via { $_ }; 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:$@]" ); }
Subject: Re: [rt.cpan.org #47474] [TEST ATTACHED] Wierd behavoir with is_X and to_X on subtyping...
Date: Wed, 1 Jul 2009 12:05:17 -0700
To: Evan Carroll via RT <bug-MooseX-Types [...] rt.cpan.org>
From: Rafael Kitover <rkitover [...] io.com>
The other thing I didn't notice last time is that you're using string types in your attributes, so that section should be: 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 ); Then everything works. On Wed, Jul 01, 2009 at 01:59:28PM -0400, Evan Carroll via RT wrote: Show quoted text
> Queue: MooseX-Types > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=47474 > > > Updated the test, I'm still not getting this... > >
Show quoted text
> #!/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 { $_ }; > > subtype okInt , as Int , where { is_Int $_ }; > coerce okInt, from Int , via { $_ }; > > subtype okBool , as Bool , where { is_Bool $_ }; > coerce okBool, from Bool , via { $_ }; > > 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:$@]" ); > > }
You're right, thanks a ton!!! Old habbits die hard. I think it should be added as a failing test with the quotes. And, maybe as a simple example with it passing without..
18 subtype okBool , as Bool , where \&is_Bool; 19 coerce okBool, from Bool , via { $_ }; also seems to work