Convert all tests to done_testing.
[gitmo/Moose.git] / t / 200_examples / 007_Child_Parent_attr_inherit.t
CommitLineData
b468a3d3 1#!/usr/bin/perl
2
3use strict;
4use warnings;
5
a28e50e4 6use Test::More;
b468a3d3 7
8=pod
9
d03bd989 10Some examples of triggers and how they can
b468a3d3 11be used to manage parent-child relationships.
12
13=cut
14
15{
ca851c73 16
b468a3d3 17 package Parent;
18 use Moose;
ca851c73 19
b468a3d3 20 has 'last_name' => (
ca851c73 21 is => 'rw',
b468a3d3 22 isa => 'Str',
23 trigger => sub {
24 my $self = shift;
ca851c73 25
b468a3d3 26 # if the parents last-name changes
27 # then so do all the childrens
ca851c73 28 foreach my $child ( @{ $self->children } ) {
29 $child->last_name( $self->last_name );
b468a3d3 30 }
31 }
32 );
ca851c73 33
34 has 'children' =>
35 ( is => 'rw', isa => 'ArrayRef', default => sub { [] } );
b468a3d3 36}
37{
ca851c73 38
b468a3d3 39 package Child;
40 use Moose;
ca851c73 41
b468a3d3 42 has 'parent' => (
ca851c73 43 is => 'rw',
44 isa => 'Parent',
45 required => 1,
46 trigger => sub {
b468a3d3 47 my $self = shift;
ca851c73 48
49 # if the parent is changed,..
50 # make sure we update
51 $self->last_name( $self->parent->last_name );
b468a3d3 52 }
53 );
ca851c73 54
b468a3d3 55 has 'last_name' => (
ca851c73 56 is => 'rw',
57 isa => 'Str',
b468a3d3 58 lazy => 1,
59 default => sub { (shift)->parent->last_name }
60 );
ca851c73 61
b468a3d3 62}
63
ca851c73 64my $parent = Parent->new( last_name => 'Smith' );
65isa_ok( $parent, 'Parent' );
b468a3d3 66
ca851c73 67is( $parent->last_name, 'Smith',
68 '... the parent has the last name we expected' );
b468a3d3 69
ca851c73 70$parent->children( [ map { Child->new( parent => $parent ) } ( 0 .. 3 ) ] );
b468a3d3 71
ca851c73 72foreach my $child ( @{ $parent->children } ) {
73 is( $child->last_name, $parent->last_name,
74 '... parent and child have the same last name ('
75 . $parent->last_name
76 . ')' );
b468a3d3 77}
78
79$parent->last_name('Jones');
ca851c73 80is( $parent->last_name, 'Jones', '... the parent has the new last name' );
b468a3d3 81
ca851c73 82foreach my $child ( @{ $parent->children } ) {
83 is( $child->last_name, $parent->last_name,
84 '... parent and child have the same last name ('
85 . $parent->last_name
86 . ')' );
b468a3d3 87}
88
89# make a new parent
90
ca851c73 91my $parent2 = Parent->new( last_name => 'Brown' );
92isa_ok( $parent2, 'Parent' );
b468a3d3 93
ca851c73 94# orphan the child
b468a3d3 95
ca851c73 96my $orphan = pop @{ $parent->children };
b468a3d3 97
ca851c73 98# and then the new parent adopts it
b468a3d3 99
100$orphan->parent($parent2);
101
ca851c73 102foreach my $child ( @{ $parent->children } ) {
103 is( $child->last_name, $parent->last_name,
104 '... parent and child have the same last name ('
105 . $parent->last_name
106 . ')' );
b468a3d3 107}
108
ca851c73 109isnt( $orphan->last_name, $parent->last_name,
110 '... the orphan child does not have the same last name anymore ('
111 . $parent2->last_name
112 . ')' );
113is( $orphan->last_name, $parent2->last_name,
114 '... parent2 and orphan child have the same last name ('
115 . $parent2->last_name
116 . ')' );
b468a3d3 117
118# make sure that changes still will not propagate
119
120$parent->last_name('Miller');
ca851c73 121is( $parent->last_name, 'Miller',
122 '... the parent has the new last name (again)' );
123
124foreach my $child ( @{ $parent->children } ) {
125 is( $child->last_name, $parent->last_name,
126 '... parent and child have the same last name ('
127 . $parent->last_name
128 . ')' );
b468a3d3 129}
130
ca851c73 131isnt( $orphan->last_name, $parent->last_name,
132 '... the orphan child is not affected by changes in the parent anymore' );
133is( $orphan->last_name, $parent2->last_name,
134 '... parent2 and orphan child have the same last name ('
135 . $parent2->last_name
136 . ')' );
a28e50e4 137
138done_testing;