File: 001_assign.t

package info (click to toggle)
libreadonly-perl 2.050-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, trixie
  • size: 216 kB
  • sloc: perl: 871; makefile: 2
file content (44 lines) | stat: -rw-r--r-- 1,392 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
#!perl -I../../lib
# Verify the Readonly function rejects initialization by assignment
use strict;
use warnings; no warnings 'misc';
use Test::More;
use Readonly;
#
sub ASSIGNMENT_ERR {
    qr/
    \QInvalid initialization by assignment\E | # Readonly assignment patch
    \QType of arg 1 to Readonly::Readonly must be one of\E # pre v5.16
/x;
}
SKIP: {
    skip 'Readonly $@% syntax is for perl 5.8 or later', 8 unless $] >= 5.008;
    #
    eval 'Readonly my $simple = 2;';
    like $@ => ASSIGNMENT_ERR, 'Reject scalar initialization by assignment';
    #
    eval 'Readonly my @a = (3, 5);';
    like $@ => ASSIGNMENT_ERR,
        'Reject array initialization by assignment';
    #
    eval 'Readonly my %h = (key => 42);';
    like $@ => ASSIGNMENT_ERR,
        'Reject hash initialization by assignment';
    #
    eval 'Readonly my %h = {key => 42};';
    like $@ => ASSIGNMENT_ERR,
        'Reject hash initialization by assignment to hash ref';
    #
    eval 'Readonly my @a;';
    is $@ => '', 'Readonly empty array OK';
    eval 'Readonly my @a; $a[0] = 2;';
    like $@ => qr/Modification of a read-only/,
        'Readonly empty array is read only';
    #
    eval 'Readonly my %h;';
    is $@ => '', 'Readonly empty hash OK';
    eval 'Readonly my %h; $h{key} = "v";';
    like $@ => qr/Modification of a read-only/,
        'Readonly empty hash is read only';
}
done_testing;