File: deeps.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 (45 lines) | stat: -rw-r--r-- 1,137 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
37
38
39
40
41
42
43
44
45
#!perl -I../../lib

# Test Scalar vs Scalar1 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/$s2 $s4/;
my $m1 = 17;
my $m2 = \$m1;

# Create (4 tests)
eval {Readonly::Scalar  $s2 => ["this", "is", "a", "test", {x => 5}]};
is $@ => '', 'Create a deep reference scalar';
eval {Readonly::Scalar  $s4 => $m2};
is $@ => '', 'Create a deep scalar ref';

# Modify (16 tests)
eval {$s2 = 7};
is $@ => expected(__LINE__-1), 'Modify s2';
eval {$s4 = 7};
is $@ => expected(__LINE__-1), 'Modify s4';

eval {$s2->[2] = "the"};
is $@ => expected(__LINE__-1), 'Deep-modify s2';
is $s2->[2] => 'a', 's2 modification supposed to fail';

eval {$s2->[4]{z} = 42};
like $@ => qr[Attempt to access disallowed key 'z' in a restricted hash], 'Deep-deep modify s2';
ok !exists($s2->[4]{z}), 's2 mod supposed to fail';

eval {$$s4 = 21};
is $@ => expected(__LINE__-1), 'Deep-modify s4 should fail';
is $m1 => 17, 's4 mod should fail';

#
done_testing;