/
076_more_complex_typemap.t
153 lines (123 loc) · 3.9 KB
/
076_more_complex_typemap.t
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
#!/usr/bin/perl
use strict;
use warnings;
use Test::More;
use Test::Moose;
use Bread::Board;
{
package Stapler;
use Moose;
package Desk;
use Moose;
package Chair;
use Moose;
package Cubicle;
use Moose;
has 'desk' => (
is => 'ro',
isa => 'Desk',
required => 1,
);
has 'chair' => (
is => 'ro',
isa => 'Chair',
required => 1,
);
package KeyCard;
use Moose;
use Moose::Util::TypeConstraints;
subtype 'KeyCardUUID' => as 'Str';
has 'uuid' => (
is => 'ro',
isa => 'KeyCardUUID',
required => 1,
);
package Employee;
use Moose;
has [ 'first_name', 'last_name' ] => (
is => 'ro',
isa => 'Str',
required => 1,
);
has 'stapler' => (
is => 'ro',
isa => 'Stapler',
predicate => 'has_stapler'
);
has 'keycard' => (
is => 'ro',
isa => 'KeyCard',
required => 1,
);
has 'work_area' => (
is => 'ro',
isa => 'Cubicle',
required => 1,
);
}
my $UUID = 0;
my $c = container 'Initech' => as {
service 'keycard_uuid_generator' => (
block => sub { ++$UUID }
);
typemap 'KeyCardUUID' => 'keycard_uuid_generator';
typemap 'Employee' => infer;
};
my $micheal = $c->resolve(
type => 'Employee',
parameters => {
first_name => 'Micheal',
last_name => 'Bolton'
}
);
my $samir = $c->resolve(
type => 'Employee',
parameters => {
first_name => 'Samir',
last_name => 'Nagheenanajar'
}
);
isa_ok($micheal, 'Employee');
is($micheal->first_name, 'Micheal', '... got the right first name');
is($micheal->last_name, 'Bolton', '... got the right last name');
isa_ok($micheal->work_area, 'Cubicle');
isa_ok($micheal->work_area->desk, 'Desk');
isa_ok($micheal->work_area->chair, 'Chair');
ok(!$micheal->has_stapler, '... Micheal doesnt have a stapler');
isa_ok($samir, 'Employee');
is($samir->first_name, 'Samir', '... got the right first name');
is($samir->last_name, 'Nagheenanajar', '... got the right last name');
isa_ok($samir->work_area, 'Cubicle');
isa_ok($samir->work_area->desk, 'Desk');
isa_ok($samir->work_area->chair, 'Chair');
ok(!$samir->has_stapler, '... Samir doesnt have a stapler');
isnt($micheal, $samir, '... two different employees');
isnt($micheal->work_area, $samir->work_area, '... two different work_areas');
isnt($micheal->work_area->chair, $samir->work_area->chair, '... two different work_area chairs');
isnt($micheal->work_area->desk, $samir->work_area->desk, '... two different work_area desks');
isnt($micheal->keycard, $samir->keycard, '... two different keycards');
isnt($micheal->keycard->uuid, $samir->keycard->uuid, '... two different keycard uuids');
my $milton = $c->resolve(
type => 'Employee',
parameters => {
first_name => 'Milton',
last_name => 'Waddams',
stapler => Stapler->new
}
);
isa_ok($milton, 'Employee');
is($milton->first_name, 'Milton', '... got the right first name');
is($milton->last_name, 'Waddams', '... got the right last name');
isa_ok($milton->work_area, 'Cubicle');
isa_ok($milton->work_area->desk, 'Desk');
isa_ok($milton->work_area->chair, 'Chair');
ok($milton->has_stapler, '... Milton does have a stapler');
foreach ( $micheal, $samir ) {
isnt($milton, $_, '... two different employees');
isnt($milton->work_area, $_->work_area, '... two different work_areas');
isnt($milton->work_area->chair, $_->work_area->chair, '... two different work_area chairs');
isnt($milton->work_area->desk, $_->work_area->desk, '... two different work_area desks');
isnt($milton->keycard, $_->keycard, '... two different keycards');
isnt($milton->keycard->uuid, $_->keycard->uuid, '... two different keycard uuids');
}
done_testing;