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