6 use Test::More tests => 34;
9 use Scalar::Util 'isweak';
19 has 'node' => (is => 'rw', isa => 'Any');
24 predicate => 'has_parent',
31 predicate => 'has_left',
33 default => sub { BinaryTree->new(parent => $_[0]) },
39 predicate => 'has_right',
41 default => sub { BinaryTree->new(parent => $_[0]) },
44 before 'right', 'left' => sub {
45 my ($self, $tree) = @_;
46 $tree->parent($self) if defined $tree;
50 my $root = BinaryTree->new(node => 'root');
51 isa_ok($root, 'BinaryTree');
53 is($root->node, 'root', '... got the right node value');
55 ok(!$root->has_left, '... no left node yet');
56 ok(!$root->has_right, '... no right node yet');
58 ok(!$root->has_parent, '... no parent for root node');
62 my $left = $root->left;
63 isa_ok($left, 'BinaryTree');
65 is($root->left, $left, '... got the same node (and it is $left)');
66 ok($root->has_left, '... we have a left node now');
68 ok($left->has_parent, '... lefts has a parent');
69 is($left->parent, $root, '... lefts parent is the root');
71 ok(isweak($left->{parent}), '... parent is a weakened ref');
73 ok(!$left->has_left, '... $left no left node yet');
74 ok(!$left->has_right, '... $left no right node yet');
76 is($left->node, undef, '... left has got no node value');
80 } '... assign to lefts node';
82 is($left->node, 'left', '... left now has a node value');
86 ok(!$root->has_right, '... still no right node yet');
88 is($root->right->node, undef, '... right has got no node value');
90 ok($root->has_right, '... now we have a right node');
92 my $right = $root->right;
93 isa_ok($right, 'BinaryTree');
97 } '... assign to rights node';
99 is($right->node, 'right', '... left now has a node value');
101 is($root->right, $right, '... got the same node (and it is $right)');
102 ok($root->has_right, '... we have a right node now');
104 ok($right->has_parent, '... rights has a parent');
105 is($right->parent, $root, '... rights parent is the root');
107 ok(isweak($right->{parent}), '... parent is a weakened ref');
109 my $left_left = $left->left;
110 isa_ok($left_left, 'BinaryTree');
112 ok($left_left->has_parent, '... left does have a parent');
114 is($left_left->parent, $left, '... got a parent node (and it is $left)');
115 ok($left->has_left, '... we have a left node now');
116 is($left->left, $left_left, '... got a left node (and it is $left_left)');
118 ok(isweak($left_left->{parent}), '... parent is a weakened ref');