File/Find/t/find.t tests 1 and 2 (was Re: [perl #17061] no strict 'garbage')
[p5sagit/p5-mst-13.2.git] / lib / Hash / Util.t
index 1046e32..20efb44 100644 (file)
@@ -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)");
+}