File: sugar.t

package info (click to toggle)
libdevel-declare-perl 0.006000-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 296 kB
  • ctags: 238
  • sloc: perl: 1,605; ansic: 756; makefile: 25
file content (89 lines) | stat: -rw-r--r-- 2,009 bytes parent folder | download | duplicates (2)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
use Devel::Declare;

BEGIN {

  Devel::Declare->install_declarator(
    'DeclareTest', 'method', DECLARE_PACKAGE | DECLARE_PROTO,
    sub {
      my ($name, $proto) = @_;
#no warnings 'uninitialized';
#warn "NP: ".join(', ', @_)."\n";
      return 'my $self = shift;' unless defined $proto && $proto ne '@_';
      return 'my ($self'.(length $proto ? ", ${proto}" : "").') = @_;';
    },
    sub {
      my ($name, $proto, $sub, @rest) = @_;
#no warnings 'uninitialized';
#warn "NPS: ".join(', ', @_)."\n";
      if (defined $name && length $name) {
        unless ($name =~ /::/) {
          $name = "DeclareTest::${name}";
        }
        no strict 'refs';
        *{$name} = $sub;
      }
      return wantarray ? ($sub, @rest) : $sub;
    }
  );

}

my ($test_method1, $test_method2, @test_list);

{
  package DeclareTest;

  method new {
    my $class = ref $self || $self;
    return bless({ @_ }, $class);
  };

  method foo ($foo) {
    return (ref $self).': Foo: '.$foo;
  };

  method upgrade(){ # no spaces to make case pathological
    bless($self, 'DeclareTest2');
  };

  method DeclareTest2::bar () {
    return 'DeclareTest2: bar';
  };

  $test_method1 = method {
    return join(', ', $self->{attr}, $_[1]);
  };

  $test_method2 = method ($what) {
    return join(', ', ref $self, $what);
  };

  method main () { return "main"; };

  #@test_list = method { 1 }, sub { 2 }, method () { 3 }, sub { 4 };

}

use Test::More 'no_plan';

my $o = DeclareTest->new(attr => "value");

isa_ok($o, 'DeclareTest');

is($o->{attr}, 'value', '@_ args ok');

is($o->foo('yay'), 'DeclareTest: Foo: yay', 'method with argument ok');

is($o->main, 'main', 'declaration of package named method ok');

$o->upgrade;

isa_ok($o, 'DeclareTest2');

is($o->bar, 'DeclareTest2: bar', 'absolute method declaration ok');

is($o->$test_method1('no', 'yes'), 'value, yes', 'anon method with @_ ok');

is($o->$test_method2('this'), 'DeclareTest2, this', 'anon method with proto ok');

#warn map { $_->() } @test_list;