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