File Coverage

blib/lib/KiokuDB/Test/Fixture/TypeMap/Default.pm
Criterion Covered Total %
statement 166 211 78.6
branch 13 30 43.3
condition 0 9 0.0
subroutine 27 29 93.1
pod 0 3 0.0
total 206 282 73.0


line stmt bran cond sub pod time code
1             #!/usr/bin/perl
2              
3 2     2   1608 use utf8;
  2         4  
  2         16  
4              
5             package KiokuDB::Test::Fixture::TypeMap::Default;
6 2     2   68 use Moose;
  2         4  
  2         15  
7              
8 2     2   9902 use Encode;
  2         4  
  2         197  
9 2     2   11 use Test::More;
  2         2  
  2         18  
10 2     2   568 use Test::Moose;
  2         4  
  2         14  
11 2     2   731 use Try::Tiny;
  2         4  
  2         132  
12              
13 2     2   10 use KiokuDB::Test::Person;
  2         3  
  2         71  
14 2     2   28 use KiokuDB::Test::Employee;
  2         3  
  2         34  
15 2     2   8 use KiokuDB::Test::Company;
  2         3  
  2         53  
16              
17 2     2   8 use namespace::clean -except => 'meta';
  2         3  
  2         19  
18              
19 2     2   967 use constant required_backend_roles => qw(TypeMap::Default);
  2         3  
  2         129  
20              
21 2     2   1219 use Tie::RefHash;
  2         7549  
  2         91  
22 2     2   12 use constant HAVE_DATETIME => try { require DateTime };
  2         3  
  2         13  
  2         379  
23 2     2   207 use constant HAVE_DATETIME_FMT => try { require DateTime::Format::Strptime };
  2         2  
  2         9  
  2         312  
24 2     2   147 use constant HAVE_URI => try { require URI };
  2         4  
  2         8  
  2         988  
25 2     2   7308 use constant HAVE_URI_WITH_BASE => try { require URI::WithBase };
  2         4  
  2         15  
  2         916  
26 2     2   1474 use constant HAVE_AUTHEN_PASSPHRASE => try { require Authen::Passphrase::SaltedDigest };
  2         213  
  2         15  
  2         385  
27 2     2   174 use constant HAVE_PATH_CLASS => try { require Path::Class };
  2         4  
  2         11  
  2         453  
28 2     2   33359 use constant HAVE_IXHASH => try { require Tie::IxHash };
  2         4  
  2         9  
  2         371  
29 2     2   155 use constant HAVE_MX_TRAITS => try { require MooseX::Traits };
  2         3  
  2         14  
  2         333  
30 2     2   180 use constant HAVE_MX_OP => try { require MooseX::Object::Pluggable };
  2         5  
  2         11  
  2         351  
