2 # This is automatically generated by author/import-moose-test.pl.
3 # DO NOT EDIT THIS FILE. ANY CHANGES WILL BE LOST!!!
4 use t::lib::MooseCompat;
13 Some examples of triggers and how they can
14 be used to manage parent-child relationships.
29 # if the parents last-name changes
30 # then so do all the childrens
31 foreach my $child ( @{ $self->children } ) {
32 $child->last_name( $self->last_name );
38 ( is => 'rw', isa => 'ArrayRef', default => sub { [] } );
52 # if the parent is changed,..
54 $self->last_name( $self->parent->last_name );
62 default => sub { (shift)->parent->last_name }
67 my $parent = Parent->new( last_name => 'Smith' );
68 isa_ok( $parent, 'Parent' );
70 is( $parent->last_name, 'Smith',
71 '... the parent has the last name we expected' );
73 $parent->children( [ map { Child->new( parent => $parent ) } ( 0 .. 3 ) ] );
75 foreach my $child ( @{ $parent->children } ) {
76 is( $child->last_name, $parent->last_name,
77 '... parent and child have the same last name ('
82 $parent->last_name('Jones');
83 is( $parent->last_name, 'Jones', '... the parent has the new last name' );
85 foreach my $child ( @{ $parent->children } ) {
86 is( $child->last_name, $parent->last_name,
87 '... parent and child have the same last name ('
94 my $parent2 = Parent->new( last_name => 'Brown' );
95 isa_ok( $parent2, 'Parent' );
99 my $orphan = pop @{ $parent->children };
101 # and then the new parent adopts it
103 $orphan->parent($parent2);
105 foreach my $child ( @{ $parent->children } ) {
106 is( $child->last_name, $parent->last_name,
107 '... parent and child have the same last name ('
112 isnt( $orphan->last_name, $parent->last_name,
113 '... the orphan child does not have the same last name anymore ('
114 . $parent2->last_name
116 is( $orphan->last_name, $parent2->last_name,
117 '... parent2 and orphan child have the same last name ('
118 . $parent2->last_name
121 # make sure that changes still will not propagate
123 $parent->last_name('Miller');
124 is( $parent->last_name, 'Miller',
125 '... the parent has the new last name (again)' );
127 foreach my $child ( @{ $parent->children } ) {
128 is( $child->last_name, $parent->last_name,
129 '... parent and child have the same last name ('
134 isnt( $orphan->last_name, $parent->last_name,
135 '... the orphan child is not affected by changes in the parent anymore' );
136 is( $orphan->last_name, $parent2->last_name,
137 '... parent2 and orphan child have the same last name ('
138 . $parent2->last_name