File: 24_autobless.t

package info (click to toggle)
libdbm-deep-perl 2.0008-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 884 kB
  • sloc: perl: 7,383; sql: 36
file content (191 lines) | stat: -rw-r--r-- 5,017 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use strict;
use warnings FATAL => 'all';

{
    package Foo;

    sub export { 'export' };
    sub foo { 'foo' };
}

use Test::More;
use t::common qw( new_dbm );

use_ok( 'DBM::Deep' );

my $dbm_factory = new_dbm( autobless => 1 );
while ( my $dbm_maker = $dbm_factory->() ) {
    {
        my $db = $dbm_maker->();

        my $obj = bless {
            a => 1,
            b => [ 1 .. 3 ],
        }, 'Foo';

        $db->{blessed} = $obj;
        is( $db->{blessed}{a}, 1 );
        is( $db->{blessed}{b}[0], 1 );
        is( $db->{blessed}{b}[1], 2 );
        is( $db->{blessed}{b}[2], 3 );

        my $obj2 = bless [
            { a => 'foo' },
            2,
        ], 'Foo';
        $db->{blessed2} = $obj2;

        is( $db->{blessed2}[0]{a}, 'foo' );
        is( $db->{blessed2}[1], '2' );

        $db->{unblessed} = {};
        $db->{unblessed}{a} = 1;
        $db->{unblessed}{b} = [];
        $db->{unblessed}{b}[0] = 1;
        $db->{unblessed}{b}[1] = 2;
        $db->{unblessed}{b}[2] = 3;

        is( $db->{unblessed}{a}, 1 );
        is( $db->{unblessed}{b}[0], 1 );
        is( $db->{unblessed}{b}[1], 2 );
        is( $db->{unblessed}{b}[2], 3 );

        $db->{blessed_long} = bless {}, 'a' x 1000;
        $db->_get_self->_engine->storage->close( $db->_get_self );
    }

    {
        my $db = $dbm_maker->();

        my $obj = $db->{blessed};
        isa_ok( $obj, 'Foo' );
        can_ok( $obj, 'export', 'foo' );
        ok( !$obj->can( 'STORE' ), "... but it cannot 'STORE'" );

        is( $obj->{a}, 1 );
        is( $obj->{b}[0], 1 );
        is( $obj->{b}[1], 2 );
        is( $obj->{b}[2], 3 );

        my $obj2 = $db->{blessed2};
        isa_ok( $obj2, 'Foo' );
        can_ok( $obj2, 'export', 'foo' );
        ok( !$obj2->can( 'STORE' ), "... but it cannot 'STORE'" );

        is( $obj2->[0]{a}, 'foo' );
        is( $obj2->[1], '2' );

        is( $db->{unblessed}{a}, 1 );
        is( $db->{unblessed}{b}[0], 1 );
        is( $db->{unblessed}{b}[1], 2 );
        is( $db->{unblessed}{b}[2], 3 );

        $obj->{c} = 'new';
        is( $db->{blessed}{c}, 'new' );

        isa_ok( $db->{blessed_long}, 'a' x 1000 );
        $db->_get_self->_engine->storage->close( $db->_get_self );
    }

    {
        my $db = $dbm_maker->();
        is( $db->{blessed}{c}, 'new' );

        my $structure = $db->export();
        use Data::Dumper;print Dumper $structure;

        my $obj = $structure->{blessed};
        isa_ok( $obj, 'Foo' );
        can_ok( $obj, 'export', 'foo' );
        ok( !$obj->can( 'STORE' ), "... but it cannot 'STORE'" );

        is( $obj->{a}, 1 );
        is( $obj->{b}[0], 1 );
        is( $obj->{b}[1], 2 );
        is( $obj->{b}[2], 3 );

        my $obj2 = $structure->{blessed2};
        isa_ok( $obj2, 'Foo' );
        can_ok( $obj2, 'export', 'foo' );
        ok( !$obj2->can( 'STORE' ), "... but it cannot 'STORE'" );

        is( $obj2->[0]{a}, 'foo' );
        is( $obj2->[1], '2' );

        is( $structure->{unblessed}{a}, 1 );
        is( $structure->{unblessed}{b}[0], 1 );
        is( $structure->{unblessed}{b}[1], 2 );
        is( $structure->{unblessed}{b}[2], 3 );
        $db->_get_self->_engine->storage->close( $db->_get_self );
    }

    {
        my $db = $dbm_maker->( autobless => 0 );

        my $obj = $db->{blessed};
        isa_ok( $obj, 'DBM::Deep' );
        can_ok( $obj, 'export', 'STORE' );
        ok( !$obj->can( 'foo' ), "... but it cannot 'foo'" );

        is( $obj->{a}, 1 );
        is( $obj->{b}[0], 1 );
        is( $obj->{b}[1], 2 );
        is( $obj->{b}[2], 3 );

        my $obj2 = $db->{blessed2};
        isa_ok( $obj2, 'DBM::Deep' );
        can_ok( $obj2, 'export', 'STORE' );
        ok( !$obj2->can( 'foo' ), "... but it cannot 'foo'" );

        is( $obj2->[0]{a}, 'foo' );
        is( $obj2->[1], '2' );

        is( $db->{unblessed}{a}, 1 );
        is( $db->{unblessed}{b}[0], 1 );
        is( $db->{unblessed}{b}[1], 2 );
        is( $db->{unblessed}{b}[2], 3 );
        $db->_get_self->_engine->storage->close( $db->_get_self );
    }
}

$dbm_factory = new_dbm( autobless => 1 );
while ( my $dbm_maker = $dbm_factory->() ) {
    {
        my $db = $dbm_maker->();
        my $obj = bless {
            a => 1,
            b => [ 1 .. 3 ],
        }, 'Foo';

        $db->import( { blessed => $obj } );
    }

    {
        my $db = $dbm_maker->();

        my $blessed = $db->{blessed};
        isa_ok( $blessed, 'Foo' );
        is( $blessed->{a}, 1 );
    }
}

# test blessing hash into short named class (Foo), then re-blessing into
# longer named class (FooFoo) and replacing key in db file, then validating
# content after that point in file to check for corruption.
$dbm_factory = new_dbm( autobless => 1 );
while ( my $dbm_maker = $dbm_factory->() ) {
    my $db = $dbm_maker->();

    my $obj = bless {}, 'Foo';

    $db->{blessed} = $obj;
    $db->{after} = "hello";

    my $obj2 = bless {}, 'FooFoo';

    $db->{blessed} = $obj2;

    is( $db->{after}, "hello" );
}

done_testing;