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