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