test hashref arg for new
[gitmo/Moo.git] / t / method-generate-constructor.t
CommitLineData
6f68f022 1use strictures 1;
2use Test::More;
3use Test::Fatal;
4
5use Method::Generate::Constructor;
3a9a65a4 6use Method::Generate::Accessor;
6f68f022 7
3a9a65a4 8my $gen = Method::Generate::Constructor->new(
9 accessor_generator => Method::Generate::Accessor->new
10);
6f68f022 11
12$gen->generate_method('Foo', 'new', {
13 one => { },
14 two => { init_arg => undef },
15 three => { init_arg => 'THREE' }
16});
17
18my $first = Foo->new({
19 one => 1,
20 two => 2,
21 three => -75,
22 THREE => 3,
23 four => 4,
24});
25
26is_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
36like(
37 exception { Bar->new },
38 qr/Missing required arguments: THREE, one/,
39 'two missing args reported correctly'
40);
41
42like(
43 exception { Bar->new(THREE => 3) },
44 qr/Missing required arguments: one/,
45 'one missing arg reported correctly'
46);
47
48is(
49 exception { Bar->new(one => 1, THREE => 3) },
50 undef,
51 'pass with both required args'
52);
53
e505d5c2 54is(
55 exception { Bar->new({ one => 1, THREE => 3 }) },
56 undef,
57 'hashrefs also supported'
58);
59
6f68f022 60done_testing;