6 use Test::More tests => 33;
9 use Scalar::Util 'isweak';
15 has 'node' => ( is => 'rw', isa => 'Any' );
20 predicate => 'has_parent',
27 predicate => 'has_left',
29 default => sub { BinaryTree->new( parent => $_[0] ) },
35 predicate => 'has_right',
37 default => sub { BinaryTree->new( parent => $_[0] ) },
40 before 'right', 'left' => sub {
41 my ( $self, $tree ) = @_;
42 $tree->parent($self) if defined $tree;
45 __PACKAGE__->meta->make_immutable( debug => 0 );
48 my $root = BinaryTree->new(node => 'root');
49 isa_ok($root, 'BinaryTree');
51 is($root->node, 'root', '... got the right node value');
53 ok(!$root->has_left, '... no left node yet');
54 ok(!$root->has_right, '... no right node yet');
56 ok(!$root->has_parent, '... no parent for root node');
60 my $left = $root->left;
61 isa_ok($left, 'BinaryTree');
63 is($root->left, $left, '... got the same node (and it is $left)');
64 ok($root->has_left, '... we have a left node now');
66 ok($left->has_parent, '... lefts has a parent');
67 is($left->parent, $root, '... lefts parent is the root');
69 ok(isweak($left->{parent}), '... parent is a weakened ref');
71 ok(!$left->has_left, '... $left no left node yet');
72 ok(!$left->has_right, '... $left no right node yet');
74 is($left->node, undef, '... left has got no node value');
78 } '... assign to lefts node';
80 is($left->node, 'left', '... left now has a node value');
84 ok(!$root->has_right, '... still no right node yet');
86 is($root->right->node, undef, '... right has got no node value');
88 ok($root->has_right, '... now we have a right node');
90 my $right = $root->right;
91 isa_ok($right, 'BinaryTree');
95 } '... assign to rights node';
97 is($right->node, 'right', '... left now has a node value');
99 is($root->right, $right, '... got the same node (and it is $right)');
100 ok($root->has_right, '... we have a right node now');
102 ok($right->has_parent, '... rights has a parent');
103 is($right->parent, $root, '... rights parent is the root');
105 ok(isweak($right->{parent}), '... parent is a weakened ref');
107 my $left_left = $left->left;
108 isa_ok($left_left, 'BinaryTree');
110 ok($left_left->has_parent, '... left does have a parent');
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)');
116 ok(isweak($left_left->{parent}), '... parent is a weakened ref');