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