File: method.t

package info (click to toggle)
libmethod-signatures-perl 20170211-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 696 kB
  • sloc: perl: 3,863; makefile: 2
file content (73 lines) | stat: -rw-r--r-- 1,423 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
73
#!/usr/bin/perl -w

use strict;
use Test::More 'no_plan';

{
    package Foo;
    use Method::Signatures;

    method new (%args) {
        return bless {%args}, $self;
    }

    method set ($key, $val) {
        return $self->{$key} = $val;
    }

    method get ($key) {
        return $self->{$key};
    }

    method no_proto {
        return($self, @_);
    }

    method empty_proto() {
        return($self, @_);
    }

    method echo(@_) {
        return($self, @_);
    }

    method caller($height = 0) {
        return (CORE::caller($height))[0..2];
    }

#line 39
    method warn($foo?) {
        my $warning = '';
        local $SIG{__WARN__} = sub { $warning = join '', @_; };
        CORE::warn "Testing warn";

        return $warning;
    }

    # Method with the same name as a loaded class.
    method strict () {
        42
    }
}

my $obj = Foo->new( foo => 42, bar => 23 );
isa_ok $obj, "Foo";
is $obj->get("foo"), 42;
is $obj->get("bar"), 23;

$obj->set(foo => 99);
is $obj->get("foo"), 99;

for my $method (qw(no_proto empty_proto)) {
    is_deeply [$obj->$method], [$obj];
    ok !eval { $obj->$method(23); 1 };
    like $@, qr{\Q$method(), was given too many arguments; it expects 0};
}

is_deeply [$obj->echo(1,2,3)], [$obj,1,2,3], "echo";

is_deeply [$obj->caller], [__PACKAGE__, $0, __LINE__], 'caller works';

is $obj->warn, "Testing warn at $0 line 42.\n";

is eval { $obj->strict }, 42;