Standardized test incantations
[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
2120a181 10use Test::More tests => 65;
fde3db1a 11use t::common qw( new_fh );
3f343de7 12
13use_ok( 'DBM::Deep' );
14
fde3db1a 15my ($fh, $filename) = new_fh();
95bbd935 16{
17 my $db = DBM::Deep->new(
2a81bf9e 18 file => $filename,
95bbd935 19 autobless => 1,
20 );
95bbd935 21
22 my $obj = bless {
23 a => 1,
24 b => [ 1 .. 3 ],
25 }, 'Foo';
26
27 $db->{blessed} = $obj;
9a187d8c 28 is( $db->{blessed}{a}, 1 );
29 is( $db->{blessed}{b}[0], 1 );
30 is( $db->{blessed}{b}[1], 2 );
31 is( $db->{blessed}{b}[2], 3 );
95bbd935 32
33 my $obj2 = bless [
34 { a => 'foo' },
35 2,
36 ], 'Foo';
37 $db->{blessed2} = $obj2;
38
9a187d8c 39 is( $db->{blessed2}[0]{a}, 'foo' );
40 is( $db->{blessed2}[1], '2' );
41
95bbd935 42 $db->{unblessed} = {};
43 $db->{unblessed}{a} = 1;
44 $db->{unblessed}{b} = [];
45 $db->{unblessed}{b}[0] = 1;
46 $db->{unblessed}{b}[1] = 2;
47 $db->{unblessed}{b}[2] = 3;
9a187d8c 48
49 is( $db->{unblessed}{a}, 1 );
50 is( $db->{unblessed}{b}[0], 1 );
51 is( $db->{unblessed}{b}[1], 2 );
52 is( $db->{unblessed}{b}[2], 3 );
2120a181 53
54 $db->{blessed_long} = bless {}, 'a' x 1000;
f1879fdc 55 $db->_get_self->_engine->storage->close( $db->_get_self );
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};
2120a181 75 isa_ok( $obj2, 'Foo' );
76 can_ok( $obj2, 'export', 'foo' );
77 ok( !$obj2->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' );
2120a181 89
90 isa_ok( $db->{blessed_long}, 'a' x 1000 );
f1879fdc 91 $db->_get_self->_engine->storage->close( $db->_get_self );
95bbd935 92}
3f343de7 93
95bbd935 94{
95 my $db = DBM::Deep->new(
2a81bf9e 96 file => $filename,
95bbd935 97 autobless => 1,
98 );
99 is( $db->{blessed}{c}, 'new' );
e7ffaba0 100
95bbd935 101 my $structure = $db->export();
2120a181 102 use Data::Dumper;print Dumper $structure;
45f047f8 103
95bbd935 104 my $obj = $structure->{blessed};
105 isa_ok( $obj, 'Foo' );
106 can_ok( $obj, 'export', 'foo' );
107 ok( !$obj->can( 'STORE' ), "... but it cannot 'STORE'" );
e7ffaba0 108
95bbd935 109 is( $obj->{a}, 1 );
110 is( $obj->{b}[0], 1 );
111 is( $obj->{b}[1], 2 );
112 is( $obj->{b}[2], 3 );
e7ffaba0 113
95bbd935 114 my $obj2 = $structure->{blessed2};
2120a181 115 isa_ok( $obj2, 'Foo' );
116 can_ok( $obj2, 'export', 'foo' );
117 ok( !$obj2->can( 'STORE' ), "... but it cannot 'STORE'" );
906c8e01 118
95bbd935 119 is( $obj2->[0]{a}, 'foo' );
120 is( $obj2->[1], '2' );
906c8e01 121
122 is( $structure->{unblessed}{a}, 1 );
123 is( $structure->{unblessed}{b}[0], 1 );
124 is( $structure->{unblessed}{b}[1], 2 );
125 is( $structure->{unblessed}{b}[2], 3 );
f1879fdc 126 $db->_get_self->_engine->storage->close( $db->_get_self );
30c95847 127}
128
95bbd935 129{
130 my $db = DBM::Deep->new(
2a81bf9e 131 file => $filename,
359a01ac 132 autobless => 0,
95bbd935 133 );
95bbd935 134
135 my $obj = $db->{blessed};
136 isa_ok( $obj, 'DBM::Deep' );
137 can_ok( $obj, 'export', 'STORE' );
138 ok( !$obj->can( 'foo' ), "... but it cannot 'foo'" );
139
140 is( $obj->{a}, 1 );
141 is( $obj->{b}[0], 1 );
142 is( $obj->{b}[1], 2 );
143 is( $obj->{b}[2], 3 );
144
145 my $obj2 = $db->{blessed2};
146 isa_ok( $obj2, 'DBM::Deep' );
147 can_ok( $obj2, 'export', 'STORE' );
148 ok( !$obj2->can( 'foo' ), "... but it cannot 'foo'" );
149
150 is( $obj2->[0]{a}, 'foo' );
151 is( $obj2->[1], '2' );
152
153 is( $db->{unblessed}{a}, 1 );
154 is( $db->{unblessed}{b}[0], 1 );
155 is( $db->{unblessed}{b}[1], 2 );
156 is( $db->{unblessed}{b}[2], 3 );
f1879fdc 157 $db->_get_self->_engine->storage->close( $db->_get_self );
3f343de7 158}
159
9cb06093 160{
2120a181 161 my ($fh2, $filename2) = new_fh();
162 {
163 my $db = DBM::Deep->new(
164 file => $filename2,
165 autobless => 1,
166 );
167 my $obj = bless {
168 a => 1,
169 b => [ 1 .. 3 ],
170 }, 'Foo';
171
172 $db->import( { blessed => $obj } );
f1879fdc 173 $db->_get_self->_engine->storage->close( $db->_get_self );
2120a181 174 }
175
176 {
177 my $db = DBM::Deep->new(
178 file => $filename2,
179 autobless => 1,
180 );
181
182 my $blessed = $db->{blessed};
183 isa_ok( $blessed, 'Foo' );
184 is( $blessed->{a}, 1 );
f1879fdc 185 $db->_get_self->_engine->storage->close( $db->_get_self );
2120a181 186 }
9cb06093 187}
70d0fb51 188
189{
45f047f8 190 ##
191 # test blessing hash into short named class (Foo), then re-blessing into
192 # longer named class (FooFoo) and replacing key in db file, then validating
193 # content after that point in file to check for corruption.
194 ##
fde3db1a 195 my ($fh3, $filename3) = new_fh();
70d0fb51 196 my $db = DBM::Deep->new(
2a81bf9e 197 file => $filename3,
70d0fb51 198 autobless => 1,
199 );
70d0fb51 200
201 my $obj = bless {}, 'Foo';
202
203 $db->{blessed} = $obj;
204 $db->{after} = "hello";
45f047f8 205
70d0fb51 206 my $obj2 = bless {}, 'FooFoo';
45f047f8 207
70d0fb51 208 $db->{blessed} = $obj2;
209
210 is( $db->{after}, "hello" );
211}