Debian Codegraph @

ext/Tie-Memoize/t/Tie-Memoize.t

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