From: Lukas Mai Date: Sun, 21 Oct 2012 17:05:13 +0000 (+0200) Subject: add ':strict' import shortcut X-Git-Tag: v0.10~3 X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=fcaf7811b0caec1b00651fbcf9d1915fe1be008f;p=p5sagit%2FFunction-Parameters.git add ':strict' import shortcut --- diff --git a/MANIFEST b/MANIFEST index a03a2cd..cd62cf6 100644 --- a/MANIFEST +++ b/MANIFEST @@ -14,6 +14,7 @@ t/attrs.t t/bonus.t t/checkered.t t/checkered_2.t +t/checkered_3.t t/defaults.t t/defaults_regress.t t/eating_strict_error.fail diff --git a/lib/Function/Parameters.pm b/lib/Function/Parameters.pm index 36e686c..c3d7125 100644 --- a/lib/Function/Parameters.pm +++ b/lib/Function/Parameters.pm @@ -58,13 +58,20 @@ for my $k (keys %type_map) { sub import { my $class = shift; - @_ or @_ = { - fun => 'function', - method => 'method', - }; + if (!@_) { + @_ = { + fun => 'function', + method => 'method', + }; + } + if (@_ == 1 && $_[0] eq ':strict') { + @_ = { + fun => 'function_strict', + method => 'method_strict', + }; + } if (@_ == 1 && ref($_[0]) eq 'HASH') { - @_ = map [$_, $_[0]{$_}], keys %{$_[0]} - or return; + @_ = map [$_, $_[0]{$_}], keys %{$_[0]}; } my %spec; @@ -267,6 +274,12 @@ The following shortcuts are available: =pod + use Function::Parameters ':strict'; + # is equivalent to # + use Function::Parameters { fun => 'function_strict', method => 'method_strict' }; + +=pod + The following shortcuts are deprecated and may be removed from a future version of this module: @@ -284,10 +297,10 @@ of this module: # is equivalent to # use Function::Parameters { 'foo' => 'function', 'bar' => 'method' }; -That is, if you want to pass arguments to L, use a -hashref, not a list of strings. +That is, if you want to create custom keywords with L, +use a hashref, not a list of strings. -You can customize the properties of the generated keywords even more by passing +You can tune the properties of the generated keywords even more by passing a hashref instead of a string. This hash can have the following keys: =over diff --git a/t/checkered_3.t b/t/checkered_3.t new file mode 100644 index 0000000..4c474de --- /dev/null +++ b/t/checkered_3.t @@ -0,0 +1,114 @@ +#!perl + +use Test::More tests => 101; + +use warnings FATAL => 'all'; +use strict; + +use Function::Parameters qw(:strict); + +fun error_like($re, $body, $name = undef) { + local $@; + ok !eval { $body->(); 1 }; + like $@, $re, $name; +} + +fun foo_any { [@_] } +fun foo_any_a(@args) { [@args] } +fun foo_any_b($x = undef, @rest) { [@_] } +fun foo_0() { [@_] } +fun foo_1($x) { [@_] } +fun foo_2($x, $y) { [@_] } +fun foo_3($x, $y, $z) { [@_] } +fun foo_0_1($x = 'D0') { [$x] } +fun foo_0_2($x = 'D0', $y = 'D1') { [$x, $y] } +fun foo_0_3($x = 'D0', $y, $z = 'D2') { [$x, $y, $z] } +fun foo_1_2($x, $y = 'D1') { [$x, $y] } +fun foo_1_3($x, $y = 'D1', $z = 'D2') { [$x, $y, $z] } +fun foo_2_3($x, $y, $z = 'D2') { [$x, $y, $z] } +fun foo_1_($x, @y) { [@_] } + +is_deeply foo_any, []; +is_deeply foo_any('a'), ['a']; +is_deeply foo_any('a', 'b'), ['a', 'b']; +is_deeply foo_any('a', 'b', 'c'), ['a', 'b', 'c']; +is_deeply foo_any('a', 'b', 'c', 'd'), ['a', 'b', 'c', 'd']; + +is_deeply foo_any_a, []; +is_deeply foo_any_a('a'), ['a']; +is_deeply foo_any_a('a', 'b'), ['a', 'b']; +is_deeply foo_any_a('a', 'b', 'c'), ['a', 'b', 'c']; +is_deeply foo_any_a('a', 'b', 'c', 'd'), ['a', 'b', 'c', 'd']; + +is_deeply foo_any_b, []; +is_deeply foo_any_b('a'), ['a']; +is_deeply foo_any_b('a', 'b'), ['a', 'b']; +is_deeply foo_any_b('a', 'b', 'c'), ['a', 'b', 'c']; +is_deeply foo_any_b('a', 'b', 'c', 'd'), ['a', 'b', 'c', 'd']; + +is_deeply foo_0, []; +error_like qr/Too many arguments.*foo_0/, fun { foo_0 'a' }; +error_like qr/Too many arguments.*foo_0/, fun { foo_0 'a', 'b' }; +error_like qr/Too many arguments.*foo_0/, fun { foo_0 'a', 'b', 'c' }; +error_like qr/Too many arguments.*foo_0/, fun { foo_0 'a', 'b', 'c', 'd' }; + +error_like qr/Not enough arguments.*foo_1/, fun { foo_1 }; +is_deeply foo_1('a'), ['a']; +error_like qr/Too many arguments.*foo_1/, fun { foo_1 'a', 'b' }; +error_like qr/Too many arguments.*foo_1/, fun { foo_1 'a', 'b', 'c' }; +error_like qr/Too many arguments.*foo_1/, fun { foo_1 'a', 'b', 'c', 'd' }; + +error_like qr/Not enough arguments.*foo_2/, fun { foo_2 }; +error_like qr/Not enough arguments.*foo_2/, fun { foo_2 'a' }; +is_deeply foo_2('a', 'b'), ['a', 'b']; +error_like qr/Too many arguments.*foo_2/, fun { foo_2 'a', 'b', 'c' }; +error_like qr/Too many arguments.*foo_2/, fun { foo_2 'a', 'b', 'c', 'd' }; + +error_like qr/Not enough arguments.*foo_3/, fun { foo_3 }; +error_like qr/Not enough arguments.*foo_3/, fun { foo_3 'a' }; +error_like qr/Not enough arguments.*foo_3/, fun { foo_3 'a', 'b' }; +is_deeply foo_3('a', 'b', 'c'), ['a', 'b', 'c']; +error_like qr/Too many arguments.*foo_3/, fun { foo_3 'a', 'b', 'c', 'd' }; + +is_deeply foo_0_1, ['D0']; +is_deeply foo_0_1('a'), ['a']; +error_like qr/Too many arguments.*foo_0_1/, fun { foo_0_1 'a', 'b' }; +error_like qr/Too many arguments.*foo_0_1/, fun { foo_0_1 'a', 'b', 'c' }; +error_like qr/Too many arguments.*foo_0_1/, fun { foo_0_1 'a', 'b', 'c', 'd' }; + +is_deeply foo_0_2, ['D0', 'D1']; +is_deeply foo_0_2('a'), ['a', 'D1']; +is_deeply foo_0_2('a', 'b'), ['a', 'b']; +error_like qr/Too many arguments.*foo_0_2/, fun { foo_0_2 'a', 'b', 'c' }; +error_like qr/Too many arguments.*foo_0_2/, fun { foo_0_2 'a', 'b', 'c', 'd' }; + +is_deeply foo_0_3, ['D0', undef, 'D2']; +is_deeply foo_0_3('a'), ['a', undef, 'D2']; +is_deeply foo_0_3('a', 'b'), ['a', 'b', 'D2']; +is_deeply foo_0_3('a', 'b', 'c'), ['a', 'b', 'c']; +error_like qr/Too many arguments.*foo_0_3/, fun { foo_0_3 'a', 'b', 'c', 'd' }; + +error_like qr/Not enough arguments.*foo_1_2/, fun { foo_1_2 }; +is_deeply foo_1_2('a'), ['a', 'D1']; +is_deeply foo_1_2('a', 'b'), ['a', 'b']; +error_like qr/Too many arguments.*foo_1_2/, fun { foo_1_2 'a', 'b', 'c' }; +error_like qr/Too many arguments.*foo_1_2/, fun { foo_1_2 'a', 'b', 'c', 'd' }; + +error_like qr/Not enough arguments.*foo_1_3/, fun { foo_1_3 }; +is_deeply foo_1_3('a'), ['a', 'D1', 'D2']; +is_deeply foo_1_3('a', 'b'), ['a', 'b', 'D2']; +is_deeply foo_1_3('a', 'b', 'c'), ['a', 'b', 'c']; +error_like qr/Too many arguments.*foo_1_3/, fun { foo_1_3 'a', 'b', 'c', 'd' }; + +error_like qr/Not enough arguments.*foo_2_3/, fun { foo_2_3 }; +error_like qr/Not enough arguments.*foo_2_3/, fun { foo_2_3 'a' }; +is_deeply foo_2_3('a', 'b'), ['a', 'b', 'D2']; +is_deeply foo_2_3('a', 'b', 'c'), ['a', 'b', 'c']; +error_like qr/Too many arguments.*foo_2_3/, fun { foo_2_3 'a', 'b', 'c', 'd' }; + +error_like qr/Not enough arguments.*foo_1_/, fun { foo_1_ }; +is_deeply foo_1_('a'), ['a']; +is_deeply foo_1_('a', 'b'), ['a', 'b']; +is_deeply foo_1_('a', 'b', 'c'), ['a', 'b', 'c']; +is_deeply foo_1_('a', 'b', 'c', 'd'), ['a', 'b', 'c', 'd']; +