fix $obj->new
[gitmo/Role-Tiny.git] / t / method-generate-constructor.t
1 use strictures 1;
2 use Test::More;
3 use Test::Fatal;
4
5 use Method::Generate::Constructor;
6 use Method::Generate::Accessor;
7
8 my $gen = Method::Generate::Constructor->new(
9   accessor_generator => Method::Generate::Accessor->new
10 );
11
12 $gen->generate_method('Foo', 'new', {
13   one => { },
14   two => { init_arg => undef },
15   three => { init_arg => 'THREE' }
16 });
17
18 my $first = Foo->new({
19   one => 1,
20   two => 2,
21   three => -75,
22   THREE => 3,
23   four => 4,
24 });
25
26 is_deeply(
27   { %$first }, { one => 1, three => 3 },
28   'init_arg handling ok'
29 );
30
31 $gen->generate_method('Bar', 'new' => {
32   one => { required => 1 },
33   three => { init_arg => 'THREE', required => 1 }
34 });
35
36 like(
37   exception { Bar->new },
38   qr/Missing required arguments: THREE, one/,
39   'two missing args reported correctly'
40 );
41
42 like(
43   exception { Bar->new(THREE => 3) },
44   qr/Missing required arguments: one/,
45   'one missing arg reported correctly'
46 );
47
48 is(
49   exception { Bar->new(one => 1, THREE => 3) },
50   undef,
51   'pass with both required args'
52 );
53
54 is(
55   exception { Bar->new({ one => 1, THREE => 3 }) },
56   undef,
57   'hashrefs also supported'
58 );
59
60 is(
61   exception { $first->new(one => 1, THREE => 3) },
62   undef,
63   'calling ->new on an object works'
64 );
65
66 done_testing;