File: buildargs.t

package info (click to toggle)
libmoo-perl 0.091011-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 476 kB
  • sloc: perl: 1,688; makefile: 4; sh: 1
file content (137 lines) | stat: -rw-r--r-- 2,784 bytes parent folder | download
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
use strictures 1;
use Test::More;

{
    package Qux;
    use Moo;

    has bar => ( is => "rw" );
    has baz => ( is => "rw" );

    package Quux;
    use Moo;

    extends qw(Qux);
}

{
    package t::non_moo;

    sub new {
        my ($class, $arg) = @_;
        bless { attr => $arg }, $class;
    }

    sub attr { shift->{attr} }

    package t::ext_non_moo::with_attr;
    use Moo;
    extends qw( t::non_moo );

    has 'attr2' => ( is => 'ro' );

    sub BUILDARGS {
        my ( $class, @args ) = @_;
        shift @args if @args % 2 == 1;
        return { @args };
    }
}


{
    package Foo;
    use Moo;

    has bar => ( is => "rw" );
    has baz => ( is => "rw" );

    sub BUILDARGS {
        my ( $class, @args ) = @_;
        unshift @args, "bar" if @args % 2 == 1;
        return $class->SUPER::BUILDARGS(@args);
    }

    package Bar;
    use Moo;

    extends qw(Foo);
}

{
    package Baz;
    use Moo;

    has bar => ( is => "rw" );
    has baz => ( is => "rw" );

    around BUILDARGS => sub {
        my $orig = shift;
        my ( $class, @args ) = @_;

        unshift @args, "bar" if @args % 2 == 1;

        return $class->$orig(@args);
    };

    package Biff;
    use Moo;

    extends qw(Baz);
}

foreach my $class (qw(Foo Bar Baz Biff)) {
    is( $class->new->bar, undef, "no args" );
    is( $class->new( bar => 42 )->bar, 42, "normal args" );
    is( $class->new( 37 )->bar, 37, "single arg" );
    {
        my $o = $class->new(bar => 42, baz => 47);
        is($o->bar, 42, '... got the right bar');
        is($o->baz, 47, '... got the right baz');
    }
    {
        my $o = $class->new(42, baz => 47);
        is($o->bar, 42, '... got the right bar');
        is($o->baz, 47, '... got the right baz');
    }
}

foreach my $class (qw(Qux Quux)) {
    my $o = $class->new(bar => 42, baz => 47);
    is($o->bar, 42, '... got the right bar');
    is($o->baz, 47, '... got the right baz');

    eval {
        $class->new( 37 );
    };
    like( $@, qr/Single parameters to new\(\) must be a HASH ref/,
        "new() requires a list or a HASH ref"
    );

    eval {
        $class->new( [ 37 ] );
    };
    like( $@, qr/Single parameters to new\(\) must be a HASH ref/,
        "new() requires a list or a HASH ref"
    );

    eval {
        $class->new( bar => 42, baz => 47, 'quux' );
    };
    like( $@, qr/You passed an odd number of arguments/,
        "new() requires a list or a HASH ref"
    );
}

my $non_moo = t::non_moo->new( 'bar' );
my $ext_non_moo = t::ext_non_moo::with_attr->new( 'bar', attr2 => 'baz' );

is $non_moo->attr, 'bar',
    "non-moo accepts params";
is $ext_non_moo->attr, 'bar',
    "extended non-moo passes params";
is $ext_non_moo->attr2, 'baz',
    "extended non-moo has own attributes";


done_testing;