Added unflocks to all tests so that the tests run on OSX
[dbsrgits/DBM-Deep.git] / t / 24_autobless.t
CommitLineData
3f343de7 1use strict;
2
3{
4 package Foo;
5
6 sub export { 'export' };
7 sub foo { 'foo' };
8}
9
9a187d8c 10use Test::More tests => 64;
2a81bf9e 11use File::Temp qw( tempfile tempdir );
58910373 12use Fcntl qw( :flock );
3f343de7 13
14use_ok( 'DBM::Deep' );
15
2a81bf9e 16my $dir = tempdir( CLEANUP => 1 );
17my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
58910373 18flock $fh, LOCK_UN;
95bbd935 19{
20 my $db = DBM::Deep->new(
2a81bf9e 21 file => $filename,
95bbd935 22 autobless => 1,
23 );
95bbd935 24
25 my $obj = bless {
26 a => 1,
27 b => [ 1 .. 3 ],
28 }, 'Foo';
29
30 $db->{blessed} = $obj;
9a187d8c 31 is( $db->{blessed}{a}, 1 );
32 is( $db->{blessed}{b}[0], 1 );
33 is( $db->{blessed}{b}[1], 2 );
34 is( $db->{blessed}{b}[2], 3 );
95bbd935 35
36 my $obj2 = bless [
37 { a => 'foo' },
38 2,
39 ], 'Foo';
40 $db->{blessed2} = $obj2;
41
9a187d8c 42 is( $db->{blessed2}[0]{a}, 'foo' );
43 is( $db->{blessed2}[1], '2' );
44
95bbd935 45 $db->{unblessed} = {};
46 $db->{unblessed}{a} = 1;
47 $db->{unblessed}{b} = [];
48 $db->{unblessed}{b}[0] = 1;
49 $db->{unblessed}{b}[1] = 2;
50 $db->{unblessed}{b}[2] = 3;
9a187d8c 51
52 is( $db->{unblessed}{a}, 1 );
53 is( $db->{unblessed}{b}[0], 1 );
54 is( $db->{unblessed}{b}[1], 2 );
55 is( $db->{unblessed}{b}[2], 3 );
3f343de7 56}
57
95bbd935 58{
59 my $db = DBM::Deep->new(
2a81bf9e 60 file => $filename,
95bbd935 61 autobless => 1,
62 );
3f343de7 63
95bbd935 64 my $obj = $db->{blessed};
65 isa_ok( $obj, 'Foo' );
66 can_ok( $obj, 'export', 'foo' );
67 ok( !$obj->can( 'STORE' ), "... but it cannot 'STORE'" );
3f343de7 68
95bbd935 69 is( $obj->{a}, 1 );
70 is( $obj->{b}[0], 1 );
71 is( $obj->{b}[1], 2 );
72 is( $obj->{b}[2], 3 );
3f343de7 73
95bbd935 74 my $obj2 = $db->{blessed2};
75 isa_ok( $obj, 'Foo' );
76 can_ok( $obj, 'export', 'foo' );
77 ok( !$obj->can( 'STORE' ), "... but it cannot 'STORE'" );
3f343de7 78
95bbd935 79 is( $obj2->[0]{a}, 'foo' );
80 is( $obj2->[1], '2' );
3f343de7 81
95bbd935 82 is( $db->{unblessed}{a}, 1 );
83 is( $db->{unblessed}{b}[0], 1 );
84 is( $db->{unblessed}{b}[1], 2 );
85 is( $db->{unblessed}{b}[2], 3 );
3f343de7 86
95bbd935 87 $obj->{c} = 'new';
88 is( $db->{blessed}{c}, 'new' );
89}
3f343de7 90
95bbd935 91{
92 my $db = DBM::Deep->new(
2a81bf9e 93 file => $filename,
95bbd935 94 autobless => 1,
95 );
96 is( $db->{blessed}{c}, 'new' );
e7ffaba0 97
95bbd935 98 my $structure = $db->export();
99
100 my $obj = $structure->{blessed};
101 isa_ok( $obj, 'Foo' );
102 can_ok( $obj, 'export', 'foo' );
103 ok( !$obj->can( 'STORE' ), "... but it cannot 'STORE'" );
e7ffaba0 104
95bbd935 105 is( $obj->{a}, 1 );
106 is( $obj->{b}[0], 1 );
107 is( $obj->{b}[1], 2 );
108 is( $obj->{b}[2], 3 );
e7ffaba0 109
95bbd935 110 my $obj2 = $structure->{blessed2};
111 isa_ok( $obj, 'Foo' );
112 can_ok( $obj, 'export', 'foo' );
113 ok( !$obj->can( 'STORE' ), "... but it cannot 'STORE'" );
906c8e01 114
95bbd935 115 is( $obj2->[0]{a}, 'foo' );
116 is( $obj2->[1], '2' );
906c8e01 117
118 is( $structure->{unblessed}{a}, 1 );
119 is( $structure->{unblessed}{b}[0], 1 );
120 is( $structure->{unblessed}{b}[1], 2 );
121 is( $structure->{unblessed}{b}[2], 3 );
30c95847 122}
123
95bbd935 124{
125 my $db = DBM::Deep->new(
2a81bf9e 126 file => $filename,
95bbd935 127 );
95bbd935 128
129 my $obj = $db->{blessed};
130 isa_ok( $obj, 'DBM::Deep' );
131 can_ok( $obj, 'export', 'STORE' );
132 ok( !$obj->can( 'foo' ), "... but it cannot 'foo'" );
133
134 is( $obj->{a}, 1 );
135 is( $obj->{b}[0], 1 );
136 is( $obj->{b}[1], 2 );
137 is( $obj->{b}[2], 3 );
138
139 my $obj2 = $db->{blessed2};
140 isa_ok( $obj2, 'DBM::Deep' );
141 can_ok( $obj2, 'export', 'STORE' );
142 ok( !$obj2->can( 'foo' ), "... but it cannot 'foo'" );
143
144 is( $obj2->[0]{a}, 'foo' );
145 is( $obj2->[1], '2' );
146
147 is( $db->{unblessed}{a}, 1 );
148 is( $db->{unblessed}{b}[0], 1 );
149 is( $db->{unblessed}{b}[1], 2 );
150 is( $db->{unblessed}{b}[2], 3 );
3f343de7 151}
152
2a81bf9e 153my ($fh2, $filename2) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
58910373 154flock $fh2, LOCK_UN;
9cb06093 155{
9cb06093 156 my $db = DBM::Deep->new(
2a81bf9e 157 file => $filename2,
9cb06093 158 autobless => 1,
159 );
9cb06093 160 my $obj = bless {
161 a => 1,
162 b => [ 1 .. 3 ],
163 }, 'Foo';
164
165 $db->import( { blessed => $obj } );
2a81bf9e 166}
9cb06093 167
2a81bf9e 168{
169 my $db = DBM::Deep->new(
170 file => $filename2,
9cb06093 171 autobless => 1,
172 );
9cb06093 173
174 my $blessed = $db->{blessed};
175 isa_ok( $blessed, 'Foo' );
176 is( $blessed->{a}, 1 );
177}
70d0fb51 178
179{
180 ##
181 # test blessing hash into short named class (Foo), then re-blessing into
182 # longer named class (FooFoo) and replacing key in db file, then validating
183 # content after that point in file to check for corruption.
184 ##
2a81bf9e 185 my ($fh3, $filename3) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
58910373 186 flock $fh3, LOCK_UN;
70d0fb51 187 my $db = DBM::Deep->new(
2a81bf9e 188 file => $filename3,
70d0fb51 189 autobless => 1,
190 );
70d0fb51 191
192 my $obj = bless {}, 'Foo';
193
194 $db->{blessed} = $obj;
195 $db->{after} = "hello";
196
197 my $obj2 = bless {}, 'FooFoo';
198
199 $db->{blessed} = $obj2;
200
201 is( $db->{after}, "hello" );
202}