diff --git a/lib/Method/Signatures.pm b/lib/Method/Signatures.pm index 3d0ad72..c45d782 100644 --- a/lib/Method/Signatures.pm +++ b/lib/Method/Signatures.pm @@ -1019,6 +1019,19 @@ sub named_param_error { $class->signature_error("does not take @keys as named argument(s)"); } +# Regex to determine if a where clause is a block. +my $when_block_re = qr{ + ^ + \s* + \{ + (?: + .* ; .* | # statements separated by semicolons + (?:(?! => ). )+ # doesn't look like a hash with fat commas + ) + \} + \s* + $ +}xs; sub inject_for_sig { my $self = shift; @@ -1053,7 +1066,7 @@ sub inject_for_sig { # Handle a default value if( defined $sig->{default_when} ) { # Handle default with 'when { block using $_ }' - if ($sig->{default_when} =~ m{^ \s* \{ (?: .* ; .* | (?:(?! => ). )* ) \} \s* $}xs) { + if ($sig->{default_when} =~ $when_block_re) { $rhs = "!$check_exists ? ($sig->{default}) : do{ no warnings; my \$arg = $rhs; (grep $sig->{default_when} \$arg) ? ($sig->{default}) : \$arg}"; } diff --git a/t/before_510.t b/t/before_510.t new file mode 100644 index 0000000..24d283d --- /dev/null +++ b/t/before_510.t @@ -0,0 +1,54 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More; + +plan skip_all => "This only applies to Perls before 5.10" if $] >= 5.010; + +use Method::Signatures; + +{ + eval + q{ + func neg_and_odd_and_prime ($x where [0..10]) { + return 1; + } + }; + + like $@, qr{\Q'where' constraint only available under Perl 5.10 or later.\E}, + "Perls <5.10 properly error out on where constraints"; +} + +{ + eval + q{ + package Stuff; + use Method::Signatures; + + method add($this //= 23, $that //= 42) { + return $this + $that; + } + }; + + like $@, qr{\Q'//=' defaults only available under Perl 5.10 or later.\E}, + "Perls <5.10 properly error out on //= declaration"; +} + +{ + eval + q{ + package Stuff; + use Method::Signatures; + method add($this = 23 when '', $that = 42 when '') { + no warnings 'uninitialized'; + return $this + $that; + } + }; + + like $@, qr{\Q'when' modifier on default only available under Perl 5.10 or later.\E}, + "Perls <5.10 properly error out on 'when' conditions"; +} + +done_testing; diff --git a/t/block_defaults.t b/t/block_defaults.t index 1180adc..f1c0666 100644 --- a/t/block_defaults.t +++ b/t/block_defaults.t @@ -5,65 +5,61 @@ use warnings; use Test::More; +BEGIN { + plan skip_all => "Perl 5.10 or higher required to test block defaults", 1 if $] < 5.010; +} + # if we don't load it up here, we get the "Devel::Declare not loaded soon enough" error use Method::Signatures; -SKIP: { - skip "Perl 5.10 or higher required to test block defaults", 1 if $] < 5.010; - - eval - q{ - package Stuff; - - use Test::More; - use Method::Signatures; - - method add($this = 23 when {$_ < 23}, $that = 42 when {42 < $_}) { - return $this + $that; - } - - # Check that it recognizes hashes - method add_block($this = 23 when { 2 => 'bad' }, $that = 42 when { 42 < $_ } ) { - return $this + $that; - } - - # Check that it disambiguates blocks - method add_dis($this = 23 when {; 2 => 'bad' }, $that = 42 when { 42 < $_ } ) { - return $this + $that; - } - - method minus($this is ro = 23 when undef, $that is ro = 42 when {($_ % 2)}) { - return $this - $that; - } - - is( Stuff->add(), 23 + 42 ); - is( Stuff->add(undef), 23 + 42 ); - is( Stuff->add(99), 99 + 42 ); - is( Stuff->add(2,3), 23 + 3 ); - is( Stuff->add(24,3), 24 + 3 ); - - is( Stuff->add_block(), 23 + 42 ); - is( Stuff->add_block(99), 99 + 42 ); - is( Stuff->add_block(2,3), 23 + 3 ); - is( Stuff->add_block(4,3), 4 + 3 ); - is( Stuff->add_block(24,3), 24 + 3 ); - - is( Stuff->add_dis(), 23 + 42 ); - is( Stuff->add_dis(99), 23 + 42 ); - is( Stuff->add_dis(2,3), 23 + 3 ); - is( Stuff->add_dis(4,3), 23 + 3 ); - is( Stuff->add_dis(24,3), 23 + 3 ); - - is( Stuff->minus(), 23 - 42 ); - is( Stuff->minus(undef), 23 - 42 ); - is( Stuff->minus(99), 99 - 42 ); - is( Stuff->minus(2, 3), 2 - 42 ); - is( Stuff->minus(2, 4), 2 - 4 ); - }; - fail "can't run tests: $@" if $@; + package Stuff; + + use Test::More; + use Method::Signatures; + + method add($this = 23 when {$_ < 23}, $that = 42 when {42 < $_}) { + return $this + $that; + } + + # Check that it recognizes hashes + method add_block($this = 23 when { 2 => 'bad' }, $that = 42 when { 42 < $_ } ) { + return $this + $that; + } + + # Check that it disambiguates blocks + method add_dis($this = 23 when {; 2 => 'bad' }, $that = 42 when { 42 < $_ } ) { + return $this + $that; + } + + method minus($this is ro = 23 when undef, $that is ro = 42 when {($_ % 2)}) { + return $this - $that; + } + + is( Stuff->add(), 23 + 42 ); + is( Stuff->add(undef), 23 + 42 ); + is( Stuff->add(99), 99 + 42 ); + is( Stuff->add(2,3), 23 + 3 ); + is( Stuff->add(24,3), 24 + 3 ); + + is( Stuff->add_block(), 23 + 42 ); + is( Stuff->add_block(99), 99 + 42 ); + is( Stuff->add_block(2,3), 23 + 3 ); + is( Stuff->add_block(4,3), 4 + 3 ); + is( Stuff->add_block(24,3), 24 + 3 ); + + is( Stuff->add_dis(), 23 + 42 ); + is( Stuff->add_dis(99), 23 + 42 ); + is( Stuff->add_dis(2,3), 23 + 3 ); + is( Stuff->add_dis(4,3), 23 + 3 ); + is( Stuff->add_dis(24,3), 23 + 3 ); + + is( Stuff->minus(), 23 - 42 ); + is( Stuff->minus(undef), 23 - 42 ); + is( Stuff->minus(99), 99 - 42 ); + is( Stuff->minus(2, 3), 2 - 42 ); + is( Stuff->minus(2, 4), 2 - 4 ); } - done_testing; diff --git a/t/defined_or_defaults.t b/t/defined_or_defaults.t index a84db98..bd62b92 100644 --- a/t/defined_or_defaults.t +++ b/t/defined_or_defaults.t @@ -5,114 +5,91 @@ use warnings; use Test::More; -# if we don't load it up here, we get the "Devel::Declare not loaded soon enough" error -use Method::Signatures; - - -if ($] < 5.010) -{ - eval - q{ - package Stuff; - use Method::Signatures; - - method add($this //= 23, $that //= 42) { - return $this + $that; - } - }; - - like $@, qr{\Q'//=' defaults only available under Perl 5.10 or later.\E}, - "Perls <5.10 properly error out on //= declaration"; +# Skip the test before Method::Signatures can try to compile it and blow up. +BEGIN { + plan skip_all => "Perl 5.10 or higher required to test where constraints" if $] < 5.010; } -else -{ - eval - q{ - package Stuff; - use Test::More; - use Method::Signatures; +{ + package Stuff; - method add($this //= 23, $that //= 42) { - return $this + $that; - } + use Test::More; + use Method::Signatures; - method minus($this is ro //= 23, $that is ro //= 42) { - return $this - $that; - } + method add($this //= 23, $that //= 42) { + return $this + $that; + } - is( Stuff->add(), 23 + 42 ); - is( Stuff->add(undef), 23 + 42 ); - is( Stuff->add(99), 99 + 42 ); - is( Stuff->add(2,3), 5 ); + method minus($this is ro //= 23, $that is ro //= 42) { + return $this - $that; + } - is( Stuff->minus(), 23 - 42 ); - is( Stuff->minus(undef), 23 - 42 ); - is( Stuff->minus(99), 99 - 42 ); - is( Stuff->minus(2, 3), 2 - 3 ); + is( Stuff->add(), 23 + 42 ); + is( Stuff->add(undef), 23 + 42 ); + is( Stuff->add(99), 99 + 42 ); + is( Stuff->add(2,3), 5 ); + is( Stuff->minus(), 23 - 42 ); + is( Stuff->minus(undef), 23 - 42 ); + is( Stuff->minus(99), 99 - 42 ); + is( Stuff->minus(2, 3), 2 - 3 ); - # Test again that undef doesn't override defaults - method echo($message //= "what?") { - return $message - } - is( Stuff->echo(), "what?" ); - is( Stuff->echo(undef), "what?" ); - is( Stuff->echo("who?"), 'who?' ); + # Test again that undef doesn't override defaults + method echo($message //= "what?") { + return $message + } + is( Stuff->echo(), "what?" ); + is( Stuff->echo(undef), "what?" ); + is( Stuff->echo("who?"), 'who?' ); - # Test that you can reference earlier args in a default - method copy_cat($this, $that //= $this) { - return $that; - } - is( Stuff->copy_cat("wibble"), "wibble" ); - is( Stuff->copy_cat("wibble", undef), "wibble" ); - is( Stuff->copy_cat(23, 42), 42 ); - }; - fail "can't run tests: $@" if $@; + # Test that you can reference earlier args in a default + method copy_cat($this, $that //= $this) { + return $that; + } + is( Stuff->copy_cat("wibble"), "wibble" ); + is( Stuff->copy_cat("wibble", undef), "wibble" ); + is( Stuff->copy_cat(23, 42), 42 ); +} - eval - q{ - package Bar; - use Test::More; - use Method::Signatures; +{ + package Bar; + use Test::More; + use Method::Signatures; - method hello($msg //= "Hello, world!") { - return $msg; - } + method hello($msg //= "Hello, world!") { + return $msg; + } - is( Bar->hello, "Hello, world!" ); - is( Bar->hello(undef), "Hello, world!" ); - is( Bar->hello("Greetings!"), "Greetings!" ); + is( Bar->hello, "Hello, world!" ); + is( Bar->hello(undef), "Hello, world!" ); + is( Bar->hello("Greetings!"), "Greetings!" ); - method hi($msg //= q,Hi,) { - return $msg; - } + method hi($msg //= q,Hi,) { + return $msg; + } - is( Bar->hi, "Hi" ); - is( Bar->hi(undef), "Hi" ); - is( Bar->hi("Yo"), "Yo" ); + is( Bar->hi, "Hi" ); + is( Bar->hi(undef), "Hi" ); + is( Bar->hi("Yo"), "Yo" ); - method list(@args = (1,2,3) when ()) { - return @args; - } + method list(@args = (1,2,3) when ()) { + return @args; + } - is_deeply [Bar->list()], [1,2,3]; + is_deeply [Bar->list()], [1,2,3]; - method code($num, $code //= sub { $num + 2 }) { - return $code->(); - } + method code($num, $code //= sub { $num + 2 }) { + return $code->(); + } - is( Bar->code(42), 44 ); - }; - fail "can't run tests: $@" if $@; + is( Bar->code(42), 44 ); } - done_testing; diff --git a/t/string_defaults.t b/t/string_defaults.t index 960218f..067b2e7 100644 --- a/t/string_defaults.t +++ b/t/string_defaults.t @@ -5,116 +5,93 @@ use warnings; use Test::More; -# if we don't load it up here, we get the "Devel::Declare not loaded soon enough" error -use Method::Signatures; - - -if ($] < 5.010) -{ - eval - q{ - package Stuff; - use Method::Signatures; - method add($this = 23 when '', $that = 42 when '') { - no warnings 'uninitialized'; - return $this + $that; - } - }; - - like $@, qr{\Q'when' modifier on default only available under Perl 5.10 or later.\E}, - "Perls <5.10 properly error out on 'when' conditions"; +# Skip the test before Method::Signatures can try to compile it and blow up. +BEGIN { + plan skip_all => "Perl 5.10 or higher required to test where constraints" if $] < 5.010; } -else -{ - eval - q{ - package Stuff; - use Test::More; - use Method::Signatures; +{ + package Stuff; - method add($this = 23 when '', $that = 42 when '') { - no warnings 'uninitialized'; - return $this + $that; - } + use Test::More; + use Method::Signatures; - method minus($this is ro = 23 when '', $that is ro = 42 when "") { - return $this - $that; - } + method add($this = 23 when '', $that = 42 when '') { + no warnings 'uninitialized'; + return $this + $that; + } - is( Stuff->add(), 23 + 42 ); - is( Stuff->add(''), 23 + 42 ); - is( Stuff->add(undef), 42 ); - is( Stuff->add(99), 99 + 42 ); - is( Stuff->add(2,3), 5 ); + method minus($this is ro = 23 when '', $that is ro = 42 when "") { + return $this - $that; + } - is( Stuff->minus(), 23 - 42 ); - is( Stuff->minus(''), 23 - 42 ); - is( Stuff->minus(99), 99 - 42 ); - is( Stuff->minus(2, 3), 2 - 3 ); + is( Stuff->add(), 23 + 42 ); + is( Stuff->add(''), 23 + 42 ); + is( Stuff->add(undef), 42 ); + is( Stuff->add(99), 99 + 42 ); + is( Stuff->add(2,3), 5 ); + is( Stuff->minus(), 23 - 42 ); + is( Stuff->minus(''), 23 - 42 ); + is( Stuff->minus(99), 99 - 42 ); + is( Stuff->minus(2, 3), 2 - 3 ); - # Test again that empty string doesn't override defaults - method echo($message = "what?" when q{}) { - return $message - } - is( Stuff->echo(), "what?" ); - is( Stuff->echo(''), "what?" ); - is( Stuff->echo("who?"), 'who?' ); + # Test again that empty string doesn't override defaults + method echo($message = "what?" when q{}) { + return $message + } + is( Stuff->echo(), "what?" ); + is( Stuff->echo(''), "what?" ); + is( Stuff->echo("who?"), 'who?' ); - # Test that you can reference earlier args in a default - method copy_cat($this, $that = $this when '') { - return $that; - } - is( Stuff->copy_cat("wibble"), "wibble" ); - is( Stuff->copy_cat("wibble", ""), "wibble" ); - is( Stuff->copy_cat(23, 42), 42 ); - }; - fail "can't run tests: $@" if $@; + # Test that you can reference earlier args in a default + method copy_cat($this, $that = $this when '') { + return $that; + } + is( Stuff->copy_cat("wibble"), "wibble" ); + is( Stuff->copy_cat("wibble", ""), "wibble" ); + is( Stuff->copy_cat(23, 42), 42 ); +} - eval - q{ - package Bar; - use Test::More; - use Method::Signatures; +{ + package Bar; + use Test::More; + use Method::Signatures; - method hello($msg = "Hello, world!" when '') { - return $msg; - } + method hello($msg = "Hello, world!" when '') { + return $msg; + } - is( Bar->hello, "Hello, world!" ); - is( Bar->hello(q{}), "Hello, world!" ); - is( Bar->hello("Greetings!"), "Greetings!" ); + is( Bar->hello, "Hello, world!" ); + is( Bar->hello(q{}), "Hello, world!" ); + is( Bar->hello("Greetings!"), "Greetings!" ); - method hi($msg = q,Hi, when '') { - return $msg; - } + method hi($msg = q,Hi, when '') { + return $msg; + } - is( Bar->hi, "Hi" ); - is( Bar->hi(q{}), "Hi" ); - is( Bar->hi("Yo"), "Yo" ); + is( Bar->hi, "Hi" ); + is( Bar->hi(q{}), "Hi" ); + is( Bar->hi("Yo"), "Yo" ); - method list(@args = (1,2,3) when ()) { - return @args; - } + method list(@args = (1,2,3) when ()) { + return @args; + } - is_deeply [Bar->list()], [1,2,3]; + is_deeply [Bar->list()], [1,2,3]; - method code($num, $code = sub { $num + 2 } when '') { - return $code->(); - } + method code($num, $code = sub { $num + 2 } when '') { + return $code->(); + } - is( Bar->code(42), 44 ); - }; - fail "can't run tests: $@" if $@; + is( Bar->code(42), 44 ); } - done_testing; diff --git a/t/undef_defaults.t b/t/undef_defaults.t index 21a2c0c..925cefb 100644 --- a/t/undef_defaults.t +++ b/t/undef_defaults.t @@ -5,101 +5,91 @@ use warnings; use Test::More; -# if we don't load it up here, we get the "Devel::Declare not loaded soon enough" error -use Method::Signatures; - +# Skip the test before Method::Signatures can try to compile it and blow up. +BEGIN { + plan skip_all => "Perl 5.10 or higher required to test where constraints" if $] < 5.010; +} -SKIP: { - skip "Perl 5.10 or higher required to test default conditions", 1 if $] < 5.010; - - eval - q{ - package Stuff; - - use Test::More; - use Method::Signatures; + package Stuff; - method add($this = 23 when undef, $that = 42 when undef) { - return $this + $that; - } + use Test::More; + use Method::Signatures; - method minus(Int|Str|Any $this is ro = 23 when undef, $that is ro = 42 when undef) { - return $this - $that; - } + method add($this = 23 when undef, $that = 42 when undef) { + return $this + $that; + } - is( Stuff->add(), 23 + 42 ); - is( Stuff->add(undef), 23 + 42 ); - is( Stuff->add(99), 99 + 42 ); - is( Stuff->add(2,3), 5 ); + method minus(Int|Str|Any $this is ro = 23 when undef, $that is ro = 42 when undef) { + return $this - $that; + } - is( Stuff->minus(), 23 - 42 ); - is( Stuff->minus(undef), 23 - 42 ); - is( Stuff->minus(99), 99 - 42 ); - is( Stuff->minus(2, 3), 2 - 3 ); + is( Stuff->add(), 23 + 42 ); + is( Stuff->add(undef), 23 + 42 ); + is( Stuff->add(99), 99 + 42 ); + is( Stuff->add(2,3), 5 ); + is( Stuff->minus(), 23 - 42 ); + is( Stuff->minus(undef), 23 - 42 ); + is( Stuff->minus(99), 99 - 42 ); + is( Stuff->minus(2, 3), 2 - 3 ); - # Test again that undef doesn't override defaults - method echo($message = "what?" when undef) { - return $message - } - is( Stuff->echo(), "what?" ); - is( Stuff->echo(undef), "what?" ); - is( Stuff->echo("who?"), 'who?' ); + # Test again that undef doesn't override defaults + method echo($message = "what?" when undef) { + return $message + } + is( Stuff->echo(), "what?" ); + is( Stuff->echo(undef), "what?" ); + is( Stuff->echo("who?"), 'who?' ); - # Test that you can reference earlier args in a default - method copy_cat($this, $that = $this when undef) { - return $that; - } - is( Stuff->copy_cat("wibble"), "wibble" ); - is( Stuff->copy_cat("wibble", undef), "wibble" ); - is( Stuff->copy_cat(23, 42), 42 ); - }; - fail "can't run tests: $@" if $@; + # Test that you can reference earlier args in a default + method copy_cat($this, $that = $this when undef) { + return $that; + } + is( Stuff->copy_cat("wibble"), "wibble" ); + is( Stuff->copy_cat("wibble", undef), "wibble" ); + is( Stuff->copy_cat(23, 42), 42 ); +} - eval - q{ - package Bar; - use Test::More; - use Method::Signatures; +{ + package Bar; + use Test::More; + use Method::Signatures; - method hello($msg = "Hello, world!" when undef) { - return $msg; - } + method hello($msg = "Hello, world!" when undef) { + return $msg; + } - is( Bar->hello, "Hello, world!" ); - is( Bar->hello(undef), "Hello, world!" ); - is( Bar->hello("Greetings!"), "Greetings!" ); + is( Bar->hello, "Hello, world!" ); + is( Bar->hello(undef), "Hello, world!" ); + is( Bar->hello("Greetings!"), "Greetings!" ); - method hi($msg = q,Hi, when undef) { - return $msg; - } + method hi($msg = q,Hi, when undef) { + return $msg; + } - is( Bar->hi, "Hi" ); - is( Bar->hi(undef), "Hi" ); - is( Bar->hi("Yo"), "Yo" ); + is( Bar->hi, "Hi" ); + is( Bar->hi(undef), "Hi" ); + is( Bar->hi("Yo"), "Yo" ); - method list(@args = (1,2,3) when ()) { - return @args; - } + method list(@args = (1,2,3) when ()) { + return @args; + } - is_deeply [Bar->list()], [1,2,3]; + is_deeply [Bar->list()], [1,2,3]; - method code($num, $code = sub { $num + 2 } when undef) { - return $code->(); - } + method code($num, $code = sub { $num + 2 } when undef) { + return $code->(); + } - is( Bar->code(42), 44 ); - }; - fail "can't run tests: $@" if $@; + is( Bar->code(42), 44 ); } - done_testing; diff --git a/t/when.t b/t/when.t new file mode 100644 index 0000000..ec1c390 --- /dev/null +++ b/t/when.t @@ -0,0 +1,36 @@ +#!/usr/bin/perl + +use strict; +use warnings; + +use Test::More; + +# Skip the test before Method::Signatures can try to compile it and blow up. +BEGIN { + plan skip_all => "Perl 5.10 or higher required to test where constraints" if $] < 5.010; +} + +use Method::Signatures; + +subtest "when {}" => sub { + func empty_hash( HashRef[Int] $ref = { foo => 23, bar => 42 } when {} ) { + return $ref; + } + + is_deeply empty_hash(), { foo => 23, bar => 42 }; + is_deeply empty_hash({}), { foo => 23, bar => 42 }; + is_deeply empty_hash({ this => 23 }), { this => 23 }; +}; + + +subtest "when []" => sub { + func empty_array( ArrayRef[Int] $ref = [1,2,3] when [] ) { + return $ref; + } + + is_deeply empty_array(), [1,2,3]; + is_deeply empty_array([]), [1,2,3]; + is_deeply empty_array([4,5,6]), [4,5,6]; +}; + +done_testing; diff --git a/t/where.t b/t/where.t index 8577a5b..bb40f0e 100644 --- a/t/where.t +++ b/t/where.t @@ -7,159 +7,134 @@ use Test::More; use Test::Warn; use Test::Exception; +# Skip the test before Method::Signatures can try to compile it and blow up. +BEGIN { + plan skip_all => "Perl 5.10 or higher required to test where constraints" if $] < 5.010; +} + use Method::Signatures; plan tests => 4; -SKIP: -{ - skip "Perl 5.10 or higher required to test where constraints", 3 if $] < 5.010; +subtest 'where { block() }' => sub { + plan tests => 3; - eval - q{ - subtest 'where { block() }' => sub { - plan tests => 3; + func small_int (Maybe[Int] $x where { $_ < 10 } is copy = 0 when undef) { + ok defined $x, "small_int($x) has defined value"; + ok $x < 10, "small_int($x) has value in range"; + return 1; + } - func small_int (Maybe[Int] $x where { $_ < 10 } is copy = 0 when undef) { - ok defined $x, "small_int($x) has defined value"; - ok $x < 10, "small_int($x) has value in range"; - return 1; - } - - subtest "small_int()" => sub { - ok eval{ small_int(); }, "small_int() called as expected" - or note $@; - }; + subtest "small_int()" => sub { + ok eval{ small_int(); }, "small_int() called as expected" + or note $@; + }; - subtest "small_int(9)" => sub { - ok eval{ small_int(9); }, "small_int(9) called as expected" - or note $@; - }; + subtest "small_int(9)" => sub { + ok eval{ small_int(9); }, "small_int(9) called as expected" + or note $@; + }; - subtest "small_int(10)" => sub { - ok !eval{ small_int(10);}, "small_int(10) not called (as expected)"; - note $@; - }; - }; + subtest "small_int(10)" => sub { + ok !eval{ small_int(10);}, "small_int(10) not called (as expected)"; + note $@; + }; +}; - subtest 'where [0..10]' => sub { - plan tests => 4; +subtest 'where [0..10]' => sub { + plan tests => 4; - func range_int (Maybe[Int] $x where [0..9] is copy = 0 when undef) { - ok defined $x, "range_int($x) has defined value"; - ok 0 <= $x && $x <= 9, "range_int($x) has value in range"; - return 1; - } + func range_int (Maybe[Int] $x where [0..9] is copy = 0 when undef) { + ok defined $x, "range_int($x) has defined value"; + ok 0 <= $x && $x <= 9, "range_int($x) has value in range"; + return 1; + } - subtest "range_int()" => sub { - ok eval{ range_int(); }, "range_int() called as expected" - or note $@; - }; + subtest "range_int()" => sub { + ok eval{ range_int(); }, "range_int() called as expected" + or note $@; + }; - subtest "range_int(9)" => sub { - ok eval{ range_int(9); }, "range_int(9) called as expected" - or note $@; - }; + subtest "range_int(9)" => sub { + ok eval{ range_int(9); }, "range_int(9) called as expected" + or note $@; + }; - subtest "range_int(10)" => sub { - ok !eval{ range_int(10);}, "range_int(10) not called (as expected)"; - note $@; - }; + subtest "range_int(10)" => sub { + ok !eval{ range_int(10);}, "range_int(10) not called (as expected)"; + note $@; + }; - subtest "range_int(-1)" => sub { - ok !eval{ range_int(-1);}, "range_int(10) not called (as expected)"; - note $@; - }; - }; + subtest "range_int(-1)" => sub { + ok !eval{ range_int(-1);}, "range_int(10) not called (as expected)"; + note $@; + }; +}; - subtest 'where { cat => 1, dog => 2}' => sub { - plan tests => 4; +subtest 'where { cat => 1, dog => 2}' => sub { + plan tests => 4; - func hash_member (Maybe[Str] $x where { cat => 1, dog => 2 } is copy = 'cat' when undef) { - ok defined $x, "hash_member($x) has defined value"; - like $x, qr{^(cat|dog)$} , "hash_member($x) has value in range"; - return 1; - } + func hash_member (Maybe[Str] $x where { cat => 1, dog => 2 } is copy = 'cat' when undef) { + ok defined $x, "hash_member($x) has defined value"; + like $x, qr{^(cat|dog)$} , "hash_member($x) has value in range"; + return 1; + } - subtest "hash_member()" => sub { - ok eval{ hash_member(); }, "hash_member() called as expected" - or note $@; - }; + subtest "hash_member()" => sub { + ok eval{ hash_member(); }, "hash_member() called as expected" + or note $@; + }; - subtest "hash_member('cat')" => sub { - ok eval{ hash_member('cat'); }, "hash_member('cat') called as expected" - or note $@; - }; + subtest "hash_member('cat')" => sub { + ok eval{ hash_member('cat'); }, "hash_member('cat') called as expected" + or note $@; + }; - subtest "hash_member('dog')" => sub { - ok eval{ hash_member('dog'); }, "hash_member('dog') called as expected" - or note $@; - }; + subtest "hash_member('dog')" => sub { + ok eval{ hash_member('dog'); }, "hash_member('dog') called as expected" + or note $@; + }; - subtest "hash_member('fish')" => sub { - ok !eval{ hash_member('fish');}, "hash_member('fish') not called (as expected)"; - note $@; - }; - }; + subtest "hash_member('fish')" => sub { + ok !eval{ hash_member('fish');}, "hash_member('fish') not called (as expected)"; + note $@; }; - fail "can't run tests: $@" if $@; -} +}; +subtest 'where where where' => sub { + plan tests => 14; -if ($] < 5.010) -{ - eval - q{ - func neg_and_odd_and_prime ($x where [0..10]) { - return 1; - } - }; + func is_prime ($x) { + return $x ~~ [2,3,5,7,11]; + } - like $@, qr{\Q'where' constraint only available under Perl 5.10 or later.\E}, - "Perls <5.10 properly error out on where constraints"; -} -else -{ - eval - q{ - subtest 'where where where' => sub { - plan tests => 14; - - func is_prime ($x) { - return $x ~~ [2,3,5,7,11]; - } + func neg_and_odd_and_prime ($x where [0..10] where { $x % 2 } where \&is_prime ) { + ok $x ~~ [3,5,7], '$x had acceptable value'; + return 1; + } - func neg_and_odd_and_prime ($x where [0..10] where { $x % 2 } where \&is_prime ) { - ok $x ~~ [3,5,7], '$x had acceptable value'; - return 1; - } + for my $n (-1..11) { + subtest "neg_and_odd_and_prime($n)" => sub { + local $@; + my $result = eval{ neg_and_odd_and_prime($n); }; + my $error = $@; - for my $n (-1..11) { - subtest "neg_and_odd_and_prime($n)" => sub { - local $@; - my $result = eval{ neg_and_odd_and_prime($n); }; - my $error = $@; - - if (defined $result) { - pass "neg_and_odd_and_prime($n) as expected"; - } - else { - like $error, qr{\$x value \("$n"\) does not satisfy constraint:} - => "neg_and_odd_and_prime($n) as expected"; - note $@; - } - }; + if (defined $result) { + pass "neg_and_odd_and_prime($n) as expected"; + } + else { + like $error, qr{\$x value \("$n"\) does not satisfy constraint:} + => "neg_and_odd_and_prime($n) as expected"; + note $@; } - - # try an undef value - my $result = eval{ neg_and_odd_and_prime(undef); }; - like $@, qr{\$x value \(undef\) does not satisfy constraint:}, "neg_and_odd_and_prime(undef) as expected"; - }; - }; - fail "can't run tests: $@" if $@; -} + } + + # try an undef value + my $result = eval{ neg_and_odd_and_prime(undef); }; + like $@, qr{\$x value \(undef\) does not satisfy constraint:}, "neg_and_odd_and_prime(undef) as expected"; +}; diff --git a/t/zero_defaults.t b/t/zero_defaults.t index e00ccac..c5b0064 100644 --- a/t/zero_defaults.t +++ b/t/zero_defaults.t @@ -5,102 +5,92 @@ use warnings; use Test::More; -# if we don't load it up here, we get the "Devel::Declare not loaded soon enough" error -use Method::Signatures; - +BEGIN { + plan skip_all => "Perl 5.10 or higher required to test default conditions", 1 if $] < 5.010; +} -SKIP: { - skip "Perl 5.10 or higher required to test default conditions", 1 if $] < 5.010; - - eval - q{ - package Stuff; + package Stuff; - use Test::More; - use Method::Signatures; + use Test::More; + use Method::Signatures; - method add($this = 23 when 0, $that = 42 when 0) { - no warnings 'uninitialized'; - return $this + $that; - } + method add($this = 23 when 0, $that = 42 when 0) { + no warnings 'uninitialized'; + return $this + $that; + } - method minus($this is ro = 23 when 0, $that is ro = 42 when 0x0) { - return $this - $that; - } + method minus($this is ro = 23 when 0, $that is ro = 42 when 0x0) { + return $this - $that; + } - is( Stuff->add(), 23 + 42 ); - is( Stuff->add(0), 23 + 42 ); - is( Stuff->add(undef), 42 ); - is( Stuff->add(99), 99 + 42 ); - is( Stuff->add(2,3), 5 ); + is( Stuff->add(), 23 + 42 ); + is( Stuff->add(0), 23 + 42 ); + is( Stuff->add(undef), 42 ); + is( Stuff->add(99), 99 + 42 ); + is( Stuff->add(2,3), 5 ); - is( Stuff->minus(), 23 - 42 ); - is( Stuff->minus(0), 23 - 42 ); - is( Stuff->minus(99), 99 - 42 ); - is( Stuff->minus(2, 3), 2 - 3 ); + is( Stuff->minus(), 23 - 42 ); + is( Stuff->minus(0), 23 - 42 ); + is( Stuff->minus(99), 99 - 42 ); + is( Stuff->minus(2, 3), 2 - 3 ); - # Test again that empty string doesn't override defaults - method echo($message = "what?" when 0.0) { - return $message - } + # Test again that empty string doesn't override defaults + method echo($message = "what?" when 0.0) { + return $message + } - is( Stuff->echo(), "what?" ); - is( Stuff->echo(0), "what?" ); - is( Stuff->echo(1), 1 ); + is( Stuff->echo(), "what?" ); + is( Stuff->echo(0), "what?" ); + is( Stuff->echo(1), 1 ); - # Test that you can reference earlier args in a default - method copy_cat($this, $that = $this when 0) { - return $that; - } - - is( Stuff->copy_cat("wibble"), "wibble" ); - is( Stuff->copy_cat("wibble", ""), "wibble" ); - is( Stuff->copy_cat(23, 42), 42 ); - }; - fail "can't run tests: $@" if $@; + # Test that you can reference earlier args in a default + method copy_cat($this, $that = $this when 0) { + return $that; + } + is( Stuff->copy_cat("wibble"), "wibble" ); + is( Stuff->copy_cat("wibble", ""), "wibble" ); + is( Stuff->copy_cat(23, 42), 42 ); +} - eval - q{ - package Bar; - use Test::More; - use Method::Signatures; +{ + package Bar; + use Test::More; + use Method::Signatures; - method hello($msg = "Hello, world!" when 0) { - return $msg; - } + method hello($msg = "Hello, world!" when 0) { + return $msg; + } - is( Bar->hello, "Hello, world!" ); - is( Bar->hello(0x0), "Hello, world!" ); - is( Bar->hello(42), 42 ); + is( Bar->hello, "Hello, world!" ); + is( Bar->hello(0x0), "Hello, world!" ); + is( Bar->hello(42), 42 ); - method hi($msg = q,Hi, when 0) { - return $msg; - } + method hi($msg = q,Hi, when 0) { + return $msg; + } - is( Bar->hi, "Hi" ); - is( Bar->hi(0.0), "Hi" ); - is( Bar->hi(1), 1 ); + is( Bar->hi, "Hi" ); + is( Bar->hi(0.0), "Hi" ); + is( Bar->hi(1), 1 ); - method list(@args = (1,2,3) when ()) { - return @args; - } + method list(@args = (1,2,3) when ()) { + return @args; + } - is_deeply [Bar->list()], [1,2,3]; + is_deeply [Bar->list()], [1,2,3]; - method code($num, $code = sub { $num + 2 } when 0) { - return $code->(); - } + method code($num, $code = sub { $num + 2 } when 0) { + return $code->(); + } - is( Bar->code(42), 44 ); - }; - fail "can't run tests: $@" if $@; + is( Bar->code(42), 44 ); }