Re: [PATCH] another Storable test (Re: perl@16005)
[p5sagit/p5-mst-13.2.git] / ext / Storable / t / restrict.t
1 #!./perl -w
2
3 #
4 #  Copyright 2002, Larry Wall.
5 #  
6 #  You may redistribute only under the same terms as Perl 5, as specified
7 #  in the README file that comes with the distribution.
8 #
9
10 sub BEGIN {
11     if ($ENV{PERL_CORE}){
12         chdir('t') if -d 't';
13         @INC = '.';
14         push @INC, '../lib';
15         require Config;
16         if ($Config::Config{'extensions'} !~ /\bStorable\b/) {
17             print "1..0 # Skip: Storable was not built\n";
18             exit 0;
19         }
20     } else {
21         unless (eval "require Hash::Util") {
22             if ($@ =~ /Can\'t locate Hash\/Util\.pm in \@INC/) {
23                 print "1..0 # Skip: No Hash::Util\n";
24                 exit 0;
25             } else {
26                 die;
27             }
28         }
29     }
30     require 'lib/st-dump.pl';
31 }
32
33
34 use Storable qw(dclone);
35 use Hash::Util qw(lock_hash unlock_value);
36
37 print "1..16\n";
38
39 my %hash = (question => '?', answer => 42, extra => 'junk', undef => undef);
40 lock_hash %hash;
41 unlock_value %hash, 'answer';
42 unlock_value %hash, 'extra';
43 delete $hash{'extra'};
44
45 my $test;
46
47 package Restrict_Test;
48
49 sub me_second {
50   return (undef, $_[0]);
51 }
52
53 package main;
54
55 sub testit {
56   my $hash = shift;
57   my $copy = dclone $hash;
58
59   my @in_keys = sort keys %$hash;
60   my @out_keys = sort keys %$copy;
61   unless (ok ++$test, "@in_keys" eq "@out_keys") {
62     print "# Failed: keys mis-match after deep clone.\n";
63     print "# Original keys: @in_keys\n";
64     print "# Copy's keys: @out_keys\n";
65   }
66
67   # $copy = $hash;      # used in initial debug of the tests
68
69   ok ++$test, Internals::SvREADONLY(%$copy), "cloned hash restricted?";
70
71   ok ++$test, Internals::SvREADONLY($copy->{question}),
72     "key 'question' not locked in copy?";
73
74   ok ++$test, !Internals::SvREADONLY($copy->{answer}),
75     "key 'answer' not locked in copy?";
76
77   eval { $copy->{extra} = 15 } ;
78   unless (ok ++$test, !$@, "Can assign to reserved key 'extra'?") {
79     my $diag = $@;
80     $diag =~ s/\n.*\z//s;
81     print "# \$\@: $diag\n";
82   }
83
84   eval { $copy->{nono} = 7 } ;
85   ok ++$test, $@, "Can not assign to invalid key 'nono'?";
86
87   ok ++$test, exists $copy->{undef},
88     "key 'undef' exists";
89
90   ok ++$test, !defined $copy->{undef},
91     "value for key 'undef' is undefined";
92 }
93
94 for $Storable::canonical (0, 1) {
95   print "# \$Storable::canonical = $Storable::canonical\n";
96   testit (\%hash);
97   my $object = \%hash;
98   # bless {}, "Restrict_Test";
99 }
100