Move is_valid_class_name into XS
[gitmo/Mouse.git] / t / 001_mouse / 032-buildargs.t
CommitLineData
d574882a 1#!/usr/bin/env perl
2use strict;
3use warnings;
5ecfc2de 4use Test::More tests => 11;
5use Test::Exception;
6
7{
8 package C;
9 use Mouse;
10}
11
12# original BUILDARGS
13
14is_deeply( C->BUILDARGS(), {} );
15is_deeply( C->BUILDARGS(foo => 42), {foo => 42} );
16is_deeply( C->BUILDARGS(foo => 42, foo => 'bar'), {foo => 'bar'} );
17is_deeply( C->BUILDARGS({foo => 1, bar => 2}), {foo => 1, bar => 2} );
18
19my %hash = (foo => 10);
20my $args = C->BUILDARGS(\%hash);
21$args->{foo}++;
22is $hash{foo}, 10, 'values must be copied';
23
24%hash = (foo => 10);
25$args = C->BUILDARGS(%hash);
26$args->{foo}++;
27is $hash{foo}, 10, 'values must be copied';
28
29throws_ok {
30 C->BUILDARGS([]);
31} qr/must be a HASH ref/;
32
33
34throws_ok {
35 C->BUILDARGS([]);
36} qr/must be a HASH ref/;
37
38
39# custom BUILDARGS
d574882a 40
3652e8c0 41do {
d574882a 42 package Foo;
43 use Mouse;
44
45 has foo => ( is => "rw" );
46
47 sub BUILDARGS {
48 my ( $self, @args ) = @_;
49 return { @args % 2 ? ( foo => @args ) : @args };
50 }
3652e8c0 51};
d574882a 52
3652e8c0 53is(Foo->new->foo, undef, "no value");
54is(Foo->new("bar")->foo, "bar", "single arg");
55is(Foo->new(foo => "bar")->foo, "bar", "twoargs");
d574882a 56