]> git.vpit.fr Git - perl/modules/Sub-Prototype-Util.git/commitdiff
Make flatten() die if not enough arugments were provided to match the given prototype
authorVincent Pit <vince@profvince.com>
Thu, 4 Jun 2009 22:15:33 +0000 (00:15 +0200)
committerVincent Pit <vince@profvince.com>
Thu, 4 Jun 2009 22:15:33 +0000 (00:15 +0200)
Also consider the '_' prototype like '$', as there's no way to know if the argument was originally provided or not.

lib/Sub/Prototype/Util.pm
t/10-flatten.t

index 20525ece0971b354170d655b1ce89b344100467c..316d079bbe85590ba6c35996ae25afb76bc54db5 100644 (file)
@@ -93,9 +93,8 @@ sub flatten {
   } elsif ($p =~ /[\@\%]/) {
    push @args, @_;
    last;
-  } elsif ($p eq '_') {
-   shift; # without prototype, this argument wouldn't have been passed
   } else {
+   croak 'Not enough arguments to match this prototype' unless @_;
    push @args, shift;
   }
  }
index 414cfcf26305652eee0a07960b31dde6a6fd618d..a2b9d6d1caa7c9565dbc1453d575593780e588a9 100644 (file)
@@ -7,14 +7,24 @@ use Test::More tests => 27;
 
 use Sub::Prototype::Util qw/flatten/;
 
+sub exception {
+ my ($msg) = @_;
+ $msg =~ s/\s+/\\s+/g;
+ return qr/^$msg.*?at\s+\Q$0\E\s+line\s+\d+/;
+}
+
+eval { flatten '$' };
+like $@, exception('Not enough arguments to match this prototype'),
+                                                          'flatten("$") croaks';
 eval { flatten '\@', undef };
-like($@, qr/^Got\s+undef/, 'flatten "\@", undef croaks');
+like $@, exception('Got undef'), 'flatten "\@", undef croaks';
 eval { flatten '\@', 1 };
-like($@, qr/^Got\s+a\s+plain\s+scalar/, 'flatten "\@", scalar croaks');
+like $@, exception('Got a plain scalar'), 'flatten "\@", scalar croaks';
 eval { flatten '\@', { foo => 1 } };
-like($@, qr/^Unexpected\s+HASH\s+reference/, 'flatten "\@", hashref croaks');
+like $@, exception('Unexpected HASH reference'), 'flatten "\@", hashref croaks';
 eval { flatten '\@', \(\1) };
-like($@, qr/^Unexpected\s+REF\s+reference/, 'flatten "\@", double ref croaks');
+like $@, exception('Unexpected REF reference'),
+                                              'flatten "\@", double ref croaks';
 
 my $a = [ 1, 2, 3 ];
 my $b = [ [ 1, 2 ], 3, { 4 => 5 }, undef, \6 ];
@@ -41,8 +51,7 @@ my @tests = (
  [ '\[$@%]',   'class got scalarref',    [ \1 ], [ 1 ] ],
  [ '\[$@%]',   'class got arrayref',  [ [ 1 ] ], [ 1 ] ],
  [ '\[$@%]',   'class got hashref', [ { 1,2 } ], [ 1, 2 ] ],
- [ '_',        '_ with argument',      [ 1, 2 ], [ ] ],
- [ '_',        '_ with no argument',        [ ], [ ] ]
+ [ '_',        '_ with argument',      [ 1, 2 ], [ 1 ] ],
 );
 
 is_deeply( [ flatten($_->[0], @{$_->[2]}) ], $_->[3], $_->[1]) for @tests;