Added autobless_2.t
[dbsrgits/DBM-Deep.git] / t / autobless_2.t
1 use strict;
2
3 {
4     package Foo;
5
6     sub export { 'export' };
7     sub foo { 'foo' };
8 }
9
10 use Test::More no_plan => 1;
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',
41     autoflush => 1,
42     autobless => 1,
43 );
44 if ($db2->error()) {
45         die "ERROR: " . $db2->error();
46 }
47
48 my $obj2 = $db2->{blessed};
49 isa_ok( $obj2, 'Foo' );
50 can_ok( $obj2, 'export', 'foo' );
51 ok( !$obj2->can( 'STORE' ), "... but it cannot 'STORE'" );
52
53 is( $obj2->{a}, 1 );
54 is( $obj2->{b}[0], 1 );
55 is( $obj2->{b}[1], 2 );
56 is( $obj2->{b}[2], 3 );
57
58 is( $db2->{unblessed}{a}, 1 );
59 is( $db2->{unblessed}{b}[0], 1 );
60 is( $db2->{unblessed}{b}[1], 2 );
61 is( $db2->{unblessed}{b}[2], 3 );
62
63 my $db3 = DBM::Deep->new(
64     file     => 't/test.db',
65     autoflush => 1,
66 #    autobless => 0,
67 );
68 if ($db3->error()) {
69         die "ERROR: " . $db3->error();
70 }
71
72 my $obj3 = $db3->{blessed};
73 isa_ok( $obj3, 'DBM::Deep' );
74 can_ok( $obj3, 'export', 'STORE' );
75 ok( !$obj3->can( 'foo' ), "... but it cannot 'foo'" );
76
77 is( $obj3->{a}, 1 );
78 is( $obj3->{b}[0], 1 );
79 is( $obj3->{b}[1], 2 );
80 is( $obj3->{b}[2], 3 );
81
82 is( $db3->{unblessed}{a}, 1 );
83 is( $db3->{unblessed}{b}[0], 1 );
84 is( $db3->{unblessed}{b}[1], 2 );
85 is( $db3->{unblessed}{b}[2], 3 );