remove trailing whitespace
[gitmo/Moose.git] / t / 020_attributes / 004_attribute_triggers.t
index 637d604..e799399 100644 (file)
@@ -5,7 +5,7 @@ use warnings;
 
 use Scalar::Util 'isweak';
 
-use Test::More tests => 36;
+use Test::More tests => 40;
 use Test::Exception;
 
 
@@ -13,32 +13,32 @@ use Test::Exception;
 {
     package Foo;
     use Moose;
-    
-    has 'bar' => (is      => 'rw', 
+
+    has 'bar' => (is      => 'rw',
                   isa     => 'Maybe[Bar]',
-                  trigger => sub { 
+                  trigger => sub {
                       my ($self, $bar) = @_;
                       $bar->foo($self) if defined $bar;
                   });
-                  
+
     has 'baz' => (writer => 'set_baz',
                   reader => 'get_baz',
                   isa    => 'Baz',
-                  trigger => sub { 
+                  trigger => sub {
                       my ($self, $baz) = @_;
                       $baz->foo($self);
-                  });              
-     
-                  
+                  });
+
+
     package Bar;
     use Moose;
-    
-    has 'foo' => (is => 'rw', isa => 'Foo', weak_ref => 1);           
-    
+
+    has 'foo' => (is => 'rw', isa => 'Foo', weak_ref => 1);
+
     package Baz;
     use Moose;
-    
-    has 'foo' => (is => 'rw', isa => 'Foo', weak_ref => 1);           
+
+    has 'foo' => (is => 'rw', isa => 'Foo', weak_ref => 1);
 }
 
 {
@@ -59,13 +59,13 @@ use Test::Exception;
     is($bar->foo, $foo, '... which in turn set the value bar.foo correctly');
 
     ok(isweak($bar->{foo}), '... bar.foo is a weak reference');
-    
+
     lives_ok {
         $foo->bar(undef);
     } '... did not die un-setting bar';
 
     is($foo->bar, undef, '... set the value foo.bar correctly');
-    is($bar->foo, $foo, '... which in turn set the value bar.foo correctly');    
+    is($bar->foo, $foo, '... which in turn set the value bar.foo correctly');
 
     # test the writer
 
@@ -85,9 +85,9 @@ use Test::Exception;
 
     my $baz = Baz->new;
     isa_ok($baz, 'Baz');
-    
+
     my $foo = Foo->new(bar => $bar, baz => $baz);
-    isa_ok($foo, 'Foo');    
+    isa_ok($foo, 'Foo');
 
     is($foo->bar, $bar, '... set the value foo.bar correctly');
     is($bar->foo, $foo, '... which in turn set the value bar.foo correctly');
@@ -105,14 +105,14 @@ use Test::Exception;
 {
     package Bling;
     use Moose;
-    
-    ::dies_ok { 
+
+    ::dies_ok {
         has('bling' => (is => 'rw', trigger => 'Fail'));
     } '... a trigger must be a CODE ref';
-    
-    ::dies_ok { 
+
+    ::dies_ok {
         has('bling' => (is => 'rw', trigger => []));
-    } '... a trigger must be a CODE ref';    
+    } '... a trigger must be a CODE ref';
 }
 
 # Triggers do not fire on built values
@@ -158,3 +158,33 @@ use Test::Exception;
     is_deeply(\%Blarg::trigger_vals, { map { $_ => "Yet another $_ value" } qw/foo bar baz/ }, 'All triggers given assigned values');
 }
 
+# Triggers do not receive the meta-attribute as an argument
+
+{
+    package Foo;
+    use Moose;
+    our @calls;
+    has foo => (is => 'rw', trigger => sub { push @calls, [@_] });
+}
+
+{
+    my $attr = Foo->meta->get_attribute('foo');
+    my $foo = Foo->new(foo => 2);
+    is_deeply(
+        \@Foo::calls,
+        [ [ $foo, 2 ] ],
+        'trigger called correctly on construction',
+    );
+    @Foo::calls = ();
+
+    $foo->foo(3);
+    is_deeply(
+        \@Foo::calls,
+        [ [ $foo, 3 ] ],
+        'trigger called correctly on set',
+    );
+    @Foo::calls = ();
+    Foo->meta->make_immutable, redo if Foo->meta->is_mutable;
+}
+
+