4 use Test::More tests => 18;
9 use_ok('MooseX::Storage');
12 # This test demonstrates two things:
14 # - cycles will not work in the default engine
15 # - you can use a special metaclass to tell MooseX::Storage to skip an attribute
25 has 'cycle' => (is => 'rw', isa => 'Circular');
29 my $circular = Circular->new;
30 isa_ok($circular, 'Circular');
32 $circular->cycle($circular);
36 }, qr/^Basic Engine does not support cycles/,
37 '... cannot collapse a cycle with the basic engine');
41 my $packed_circular = { __CLASS__ => 'Circular' };
42 $packed_circular->{cycle} = $packed_circular;
45 Circular->unpack($packed_circular);
46 }, qr/^Basic Engine does not support cycles/,
47 '... cannot expand a cycle with the basic engine');
58 has 'node' => (is => 'rw');
67 metaclass => 'DoNotSerialize',
73 my ($self, $child) = @_;
74 $child->parent($self);
75 push @{$self->children} => $child;
80 my $t = Tree->new(node => 100);
90 '... got the right packed version');
92 my $t2 = Tree->new(node => 200);
97 cmp_deeply($t->children, [ $t2 ], '... got the right children in $t');
99 is($t2->parent, $t, '... created the cycle correctly');
100 isa_ok($t2->parent, 'Tree');
115 '... got the right packed version (with parent attribute skipped in child)');
124 '... got the right packed version (with parent attribute skipped)');
127 ### this fails with cycle detection on
133 has 'x' => ( is => 'rw', isa => 'HashRef' );
134 has 'y' => ( is => 'rw', isa => 'HashRef' );
139 my $double = Double->new( 'x' => $ref, 'y' => $ref );
141 ### currently, the cycle checker's too naive to figure out this is not
142 ### a problem, pass an empty hashref to the 2nd test to make sure it
145 local $TODO = "Cycle check is too naive";
146 my $pack = eval { $double->pack; };
147 ok( $pack, "Object with 2 references packed" );
148 ok( Double->unpack( $pack || {} ),
149 " And unpacked again" );
152 my $pack = $double->pack( engine_traits => [qw/DisableCycleDetection/] );
153 ok( $pack, " Object packs when cycle check is disabled");
154 ok( Double->unpack( $pack ),
155 " And unpacked again" );
159 ### the same as above, but now done with a trait
160 ### this fails with cycle detection on
161 { package DoubleNoCycle;
164 with Storage( traits => ['DisableCycleDetection'] );
166 has 'x' => ( is => 'rw', isa => 'HashRef' );
167 has 'y' => ( is => 'rw', isa => 'HashRef' );
172 my $double = DoubleNoCycle->new( 'x' => $ref, 'y' => $ref );
173 my $pack = $double->pack;
174 ok( $pack, "Object packs with DisableCycleDetection trait");
175 ok( DoubleNoCycle->unpack( $pack ),