31              
32             {
33             package Some::Role;
34 2     2   137 use Moose::Role;
  2         3  
  2         21  
35              
36             has role_attr => ( is => "rw" );
37              
38             package Some::Other::Role;
39 2     2   8516 use Moose::Role;
  2         4  
  2         8  
40              
41             has other_role_attr => ( is => "rw" );
42              
43             package Some::Third::Role;
44 2     2   7567 use Moose::Role;
  2         5  
  2         8  
45              
46 0     0 0 0 sub a_role_method { "hello" }
47              
48             package Some::Class;
49 2     2   7434 use Moose;
  2         6  
  2         12  
50              
51             if ( KiokuDB::Test::Fixture::TypeMap::Default::HAVE_MX_TRAITS ) {
52             with qw(MooseX::Traits);
53             }
54              
55             if ( KiokuDB::Test::Fixture::TypeMap::Default::HAVE_MX_OP ) {
56             with qw(MooseX::Object::Pluggable);
57             }
58              
59             has name => ( is => "rw" );
60             }
61              
62             with qw(KiokuDB::Test::Fixture) => { excludes => 'required_backend_roles' };
63              
64             sub create {
65 33     33 0 323 tie my %refhash, 'Tie::RefHash';
66              
67 33         587 $refhash{["foo"]} = "bar";
68              
69 33         506 $refhash{"blah"} = "oink";
70              
71 33         260 my %ixhash;
72 33 50       224 tie %ixhash, 'Tie::IxHash' if HAVE_IXHASH;
73 33         253 %ixhash = ( first => 1, second => "yes", third => "maybe", fourth => "a charm" );
74              
75 33         1193 my $homer = KiokuDB::Test::Employee->new(
76             name => "Homer Simpson",
77             company => KiokuDB::Test::Company->new(
78             name => "Springfield Power Plant",
79             ),
80             );
81              
82 33         300 Some::Role->meta->apply($homer);
83              
84 33         126571 $homer->role_attr("foo");
85              
86 33         58 my $foo = "blah";
87              
88 33         118 my @x = ( 1 );
89              
90             return (
91             scalar => \$foo,
92             refhash => \%refhash,
93 0     0   0 coderef => sub { $x[0]++; },
94             HAVE_IXHASH ? ( ixhash => \%ixhash ) : (),
95             HAVE_DATETIME ? ( datetime => { obj => DateTime->now } ) : (),
96             HAVE_DATETIME_FMT ? ( datetime_fmt => { obj => DateTime->now(formatter => DateTime::Format::Strptime->new( pattern => '%F' ) ) } ) : (),
97             HAVE_PATH_CLASS ? ( path_class => { obj => Path::Class::file('bar', 'foo.txt') } ) : (),
98             HAVE_URI ? ( uri => { obj => URI->new('http://www.google.com/') } ) : (),
99             HAVE_URI_WITH_BASE ? (
100             with_base => {
101             obj => URI::WithBase->new(
102             URI->new('foo'),
103             URI->new('http://www.google.com/')
104             ),
105             },
106             ) : (),
107             HAVE_AUTHEN_PASSPHRASE ? (
108             passphrase => {
109             obj => Authen::Passphrase::SaltedDigest->new(
110             algorithm => "SHA-1", salt_random => 20,
111             passphrase => "passphrase"
112             ),
113             },
114             ) : (),
115             HAVE_MX_TRAITS ? (
116             traits => {
117             obj => Some::Class->new_with_traits(
118             traits => [qw(Some::Other::Role Some::Third::Role)],
119             name => "blah",
120             other_role_attr => "foo",
121             ),
122             },
123             ) : (),
124             HAVE_MX_OP ? (
125             op_one => do {
126 0         0 my $obj = Some::Class->new( name => "first" );
127              
128 0         0 $obj->load_plugin("+Some::Other::Role");
129              
130 0         0 $obj->other_role_attr("after");
131              
132 0         0 $obj;
133             },
134 33 50       569 op_two => do {
    50          
    50          
    50          
    50          
    50          
135 0         0 my $obj = Some::Class->new( name => "second" );
136            
137 0         0 $obj->load_plugin("+Some::Other::Role");
138              
139 0         0 $obj->other_role_attr("after");
140              
141 0         0 $obj->load_plugin("+Some::Third::Role");
142              
143 0         0 $obj;
144             },
145             ) : (),
146             homer => $homer,
147             );
148             }
149              
150             sub verify {
151 33     33 0 100 my $self = shift;
152              
153             {
154 33         49 my $s = $self->new_scope;
  33         171  
155              
156 33         199 my $scalar = $self->lookup_ok("scalar");
157              
158 33         172 is( ref($scalar), "SCALAR", "reftype for scalar" );
159              
160 33         11201 is( $$scalar, "blah", "value" );
161             }
162              
163 33         173 $self->no_live_objects;
164              
165             {
166 33         59 my $s = $self->new_scope;
  33         152  
167              
168 33         141 my $rh = $self->lookup_ok("refhash");
169              
170 33         193 is( ref($rh), "HASH", "plain hash" );
171 33         11263 isa_ok( tied(%$rh), "Tie::RefHash", "tied" );
172              
173 33 0       13200 is_deeply( [ sort { ref($a) ? -1 : ( ref($b) ? 1 : ( $a cmp $b ) ) } keys %$rh ], [ ["foo"], "blah" ], "keys" );
  33 50       1582  
174              
175             }
176              
177 33         275 $self->no_live_objects;
178              
179             {
180 33         58 my $s = $self->new_scope;
  33         162  
181              
182 33         140 my $c = $self->lookup_ok("coderef");
183              
184 33         167 is( ref($c), "CODE", "coderef" );
185              
186 33         11884 is( $c->(), 1, "invoke closure" );
187 33         11942 is( $c->(), 2, "invoke closure" );
188             }
189              
190 33         258 $self->no_live_objects;
191              
192             {
193 33         58 my $s = $self->new_scope;
  33         146  
194              
195 33         151 my $c = $self->lookup_ok("coderef");
196              
197 33         173 is( ref($c), "CODE", "coderef" );
198              
199 33         11799 is( $c->(), 1, "invoke closure" );
200 33         11665 is( $c->(), 2, "invoke closure" );
201              
202 33         10622 $self->store_ok($c);
203             }
204              
205 33         1008 $self->no_live_objects;
206              
207             {
208 33         65 my $s = $self->new_scope;
  33         179  
209              
210 33         161 my $c = $self->lookup_ok("coderef");
211              
212 33         178 is( ref($c), "CODE", "coderef" );
213              
214 33         11621 is( $c->(), 3, "closure updated" );
215             }
216              
217 33         161 $self->no_live_objects;
218              
219             {
220 33         60 my $s = $self->new_scope;
  33         197  
221              
222 33         162 my $homer = $self->lookup_ok("homer");
223              
224 33         193 isa_ok( $homer, "KiokuDB::Test::Person" );
225 33         13969 is( $homer->name, "Homer Simpson", "class attr" );
226 33         11298 does_ok( $homer, "Some::Role", "does runtime role" );
227 33         21564 is( $homer->role_attr, "foo", "role attr" );
228 33         10576 ok( $homer->meta->is_anon_class, "anon class" );
229 33         13310 isa_ok( $homer->company, "KiokuDB::Test::Company" );
230              
231 33         13010 undef $homer;
232             }
233              
234 33 50       231 if ( HAVE_IXHASH ) {
235 0         0 $self->no_live_objects;
236 0         0 my $s = $self->new_scope;
237              
238 0         0 my $ix = $self->lookup_ok("ixhash");
239              
240 0         0 is( ref($ix), "HASH", "plain hash" );
241 0         0 isa_ok( tied(%$ix), "Tie::IxHash", "tied" );
242              
243 0         0 is_deeply( [ keys %$ix ], [ qw(first second third fourth) ], "key order preserved" );
244             }
245              
246 33 50       141 if ( HAVE_DATETIME ) {
247 0         0 $self->no_live_objects;
248 0         0 my $s = $self->new_scope;
249              
250 0         0 my $date = $self->lookup_ok("datetime")->{obj};
251              
252 0         0 isa_ok( $date, "DateTime" );
253             }
254            
255 33 50       158 if ( HAVE_DATETIME_FMT ) {
256 0         0 $self->no_live_objects;
257 0         0 my $s = $self->new_scope;
258              
259 0         0 my $date = $self->lookup_ok("datetime_fmt")->{obj};
260              
261 0         0 isa_ok( $date, "DateTime" );
262            
263 0 0 0     0 SKIP: {
      0        
      0        
264 0         0 skip "Not possible with JSON atm", 1 if (
265             ( $self->directory->backend->can("serializer")
266             and $self->directory->backend->serializer->isa('KiokuDB::Serializer::JSON') )
267             or $self->directory->backend->does("KiokuDB::Backend::Serialize::JSON")
268             or $self->directory->backend->does("KiokuDB::Backend::Serialize::JSPON")
269             );
270            
271 0         0 isa_ok( $date->formatter, "DateTime::Format::Strptime" );
272             }
273            
274             }
275            
276              
277 33         54 if ( HAVE_URI ) {
278 33         162 $self->no_live_objects;
279 33         141 my $s = $self->new_scope;
280              
281 33         143 my $uri = $self->lookup_ok("uri")->{obj};
282              
283 33         174 isa_ok( $uri, "URI" );
284 33         16146 is( "$uri", "http://www.google.com/", "uri" );
285             }
286              
287 33         363 if ( HAVE_URI_WITH_BASE ) {
288 33         170 $self->no_live_objects;
289 33         158 my $s = $self->new_scope;
290              
291 33         150 my $uri = $self->lookup_ok("with_base")->{obj};
292              
293 33         144 isa_ok( $uri, "URI::WithBase" );
294              
295 33         13178 isa_ok( $uri->base, "URI" );
296             }
297              
298 33         131 if ( HAVE_PATH_CLASS ) {
299 33         148 $self->no_live_objects;
300 33         159 my $s = $self->new_scope;
301              
302 33         200 my $file = $self->lookup_ok("path_class")->{obj};
303              
304 33         145 isa_ok( $file, "Path::Class::Entity" );
305 33         11971 isa_ok( $file, "Path::Class::File" );
306              
307 33         11714 is( $file->basename, "foo.txt", "basename" );
308             }
309              
310 33 50       238 if ( HAVE_MX_TRAITS ) {
311 0         0 $self->no_live_objects;
312 0         0 my $s = $self->new_scope;
313              
314 0         0 my $obj = $self->lookup_ok("traits")->{obj};
315              
316 0         0 does_ok( $obj, "Some::Other::Role" );
317 0         0 does_ok( $obj, "Some::Third::Role" );
318              
319 0         0 is( $obj->other_role_attr, "foo", "trait attr" );
320            
321 0         0 is( $obj->name, "blah", "normal attr" );
322             }
323              
324 33 50       282 if ( HAVE_MX_OP ) {
325 0           $self->no_live_objects;
326 0           my $s = $self->new_scope;
327              
328 0           my $one = $self->lookup_ok("op_one");
329              
330 0           does_ok( $one, "Some::Other::Role" );
331              
332 0           is( $one->other_role_attr, "after", "role attr" );
333              
334 0           my $two = $self->lookup_ok("op_two");
335              
336 0           does_ok( $two, "Some::Other::Role" );
337 0           does_ok( $two, "Some::Third::Role" );
338              
339 0           is( eval { $two->other_role_attr }, "after", "role attr" );
  0            
340             }
341             }
342              
343             __PACKAGE__
344              
345             __END__