Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Change the popn/shiftn arg checks to be positive instead of negative.

Insted of enumerating all the ways it could go wrong, tell the user how it should go right.

For #149
  • Loading branch information...
commit 9f64b2e5151500f6e8ef2151cb747ea1ca44e6b2 1 parent 0c2d470
@schwern schwern authored
Showing with 12 additions and 16 deletions.
  1. +6 −10 lib/perl5i/2/ARRAY.pm
  2. +3 −3 t/popn.t
  3. +3 −3 t/shiftn.t
View
16 lib/perl5i/2/ARRAY.pm
@@ -56,12 +56,10 @@ method grep($filter) {
}
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;
- Carp::croak("popn() takes a numerical argument")
- unless $times->is_integer;
- Carp::croak("popn() does not take negative arguments")
- if $times < 0;
+ Carp::croak("popn() takes a positive integer or zero")
+ unless $times->is_integer && ($times->is_positive or $times == 0);
# splice() will choke if you walk off the array, so rein it in
$times = scalar(@$self) if ($times > scalar(@$self));
@@ -71,12 +69,10 @@ method popn($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;
- Carp::croak("shiftn() takes a numerical argument")
- unless $times->is_integer;
- Carp::croak("shiftn() does not take negative arguments")
- if $times < 0;
+ Carp::croak("shiftn() takes a positive integer or zero")
+ unless $times->is_integer && ($times->is_positive or $times == 0);
# splice() will choke if you walk off the array, so rein it in
$times = scalar(@$self) if ($times > scalar(@$self));
View
6 t/popn.t
@@ -8,19 +8,19 @@ use Test::More;
note "popn with no args"; {
my @array = (1, 2, 3);
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"; {
my @array = (1, 2, 3);
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"; {
my @array = (1, 2, 3);
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"; {
View
6 t/shiftn.t
@@ -8,19 +8,19 @@ use Test::More;
note "shiftn with no args"; {
my @array = (1, 2, 3);
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"; {
my @array = (1, 2, 3);
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"; {
my @array = (1, 2, 3);
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"; {
Please sign in to comment.
Something went wrong with that request. Please try again.