deferred constructor construction
[gitmo/Moo.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
7 my $gen = Method::Generate::Constructor->new;
8
9 $gen->generate_method('Foo', 'new', {
10   one => { },
11   two => { init_arg => undef },
12   three => { init_arg => 'THREE' }
13 });
14
15 my $first = Foo->new({
16   one => 1,
17   two => 2,
18   three => -75,
19   THREE => 3,
20   four => 4,
21 });
22
23 is_deeply(
24   { %$first }, { one => 1, three => 3 },
25   'init_arg handling ok'
26 );
27
28 $gen->generate_method('Bar', 'new' => {
29   one => { required => 1 },
30   three => { init_arg => 'THREE', required => 1 }
31 });
32
33 like(
34   exception { Bar->new },
35   qr/Missing required arguments: THREE, one/,
36   'two missing args reported correctly'
37 );
38
39 like(
40   exception { Bar->new(THREE => 3) },
41   qr/Missing required arguments: one/,
42   'one missing arg reported correctly'
43 );
44
45 is(
46   exception { Bar->new(one => 1, THREE => 3) },
47   undef,
48   'pass with both required args'
49 );
50
51 done_testing;