Add tests for triggers on a native Hash trait attr
Dave Rolsky [Sat, 25 Sep 2010 23:23:40 +0000 (18:23 -0500)]
t/070_native_traits/052_hash_trigger.t [new file with mode: 0644]

diff --git a/t/070_native_traits/052_hash_trigger.t b/t/070_native_traits/052_hash_trigger.t
new file mode 100644 (file)
index 0000000..26f343c
--- /dev/null
@@ -0,0 +1,55 @@
+use strict;
+use warnings;
+
+use Test::More;
+use Test::Exception;
+
+{
+
+    package Foo;
+    use Moose;
+
+    our @TriggerArgs;
+
+    has hash => (
+        traits  => ['Hash'],
+        is      => 'rw',
+        isa     => 'HashRef',
+        handles => {
+            delete_key => 'delete',
+            set_key    => 'set',
+        },
+        clearer => 'clear_key',
+        trigger => sub { @TriggerArgs = @_ },
+    );
+}
+
+my $foo = Foo->new;
+
+{
+    $foo->hash( { x => 1, y => 2 } );
+
+    is_deeply(
+        \@Foo::TriggerArgs,
+        [ $foo, { x => 1, y => 2 } ],
+        'trigger was called for normal writer'
+    );
+
+    $foo->set_key( z => 5 );
+
+    is_deeply(
+        \@Foo::TriggerArgs,
+        [ $foo, { x => 1, y => 2, z => 5 }, { x => 1, y => 2 } ],
+        'trigger was called on set'
+    );
+
+    $foo->delete_key('y');
+
+    is_deeply(
+        \@Foo::TriggerArgs,
+        [ $foo, { x => 1, z => 5 }, { x => 1, y => 2, z => 5 } ],
+        'trigger was called on delete'
+    );
+}
+
+done_testing;