6 use Test::More tests => 27;
9 use Scalar::Util 'isweak';
24 predicate => 'has_parent',
31 predicate => 'has_left',
37 predicate => 'has_right',
40 before 'right', 'left' => sub {
41 my ($self, $tree) = @_;
42 $tree->parent($self) if defined $tree;
46 my ($self, $params) = @_;
47 if ($params->{parent}) {
48 # yeah this is a little
49 # weird I know, but I wanted
50 # to check the weaken stuff
51 # in the constructor :)
52 if ($params->{parent}->has_left) {
53 $params->{parent}->right($self);
56 $params->{parent}->left($self);
62 my $root = BinaryTree->new();
63 isa_ok($root, 'BinaryTree');
65 is($root->left, undef, '... no left node yet');
66 is($root->right, undef, '... no right node yet');
68 ok(!$root->has_left, '... no left node yet');
69 ok(!$root->has_right, '... no right node yet');
71 ok(!$root->has_parent, '... no parent for root node');
73 my $left = BinaryTree->new();
74 isa_ok($left, 'BinaryTree');
76 ok(!$left->has_parent, '... left does not have a parent');
80 is($root->left, $left, '... got a left node now (and it is $left)');
81 ok($root->has_left, '... we have a left node now');
83 ok($left->has_parent, '... lefts has a parent');
84 is($left->parent, $root, '... lefts parent is the root');
86 ok(isweak($left->{parent}), '... parent is a weakened ref');
88 my $right = BinaryTree->new();
89 isa_ok($right, 'BinaryTree');
91 ok(!$right->has_parent, '... right does not have a parent');
95 is($root->right, $right, '... got a right node now (and it is $right)');
96 ok($root->has_right, '... we have a right node now');
98 ok($right->has_parent, '... rights has a parent');
99 is($right->parent, $root, '... rights parent is the root');
101 ok(isweak($right->{parent}), '... parent is a weakened ref');
103 my $left_left = BinaryTree->new(parent => $left);
104 isa_ok($left_left, 'BinaryTree');
106 ok($left_left->has_parent, '... left does have a parent');
108 is($left_left->parent, $left, '... got a parent node (and it is $left)');
109 ok($left->has_left, '... we have a left node now');
110 is($left->left, $left_left, '... got a left node (and it is $left_left)');
112 ok(isweak($left_left->{parent}), '... parent is a weakened ref');