Revision history for Perl extension Class-MOP.
+
+ * Class::MOP::Attribute
+ - Allow default values to be Class::MOP::Methods.
+ (Florian Ragwitz)
+ - Test the above. (Rhesa Rozendaal)
+ - Tweak original commit so the intent matches the accepted behavior
+ (Nicholas Perez)
+
0.84 Tue, May 12, 2009
* Makefile.PL
- Depend on Text::Exception 0.27 to avoid failing tests ond old
}
sub is_default_a_coderef {
- ('CODE' eq ref($_[0]->{'default'}))
+ my ($value) = $_[0]->{'default'};
+ return unless ref($value);
+ return ref($value) eq 'CODE' || (blessed($value) && $value->isa('Class::MOP::Method'));
}
sub default {
use Scalar::Util 'reftype', 'blessed';
-use Test::More tests => 100;
+use Test::More tests => 104;
use Test::Exception;
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};
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');
+}