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