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