Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implemented array methods: first/last_index, slice, range, head and t…
…ail. Docs and tests in too.
- Loading branch information
Showing
6 changed files
with
215 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#!/usr/bin/env perl | ||
use strict; | ||
use warnings; | ||
use Test::More; | ||
use autobox::Core; | ||
|
||
my @array = qw(foo bar baz); | ||
|
||
ok @array->slice(0); | ||
is_deeply [@array->slice(0)], ['foo']; | ||
is_deeply [@array->slice(0,2)], ['foo', 'baz']; | ||
|
||
my @slice = @array->slice(0,1); | ||
|
||
is scalar @slice, 2, "Returns an array in list context"; | ||
|
||
my $slice = @array->slice(0,1); | ||
|
||
is ref $slice, 'ARRAY', "Returns an arrayref in scalar context"; | ||
|
||
done_testing(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
#!/usr/bin/env perl | ||
|
||
use autobox::Core; | ||
use Test::More; | ||
|
||
my @numbers = ( 1 .. 10 ); | ||
|
||
is( @numbers->first_index, 0 ); | ||
is( @numbers->first_index( sub { $_[0] > 9 } ), 9 ); | ||
|
||
is( @numbers->first_index( qr/^2/ ), 1 ); | ||
|
||
done_testing(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#!/usr/bin/env perl | ||
use strict; | ||
use warnings; | ||
use Test::More; | ||
use autobox::Core; | ||
|
||
my @array = qw(foo bar baz); | ||
|
||
ok @array->head; | ||
is @array->head, 'foo'; | ||
|
||
done_testing(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
#!/usr/bin/env perl | ||
use strict; | ||
use warnings; | ||
use Test::More; | ||
use autobox::Core; | ||
|
||
my @array = qw(foo bar baz gorch); | ||
|
||
ok @array->range(0,1); | ||
is_deeply [@array->range(0,1)], ['foo', 'bar']; | ||
|
||
my @slice = @array->range(0,2); | ||
|
||
is scalar @slice, 3, "Returns an array in list context"; | ||
|
||
my $slice = @array->range(0,2); | ||
|
||
is ref $slice, 'ARRAY', "Returns an arrayref in scalar context"; | ||
|
||
done_testing(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
#!/usr/bin/env perl | ||
use strict; | ||
use warnings; | ||
use Test::More; | ||
use Test::Exception; | ||
use autobox::Core; | ||
|
||
my @array = qw(foo bar baz); | ||
|
||
ok @array->tail; | ||
is_deeply [@array->tail], ['bar', 'baz']; | ||
|
||
is_deeply [@array->tail(1)], ['baz']; | ||
is_deeply [@array->tail(2)], ['bar', 'baz']; | ||
is_deeply [@array->tail(3)], ['foo', 'bar', 'baz']; | ||
|
||
throws_ok { @array->tail(4) } qr/^Not enough elements/; | ||
|
||
my @tail = @array->tail; | ||
is scalar @tail, 2, "Returns a list in list context"; | ||
|
||
my $tail = @array->tail; | ||
is ref $tail, 'ARRAY', "Returns an arrayref in scalar context"; | ||
|
||
done_testing(); |