From: Stevan Little Date: Sun, 23 Apr 2006 12:39:12 +0000 (+0000) Subject: unions X-Git-Tag: 0_05~8 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=2d2b92e5d0efdc005c64649c9504b0d288b71e9b;p=gitmo%2FMoose.git unions --- diff --git a/lib/Moose.pm b/lib/Moose.pm index 6d18e15..ed64f64 100644 --- a/lib/Moose.pm +++ b/lib/Moose.pm @@ -78,12 +78,17 @@ use Moose::Util::TypeConstraints; my $meta = _find_meta(); return subname 'Moose::has' => sub { my ($name, %options) = @_; - if ($options{metaclass}) { - _load_all_classes($options{metaclass}); - $meta->add_attribute($options{metaclass}->new($name, %options)); + if ($name =~ /^\+(.*)/) { + warn $1; } else { - $meta->add_attribute($name, %options); + if ($options{metaclass}) { + _load_all_classes($options{metaclass}); + $meta->add_attribute($options{metaclass}->new($name, %options)); + } + else { + $meta->add_attribute($name, %options); + } } }; }, diff --git a/t/036_custom_attribute_metaclass.t b/t/036_attribute_custom_metaclass.t similarity index 100% rename from t/036_custom_attribute_metaclass.t rename to t/036_attribute_custom_metaclass.t diff --git a/t/037_attribute_type_unions.t b/t/037_attribute_type_unions.t index b81f64b..065eb35 100644 --- a/t/037_attribute_type_unions.t +++ b/t/037_attribute_type_unions.t @@ -3,7 +3,7 @@ use strict; use warnings; -use Test::More tests => 10; +use Test::More tests => 19; use Test::Exception; BEGIN { @@ -54,6 +54,52 @@ dies_ok { dies_ok { Foo->new(bar => sub {}) -} '... didnt create a new Foo with bar as a number'; +} '... didnt create a new Foo with bar as a CODE ref'; + +{ + package Bar; + use strict; + use warnings; + use Moose; + + has 'baz' => (is => 'rw', isa => 'Str | CodeRef'); +} + +my $bar = Bar->new; +isa_ok($bar, 'Bar'); + +lives_ok { + $bar->baz('a string') +} '... set baz successfully with a string'; + +lives_ok { + $bar->baz(sub { 'a sub' }) +} '... set baz successfully with a CODE ref'; + +dies_ok { + $bar->baz(\(my $var1)) +} '... couldnt set baz successfully with a SCALAR ref'; + +dies_ok { + $bar->baz({}) +} '... couldnt set bar successfully with a HASH ref'; + +# check the constructor + +lives_ok { + Bar->new(baz => 'a string') +} '... created new Bar with baz successfully set with a string'; + +lives_ok { + Bar->new(baz => sub { 'a sub' }) +} '... created new Bar with baz successfully set with a CODE ref'; + +dies_ok { + Bar->new(baz => \(my $var2)) +} '... didnt create a new Bar with baz as a number'; + +dies_ok { + Bar->new(baz => {}) +} '... didnt create a new Bar with baz as a HASH ref';