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