Commit | Line | Data |
ffed8b01 |
1 | ## |
2 | # DBM::Deep Test |
3 | ## |
4 | use strict; |
9a63e1f2 |
5 | use Test::More tests => 21; |
6 | use Test::Deep; |
fde3db1a |
7 | use t::common qw( new_fh ); |
ffed8b01 |
8 | |
9 | use_ok( 'DBM::Deep' ); |
10 | |
fde3db1a |
11 | my ($fh, $filename) = new_fh(); |
ffed8b01 |
12 | my $db = DBM::Deep->new( |
2a81bf9e |
13 | file => $filename, |
ffed8b01 |
14 | ); |
ffed8b01 |
15 | |
16 | ok( !$db->set_filter( 'floober', sub {} ), "floober isn't a value filter key" ); |
17 | |
18 | ## |
19 | # First try store filters only (values will be unfiltered) |
20 | ## |
21 | ok( $db->set_filter( 'store_key', \&my_filter_store_key ), "set the store_key filter" ); |
22 | ok( $db->set_filter( 'store_value', \&my_filter_store_value ), "set the store_value filter" ); |
23 | |
24 | $db->{key1} = "value1"; |
25 | $db->{key2} = "value2"; |
26 | |
27 | is($db->{key1}, "MYFILTERvalue1", "The value for key1 was filtered correctly" ); |
28 | is($db->{key2}, "MYFILTERvalue2", "The value for key2 was filtered correctly" ); |
29 | |
30 | ## |
31 | # Now try fetch filters as well |
32 | ## |
33 | ok( $db->set_filter( 'fetch_key', \&my_filter_fetch_key ), "Set the fetch_key filter" ); |
34 | ok( $db->set_filter( 'fetch_value', \&my_filter_fetch_value), "Set the fetch_value filter" ); |
35 | |
36 | is($db->{key1}, "value1", "Fetchfilters worked right"); |
37 | is($db->{key2}, "value2", "Fetchfilters worked right"); |
38 | |
39 | ## |
40 | # Try fetching keys as well as values |
41 | ## |
9a63e1f2 |
42 | cmp_bag( [ keys %$db ], [qw( key1 key2 )], "DB keys correct" ); |
ffed8b01 |
43 | |
9a63e1f2 |
44 | # Exists and delete tests |
45 | ok( exists $db->{key1}, "Key1 exists" ); |
46 | ok( exists $db->{key2}, "Key2 exists" ); |
47 | |
48 | is( delete $db->{key1}, 'value1', "Delete returns the right value" ); |
49 | |
50 | ok( !exists $db->{key1}, "Key1 no longer exists" ); |
51 | ok( exists $db->{key2}, "Key2 exists" ); |
ffed8b01 |
52 | |
53 | ## |
54 | # Now clear all filters, and make sure all is unfiltered |
55 | ## |
d005c2e8 |
56 | ok( $db->filter_store_key( undef ), "Unset store_key filter" ); |
57 | ok( $db->filter_store_value( undef ), "Unset store_value filter" ); |
58 | ok( $db->filter_fetch_key( undef ), "Unset fetch_key filter" ); |
59 | ok( $db->filter_fetch_value( undef ), "Unset fetch_value filter" ); |
ffed8b01 |
60 | |
9a63e1f2 |
61 | is( $db->{MYFILTERkey2}, "MYFILTERvalue2", "We get the right unfiltered value" ); |
ffed8b01 |
62 | |
63 | sub my_filter_store_key { return 'MYFILTER' . $_[0]; } |
64 | sub my_filter_store_value { return 'MYFILTER' . $_[0]; } |
65 | |
66 | sub my_filter_fetch_key { $_[0] =~ s/^MYFILTER//; return $_[0]; } |
67 | sub my_filter_fetch_value { $_[0] =~ s/^MYFILTER//; return $_[0]; } |