Moose now warns when you try to load it from the main package. Added a
[gitmo/Moose.git] / t / 000_recipes / basics / 003_binary_tree.t
1 #!/usr/bin/perl
2
3 use strict;
4 use warnings;
5
6 use Test::More tests => 33;
7 use Test::Exception;
8
9 use Scalar::Util 'isweak';
10
11 {
12     package BinaryTree;
13     use Moose;
14
15     has 'node' => (is => 'rw', isa => 'Any');
16
17     has 'parent' => (
18                 is        => 'rw',
19                 isa       => 'BinaryTree',      
20         predicate => 'has_parent',
21                 weak_ref  => 1,
22     );
23
24     has 'left' => (
25                 is        => 'rw',      
26                 isa       => 'BinaryTree',              
27         predicate => 'has_left',  
28         lazy      => 1,
29         default   => sub { BinaryTree->new(parent => $_[0]) },       
30     );
31
32     has 'right' => (
33                 is        => 'rw',      
34                 isa       => 'BinaryTree',              
35         predicate => 'has_right',   
36         lazy      => 1,       
37         default   => sub { BinaryTree->new(parent => $_[0]) },       
38     );
39
40     before 'right', 'left' => sub {
41         my ($self, $tree) = @_;
42             $tree->parent($self) if defined $tree;   
43         };
44         
45     __PACKAGE__->meta->make_immutable(debug => 0);      
46 }
47
48 my $root = BinaryTree->new(node => 'root');
49 isa_ok($root, 'BinaryTree');
50
51 is($root->node, 'root', '... got the right node value');
52
53 ok(!$root->has_left, '... no left node yet');
54 ok(!$root->has_right, '... no right node yet');
55
56 ok(!$root->has_parent, '... no parent for root node');
57
58 # make a left node
59
60 my $left = $root->left;
61 isa_ok($left, 'BinaryTree');
62
63 is($root->left, $left, '... got the same node (and it is $left)');
64 ok($root->has_left, '... we have a left node now');
65
66 ok($left->has_parent, '... lefts has a parent');
67 is($left->parent, $root, '... lefts parent is the root');
68
69 ok(isweak($left->{parent}), '... parent is a weakened ref');
70
71 ok(!$left->has_left, '... $left no left node yet');
72 ok(!$left->has_right, '... $left no right node yet');
73
74 is($left->node, undef, '... left has got no node value');
75
76 lives_ok {
77     $left->node('left')
78 } '... assign to lefts node';
79
80 is($left->node, 'left', '... left now has a node value');
81
82 # make a right node
83
84 ok(!$root->has_right, '... still no right node yet');
85
86 is($root->right->node, undef, '... right has got no node value');
87
88 ok($root->has_right, '... now we have a right node');
89
90 my $right = $root->right;
91 isa_ok($right, 'BinaryTree');
92
93 lives_ok {
94     $right->node('right')
95 } '... assign to rights node';
96
97 is($right->node, 'right', '... left now has a node value');
98
99 is($root->right, $right, '... got the same node (and it is $right)');
100 ok($root->has_right, '... we have a right node now');
101
102 ok($right->has_parent, '... rights has a parent');
103 is($right->parent, $root, '... rights parent is the root');
104
105 ok(isweak($right->{parent}), '... parent is a weakened ref');
106
107 my $left_left = $left->left;
108 isa_ok($left_left, 'BinaryTree');
109
110 ok($left_left->has_parent, '... left does have a parent');
111
112 is($left_left->parent, $left, '... got a parent node (and it is $left)');
113 ok($left->has_left, '... we have a left node now');
114 is($left->left, $left_left, '... got a left node (and it is $left_left)');
115
116 ok(isweak($left_left->{parent}), '... parent is a weakened ref');
117