Removed _get_self() call in _copy_node
[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 );
3f343de7 12
13use_ok( 'DBM::Deep' );
14
2a81bf9e 15my $dir = tempdir( CLEANUP => 1 );
16my ($fh, $filename) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
95bbd935 17{
18 my $db = DBM::Deep->new(
2a81bf9e 19 file => $filename,
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 );
3f343de7 54}
55
95bbd935 56{
57 my $db = DBM::Deep->new(
2a81bf9e 58 file => $filename,
95bbd935 59 autobless => 1,
60 );
3f343de7 61
95bbd935 62 my $obj = $db->{blessed};
63 isa_ok( $obj, 'Foo' );
64 can_ok( $obj, 'export', 'foo' );
65 ok( !$obj->can( 'STORE' ), "... but it cannot 'STORE'" );
3f343de7 66
95bbd935 67 is( $obj->{a}, 1 );
68 is( $obj->{b}[0], 1 );
69 is( $obj->{b}[1], 2 );
70 is( $obj->{b}[2], 3 );
3f343de7 71
95bbd935 72 my $obj2 = $db->{blessed2};
73 isa_ok( $obj, 'Foo' );
74 can_ok( $obj, 'export', 'foo' );
75 ok( !$obj->can( 'STORE' ), "... but it cannot 'STORE'" );
3f343de7 76
95bbd935 77 is( $obj2->[0]{a}, 'foo' );
78 is( $obj2->[1], '2' );
3f343de7 79
95bbd935 80 is( $db->{unblessed}{a}, 1 );
81 is( $db->{unblessed}{b}[0], 1 );
82 is( $db->{unblessed}{b}[1], 2 );
83 is( $db->{unblessed}{b}[2], 3 );
3f343de7 84
95bbd935 85 $obj->{c} = 'new';
86 is( $db->{blessed}{c}, 'new' );
87}
3f343de7 88
95bbd935 89{
90 my $db = DBM::Deep->new(
2a81bf9e 91 file => $filename,
95bbd935 92 autobless => 1,
93 );
94 is( $db->{blessed}{c}, 'new' );
e7ffaba0 95
95bbd935 96 my $structure = $db->export();
97
98 my $obj = $structure->{blessed};
99 isa_ok( $obj, 'Foo' );
100 can_ok( $obj, 'export', 'foo' );
101 ok( !$obj->can( 'STORE' ), "... but it cannot 'STORE'" );
e7ffaba0 102
95bbd935 103 is( $obj->{a}, 1 );
104 is( $obj->{b}[0], 1 );
105 is( $obj->{b}[1], 2 );
106 is( $obj->{b}[2], 3 );
e7ffaba0 107
95bbd935 108 my $obj2 = $structure->{blessed2};
109 isa_ok( $obj, 'Foo' );
110 can_ok( $obj, 'export', 'foo' );
111 ok( !$obj->can( 'STORE' ), "... but it cannot 'STORE'" );
906c8e01 112
95bbd935 113 is( $obj2->[0]{a}, 'foo' );
114 is( $obj2->[1], '2' );
906c8e01 115
116 is( $structure->{unblessed}{a}, 1 );
117 is( $structure->{unblessed}{b}[0], 1 );
118 is( $structure->{unblessed}{b}[1], 2 );
119 is( $structure->{unblessed}{b}[2], 3 );
30c95847 120}
121
95bbd935 122{
123 my $db = DBM::Deep->new(
2a81bf9e 124 file => $filename,
95bbd935 125 );
95bbd935 126
127 my $obj = $db->{blessed};
128 isa_ok( $obj, 'DBM::Deep' );
129 can_ok( $obj, 'export', 'STORE' );
130 ok( !$obj->can( 'foo' ), "... but it cannot 'foo'" );
131
132 is( $obj->{a}, 1 );
133 is( $obj->{b}[0], 1 );
134 is( $obj->{b}[1], 2 );
135 is( $obj->{b}[2], 3 );
136
137 my $obj2 = $db->{blessed2};
138 isa_ok( $obj2, 'DBM::Deep' );
139 can_ok( $obj2, 'export', 'STORE' );
140 ok( !$obj2->can( 'foo' ), "... but it cannot 'foo'" );
141
142 is( $obj2->[0]{a}, 'foo' );
143 is( $obj2->[1], '2' );
144
145 is( $db->{unblessed}{a}, 1 );
146 is( $db->{unblessed}{b}[0], 1 );
147 is( $db->{unblessed}{b}[1], 2 );
148 is( $db->{unblessed}{b}[2], 3 );
3f343de7 149}
150
2a81bf9e 151my ($fh2, $filename2) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
9cb06093 152{
9cb06093 153 my $db = DBM::Deep->new(
2a81bf9e 154 file => $filename2,
9cb06093 155 autobless => 1,
156 );
9cb06093 157 my $obj = bless {
158 a => 1,
159 b => [ 1 .. 3 ],
160 }, 'Foo';
161
162 $db->import( { blessed => $obj } );
2a81bf9e 163}
9cb06093 164
2a81bf9e 165{
166 my $db = DBM::Deep->new(
167 file => $filename2,
9cb06093 168 autobless => 1,
169 );
9cb06093 170
171 my $blessed = $db->{blessed};
172 isa_ok( $blessed, 'Foo' );
173 is( $blessed->{a}, 1 );
174}
70d0fb51 175
176{
177 ##
178 # test blessing hash into short named class (Foo), then re-blessing into
179 # longer named class (FooFoo) and replacing key in db file, then validating
180 # content after that point in file to check for corruption.
181 ##
2a81bf9e 182 my ($fh3, $filename3) = tempfile( 'tmpXXXX', UNLINK => 1, DIR => $dir );
70d0fb51 183 my $db = DBM::Deep->new(
2a81bf9e 184 file => $filename3,
70d0fb51 185 autobless => 1,
186 );
70d0fb51 187
188 my $obj = bless {}, 'Foo';
189
190 $db->{blessed} = $obj;
191 $db->{after} = "hello";
192
193 my $obj2 = bless {}, 'FooFoo';
194
195 $db->{blessed} = $obj2;
196
197 is( $db->{after}, "hello" );
198}