From: Shawn M Moore Date: Thu, 19 Jun 2008 00:10:34 +0000 (+0000) Subject: Some fixes and test improvements for roles X-Git-Tag: 0.19~283 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=69ac1dcfa408b64733ba3d2e47e8e791e4548f95;p=gitmo%2FMouse.git Some fixes and test improvements for roles --- diff --git a/lib/Mouse/Meta/Role.pm b/lib/Mouse/Meta/Role.pm index 734bebe..870b69d 100644 --- a/lib/Mouse/Meta/Role.pm +++ b/lib/Mouse/Meta/Role.pm @@ -37,12 +37,13 @@ sub name { $_[0]->{name} } sub add_attribute { my $self = shift; my $name = shift; - $self->{attributes}->{$name} = [ @_ ]; + my $spec = shift; + $self->{attributes}->{$name} = $spec; } sub has_attribute { exists $_[0]->{attributes}->{$_[1]} } sub get_attribute_list { keys %{ $_[0]->{attributes} } } -sub get_attribute { $_->[0]->{attributes}->{$_[1]} } +sub get_attribute { $_[0]->{attributes}->{$_[1]} } sub apply { my $self = shift; @@ -51,7 +52,7 @@ sub apply { for my $name ($self->get_attribute_list) { my $spec = $self->get_attribute($name); - Mouse::Meta::Attribute->create($pkg, $name, @$spec); + Mouse::Meta::Attribute->create($pkg, $name, %$spec); } } diff --git a/lib/Mouse/Role.pm b/lib/Mouse/Role.pm index 0375d0c..10621cc 100644 --- a/lib/Mouse/Role.pm +++ b/lib/Mouse/Role.pm @@ -35,9 +35,9 @@ do { my $caller = $CALLER; return sub { my $name = shift; - my @opts = @_; + my %opts = @_; - $caller->meta->add_attribute($name => \@opts); + $caller->meta->add_attribute($name => \%opts); } }, with => sub { diff --git a/t/402-basic-role-application.t b/t/402-basic-role-application.t index 89e2aee..1191d03 100644 --- a/t/402-basic-role-application.t +++ b/t/402-basic-role-application.t @@ -1,17 +1,22 @@ #!/usr/bin/env perl use strict; use warnings; -use Test::More tests => 1; +use Test::More tests => 5; +use Test::Exception; do { package Role; use Mouse::Role; - has 'attr'; + has 'attr' => ( + default => 'Role', + ); no Mouse::Role; }; +is_deeply(Role->meta->get_attribute('attr'), {default => 'Role'}); + do { package Class; use Mouse; @@ -21,4 +26,28 @@ do { }; ok(Class->meta->has_attribute('attr'), "role application added the attribute"); +is(Class->meta->get_attribute('attr')->default, 'Role'); + +do { + package Role2; + use Mouse::Role; + + has 'attr' => ( + default => 'Role2', + ); + + no Mouse::Role; +}; + +lives_ok { + package Class2; + use Mouse; + with 'Role'; + with 'Role2'; +}; + +TODO: { + local $TODO = "Moose prefers first definition, Mouse the last"; + is(Class2->meta->get_attribute('attr')->default, 'Role'); +};