| 1 | #!./perl -w |
| 2 | |
| 3 | BEGIN { |
| 4 | chdir 't' if -d 't'; |
| 5 | @INC = '../lib'; |
| 6 | } |
| 7 | |
| 8 | use strict; |
| 9 | use Tie::Memoize; |
| 10 | use Test::More tests => 28; |
| 11 | use File::Spec; |
| 12 | |
| 13 | sub slurp { |
| 14 | my ($key, $dir) = @_; |
| 15 | open my $h, '<', File::Spec->catfile($dir, $key) or return; |
| 16 | local $/; |
| 17 | <$h> # slurp it all |
| 18 | } |
| 19 | sub exists { my ($key, $dir) = @_; return -f File::Spec->catfile($dir, $key) } |
| 20 | |
| 21 | my $directory = File::Spec->catdir(File::Spec->updir, 'lib'); |
| 22 | |
| 23 | tie my %hash, 'Tie::Memoize', \&slurp, $directory, \&exists, |
| 24 | { fake_file1 => 123, fake_file2 => 45678 }, |
| 25 | { 'strict.pm' => 0, known_to_exist => 1 }; |
| 26 | |
| 27 | ok(not exists $hash{'strict.pm'}); |
| 28 | ok(exists $hash{known_to_exist}); |
| 29 | ok($hash{fake_file2} eq 45678); |
| 30 | ok($hash{fake_file1} eq 123); |
| 31 | ok(exists $hash{known_to_exist}); |
| 32 | ok(not exists $hash{'strict.pm'}); |
| 33 | ok(not defined $hash{fake_file3}); |
| 34 | ok(not defined $hash{known_to_exist}); |
| 35 | ok(not exists $hash{known_to_exist}); |
| 36 | ok(not exists $hash{'strict.pm'}); |
| 37 | my $c = slurp('constant.pm', $directory); |
| 38 | ok($c); |
| 39 | ok($hash{'constant.pm'} eq $c); |
| 40 | ok($hash{'constant.pm'} eq $c); |
| 41 | ok(not exists $hash{'strict.pm'}); |
| 42 | ok(exists $hash{'blib.pm'}); |
| 43 | |
| 44 | untie %hash; |
| 45 | |
| 46 | tie %hash, 'Tie::Memoize', \&slurp, $directory; |
| 47 | |
| 48 | ok(exists $hash{'strict.pm'}, 'existing file'); |
| 49 | ok(not exists $hash{fake_file2}); |
| 50 | ok(not exists $hash{fake_file1}); |
| 51 | ok(not exists $hash{known_to_exist}); |
| 52 | ok(exists $hash{'strict.pm'}, 'existing file again'); |
| 53 | ok(not defined $hash{fake_file3}); |
| 54 | ok(not defined $hash{known_to_exist}); |
| 55 | ok(not exists $hash{known_to_exist}); |
| 56 | ok(exists $hash{'strict.pm'}, 'existing file again'); |
| 57 | ok($hash{'constant.pm'} eq $c); |
| 58 | ok($hash{'constant.pm'} eq $c); |
| 59 | ok(exists $hash{'strict.pm'}, 'existing file again'); |
| 60 | ok(exists $hash{'blib.pm'}, 'another existing file'); |
| 61 | |