| 1 | #!/usr/bin/perl -Tw
|
|---|
| 2 |
|
|---|
| 3 | BEGIN {
|
|---|
| 4 | if( $ENV{PERL_CORE} ) {
|
|---|
| 5 | @INC = '../lib';
|
|---|
| 6 | chdir 't';
|
|---|
| 7 | }
|
|---|
| 8 | }
|
|---|
| 9 | use Test::More tests => 179;
|
|---|
| 10 | use strict;
|
|---|
| 11 |
|
|---|
| 12 | my @Exported_Funcs;
|
|---|
| 13 | BEGIN {
|
|---|
| 14 | @Exported_Funcs = qw(lock_keys unlock_keys
|
|---|
| 15 | lock_value unlock_value
|
|---|
| 16 | lock_hash unlock_hash
|
|---|
| 17 | hash_seed
|
|---|
| 18 | );
|
|---|
| 19 | use_ok 'Hash::Util', @Exported_Funcs;
|
|---|
| 20 | }
|
|---|
| 21 | foreach my $func (@Exported_Funcs) {
|
|---|
| 22 | can_ok __PACKAGE__, $func;
|
|---|
| 23 | }
|
|---|
| 24 |
|
|---|
| 25 | my %hash = (foo => 42, bar => 23, locked => 'yep');
|
|---|
| 26 | lock_keys(%hash);
|
|---|
| 27 | eval { $hash{baz} = 99; };
|
|---|
| 28 | like( $@, qr/^Attempt to access disallowed key 'baz' in a restricted hash/,
|
|---|
| 29 | 'lock_keys()');
|
|---|
| 30 | is( $hash{bar}, 23 );
|
|---|
| 31 | ok( !exists $hash{baz} );
|
|---|
| 32 |
|
|---|
| 33 | delete $hash{bar};
|
|---|
| 34 | ok( !exists $hash{bar} );
|
|---|
| 35 | $hash{bar} = 69;
|
|---|
| 36 | is( $hash{bar}, 69 );
|
|---|
| 37 |
|
|---|
| 38 | eval { () = $hash{i_dont_exist} };
|
|---|
| 39 | like( $@, qr/^Attempt to access disallowed key 'i_dont_exist' in a restricted hash/ );
|
|---|
| 40 |
|
|---|
| 41 | lock_value(%hash, 'locked');
|
|---|
| 42 | eval { print "# oops" if $hash{four} };
|
|---|
| 43 | like( $@, qr/^Attempt to access disallowed key 'four' in a restricted hash/ );
|
|---|
| 44 |
|
|---|
| 45 | eval { $hash{"\x{2323}"} = 3 };
|
|---|
| 46 | like( $@, qr/^Attempt to access disallowed key '(.*)' in a restricted hash/,
|
|---|
|
|---|