File: deeph.t

package info (click to toggle)
libreadonlyx-perl 1.04-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 192 kB
  • sloc: perl: 577; makefile: 2
file content (36 lines) | stat: -rw-r--r-- 803 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!perl -I../../lib

# Test Hash vs Hash1 functionality

use strict;
use Test::More;
use ReadonlyX;

sub expected
{
    my $line = shift;
    $@ =~ s/\.$//;   # difference between croak and die
    return "Modification of a read-only value attempted at " . __FILE__ . " line $line\n";
}

use vars qw/%h1 /;
my $m1 = 17;

# Create (2 tests)
eval {Readonly::Hash  %h1 => (key1 => \$m1, key2 => {x => 5, z => [1, 2, 3]})};
is $@ => '', 'Create a deep reference array';

# Modify (10 tests)
eval {$h1{key1} = 7};
is $@ => expected(__LINE__-1), 'Modify h1';

eval {${$h1{key1}} = "the"};
is $@ => expected(__LINE__-1), 'Deep-modify h1';
is $m1 => 17, 'h1 unchanged';

eval {$h1{key2}{z}[1] = 42};
is $@ => expected(__LINE__-1), 'Deep-deep modify h1';
is $h1{key2}{z}[1] => 2, 'h1 unchanged';

#
done_testing;