don't load Moo if Moose is already present
[p5sagit/Function-Parameters.git] / t / invocant.t
1 #!perl
2
3 use Test::More tests => 25;
4
5 use warnings FATAL => 'all';
6 use strict;
7
8 use 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
30 my $o = Foo->new;
31 ok $o->isa('Foo'), "Foo->new->isa('Foo')";
32
33 is $o->get_x, 1;
34 is $o->get_y, 2;
35 is $o->get_z, 3;
36
37 $o->set_x("A");
38 $o->set_y("B");
39 $o->set_z("C");
40
41 is $o->get_x, "A";
42 is $o->get_y, "B";
43 is $o->get_z, "C";
44
45 is eval { $o->get_z(42) }, undef;
46 like $@, qr/many arguments/;
47
48 is eval { $o->set_z }, undef;
49 like $@, qr/enough arguments/;
50
51 is eval q{fun ($self:) {}}, undef;
52 like $@, qr/invocant/;
53
54 is eval q{fun ($x : $y) {}}, undef;
55 like $@, qr/invocant/;
56
57 is eval q{method (@x:) {}}, undef;
58 like $@, qr/invocant/;
59
60 is eval q{method (%x:) {}}, undef;
61 like $@, 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 }