summaryrefslogtreecommitdiff
path: root/t/cmop/attribute_initializer.t
diff options
context:
space:
mode:
Diffstat (limited to 't/cmop/attribute_initializer.t')
-rw-r--r--t/cmop/attribute_initializer.t50
1 files changed, 50 insertions, 0 deletions
diff --git a/t/cmop/attribute_initializer.t b/t/cmop/attribute_initializer.t
new file mode 100644
index 0000000..7d8ca32
--- /dev/null
+++ b/t/cmop/attribute_initializer.t
@@ -0,0 +1,50 @@
+use strict;
+use warnings;
+
+use Scalar::Util 'reftype';
+use Test::More;
+use Class::MOP;
+
+=pod
+
+This checks that the initializer is used to set the initial value.
+
+=cut
+
+{
+ package Foo;
+ use metaclass;
+
+ Foo->meta->add_attribute('bar' =>
+ reader => 'get_bar',
+ writer => 'set_bar',
+ initializer => sub {
+ my ($self, $value, $callback, $attr) = @_;
+
+ ::isa_ok($attr, 'Class::MOP::Attribute');
+ ::is($attr->name, 'bar', '... the attribute is our own');
+
+ $callback->($value * 2);
+ },
+ );
+}
+
+can_ok('Foo', 'get_bar');
+can_ok('Foo', 'set_bar');
+
+my $foo = Foo->meta->new_object(bar => 10);
+is($foo->get_bar, 20, "... initial argument was doubled as expected");
+
+$foo->set_bar(30);
+
+is($foo->get_bar, 30, "... and setter works correctly");
+
+# meta tests ...
+
+my $bar = Foo->meta->get_attribute('bar');
+isa_ok($bar, 'Class::MOP::Attribute');
+
+ok($bar->has_initializer, '... bar has an initializer');
+is(reftype $bar->initializer, 'CODE', '... the initializer is a CODE ref');
+
+done_testing;