Include method name in immutable methods (fixes #49680)
[gitmo/Class-MOP.git] / t / 106_LazyClass_test.t
CommitLineData
4300f56a 1use strict;
2use warnings;
3
efd3d14c 4use Test::More tests => 25;
4300f56a 5use File::Spec;
6
efd3d14c 7BEGIN {use Class::MOP;
10dd437b 8 require_ok(File::Spec->catfile('examples', 'LazyClass.pod'));
4300f56a 9}
10
11{
12 package BinaryTree;
13
1becdfcc 14 use metaclass (
c23184fc 15 'attribute_metaclass' => 'LazyClass::Attribute',
16 'instance_metaclass' => 'LazyClass::Instance',
677eb158 17 );
4300f56a 18
1aeb4c53 19 BinaryTree->meta->add_attribute('node' => (
4300f56a 20 accessor => 'node',
c23184fc 21 init_arg => 'node'
4300f56a 22 ));
23
1aeb4c53 24 BinaryTree->meta->add_attribute('left' => (
4300f56a 25 reader => 'left',
26 default => sub { BinaryTree->new() }
27 ));
28
1aeb4c53 29 BinaryTree->meta->add_attribute('right' => (
4300f56a 30 reader => 'right',
31 default => sub { BinaryTree->new() }
32 ));
33
34 sub new {
35 my $class = shift;
d69fb6b3 36 bless $class->meta->new_object(@_) => $class;
4300f56a 37 }
38}
39
c23184fc 40my $root = BinaryTree->new('node' => 0);
4300f56a 41isa_ok($root, 'BinaryTree');
42
1aeb4c53 43ok(exists($root->{'node'}), '... node attribute has been initialized yet');
44ok(!exists($root->{'left'}), '... left attribute has not been initialized yet');
45ok(!exists($root->{'right'}), '... right attribute has not been initialized yet');
4300f56a 46
47isa_ok($root->left, 'BinaryTree');
48isa_ok($root->right, 'BinaryTree');
49
1aeb4c53 50ok(exists($root->{'left'}), '... left attribute has now been initialized');
51ok(exists($root->{'right'}), '... right attribute has now been initialized');
4300f56a 52
1aeb4c53 53ok(!exists($root->left->{'node'}), '... node attribute has not been initialized yet');
54ok(!exists($root->left->{'left'}), '... left attribute has not been initialized yet');
55ok(!exists($root->left->{'right'}), '... right attribute has not been initialized yet');
4300f56a 56
1aeb4c53 57ok(!exists($root->right->{'node'}), '... node attribute has not been initialized yet');
58ok(!exists($root->right->{'left'}), '... left attribute has not been initialized yet');
59ok(!exists($root->right->{'right'}), '... right attribute has not been initialized yet');
4300f56a 60
61is($root->left->node(), undef, '... the left node is uninitialized');
62
1aeb4c53 63ok(exists($root->left->{'node'}), '... node attribute has now been initialized');
4300f56a 64
65$root->left->node(1);
66is($root->left->node(), 1, '... the left node == 1');
67
1aeb4c53 68ok(!exists($root->left->{'left'}), '... left attribute still has not been initialized yet');
69ok(!exists($root->left->{'right'}), '... right attribute still has not been initialized yet');
4300f56a 70
71is($root->right->node(), undef, '... the right node is uninitialized');
72
1aeb4c53 73ok(exists($root->right->{'node'}), '... node attribute has now been initialized');
4300f56a 74
75$root->right->node(2);
76is($root->right->node(), 2, '... the right node == 1');
77
1aeb4c53 78ok(!exists($root->right->{'left'}), '... left attribute still has not been initialized yet');
79ok(!exists($root->right->{'right'}), '... right attribute still has not been initialized yet');
4300f56a 80