X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2F020_attribute.t;h=48ddbf97611b969936c9440965fe475faf849f5e;hb=86a4d8730cfe673db674c692f7703632b700c7c9;hp=33cafdde9d7959e3dd24042e56000d012f26aff8;hpb=1d68af0454f55a8b088f8bc1887a0a5ce54d2a22;p=gitmo%2FClass-MOP.git diff --git a/t/020_attribute.t b/t/020_attribute.t index 33cafdd..48ddbf9 100644 --- a/t/020_attribute.t +++ b/t/020_attribute.t @@ -1,15 +1,18 @@ -#!/usr/bin/perl - use strict; use warnings; -use Test::More tests => 73; +use Scalar::Util 'reftype', 'blessed'; + +use Test::More; use Test::Exception; -BEGIN { - use_ok('Class::MOP'); - use_ok('Class::MOP::Attribute'); -} +use Class::MOP; +use Class::MOP::Attribute; +use Class::MOP::Method; + + +dies_ok { Class::MOP::Attribute->name } q{... can't call name() as a class method}; + { my $attr = Class::MOP::Attribute->new('$foo'); @@ -25,6 +28,17 @@ BEGIN { ok(!$attr->has_default, '... $attr does not have an default'); ok(!$attr->has_builder, '... $attr does not have a builder'); + { + my $reader = $attr->get_read_method_ref; + my $writer = $attr->get_write_method_ref; + + ok(!blessed($reader), '... it is a plain old sub'); + ok(!blessed($writer), '... it is a plain old sub'); + + is(reftype($reader), 'CODE', '... it is a plain old sub'); + is(reftype($writer), 'CODE', '... it is a plain old sub'); + } + my $class = Class::MOP::Class->initialize('Foo'); isa_ok($class, 'Class::MOP::Class'); @@ -34,6 +48,20 @@ BEGIN { is($attr->associated_class, $class, '... the class was associated correctly'); + ok(!$attr->get_read_method, '... $attr does not have an read method'); + ok(!$attr->get_write_method, '... $attr does not have an write method'); + + { + my $reader = $attr->get_read_method_ref; + my $writer = $attr->get_write_method_ref; + + ok(blessed($reader), '... it is a plain old sub'); + ok(blessed($writer), '... it is a plain old sub'); + + isa_ok($reader, 'Class::MOP::Method'); + isa_ok($writer, 'Class::MOP::Method'); + } + my $attr_clone = $attr->clone(); isa_ok($attr_clone, 'Class::MOP::Attribute'); isnt($attr, $attr_clone, '... but they are different instances'); @@ -64,6 +92,20 @@ BEGIN { ok(!$attr->has_reader, '... $attr does not have an reader'); ok(!$attr->has_writer, '... $attr does not have an writer'); + ok(!$attr->get_read_method, '... $attr does not have an read method'); + ok(!$attr->get_write_method, '... $attr does not have an write method'); + + { + my $reader = $attr->get_read_method_ref; + my $writer = $attr->get_write_method_ref; + + ok(!blessed($reader), '... it is a plain old sub'); + ok(!blessed($writer), '... it is a plain old sub'); + + is(reftype($reader), 'CODE', '... it is a plain old sub'); + is(reftype($writer), 'CODE', '... it is a plain old sub'); + } + my $attr_clone = $attr->clone(); isa_ok($attr_clone, 'Class::MOP::Attribute'); isnt($attr, $attr_clone, '... but they are different instances'); @@ -96,6 +138,20 @@ BEGIN { ok(!$attr->has_reader, '... $attr does not have an reader'); ok(!$attr->has_writer, '... $attr does not have an writer'); + is($attr->get_read_method, 'foo', '... $attr does not have an read method'); + is($attr->get_write_method, 'foo', '... $attr does not have an write method'); + + { + my $reader = $attr->get_read_method_ref; + my $writer = $attr->get_write_method_ref; + + ok(!blessed($reader), '... it is not a plain old sub'); + ok(!blessed($writer), '... it is not a plain old sub'); + + is(reftype($reader), 'CODE', '... it is a plain old sub'); + is(reftype($writer), 'CODE', '... it is a plain old sub'); + } + my $attr_clone = $attr->clone(); isa_ok($attr_clone, 'Class::MOP::Attribute'); isnt($attr, $attr_clone, '... but they are different instances'); @@ -126,6 +182,20 @@ BEGIN { ok(!$attr->has_accessor, '... $attr does not have an accessor'); + is($attr->get_read_method, 'get_foo', '... $attr does not have an read method'); + is($attr->get_write_method, 'set_foo', '... $attr does not have an write method'); + + { + my $reader = $attr->get_read_method_ref; + my $writer = $attr->get_write_method_ref; + + ok(!blessed($reader), '... it is not a plain old sub'); + ok(!blessed($writer), '... it is not a plain old sub'); + + is(reftype($reader), 'CODE', '... it is a plain old sub'); + is(reftype($writer), 'CODE', '... it is a plain old sub'); + } + my $attr_clone = $attr->clone(); isa_ok($attr_clone, 'Class::MOP::Attribute'); isnt($attr, $attr_clone, '... but they are different instances'); @@ -156,3 +226,23 @@ BEGIN { is($attr->builder, 'foo_builder', '... $attr->builder == foo_builder'); } + +{ + for my $value ({}, bless({}, 'Foo')) { + throws_ok { + Class::MOP::Attribute->new('$foo', default => $value); + } qr/References are not allowed as default values/; + } +} + +{ + my $attr; + lives_ok { + my $meth = Class::MOP::Method->wrap(sub {shift}, name => 'foo', package_name => 'bar'); + $attr = Class::MOP::Attribute->new('$foo', default => $meth); + } 'Class::MOP::Methods accepted as default'; + + is($attr->default(42), 42, 'passthrough for default on attribute'); +} + +done_testing;