Permalink
Browse files

make it name_test, not val_test

  • Loading branch information...
1 parent 840f08e commit 4c11e7f2fccbfd86088dbcb6d34fac93c3828145 @rjbs committed Apr 8, 2011
Showing with 36 additions and 18 deletions.
  1. +8 −8 lib/Data/OptList.pm
  2. +28 −10 t/mkopt.t
View
@@ -153,19 +153,19 @@ sub mkopt {
my ($opt_list) = shift;
my ($moniker, $require_unique, $must_be); # the old positional args
- my $val_test;
+ my $name_test;
if (@_ == 1 and Params::Util::_HASHLIKE($_[0])) {
my $arg = $_[0];
- ($moniker, $require_unique, $must_be, $val_test)
- = @$arg{ qw(moniker require_unique must_be val_test) };
+ ($moniker, $require_unique, $must_be, $name_test)
+ = @$arg{ qw(moniker require_unique must_be name_test) };
} else {
($moniker, $require_unique, $must_be) = @_;
}
return [] unless $opt_list;
- $val_test ||= sub { ref $_[0] };
+ $name_test ||= sub { ! ref $_[0] };
$opt_list = [
map { $_ => (ref $opt_list->{$_} ? $opt_list->{$_} : ()) } keys %$opt_list
@@ -182,10 +182,10 @@ sub mkopt {
Carp::croak "multiple definitions provided for $name" if $seen{$name}++;
}
- if ($i == $#$opt_list) { $value = undef; }
- elsif (not defined $opt_list->[$i+1]) { $value = undef; $i++ }
- elsif ($val_test->($opt_list->[$i+1])) { $value = $opt_list->[++$i] }
- else { $value = undef; }
+ if ($i == $#$opt_list) { $value = undef; }
+ elsif (not defined $opt_list->[$i+1]) { $value = undef; $i++ }
+ elsif ($name_test->($opt_list->[$i+1])) { $value = undef; }
+ else { $value = $opt_list->[++$i] }
if ($must_be and defined $value) {
unless (__is_a($value, $must_be)) {
View
@@ -137,20 +137,23 @@ is_deeply(
"previously tested expansion OK with require_unique",
);
-# This one is complicated. We defined valid values as only hash-like
-# references, so other reference types, like arrayrefs, can be "names."
+# This one is complicated. We defined name values as only non-references (the
+# default) or arrayrefs, so arrayrefs are not turned into values as they
+# usually are. (The subsequent test shows the default expectation.)
# -- rjbs, 2011-04-08
+my @input =(
+ foo => { a => 1 },
+ bar => undef,
+ baz =>
+ xyz => [ 1, 2, 3 ],
+);
+
is_deeply(
mkopt(
- [
- foo => { a => 1 },
- bar => undef,
- baz =>
- xyz => [ 1, 2, 3 ],
- ],
+ [ @input ],
{
- moniker => 'test',
- val_test => sub { Params::Util::_HASHLIKE($_[0]) },
+ moniker => 'test',
+ name_test => sub { ! ref $_[0] or Params::Util::_ARRAYLIKE($_[0]) },
},
),
[
@@ -162,4 +165,19 @@ is_deeply(
],
);
+is_deeply(
+ mkopt(
+ [ @input ],
+ {
+ moniker => 'test',
+ },
+ ),
+ [
+ [ foo => { a => 1 } ],
+ [ bar => undef ],
+ [ baz => undef ],
+ [ xyz => [ 1, 2, 3 ] ],
+ ],
+);
+
done_testing;

0 comments on commit 4c11e7f

Please sign in to comment.