File: method-generate-constructor.t

package info (click to toggle)
libmoo-perl 0.091011-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 476 kB
  • sloc: perl: 1,688; makefile: 4; sh: 1
file content (66 lines) | stat: -rw-r--r-- 1,211 bytes parent folder | download
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
57
58
59
60
61
62
63
64
65
66
use strictures 1;
use Test::More;
use Test::Fatal;

use Method::Generate::Constructor;
use Method::Generate::Accessor;

my $gen = Method::Generate::Constructor->new(
  accessor_generator => Method::Generate::Accessor->new
);

$gen->generate_method('Foo', 'new', {
  one => { },
  two => { init_arg => undef },
  three => { init_arg => 'THREE' }
});

my $first = Foo->new({
  one => 1,
  two => 2,
  three => -75,
  THREE => 3,
  four => 4,
});

is_deeply(
  { %$first }, { one => 1, three => 3 },
  'init_arg handling ok'
);

$gen->generate_method('Bar', 'new' => {
  one => { required => 1 },
  three => { init_arg => 'THREE', required => 1 }
});

like(
  exception { Bar->new },
  qr/Missing required arguments: THREE, one/,
  'two missing args reported correctly'
);

like(
  exception { Bar->new(THREE => 3) },
  qr/Missing required arguments: one/,
  'one missing arg reported correctly'
);

is(
  exception { Bar->new(one => 1, THREE => 3) },
  undef,
  'pass with both required args'
);

is(
  exception { Bar->new({ one => 1, THREE => 3 }) },
  undef,
  'hashrefs also supported'
);

is(
  exception { $first->new(one => 1, THREE => 3) },
  undef,
  'calling ->new on an object works'
);

done_testing;