implement $invocant: syntax
[p5sagit/Function-Parameters.git] / t / invocant.t
CommitLineData
d8e5d540 1#!perl
2
3use Test::More tests => 25;
4
5use warnings FATAL => 'all';
6use strict;
7
8use Function::Parameters { fun => 'function_strict', method => 'method_strict' };
9
10{
11 package Foo;
12
13 method new($class : ) {
14 return bless {
15 x => 1,
16 y => 2,
17 z => 3,
18 }, $class;
19 }
20
21 method get_x() { $self->{x} }
22 method get_y($self:) { $self->{y} }
23 method get_z($this:) { $this->{z} }
24
25 method set_x($val) { $self->{x} = $val; }
26 method set_y($self:$val) { $self->{y} = $val; }
27 method set_z($this: $val) { $this->{z} = $val; }
28}
29
30my $o = Foo->new;
31ok $o->isa('Foo'), "Foo->new->isa('Foo')";
32
33is $o->get_x, 1;
34is $o->get_y, 2;
35is $o->get_z, 3;
36
37$o->set_x("A");
38$o->set_y("B");
39$o->set_z("C");
40
41is $o->get_x, "A";
42is $o->get_y, "B";
43is $o->get_z, "C";
44
45is eval { $o->get_z(42) }, undef;
46like $@, qr/many arguments/;
47
48is eval { $o->set_z }, undef;
49like $@, qr/enough arguments/;
50
51is eval q{fun ($self:) {}}, undef;
52like $@, qr/invocant/;
53
54is eval q{fun ($x : $y) {}}, undef;
55like $@, qr/invocant/;
56
57is eval q{method (@x:) {}}, undef;
58like $@, qr/invocant/;
59
60is eval q{method (%x:) {}}, undef;
61like $@, qr/invocant/;
62
63{
64 use Function::Parameters {
65 def => {
66 invocant => 1,
67 }
68 };
69
70 def foo1($x) { join ' ', $x, @_ }
71 def foo2($x: $y) { join ' ', $x, $y, @_ }
72 def foo3($x, $y) { join ' ', $x, $y, @_ }
73
74 is foo1("a"), "a a";
75 is foo2("a", "b"), "a b b";
76 is foo3("a", "b"), "a b a b";
77 is foo1("a", "b"), "a a b";
78 is foo2("a", "b", "c"), "a b b c";
79 is foo3("a", "b", "c"), "a b a b c";
80}