Commit | Line | Data |
e5ebe4ce |
1 | #!/usr/bin/perl |
2 | |
3 | use strict; |
4 | use warnings; |
5 | |
a15dff8d |
6 | use Test::More tests => 21; |
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; |
17 | use strict; |
18 | use warnings; |
19 | use Moose; |
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', |
31 | predicate => 'has_left', |
e5ebe4ce |
32 | ); |
33 | |
a15dff8d |
34 | has 'right' => ( |
cc65ead0 |
35 | is => 'rw', |
29db16a9 |
36 | isa => 'BinaryTree', |
37 | predicate => 'has_right', |
e5ebe4ce |
38 | ); |
39 | |
40 | before 'right', 'left' => sub { |
41 | my ($self, $tree) = @_; |
42 | $tree->parent($self) if defined $tree; |
43 | }; |
44 | } |
45 | |
46 | my $root = BinaryTree->new(); |
47 | isa_ok($root, 'BinaryTree'); |
48 | |
49 | is($root->left, undef, '... no left node yet'); |
50 | is($root->right, undef, '... no right node yet'); |
51 | |
52 | ok(!$root->has_left, '... no left node yet'); |
53 | ok(!$root->has_right, '... no right node yet'); |
54 | |
a15dff8d |
55 | ok(!$root->has_parent, '... no parent for root node'); |
56 | |
e5ebe4ce |
57 | my $left = BinaryTree->new(); |
58 | isa_ok($left, 'BinaryTree'); |
59 | |
60 | ok(!$left->has_parent, '... left does not have a parent'); |
61 | |
62 | $root->left($left); |
63 | |
64 | is($root->left, $left, '... got a left node now (and it is $left)'); |
65 | ok($root->has_left, '... we have a left node now'); |
66 | |
67 | ok($left->has_parent, '... lefts has a parent'); |
68 | is($left->parent, $root, '... lefts parent is the root'); |
69 | |
a15dff8d |
70 | ok(isweak($left->{parent}), '... parent is a weakened ref'); |
71 | |
e5ebe4ce |
72 | my $right = BinaryTree->new(); |
73 | isa_ok($right, 'BinaryTree'); |
74 | |
75 | ok(!$right->has_parent, '... right does not have a parent'); |
76 | |
77 | $root->right($right); |
78 | |
79 | is($root->right, $right, '... got a right node now (and it is $right)'); |
80 | ok($root->has_right, '... we have a right node now'); |
81 | |
82 | ok($right->has_parent, '... rights has a parent'); |
83 | is($right->parent, $root, '... rights parent is the root'); |
a15dff8d |
84 | |
85 | ok(isweak($right->{parent}), '... parent is a weakened ref'); |