From: Moritz Onken Date: Sun, 16 Jan 2011 18:12:23 +0000 (+0100) Subject: merged with delegation_bugs branch X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a9d2b1a78f09fa9f0413953fb089ba0918e5da13;p=gitmo%2FMooseX-ClassAttribute.git merged with delegation_bugs branch fixed #59663 fixed #59573 made failing tests TODOs --- diff --git a/t/08-role-composition-of-delegates.t b/t/08-role-composition-of-delegates.t new file mode 100644 index 0000000..296ee0e --- /dev/null +++ b/t/08-role-composition-of-delegates.t @@ -0,0 +1,65 @@ + +# Reported in https://rt.cpan.org/Public/Bug/Display.html?id=59572 + +use strict; +use warnings; + +use Test::More tests => 4; +#use Test::NoWarnings; +use Test::Exception; + +{ + package Role1; + use Moose::Role; + + # I do nothing! +} +{ + package Role2; + + use Moose::Role; + use MooseX::ClassAttribute; + + class_has attr => ( + is => 'ro', isa => 'HashRef[Str]', + lazy => 1, + default => sub { {} }, + traits => ['Hash'], + handles => { + has_attr => 'exists', + }, + ); + + has foo => ( is => 'rw' ); + + sub normal_method + { + Test::More::pass('a regular method from the role is composed'); + } +} + +{ + package Foo; + use strict; use warnings; + use Moose; + with 'Role1', 'Role2'; + 1; +} + +package main; +local $TODO = 'Class attributes are lost during role composition'; +# this runs, so Role2 did get composed... +Foo->normal_method; +ok(Foo->can('foo'), 'Standard attribute applied ok'); + +# Except the delegated method ended up in the wrong place! + +# ..and MX::Class never got applied properly! +ok(Moose::Util::does_role(Foo->meta, 'MooseX::ClassAttribute::Trait::Class'), + 'metaclass gets MX:CA metaclass trait'); + +# in Moose 1.08/MooseX::ClassAttribute 0.16, this dies with: +# Can't locate object method "has_attr" via package "Foo" +lives_ok { Foo->has_attr('key') } + 'Delegated method from native attribute trait is properly composed from a role composed in a list of roles'; + diff --git a/t/09-bare-native-attribute-trait.t b/t/09-bare-native-attribute-trait.t new file mode 100644 index 0000000..32a3b31 --- /dev/null +++ b/t/09-bare-native-attribute-trait.t @@ -0,0 +1,33 @@ + +# reported in https://rt.cpan.org/Public/Bug/Display.html?id=59573 + +use strict; +use warnings; + +use Test::More tests => 2; +use Test::NoWarnings; +use Test::Exception; + +{ + package Foo; + use Moose; + use MooseX::ClassAttribute; + class_has attr => ( + is => 'bare', isa => 'HashRef[Str]', + lazy => 1, + default => sub { {} }, + traits => ['Hash'], + handles => { + has_attr => 'exists', + }, + ); +} + +package main; + +# in Moose 1.08/MooseX::ClassAttribute 0.16, this dies with: +# Can't use an undefined value as a HASH reference +lives_ok { Foo->has_attr('key') } + 'Default builder in a native attribute trait is properly run when the attribute is defined with no standard accessors'; + + diff --git a/t/10-multiple-role-composition.t b/t/10-multiple-role-composition.t new file mode 100644 index 0000000..d881845 --- /dev/null +++ b/t/10-multiple-role-composition.t @@ -0,0 +1,65 @@ + +# Reported as https://rt.cpan.org/Public/Bug/Display.html?id=59610 + +{ + package Role1; + use Moose::Role; + + # I do nothing! +} +{ + package Role2; + + use Moose::Role; + use MooseX::ClassAttribute; + + # I also do nothing, except use MX:CA +} + +{ + package Foo; + use strict; use warnings; + use Moose; + with 'Role1'; + with 'Role2'; +} + +{ + package Bar; + use strict; use warnings; + use Moose; + with 'Role2'; + with 'Role1'; +} + +{ + package Baz; + use strict; use warnings; + use Moose; + with 'Role1', 'Role2'; +} + +{ + package Quux; + use strict; use warnings; + use Moose; + with 'Role2', 'Role1'; +} + +# in this example, all roles actually disappear if the second one uses MX:CA! + +package main; + +use Test::More tests => 5; +use Test::NoWarnings; +use Test::Deep; +local $TODO = 'Class attributes are lost during role composition'; +foreach my $class (qw(Foo Bar Baz Quux)) +{ + cmp_deeply( + [ map { $_->name} $class->meta->calculate_all_roles ], + superbagof(qw(Role1 Role2)), + 'Both roles are consumed by class ' . $class, + ); +} + diff --git a/t/11-strict-role-composition.t b/t/11-strict-role-composition.t new file mode 100644 index 0000000..cbbadbd --- /dev/null +++ b/t/11-strict-role-composition.t @@ -0,0 +1,51 @@ + +# Reported as https://rt.cpan.org/Public/Bug/Display.html?id=59663 + +package main; +use Test::More tests => 3; +use Test::Exception; + +use Test::Requires { + 'MooseX::Role::Strict' => 0.01, # skip all if not installed +}; + +{ + package Role; + + use MooseX::Role::Strict; + use MooseX::ClassAttribute; + + class_has attr => ( + is => 'ro', isa => 'HashRef[Str]', + lazy => 1, + default => sub { {} }, + traits => ['Hash'], + handles => { + has_attr => 'exists', + }, + ); + + + sub normal_method + { + Test::More::pass('a regular method from the role is composed'); + } + +} + +{ + package Foo; + use strict; use warnings; + use Moose; + with 'Role'; +} + +package main; +use Test::NoWarnings; + +Foo->normal_method; + +lives_ok { Foo->has_attr('key') } + 'Delegated method from native attribute trait is properly composed from a strict role'; + + diff --git a/t/12-moose-exporter.t b/t/12-moose-exporter.t new file mode 100644 index 0000000..e50780a --- /dev/null +++ b/t/12-moose-exporter.t @@ -0,0 +1,35 @@ +use Test::More tests => 4; +use Test::NoWarnings; +use Test::Exception; + +{ + package MooseX::Foo; + + use strict; + use warnings; + + use Moose::Exporter; + use MooseX::ClassAttribute (); + + Moose::Exporter->setup_import_methods( + also => [ 'MooseX::ClassAttribute' ], + ); +} + +{ + package MyClass; + + use Moose; + MooseX::Foo->import; + &MyClass::class_has ( attr => ( is => 'ro' ) ); + +}; + + +package main; + +ok(MyClass->can('has')); +ok(MyClass->can('class_has')); +ok(Moose::Util::does_role(MyClass->meta, 'MooseX::ClassAttribute::Trait::Class'), + 'metaclass gets MX:CA metaclass trait'); +