X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2FHash%2FUtil.t;h=20efb443efcda108e16f2b2f0681d531f7696a1a;hb=912440bebca4a2e8a42a1d56c8be458397341faf;hp=1046e32c54cec24c1be583bb7859bd17976f6bb2;hpb=80a5d8e74b5512d4ab704d0e83466ae41247ce55;p=p5sagit%2Fp5-mst-13.2.git diff --git a/lib/Hash/Util.t b/lib/Hash/Util.t index 1046e32..20efb44 100644 --- a/lib/Hash/Util.t +++ b/lib/Hash/Util.t @@ -6,7 +6,8 @@ BEGIN { chdir 't'; } } -use Test::More tests => 45; +use Test::More tests => 61; +use strict; my @Exported_Funcs; BEGIN { @@ -168,3 +169,61 @@ TODO: { lock_keys(%ENV); eval { () = $ENV{I_DONT_EXIST} }; like( $@, qr/^Attempt to access disallowed key 'I_DONT_EXIST' in a restricted hash/, 'locked %ENV'); + +{ + my %hash; + + lock_keys(%hash, 'first'); + + is (scalar keys %hash, 0, "place holder isn't a key"); + $hash{first} = 1; + is (scalar keys %hash, 1, "we now have a key"); + delete $hash{first}; + is (scalar keys %hash, 0, "now no key"); + + unlock_keys(%hash); + + $hash{interregnum} = 1.5; + is (scalar keys %hash, 1, "key again"); + delete $hash{interregnum}; + is (scalar keys %hash, 0, "no key again"); + + lock_keys(%hash, 'second'); + + is (scalar keys %hash, 0, "place holder isn't a key"); + + eval {$hash{zeroeth} = 0}; + like ($@, + qr/^Attempt to access disallowed key 'zeroeth' in a restricted hash/, + 'locked key never mentioned before should fail'); + eval {$hash{first} = -1}; + like ($@, + qr/^Attempt to access disallowed key 'first' in a restricted hash/, + 'previously locked place holders should also fail'); + is (scalar keys %hash, 0, "and therefore there are no keys"); + $hash{second} = 1; + is (scalar keys %hash, 1, "we now have just one key"); + delete $hash{second}; + is (scalar keys %hash, 0, "back to zero"); + + unlock_keys(%hash); # We have deliberately left a placeholder. + + $hash{void} = undef; + $hash{nowt} = undef; + + is (scalar keys %hash, 2, "two keys, values both undef"); + + lock_keys(%hash); + + is (scalar keys %hash, 2, "still two keys after locking"); + + eval {$hash{second} = -1}; + like ($@, + qr/^Attempt to access disallowed key 'second' in a restricted hash/, + 'previously locked place holders should fail'); + + is ($hash{void}, undef, + "undef values should not be misunderstood as placeholders"); + is ($hash{nowt}, undef, + "undef values should not be misunderstood as placeholders (again)"); +}