X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F005_attributes.t;h=12e2a01377d755a44274db34f46e32b5b0f7c4cb;hb=b4bd10ecd2eabe1a2c1bc3addad22b207f6592ee;hp=63158f157ba6c2b11abd089c9e01a918622d73e1;hpb=058c1cf58078cdd0fcb9aa2f4a5c7556e0b8c23a;p=gitmo%2FClass-MOP.git diff --git a/t/005_attributes.t b/t/005_attributes.t index 63158f1..12e2a01 100644 --- a/t/005_attributes.t +++ b/t/005_attributes.t @@ -1,14 +1,10 @@ -#!/usr/bin/perl - use strict; use warnings; -use Test::More tests => 43; +use Test::More tests => 70; use Test::Exception; -BEGIN { - use_ok('Class::MOP'); -} +use Class::MOP; my $FOO_ATTR = Class::MOP::Attribute->new('$foo'); my $BAR_ATTR = Class::MOP::Attribute->new('$bar' => ( @@ -16,11 +12,20 @@ my $BAR_ATTR = Class::MOP::Attribute->new('$bar' => ( )); my $BAZ_ATTR = Class::MOP::Attribute->new('$baz' => ( reader => 'get_baz', - writer => 'set_baz', + writer => 'set_baz', )); my $BAR_ATTR_2 = Class::MOP::Attribute->new('$bar'); +my $FOO_ATTR_2 = Class::MOP::Attribute->new('$foo' => ( + accessor => 'foo', + builder => 'build_foo' +)); + +is($FOO_ATTR->name, '$foo', '... got the attributes name correctly'); +is($BAR_ATTR->name, '$bar', '... got the attributes name correctly'); +is($BAZ_ATTR->name, '$baz', '... got the attributes name correctly'); + { package Foo; use metaclass; @@ -31,21 +36,21 @@ my $BAR_ATTR_2 = Class::MOP::Attribute->new('$bar'); } '... we added an attribute to Foo successfully'; ::ok($meta->has_attribute('$foo'), '... Foo has $foo attribute'); ::is($meta->get_attribute('$foo'), $FOO_ATTR, '... got the right attribute back for Foo'); - + ::ok(!$meta->has_method('foo'), '... no accessor created'); - + ::lives_ok { $meta->add_attribute($BAR_ATTR_2); } '... we added an attribute to Foo successfully'; ::ok($meta->has_attribute('$bar'), '... Foo has $bar attribute'); - ::is($meta->get_attribute('$bar'), $BAR_ATTR_2, '... got the right attribute back for Foo'); + ::is($meta->get_attribute('$bar'), $BAR_ATTR_2, '... got the right attribute back for Foo'); ::ok(!$meta->has_method('bar'), '... no accessor created'); } { package Bar; our @ISA = ('Foo'); - + my $meta = Bar->meta; ::lives_ok { $meta->add_attribute($BAR_ATTR); @@ -53,66 +58,74 @@ my $BAR_ATTR_2 = Class::MOP::Attribute->new('$bar'); ::ok($meta->has_attribute('$bar'), '... Bar has $bar attribute'); ::is($meta->get_attribute('$bar'), $BAR_ATTR, '... got the right attribute back for Bar'); + my $attr = $meta->get_attribute('$bar'); + ::is($attr->get_read_method, 'bar', '... got the right read method for Bar'); + ::is($attr->get_write_method, 'bar', '... got the right write method for Bar'); + ::ok($meta->has_method('bar'), '... an accessor has been created'); - ::isa_ok($meta->get_method('bar'), 'Class::MOP::Attribute::Accessor'); + ::isa_ok($meta->get_method('bar'), 'Class::MOP::Method::Accessor'); } { package Baz; our @ISA = ('Bar'); - + my $meta = Baz->meta; ::lives_ok { $meta->add_attribute($BAZ_ATTR); } '... we added an attribute to Baz successfully'; - ::ok($meta->has_attribute('$baz'), '... Baz has $baz attribute'); + ::ok($meta->has_attribute('$baz'), '... Baz has $baz attribute'); ::is($meta->get_attribute('$baz'), $BAZ_ATTR, '... got the right attribute back for Baz'); + my $attr = $meta->get_attribute('$baz'); + ::is($attr->get_read_method, 'get_baz', '... got the right read method for Baz'); + ::is($attr->get_write_method, 'set_baz', '... got the right write method for Baz'); + ::ok($meta->has_method('get_baz'), '... a reader has been created'); ::ok($meta->has_method('set_baz'), '... a writer has been created'); - ::isa_ok($meta->get_method('get_baz'), 'Class::MOP::Attribute::Accessor'); - ::isa_ok($meta->get_method('set_baz'), 'Class::MOP::Attribute::Accessor'); + ::isa_ok($meta->get_method('get_baz'), 'Class::MOP::Method::Accessor'); + ::isa_ok($meta->get_method('set_baz'), 'Class::MOP::Method::Accessor'); } { my $meta = Baz->meta; isa_ok($meta, 'Class::MOP::Class'); - + is($meta->find_attribute_by_name('$bar'), $BAR_ATTR, '... got the right attribute for "bar"'); - is($meta->find_attribute_by_name('$baz'), $BAZ_ATTR, '... got the right attribute for "baz"'); - is($meta->find_attribute_by_name('$foo'), $FOO_ATTR, '... got the right attribute for "foo"'); - + is($meta->find_attribute_by_name('$baz'), $BAZ_ATTR, '... got the right attribute for "baz"'); + is($meta->find_attribute_by_name('$foo'), $FOO_ATTR, '... got the right attribute for "foo"'); + is_deeply( [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ], - [ + [ $BAR_ATTR, $BAZ_ATTR, - $FOO_ATTR, + $FOO_ATTR, ], '... got the right list of applicable attributes for Baz'); - + is_deeply( [ map { $_->associated_class } sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ], [ Bar->meta, Baz->meta, Foo->meta ], - '... got the right list of associated classes from the applicable attributes for Baz'); - + '... got the right list of associated classes from the applicable attributes for Baz'); + my $attr; lives_ok { $attr = $meta->remove_attribute('$baz'); } '... removed the $baz attribute successfully'; - is($attr, $BAZ_ATTR, '... got the right attribute back for Baz'); - - ok(!$meta->has_attribute('$baz'), '... Baz no longer has $baz attribute'); - is($meta->get_attribute('$baz'), undef, '... Baz no longer has $baz attribute'); + is($attr, $BAZ_ATTR, '... got the right attribute back for Baz'); + + ok(!$meta->has_attribute('$baz'), '... Baz no longer has $baz attribute'); + is($meta->get_attribute('$baz'), undef, '... Baz no longer has $baz attribute'); ok(!$meta->has_method('get_baz'), '... a reader has been removed'); ok(!$meta->has_method('set_baz'), '... a writer has been removed'); is_deeply( [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ], - [ + [ $BAR_ATTR, - $FOO_ATTR, + $FOO_ATTR, ], '... got the right list of applicable attributes for Baz'); @@ -126,18 +139,18 @@ my $BAR_ATTR_2 = Class::MOP::Attribute->new('$bar'); lives_ok { $attr = Bar->meta->remove_attribute('$bar'); } '... removed the $bar attribute successfully'; - is($attr, $BAR_ATTR, '... got the right attribute back for Bar'); + is($attr, $BAR_ATTR, '... got the right attribute back for Bar'); - ok(!Bar->meta->has_attribute('$bar'), '... Bar no longer has $bar attribute'); + ok(!Bar->meta->has_attribute('$bar'), '... Bar no longer has $bar attribute'); ok(!Bar->meta->has_method('bar'), '... a accessor has been removed'); } is_deeply( [ sort { $a->name cmp $b->name } $meta->compute_all_applicable_attributes() ], - [ + [ $BAR_ATTR_2, - $FOO_ATTR, + $FOO_ATTR, ], '... got the right list of applicable attributes for Baz'); @@ -154,3 +167,77 @@ my $BAR_ATTR_2 = Class::MOP::Attribute->new('$bar'); is($val, undef, '... got the right value back (undef)'); } + +{ + package Buzz; + use metaclass; + use Scalar::Util qw/blessed/; + + my $meta = Buzz->meta; + ::lives_ok { + $meta->add_attribute($FOO_ATTR_2); + } '... we added an attribute to Buzz successfully'; + + ::lives_ok { + $meta->add_attribute( + Class::MOP::Attribute->new( + '$bar' => ( + accessor => 'bar', + predicate => 'has_bar', + clearer => 'clear_bar', + ) + ) + ); + } '... we added an attribute to Buzz successfully'; + + ::lives_ok { + $meta->add_attribute( + Class::MOP::Attribute->new( + '$bah' => ( + accessor => 'bah', + predicate => 'has_bah', + clearer => 'clear_bah', + default => 'BAH', + ) + ) + ); + } '... we added an attribute to Buzz successfully'; + + ::lives_ok { + $meta->add_method(build_foo => sub{ blessed shift; }); + } '... we added a method to Buzz successfully'; +} + +{ + my $buzz; + ::lives_ok { $buzz = Buzz->meta->new_object } '...Buzz instantiated successfully'; + ::is($buzz->foo, 'Buzz', '...foo builder works as expected'); + ::ok(!$buzz->has_bar, '...bar is not set'); + ::is($buzz->bar, undef, '...bar returns undef'); + ::ok(!$buzz->has_bar, '...bar was not autovivified'); + + $buzz->bar(undef); + ::ok($buzz->has_bar, '...bar is set'); + ::is($buzz->bar, undef, '...bar is undef'); + $buzz->clear_bar; + ::ok(!$buzz->has_bar, '...bar is no longerset'); + + my $buzz2; + ::lives_ok { $buzz2 = Buzz->meta->new_object('$bar' => undef) } '...Buzz instantiated successfully'; + ::ok($buzz2->has_bar, '...bar is set'); + ::is($buzz2->bar, undef, '...bar is undef'); + +} + +{ + my $buzz; + ::lives_ok { $buzz = Buzz->meta->new_object } '...Buzz instantiated successfully'; + ::ok($buzz->has_bah, '...bah is set'); + ::is($buzz->bah, 'BAH', '...bah returns "BAH" '); + + my $buzz2; + ::lives_ok { $buzz2 = Buzz->meta->new_object('$bah' => undef) } '...Buzz instantiated successfully'; + ::ok($buzz2->has_bah, '...bah is set'); + ::is($buzz2->bah, undef, '...bah is undef'); + +}