From: gfx Date: Fri, 2 Oct 2009 12:00:00 +0000 (+0900) Subject: Add 2 test files X-Git-Tag: 0.37_02~28 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=a72478f2a428185e9e81327e3af7c1fd36c7ce3d;p=gitmo%2FMouse.git Add 2 test files --- diff --git a/t/020_attributes/016_attribute_traits_registered.t b/t/020_attributes/016_attribute_traits_registered.t new file mode 100755 index 0000000..91dc88a --- /dev/null +++ b/t/020_attributes/016_attribute_traits_registered.t @@ -0,0 +1,117 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 23; +use Test::Exception; +use Test::Moose; + + + +{ + package My::Attribute::Trait; + use Moose::Role; + + has 'alias_to' => (is => 'ro', isa => 'Str'); + + has foo => ( is => "ro", default => "blah" ); + + after 'install_accessors' => sub { + my $self = shift; + $self->associated_class->add_method( + $self->alias_to, + $self->get_read_method_ref + ); + }; + + package Moose::Meta::Attribute::Custom::Trait::Aliased; + sub register_implementation { 'My::Attribute::Trait' } +} + +{ + package My::Other::Attribute::Trait; + use Moose::Role; + + my $method = sub { + 42; + }; + + has the_other_attr => ( isa => "Str", is => "rw", default => "oink" ); + + after 'install_accessors' => sub { + my $self = shift; + $self->associated_class->add_method( + 'additional_method', + $method + ); + }; + + package Moose::Meta::Attribute::Custom::Trait::Other; + sub register_implementation { 'My::Other::Attribute::Trait' } +} + +{ + package My::Class; + use Moose; + + has 'bar' => ( + traits => [qw/Aliased/], + is => 'ro', + isa => 'Int', + alias_to => 'baz', + ); +} + +{ + package My::Derived::Class; + use Moose; + + extends 'My::Class'; + + has '+bar' => ( + traits => [qw/Other/], + ); +} + +my $c = My::Class->new(bar => 100); +isa_ok($c, 'My::Class'); + +is($c->bar, 100, '... got the right value for bar'); + +can_ok($c, 'baz') and +is($c->baz, 100, '... got the right value for baz'); + +my $bar_attr = $c->meta->get_attribute('bar'); +does_ok($bar_attr, 'My::Attribute::Trait'); +is($bar_attr->foo, "blah", "attr initialized"); + +ok(!$bar_attr->meta->does_role('Aliased'), "does_role ignores aliases for sanity"); +ok($bar_attr->does('Aliased'), "attr->does uses aliases"); +ok(!$bar_attr->meta->does_role('Fictional'), "does_role returns false for nonexistent roles"); +ok(!$bar_attr->does('Fictional'), "attr->does returns false for nonexistent roles"); + +my $quux = My::Derived::Class->new(bar => 1000); + +is($quux->bar, 1000, '... got the right value for bar'); + +can_ok($quux, 'baz'); +is($quux->baz, 1000, '... got the right value for baz'); + +my $derived_bar_attr = $quux->meta->get_attribute("bar"); +does_ok($derived_bar_attr, 'My::Attribute::Trait' ); + +is( $derived_bar_attr->foo, "blah", "attr initialized" ); + +does_ok($derived_bar_attr, 'My::Other::Attribute::Trait' ); + +is($derived_bar_attr->the_other_attr, "oink", "attr initialized" ); + +ok(!$derived_bar_attr->meta->does_role('Aliased'), "does_role ignores aliases for sanity"); +ok($derived_bar_attr->does('Aliased'), "attr->does uses aliases"); +ok(!$derived_bar_attr->meta->does_role('Fictional'), "does_role returns false for nonexistent roles"); +ok(!$derived_bar_attr->does('Fictional'), "attr->does returns false for nonexistent roles"); + +can_ok($quux, 'additional_method'); +is(eval { $quux->additional_method }, 42, '... got the right value for additional_method'); + diff --git a/t/020_attributes/017_attribute_traits_n_meta.t b/t/020_attributes/017_attribute_traits_n_meta.t new file mode 100755 index 0000000..253a345 --- /dev/null +++ b/t/020_attributes/017_attribute_traits_n_meta.t @@ -0,0 +1,68 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More tests => 7; +use Test::Exception; + +use lib 't/lib'; +use Test::Mouse; + +{ + package My::Meta::Attribute::DefaultReadOnly; + use Mouse; + + extends 'Mouse::Meta::Attribute'; + + around 'new' => sub { + my $next = shift; + my ($self, $name, %options) = @_; + $options{is} = 'ro' + unless exists $options{is}; + $next->($self, $name, %options); + }; +} + +{ + package My::Attribute::Trait; + use Mouse::Role; + + has 'alias_to' => (is => 'ro', isa => 'Str'); + + after 'install_accessors' => sub { + my $self = shift; + $self->associated_class->add_method( + $self->alias_to, + $self->get_read_method_ref + ); + }; +} + +{ + package My::Class; + use Mouse; + + has 'bar' => ( + metaclass => 'My::Meta::Attribute::DefaultReadOnly', + traits => [qw/My::Attribute::Trait/], + isa => 'Int', + alias_to => 'baz', + ); +} + +my $c = My::Class->new(bar => 100); +isa_ok($c, 'My::Class'); + +is($c->bar, 100, '... got the right value for bar'); + +can_ok($c, 'baz'); +is($c->baz, 100, '... got the right value for baz'); + +isa_ok($c->meta->get_attribute('bar'), 'My::Meta::Attribute::DefaultReadOnly'); +does_ok($c->meta->get_attribute('bar'), 'My::Attribute::Trait'); +is($c->meta->get_attribute('bar')->_is_metadata, 'ro', '... got the right metaclass customization'); + + + +