File: invocant.t

package info (click to toggle)
libmethod-signatures-perl 20170211-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 672 kB
  • sloc: perl: 3,860; makefile: 2
file content (72 lines) | stat: -r--r--r-- 1,361 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#!/usr/bin/perl

# Test that you can change the invocant.

use strict;
use warnings;

use Test::More 'no_plan';

our $skip_no_invocants;

{
    package Stuff;

    use Test::More;
    use Method::Signatures;

    sub new { bless {}, __PACKAGE__ }

    method bar($arg) {
        return ref $arg || $arg;
    }

    method invocant($class:) {
        $class->bar(0);
    }

    method with_arg($class: $arg) {
        $class->bar($arg);
    }

    method without_space($class:$arg) {
        $class->bar($arg);
    }

    method with_space_before_invocant( $class: $arg) {
        $class->bar($arg);
    }

    eval q{

        method no_invocant_class_type(Foo::Bar $arg) {
            $self->bar($arg);
        }

        method no_invocant_named_param(Foo :$arg) {
            $self->bar($arg);
        }

    };
    is $@, '', 'compiles without invocant';
}

{
    package Foo;
    sub new { bless {}, __PACKAGE__ }
}

{
    package Foo::Bar;
    sub new { bless {}, __PACKAGE__ }
}


is( Stuff->invocant,                            0 );
is( Stuff->with_arg(42),                        42 );
is( Stuff->without_space(42),                   42 );
is( Stuff->with_space_before_invocant(42),      42 );

my $stuff = Stuff->new;
is( $stuff->no_invocant_class_type(Foo::Bar->new),     'Foo::Bar' );
is( $stuff->no_invocant_named_param(arg => Foo->new),  'Foo' );