Standardized test incantations
[dbsrgits/DBM-Deep.git] / t / 19_crossref.t
1 ##
2 # DBM::Deep Test
3 ##
4 use strict;
5 use Test::More tests => 9;
6 use Test::Exception;
7 use t::common qw( new_fh );
8
9 use_ok( 'DBM::Deep' );
10
11 my ($fh2, $filename2) = new_fh();
12 my $db2 = DBM::Deep->new( $filename2 );
13
14 SKIP: {
15     skip "Apparently, we cannot detect a tied scalar?", 1;
16     tie my $foo, 'Tied::Scalar';
17     throws_ok {
18         $db2->{failure} = $foo;
19     } qr/Cannot store something that is tied\./, "tied scalar storage fails";
20 }
21
22 {
23     tie my @foo, 'Tied::Array';
24     throws_ok {
25         $db2->{failure} = \@foo;
26     } qr/Cannot store something that is tied\./, "tied array storage fails";
27 }
28
29 {
30     tie my %foo, 'Tied::Hash';
31     throws_ok {
32         $db2->{failure} = \%foo;
33     } qr/Cannot store something that is tied\./, "tied hash storage fails";
34 }
35
36 {
37     my ($fh, $filename) = new_fh();
38     my $db = DBM::Deep->new( $filename );
39
40     ##
41     # Create structure in $db
42     ##
43     $db->import({
44         hash1 => {
45             subkey1 => "subvalue1",
46             subkey2 => "subvalue2",
47         }
48     });
49     is( $db->{hash1}{subkey1}, 'subvalue1', "Value imported correctly" );
50     is( $db->{hash1}{subkey2}, 'subvalue2', "Value imported correctly" );
51
52     # Test cross-ref nested hash accross DB objects
53     throws_ok {
54         $db2->{copy} = $db->{hash1};
55     } qr/Cannot store values across DBM::Deep files\. Please use export\(\) instead\./, "cross-ref fails";
56
57     # This error text is for when internal cross-refs are implemented
58     #} qr/Cannot cross-reference\. Use export\(\) instead\./, "cross-ref fails";
59
60     $db2->{copy} = $db->{hash1}->export;
61 }
62
63 ##
64 # Make sure $db2 has copy of $db's hash structure
65 ##
66 is( $db2->{copy}{subkey1}, 'subvalue1', "Value copied correctly" );
67 is( $db2->{copy}{subkey2}, 'subvalue2', "Value copied correctly" );
68
69 package Tied::Scalar;
70 sub TIESCALAR { bless {}, $_[0]; }
71 sub FETCH{}
72
73 package Tied::Array;
74 sub TIEARRAY { bless {}, $_[0]; }
75
76 package Tied::Hash;
77 sub TIEHASH { bless {}, $_[0]; }