add shallow_clone to Array and Hash traits
[gitmo/Moose.git] / t / native_traits / shallow_clone.t
diff --git a/t/native_traits/shallow_clone.t b/t/native_traits/shallow_clone.t
new file mode 100644 (file)
index 0000000..bdcf005
--- /dev/null
@@ -0,0 +1,45 @@
+#!/usr/bin/perl
+
+use strict;
+use warnings;
+
+use Test::More;
+use Test::Fatal;
+use Scalar::Util qw(refaddr);
+
+{
+    package Foo;
+    use Moose;
+
+    has 'array' => (
+        traits  => ['Array'],
+        is      => 'ro',
+        handles => { array_clone => 'shallow_clone' },
+    );
+
+    has 'hash' => (
+        traits  => ['Hash'],
+        is      => 'ro',
+        handles => { hash_clone => 'shallow_clone' },
+    );
+
+    no Moose;
+}
+
+my $array = [ 1, 2, 3 ];
+my $hash  = { a => 1, b => 2 };
+
+my $obj = Foo->new({
+  array => $array,
+  hash  => $hash,
+});
+
+my $array_clone = $obj->array_clone;
+my $hash_clone  = $obj->hash_clone;
+
+isnt(refaddr($array), refaddr($array_clone), "array clone refers to new copy");
+is_deeply($array_clone, $array, "...but contents are the same");
+isnt(refaddr($hash),  refaddr($hash_clone),  "hash clone refers to new copy");
+is_deeply($hash_clone, $hash, "...but contents are the same");
+
+done_testing;