| 1 | package CHI::t::Config; | ||
| 2 | $CHI::t::Config::VERSION = '0.60'; | ||
| 3 | use CHI::Util qw(dump_one_line); | ||
| 4 | use CHI::Test; | ||
| 5 | use File::Temp qw(tempdir); | ||
| 6 | use strict; | ||
| 7 | use warnings; | ||
| 8 | use base qw(CHI::Test::Class); | ||
| 9 | |||
| 10 | my $root_dir = tempdir( 'CHI-t-Config-XXXX', TMPDIR => 1, CLEANUP => 1 ); | ||
| 11 | |||
| 12 | my %config = ( | ||
| 13 | storage => { | ||
| 14 | memory => { driver => 'Memory', global => 1 }, | ||
| 15 | file => { driver => 'File', root_dir => $root_dir }, | ||
| 16 | }, | ||
| 17 | namespace => { | ||
| 18 | 'Foo' => { storage => 'file' }, | ||
| 19 | 'Bar' => { storage => 'file', depth => 3 }, | ||
| 20 | }, | ||
| 21 | defaults => { storage => 'memory' }, | ||
| 22 | ); | ||
| 23 | |||
| 24 | { | ||
| 25 | package My::CHI; | ||
| 26 | $My::CHI::VERSION = '0.60'; | ||
| 27 | use base qw(CHI); | ||
| 28 | My::CHI->config( {%config} ); | ||
| 29 | } | ||
| 30 | |||
| 31 | { | ||
| 32 | package My::CHI::Subclass; | ||
| 33 | $My::CHI::Subclass::VERSION = '0.60'; | ||
| 34 | use base qw(My::CHI); | ||
| 35 | } | ||
| 36 | |||
| 37 | { | ||
| 38 | package My::CHI::Memo; | ||
| 39 | $My::CHI::Memo::VERSION = '0.60'; | ||
| 40 | |||
| 41 | use base qw(CHI); | ||
| 42 | My::CHI::Memo->config( { %config, memoize_cache_objects => 1 } ); | ||
| 43 | } | ||
| 44 | |||
| 45 | sub _create { | ||
| 46 | my ( $params, $checks ) = @_; | ||
| 47 | |||
| 48 | my $desc = dump_one_line($params); | ||
| 49 | foreach my $class (qw(My::CHI My::CHI::Subclass)) { | ||
| 50 | my $cache = $class->new(%$params); | ||
| 51 | while ( my ( $key, $value ) = each(%$checks) ) { | ||
| 52 | is( $cache->$key, $value, "$key == $value ($desc)" ); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | sub test_config : Tests { | ||
| 58 | my $self = shift; | ||
| 59 | |||
| 60 | _create( | ||
| 61 | { namespace => 'Foo' }, | ||
| 62 | { | ||
| 63 | namespace => 'Foo', | ||
| 64 | storage => 'file', | ||
| 65 | short_driver_name => 'File', | ||
| 66 | root_dir => $root_dir, | ||
| 67 | depth => 2 | ||
| 68 | }, | ||
| 69 | ); | ||
| 70 | _create( | ||
| 71 | { namespace => 'Bar' }, | ||
| 72 | { | ||
| 73 | namespace => 'Bar', | ||
| 74 | storage => 'file', | ||
| 75 | short_driver_name => 'File', | ||
| 76 | root_dir => $root_dir, | ||
| 77 | depth => 3 | ||
| 78 | } | ||
| 79 | ); | ||
| 80 | _create( | ||
| 81 | { namespace => 'Foo', depth => 4 }, | ||
| 82 | { | ||
| 83 | namespace => 'Foo', | ||
| 84 | storage => 'file', | ||
| 85 | short_driver_name => 'File', | ||
| 86 | root_dir => $root_dir, | ||
| 87 | depth => 4 | ||
| 88 | } | ||
| 89 | ); | ||
| 90 | _create( | ||
| 91 | { namespace => 'Bar', depth => 4 }, | ||
| 92 | { | ||
| 93 | namespace => 'Bar', | ||
| 94 | storage => 'file', | ||
| 95 | short_driver_name => 'File', | ||
| 96 | root_dir => $root_dir, | ||
| 97 | depth => 4 | ||
| 98 | } | ||
| 99 | ); | ||
| 100 | |||
| 101 | my %new_config = %config; | ||
| 102 | $new_config{namespace}->{'Bar'}->{depth} = 5; | ||
| 103 | My::CHI->config( {%new_config} ); | ||
| 104 | _create( | ||
| 105 | { namespace => 'Bar' }, | ||
| 106 | { | ||
| 107 | namespace => 'Bar', | ||
| 108 | storage => 'file', | ||
| 109 | short_driver_name => 'File', | ||
| 110 | root_dir => $root_dir, | ||
| 111 | depth => 5 | ||
| 112 | } | ||
| 113 | ); | ||
| 114 | } | ||
| 115 | |||
| 116 | sub test_memoize : Tests { | ||
| 117 | my $cache1 = My::CHI::Memo->new( namespace => 'Foo' ); | ||
| 118 | my $cache2 = My::CHI::Memo->new( namespace => 'Foo' ); | ||
| 119 | is( $cache1, $cache2, "same - namespace Foo" ); | ||
| 120 | |||
| 121 | my $cache3 = My::CHI::Memo->new( namespace => 'Bar', depth => 4 ); | ||
| 122 | my $cache4 = My::CHI::Memo->new( namespace => 'Bar', depth => 4 ); | ||
| 123 | isnt( $cache3, $cache4, "different - namespace Bar" ); | ||
| 124 | |||
| 125 | My::CHI::Memo->clear_memoized_cache_objects(); | ||
| 126 | my $cache5 = My::CHI::Memo->new( namespace => 'Foo' ); | ||
| 127 | my $cache6 = My::CHI::Memo->new( namespace => 'Foo' ); | ||
| 128 | is( $cache5, $cache6, "same - namespace Foo" ); | ||
| 129 | isnt( $cache1, $cache3, "different - post-clear" ); | ||
| 130 | |||
| 131 | my $cache7 = My::CHI->new( namespace => 'Foo' ); | ||
| 132 | my $cache8 = My::CHI->new( namespace => 'Foo' ); | ||
| 133 | isnt( $cache7, $cache8, "different - namespace Foo - no memoization" ); | ||
| 134 | } | ||
| 135 | |||
| 136 | 1; |