File: method-generate-constructor.t

package info (click to toggle)
libmoo-perl 2.002005-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 856 kB
  • ctags: 192
  • sloc: perl: 2,561; makefile: 6
file content (96 lines) | stat: -rw-r--r-- 1,918 bytes parent folder | download | duplicates (3)
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
use Moo::_strictures;
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'
);

like(
  exception { $gen->register_attribute_specs('seventeen'
      => { is => 'ro', init_arg => undef, required => 1 }) },
  qr/You cannot have a required attribute/,
  'required not allowed with init_arg undef'
);

is(
  exception { $gen->register_attribute_specs('eighteen'
      => { is => 'ro', init_arg => undef, required => 1, default => 'foo' }) },
  undef,
  'required allowed with init_arg undef if given a default'
);

is ref($gen->current_constructor('Bar')), 'CODE',
  'can find constructor';

{
  package Baz;
  sub baz {};
}

is $gen->current_constructor('Baz'), undef,
  'nonexistent constructor returns undef';

{
  is $gen->_cap_call('welp'), 'welp',
    "_cap_call returns code";
}

done_testing;