File: 032-buildargs.t

package info (click to toggle)
libmouse-perl 2.6.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,156 kB
  • sloc: perl: 14,569; ansic: 218; makefile: 8
file content (56 lines) | stat: -rw-r--r-- 1,078 bytes parent folder | download | duplicates (8)
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
#!/usr/bin/env perl
use strict;
use warnings;
use Test::More tests => 11;
use Test::Exception;

{
    package C;
    use Mouse;
}

# original BUILDARGS

is_deeply( C->BUILDARGS(), {} );
is_deeply( C->BUILDARGS(foo => 42), {foo => 42} );
is_deeply( C->BUILDARGS(foo => 42, foo => 'bar'), {foo => 'bar'} );
is_deeply( C->BUILDARGS({foo => 1, bar => 2}), {foo => 1, bar => 2} );

my %hash = (foo => 10);
my $args = C->BUILDARGS(\%hash);
$args->{foo}++;
is $hash{foo}, 10, 'values must be copied';

%hash = (foo => 10);
$args = C->BUILDARGS(%hash);
$args->{foo}++;
is $hash{foo}, 10, 'values must be copied';

throws_ok {
    C->BUILDARGS([]);
} qr/must be a HASH ref/;


throws_ok {
    C->BUILDARGS([]);
} qr/must be a HASH ref/;


# custom BUILDARGS

do {
    package Foo;
    use Mouse;

    has foo => ( is => "rw" );

    sub BUILDARGS {
        my ( $self, @args ) = @_;
        return { @args % 2 ? ( foo => @args ) : @args };
    }
};

is(Foo->new->foo, undef, "no value");
is(Foo->new("bar")->foo, "bar", "single arg");
is(Foo->new(foo => "bar")->foo, "bar", "twoargs");