cycle stuff
[gitmo/MooseX-Storage.git] / t / 004_w_cycles.t
index d08c9ae..3cb3a0b 100644 (file)
@@ -10,6 +10,16 @@ BEGIN {
     use_ok('MooseX::Storage');
 }
 
+=pod
+
+This test demonstrates two things:
+
+- cycles will not work in the default engine
+- you can use a special metaclass to tell 
+  MooseX::Storage to skip an attribute
+
+=cut
+
 {
 
     package Circular;
@@ -43,5 +53,81 @@ BEGIN {
     '... cannot expand a cycle with the basic engine';
 }
 
+{
+
+    package Tree;
+    use Moose;
+    use MooseX::Storage;
+
+    with Storage;
+
+    has 'node' => (is => 'rw');
+    
+    has 'children' => (
+        is      => 'ro', 
+        isa     => 'ArrayRef', 
+        default => sub {[]}
+    );
+    
+    has 'parent' => (
+        metaclass => 'MooseX::Storage::Meta::Attribute::DoNotSerialize',
+        is        => 'rw', 
+        isa       => 'Tree',
+    );
+    
+    sub add_child {
+        my ($self, $child) = @_;
+        $child->parent($self);
+        push @{$self->children} => $child;
+    }
+}
+
+{
+    my $t = Tree->new(node => 100);
+    isa_ok($t, 'Tree');
+    
+    is_deeply(
+        $t->pack, 
+        {
+            __CLASS__ => 'Tree',
+            node      => 100,
+            children  => [],
+        },
+    '... got the right packed version');
+    
+    my $t2 = Tree->new(node => 200);
+    isa_ok($t2, 'Tree');    
+    
+    $t->add_child($t2);
+    
+    is_deeply($t->children, [ $t2 ], '... got the right children in $t');
+    
+    is($t2->parent, $t, '... created the cycle correctly');
+    isa_ok($t2->parent, 'Tree');        
+    
+    is_deeply(
+        $t->pack, 
+        {
+            __CLASS__ => 'Tree',
+            node      => 100,
+            children  => [
+               {
+                   __CLASS__ => 'Tree',
+                   node      => 200,
+                   children  => [],            
+               } 
+            ],
+        },
+    '... got the right packed version (with parent attribute skipped in child)');    
+    
+    is_deeply(
+        $t2->pack, 
+        {
+            __CLASS__ => 'Tree',
+            node      => 200,
+            children  => [],            
+        },
+    '... got the right packed version (with parent attribute skipped)');
+}