Commit | Line | Data |
3f343de7 |
1 | use strict; |
2 | |
3 | { |
4 | package Foo; |
5 | |
6 | sub export { 'export' }; |
7 | sub foo { 'foo' }; |
8 | } |
9 | |
9cb06093 |
10 | use Test::More tests => 26; |
3f343de7 |
11 | |
12 | use_ok( 'DBM::Deep' ); |
13 | |
14 | unlink 't/test.db'; |
15 | my $db = DBM::Deep->new( |
16 | file => "t/test.db", |
17 | autobless => 1, |
18 | ); |
19 | if ($db->error()) { |
20 | die "ERROR: " . $db->error(); |
21 | } |
22 | |
23 | my $obj = bless { |
24 | a => 1, |
25 | b => [ 1 .. 3 ], |
26 | }, 'Foo'; |
27 | |
28 | $db->{blessed} = $obj; |
29 | |
30 | $db->{unblessed} = {}; |
31 | $db->{unblessed}{a} = 1; |
32 | $db->{unblessed}{b} = []; |
33 | $db->{unblessed}{b}[0] = 1; |
34 | $db->{unblessed}{b}[1] = 2; |
35 | $db->{unblessed}{b}[2] = 3; |
36 | |
37 | undef $db; |
38 | |
39 | my $db2 = DBM::Deep->new( |
40 | file => 't/test.db', |
3f343de7 |
41 | autobless => 1, |
42 | ); |
43 | if ($db2->error()) { |
44 | die "ERROR: " . $db2->error(); |
45 | } |
46 | |
47 | my $obj2 = $db2->{blessed}; |
48 | isa_ok( $obj2, 'Foo' ); |
49 | can_ok( $obj2, 'export', 'foo' ); |
50 | ok( !$obj2->can( 'STORE' ), "... but it cannot 'STORE'" ); |
51 | |
52 | is( $obj2->{a}, 1 ); |
53 | is( $obj2->{b}[0], 1 ); |
54 | is( $obj2->{b}[1], 2 ); |
55 | is( $obj2->{b}[2], 3 ); |
56 | |
57 | is( $db2->{unblessed}{a}, 1 ); |
58 | is( $db2->{unblessed}{b}[0], 1 ); |
59 | is( $db2->{unblessed}{b}[1], 2 ); |
60 | is( $db2->{unblessed}{b}[2], 3 ); |
61 | |
30c95847 |
62 | TODO: { |
63 | todo_skip "_copy_node() doesn't work with autobless", 1; |
64 | my $structure = $db2->export(); |
65 | ok( 1 ); |
66 | } |
67 | |
3f343de7 |
68 | my $db3 = DBM::Deep->new( |
69 | file => 't/test.db', |
3f343de7 |
70 | ); |
71 | if ($db3->error()) { |
72 | die "ERROR: " . $db3->error(); |
73 | } |
74 | |
75 | my $obj3 = $db3->{blessed}; |
76 | isa_ok( $obj3, 'DBM::Deep' ); |
77 | can_ok( $obj3, 'export', 'STORE' ); |
78 | ok( !$obj3->can( 'foo' ), "... but it cannot 'foo'" ); |
79 | |
80 | is( $obj3->{a}, 1 ); |
81 | is( $obj3->{b}[0], 1 ); |
82 | is( $obj3->{b}[1], 2 ); |
83 | is( $obj3->{b}[2], 3 ); |
84 | |
85 | is( $db3->{unblessed}{a}, 1 ); |
86 | is( $db3->{unblessed}{b}[0], 1 ); |
87 | is( $db3->{unblessed}{b}[1], 2 ); |
88 | is( $db3->{unblessed}{b}[2], 3 ); |
9cb06093 |
89 | |
90 | undef $db; |
91 | undef $db2; |
92 | undef $db3; |
93 | |
94 | { |
95 | unlink 't/test.db'; |
96 | my $db = DBM::Deep->new( |
97 | file => "t/test.db", |
98 | autobless => 1, |
99 | ); |
100 | if ($db->error()) { |
101 | die "ERROR: " . $db->error(); |
102 | } |
103 | |
104 | my $obj = bless { |
105 | a => 1, |
106 | b => [ 1 .. 3 ], |
107 | }, 'Foo'; |
108 | |
109 | $db->import( { blessed => $obj } ); |
110 | |
111 | undef $db; |
112 | |
113 | $db = DBM::Deep->new( |
114 | file => "t/test.db", |
115 | autobless => 1, |
116 | ); |
117 | if ($db->error()) { |
118 | die "ERROR: " . $db->error(); |
119 | } |
120 | |
121 | my $blessed = $db->{blessed}; |
122 | isa_ok( $blessed, 'Foo' ); |
123 | is( $blessed->{a}, 1 ); |
124 | } |