Commit | Line | Data |
633048d5 |
1 | #!perl |
2 | use strict; |
3 | use warnings FATAL => 'all'; |
4 | |
5 | use Test::More 'no_plan'; |
6 | |
7 | { |
8 | package Stuff; |
9 | |
10 | use Test::More; |
11 | use Function::Parameters qw(:strict); |
12 | |
13 | method add($this = 23, $that = 42) { |
14 | return $this + $that; |
15 | } |
16 | |
17 | method minus($this = 23, $that = 42) { |
18 | return $this - $that; |
19 | } |
20 | |
21 | is( Stuff->add(), 23 + 42 ); |
22 | is( Stuff->add(99), 99 + 42 ); |
23 | is( Stuff->add(2,3), 5 ); |
24 | |
25 | is( Stuff->minus(), 23 - 42 ); |
26 | is( Stuff->minus(99), 99 - 42 ); |
27 | is( Stuff->minus(2, 3), 2 - 3 ); |
28 | |
29 | |
30 | # Test that undef overrides defaults |
31 | method echo($message = "what?") { |
32 | return $message |
33 | } |
34 | |
35 | is( Stuff->echo(), "what?" ); |
36 | is( Stuff->echo(undef), undef ); |
37 | is( Stuff->echo("who?"), 'who?' ); |
38 | |
39 | |
40 | # Test that you can reference earlier args in a default |
41 | method copy_cat($this, $that = $this) { |
42 | return $that; |
43 | } |
44 | |
45 | is( Stuff->copy_cat("wibble"), "wibble" ); |
46 | is( Stuff->copy_cat(23, 42), 42 ); |
47 | } |
48 | |
49 | |
50 | { |
51 | package Bar; |
52 | use Test::More; |
53 | use Function::Parameters qw(:strict); |
54 | |
55 | method hello($msg = "Hello, world!") { |
56 | return $msg; |
57 | } |
58 | |
59 | is( Bar->hello, "Hello, world!" ); |
60 | is( Bar->hello("Greetings!"), "Greetings!" ); |
61 | |
62 | |
63 | method hi($msg = q,Hi,) { |
64 | return $msg; |
65 | } |
66 | |
67 | is( Bar->hi, "Hi" ); |
68 | is( Bar->hi("Yo"), "Yo" ); |
69 | |
70 | |
71 | # method list(@args = (1,2,3)) { |
72 | # return @args; |
73 | # } |
74 | # |
75 | # is_deeply [Bar->list()], [1,2,3]; |
76 | |
77 | |
78 | method code($num, $code = sub { $num + 2 }) { |
79 | return $code->(); |
80 | } |
81 | |
82 | is( Bar->code(42), 44 ); |
83 | } |