X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=t%2Fforeign%2FMethod-Signatures%2Fdefaults.t;fp=t%2Fforeign%2FMethod-Signatures%2Fdefaults.t;h=c15c8a01f2dd4640ea478d1c657ecdcc6da932f7;hb=633048d587df4a3745ae768ce6ed550cba5e329f;hp=0000000000000000000000000000000000000000;hpb=595edbcf92e56b601822a62deddef2758aa7f3b5;p=p5sagit%2FFunction-Parameters.git diff --git a/t/foreign/Method-Signatures/defaults.t b/t/foreign/Method-Signatures/defaults.t new file mode 100644 index 0000000..c15c8a0 --- /dev/null +++ b/t/foreign/Method-Signatures/defaults.t @@ -0,0 +1,83 @@ +#!perl +use strict; +use warnings FATAL => 'all'; + +use Test::More 'no_plan'; + +{ + package Stuff; + + use Test::More; + use Function::Parameters qw(:strict); + + method add($this = 23, $that = 42) { + return $this + $that; + } + + method minus($this = 23, $that = 42) { + return $this - $that; + } + + is( Stuff->add(), 23 + 42 ); + is( Stuff->add(99), 99 + 42 ); + is( Stuff->add(2,3), 5 ); + + is( Stuff->minus(), 23 - 42 ); + is( Stuff->minus(99), 99 - 42 ); + is( Stuff->minus(2, 3), 2 - 3 ); + + + # Test that undef overrides defaults + method echo($message = "what?") { + return $message + } + + is( Stuff->echo(), "what?" ); + is( Stuff->echo(undef), undef ); + 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(23, 42), 42 ); +} + + +{ + package Bar; + use Test::More; + use Function::Parameters qw(:strict); + + method hello($msg = "Hello, world!") { + return $msg; + } + + is( Bar->hello, "Hello, world!" ); + is( Bar->hello("Greetings!"), "Greetings!" ); + + + method hi($msg = q,Hi,) { + return $msg; + } + + is( Bar->hi, "Hi" ); + is( Bar->hi("Yo"), "Yo" ); + + +# method list(@args = (1,2,3)) { +# return @args; +# } +# +# is_deeply [Bar->list()], [1,2,3]; + + + method code($num, $code = sub { $num + 2 }) { + return $code->(); + } + + is( Bar->code(42), 44 ); +}