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