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