Skip to content

Commit

Permalink
Change shitfn/popn() and shiftn/popn(negative) to be errors.
Browse files Browse the repository at this point in the history
For #149
  • Loading branch information
schwern committed Nov 24, 2011
1 parent c9ff917 commit 5f10f01
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 35 deletions.
16 changes: 14 additions & 2 deletions lib/perl5i/2/ARRAY.pm
Expand Up @@ -56,15 +56,27 @@ method grep($filter) {
}

method popn($times) {
$times = 0 unless ($times && $times > 0);
Carp::croak("popn() takes a single argument, the number of elements to pop")
unless defined $times;
Carp::croak("popn() does not take negative arguments")
if $times < 0;

# splice() will choke if you walk off the array, so rein it in
$times = scalar(@$self) if ($times > scalar(@$self));

my @result = splice(@$self, -$times, $times);
return wantarray ? @result : \@result;
}

method shiftn($times) {
$times = 0 unless ($times && $times > 0);
Carp::croak("shiftn() takes a single argument, the number of elements to pop")
unless defined $times;
Carp::croak("shiftn() does not take negative arguments")
if $times < 0;

# splice() will choke if you walk off the array, so rein it in
$times = scalar(@$self) if ($times > scalar(@$self));

my @result = splice(@$self, 0, $times);
return wantarray ? @result : \@result;
}
Expand Down
22 changes: 7 additions & 15 deletions t/popn.t
@@ -1,28 +1,20 @@
#!/usr/bin/perl

use lib 't/lib';
use perl5i::latest;
use Test::perl5i;
use Test::More;

note "popn with no args"; {
my @array = (1, 2, 3);
my @newarray = @array->popn();

my @want = (1, 2, 3);
my @newwant = ();

is_deeply \@array, \@want;
is_deeply \@newarray, \@newwant;
throws_ok { @array->popn(); }
qr{^\Qpopn() takes a single argument, the number of elements to pop};
}

note "popn with arg < 0"; {
note "popn with negative arg"; {
my @array = (1, 2, 3);
my @newarray = @array->popn(-20);

my @want = (1, 2, 3);
my @newwant = ();

is_deeply \@array, \@want;
is_deeply \@newarray, \@newwant;
throws_ok { @array->popn(-20); }
qr{^\Qpopn() does not take negative arguments at $0 line};
}

note "popn with arg == 0"; {
Expand Down
29 changes: 11 additions & 18 deletions t/shiftn.t
@@ -1,29 +1,22 @@
#!/usr/bin/perl

use lib 't/lib';
use Test::perl5i;
use perl5i::2;
use Test::More;

note "shiftn with no args"; {
my @array = (1, 2, 3, 4);
my @newarray = @array->shiftn();

my @want = (1, 2, 3, 4);
my @newwant = ();

is_deeply \@array, \@want;
is_deeply \@newarray, \@newwant;
my @array = (1, 2, 3);
throws_ok { @array->shiftn(); }
qr{^\Qshiftn() takes a single argument, the number of elements to pop};
}

note "shiftn with arg < 0"; {
my @array = (1, 2, 3, 4, 5);
my @newarray = @array->shiftn(-20);

my @want = (1, 2, 3, 4, 5);
my @newwant = ();

is_deeply \@array, \@want;
is_deeply \@newarray, \@newwant;
}
note "shiftn with negative arg"; {
my @array = (1, 2, 3);
throws_ok { @array->shiftn(-20); }
qr{^\Qshiftn() does not take negative arguments at $0 line};
}


note "shiftn with arg == 0"; {
my @array = (1, 2, 3);
Expand Down

0 comments on commit 5f10f01

Please sign in to comment.