X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F300_immutable%2F004_inlined_constructors_n_types.t;h=0c47a8c5e55507944062f5b3e99aa2ce59bced7a;hb=a28e50e44945358d15eb19e4688573741a319fe0;hp=783075274aa151a3a99ce583b87f64c74efae936;hpb=849811463e6a7531a72f545a9a78902724b4085e;p=gitmo%2FMoose.git diff --git a/t/300_immutable/004_inlined_constructors_n_types.t b/t/300_immutable/004_inlined_constructors_n_types.t index 7830752..0c47a8c 100644 --- a/t/300_immutable/004_inlined_constructors_n_types.t +++ b/t/300_immutable/004_inlined_constructors_n_types.t @@ -3,18 +3,14 @@ use strict; use warnings; -use Test::More tests => 4; +use Test::More; use Test::Exception; -BEGIN { - use_ok('Moose'); -} - =pod This tests to make sure that the inlined constructor -has all the type constraints in order, even in the -cases when there is no type constraint available, such +has all the type constraints in order, even in the +cases when there is no type constraint available, such as with a Class::MOP::Attribute object. =cut @@ -22,11 +18,18 @@ as with a Class::MOP::Attribute object. { package Foo; use Moose; + use Moose::Util::TypeConstraints; - has 'foo' => (is => 'rw', isa => 'Int'); + coerce 'Int' => from 'Str' => via { length $_ ? $_ : 69 }; + + has 'foo' => (is => 'rw', isa => 'Int'); has 'baz' => (is => 'rw', isa => 'Int'); has 'zot' => (is => 'rw', isa => 'Int', init_arg => undef); - + has 'moo' => (is => 'rw', isa => 'Int', coerce => 1, default => '', required => 1); + has 'boo' => (is => 'rw', isa => 'Int', coerce => 1, builder => '_build_boo', required => 1); + + sub _build_boo { '' } + Foo->meta->add_attribute( Class::MOP::Attribute->new( 'bar' => ( @@ -34,22 +37,26 @@ as with a Class::MOP::Attribute object. ) ) ); - - Foo->meta->make_immutable(debug => 0); } -lives_ok { - Foo->new(foo => 10, bar => "Hello World", baz => 10, zot => 4); -} '... this passes the constuctor correctly'; - -lives_ok { - Foo->new(foo => 10, bar => "Hello World", baz => 10, zot => "not an int"); -} "... the constructor doesn't care about 'zot'"; - -dies_ok { - Foo->new(foo => "Hello World", bar => 100, baz => "Hello World"); -} '... this fails the constuctor correctly'; +for (1..2) { + my $is_immutable = Foo->meta->is_immutable; + my $mutable_string = $is_immutable ? 'immutable' : 'mutable'; + lives_ok { + my $f = Foo->new(foo => 10, bar => "Hello World", baz => 10, zot => 4); + is($f->moo, 69, "Type coercion works as expected on default ($mutable_string)"); + is($f->boo, 69, "Type coercion works as expected on builder ($mutable_string)"); + } "... this passes the constuctor correctly ($mutable_string)"; + lives_ok { + Foo->new(foo => 10, bar => "Hello World", baz => 10, zot => "not an int"); + } "... the constructor doesn't care about 'zot' ($mutable_string)"; + dies_ok { + Foo->new(foo => "Hello World", bar => 100, baz => "Hello World"); + } "... this fails the constuctor correctly ($mutable_string)"; + Foo->meta->make_immutable(debug => 0) unless $is_immutable; +} +done_testing;