Skip to content

Commit

Permalink
Change the popn/shiftn arg checks to be positive instead of negative.
Browse files Browse the repository at this point in the history
Insted of enumerating all the ways it could go wrong, tell the user how it should go right.

For #149
  • Loading branch information
schwern committed Nov 25, 2011
1 parent 0c2d470 commit 9f64b2e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 16 deletions.
16 changes: 6 additions & 10 deletions lib/perl5i/2/ARRAY.pm
Expand Up @@ -56,12 +56,10 @@ method grep($filter) {
} }


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


# splice() will choke if you walk off the array, so rein it in # splice() will choke if you walk off the array, so rein it in
$times = scalar(@$self) if ($times > scalar(@$self)); $times = scalar(@$self) if ($times > scalar(@$self));
Expand All @@ -71,12 +69,10 @@ method popn($times) {
} }


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


# splice() will choke if you walk off the array, so rein it in # splice() will choke if you walk off the array, so rein it in
$times = scalar(@$self) if ($times > scalar(@$self)); $times = scalar(@$self) if ($times > scalar(@$self));
Expand Down
6 changes: 3 additions & 3 deletions t/popn.t
Expand Up @@ -8,19 +8,19 @@ use Test::More;
note "popn with no args"; { note "popn with no args"; {
my @array = (1, 2, 3); my @array = (1, 2, 3);
throws_ok { @array->popn(); } throws_ok { @array->popn(); }
qr{^\Qpopn() takes a single argument, the number of elements to pop}; qr{^\Qpopn() takes the number of elements to pop at $0 line };
} }


note "popn with negative arg"; { note "popn with negative arg"; {
my @array = (1, 2, 3); my @array = (1, 2, 3);
throws_ok { @array->popn(-20); } throws_ok { @array->popn(-20); }
qr{^\Qpopn() does not take negative arguments at $0 line}; qr{^\Qpopn() takes a positive integer or zero at $0 line };
} }


note "popn with non-numerical argument"; { note "popn with non-numerical argument"; {
my @array = (1, 2, 3); my @array = (1, 2, 3);
throws_ok { @array->popn("rawr"); } throws_ok { @array->popn("rawr"); }
qr{^\Qpopn() takes a numerical argument}; qr{^\Qpopn() takes a positive integer or zero at $0 line };
} }


note "popn with arg == 0"; { note "popn with arg == 0"; {
Expand Down
6 changes: 3 additions & 3 deletions t/shiftn.t
Expand Up @@ -8,19 +8,19 @@ use Test::More;
note "shiftn with no args"; { note "shiftn with no args"; {
my @array = (1, 2, 3); my @array = (1, 2, 3);
throws_ok { @array->shiftn(); } throws_ok { @array->shiftn(); }
qr{^\Qshiftn() takes a single argument, the number of elements to pop}; qr{^\Qshiftn() takes the number of elements to shift at $0 line };
} }


note "shiftn with negative arg"; { note "shiftn with negative arg"; {
my @array = (1, 2, 3); my @array = (1, 2, 3);
throws_ok { @array->shiftn(-20); } throws_ok { @array->shiftn(-20); }
qr{^\Qshiftn() does not take negative arguments at $0 line}; qr{^\Qshiftn() takes a positive integer or zero at $0 line };
} }


note "shiftn with non-numerical argument"; { note "shiftn with non-numerical argument"; {
my @array = (1, 2, 3); my @array = (1, 2, 3);
throws_ok { @array->shiftn("meow"); } throws_ok { @array->shiftn("meow"); }
qr{^\Qshiftn() takes a numerical argument}; qr{^\Qshiftn() takes a positive integer or zero at $0 line };
} }


note "shiftn with arg == 0"; { note "shiftn with arg == 0"; {
Expand Down

0 comments on commit 9f64b2e

Please sign in to comment.