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