cooking-with-gas
[gitmo/Moose.git] / t / 003_basic.t
CommitLineData
e5ebe4ce 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
7c6cacb4 6use Test::More tests => 25;
e5ebe4ce 7use Test::Exception;
8
a15dff8d 9use Scalar::Util 'isweak';
10
e5ebe4ce 11BEGIN {
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',
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
50my $root = BinaryTree->new();
51isa_ok($root, 'BinaryTree');
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
7c6cacb4 74# make a right node
e5ebe4ce 75
7c6cacb4 76my $right = $root->right;
77isa_ok($right, 'BinaryTree');
e5ebe4ce 78
7c6cacb4 79is($root->right, $right, '... got the same node (and it is $right)');
e5ebe4ce 80ok($root->has_right, '... we have a right node now');
81
82ok($right->has_parent, '... rights has a parent');
83is($right->parent, $root, '... rights parent is the root');
a15dff8d 84
85ok(isweak($right->{parent}), '... parent is a weakened ref');
6ba6d68c 86
7c6cacb4 87my $left_left = $left->left;
6ba6d68c 88isa_ok($left_left, 'BinaryTree');
89
90ok($left_left->has_parent, '... left does have a parent');
91
92is($left_left->parent, $left, '... got a parent node (and it is $left)');
93ok($left->has_left, '... we have a left node now');
94is($left->left, $left_left, '... got a left node (and it is $left_left)');
95
96ok(isweak($left_left->{parent}), '... parent is a weakened ref');
97