Require Dist::Zilla 4.200016+
[gitmo/Moose.git] / t / native_traits / trait_array.t
index decf151..f1f7b0c 100644 (file)
@@ -53,6 +53,8 @@ use Test::Moose;
         grep_curried  => [ grep => ( sub { $_ < 5 } ) ],
         first         => 'first',
         first_curried => [ first => ( sub { $_ % 2 } ) ],
+        first_index   => 'first_index',
+        first_index_curried => [ first_index => ( sub { $_ % 2 } ) ],
         join          => 'join',
         join_curried => [ join => '-' ],
         shuffle      => 'shuffle',
@@ -106,6 +108,19 @@ use Test::Moose;
 }
 
 {
+    package OverloadStr;
+    use overload
+        q{""} => sub { ${ $_[0] } },
+        fallback => 1;
+
+    sub new {
+        my $class = shift;
+        my $str   = shift;
+        return bless \$str, $class;
+    }
+}
+
+{
     run_tests(build_class);
     run_tests( build_class( lazy => 1, default => sub { [ 42, 84 ] } ) );
     run_tests( build_class( trigger => sub { } ) );
@@ -523,6 +538,32 @@ sub run_tests {
             $obj->first_curried( sub { } );
         }, qr/Cannot call first with more than 1 argument/, 'throws an error when passing one argument passed to first_curried' );
 
+
+        is(
+            $obj->first_index( sub { $_ % 2 } ),
+            3,
+            'first_index returns expected value'
+        );
+
+        like( exception { $obj->first_index }, qr/Cannot call first_index without at least 1 argument/, 'throws an error when passing no arguments to first_index' );
+
+        like( exception {
+            $obj->first_index( sub { }, 2 );
+        }, qr/Cannot call first_index with more than 1 argument/, 'throws an error when passing two arguments to first_index' );
+
+        like( exception { $obj->first_index( {} ) }, qr/The argument passed to first_index must be a code reference/, 'throws an error when passing a non coderef to first_index' );
+
+        is(
+            $obj->first_index_curried,
+            3,
+            'first_index_curried returns expected value'
+        );
+
+        like( exception {
+            $obj->first_index_curried( sub { } );
+        }, qr/Cannot call first_index with more than 1 argument/, 'throws an error when passing one argument passed to first_index_curried' );
+
+
         $obj->_values( [ 1 .. 4 ] );
 
         is(
@@ -535,6 +576,11 @@ sub run_tests {
             'join returns expected result when joining with empty string'
         );
 
+        is(
+            $obj->join( OverloadStr->new(q{}) ), '1234',
+            'join returns expected result when joining with empty string'
+        );
+
         like( exception { $obj->join }, qr/Cannot call join without at least 1 argument/, 'throws an error when passing no arguments to join' );
 
         like( exception { $obj->join( '-', 2 ) }, qr/Cannot call join with more than 1 argument/, 'throws an error when passing two arguments to join' );
@@ -669,4 +715,22 @@ sub run_tests {
     $class;
 }
 
+{
+    my ( $class, $handles ) = build_class( isa => 'ArrayRef' );
+    my $obj = $class->new;
+    with_immutable {
+        is(
+            exception { $obj->accessor( 0, undef ) },
+            undef,
+            'can use accessor to set value to undef'
+        );
+        is(
+            exception { $obj->accessor_curried_1(undef) },
+            undef,
+            'can use curried accessor to set value to undef'
+        );
+    }
+    $class;
+}
+
 done_testing;