Commit | Line | Data |
4300f56a |
1 | use strict; |
2 | use warnings; |
3 | |
efd3d14c |
4 | use Test::More tests => 25; |
4300f56a |
5 | use File::Spec; |
6 | |
efd3d14c |
7 | BEGIN {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; |
36 | bless $class->meta->construct_instance(@_) => $class; |
37 | } |
38 | } |
39 | |
c23184fc |
40 | my $root = BinaryTree->new('node' => 0); |
4300f56a |
41 | isa_ok($root, 'BinaryTree'); |
42 | |
1aeb4c53 |
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'); |
4300f56a |
46 | |
47 | isa_ok($root->left, 'BinaryTree'); |
48 | isa_ok($root->right, 'BinaryTree'); |
49 | |
1aeb4c53 |
50 | ok(exists($root->{'left'}), '... left attribute has now been initialized'); |
51 | ok(exists($root->{'right'}), '... right attribute has now been initialized'); |
4300f56a |
52 | |
1aeb4c53 |
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'); |
4300f56a |
56 | |
1aeb4c53 |
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'); |
4300f56a |
60 | |
61 | is($root->left->node(), undef, '... the left node is uninitialized'); |
62 | |
1aeb4c53 |
63 | ok(exists($root->left->{'node'}), '... node attribute has now been initialized'); |
4300f56a |
64 | |
65 | $root->left->node(1); |
66 | is($root->left->node(), 1, '... the left node == 1'); |
67 | |
1aeb4c53 |
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'); |
4300f56a |
70 | |
71 | is($root->right->node(), undef, '... the right node is uninitialized'); |
72 | |
1aeb4c53 |
73 | ok(exists($root->right->{'node'}), '... node attribute has now been initialized'); |
4300f56a |
74 | |
75 | $root->right->node(2); |
76 | is($root->right->node(), 2, '... the right node == 1'); |
77 | |
1aeb4c53 |
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'); |
4300f56a |
80 | |