add begin.t from Method::Signatures
[p5sagit/Function-Parameters.git] / t / name.t
CommitLineData
20f5d15a 1use warnings;
2use strict;
3
6f61373b 4use Test::More tests => 12;
20f5d15a 5
6use Dir::Self;
7
8use Function::Parameters {
9 func => {
10 name => 'required',
11 },
12
13 f => {
14 name => 'prohibited',
15 },
16
17 method => {
18 name => 'required',
19 shift => '$this',
20 },
21};
22
23func foo($x, $y, $z) {
24 $x .= $z;
25 return $y . $x . $y;
26}
27
28method bar($k, $d) {
29 $d = $k . $d;
30 return $d . $this->{$k} . $d;
31}
32
33is foo('a', 'b', 'c'), 'bacb';
6f61373b 34is bar({ab => 'cd'}, 'ab', 'e'), 'abecdabe';
20f5d15a 35
36my $baz = f ($x) { $x * 2 + 1 };
37is $baz->(11), 23;
38is $baz->(-0.5), 0;
39
6f61373b 40for my $fail (
e158cf8f 41 map [__DIR__ . "/name_$_->[0].fail", @$_[1 .. $#$_]],
6f61373b 42 ['1', qr/expect.*function.*name/],
43 ['2', qr/expect.*function.*body/],
44 ['3', qr/expect.*function.*name/],
45 ['4', qr/Global symbol "\$self" requires explicit package name/]
46) {
47 my ($file, $pat) = @$fail;
20f5d15a 48 my $done = do $file;
49 my $exc = $@;
50 my $err = $!;
51
52 is $done, undef, "faulty code doesn't load";
53 $exc or die "$file: $err";
6f61373b 54 like $exc, $pat;
20f5d15a 55}