File: scalar.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 (40 lines) | stat: -rw-r--r-- 1,059 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
#!perl -I../../lib

# Readonly scalar tests

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/$s1 $s2/;
my ($ms1, $ms2);

# creation (4 tests)
eval {Readonly::Scalar $s1 => 13};
is $@ => '', 'Create a global scalar';
eval {Readonly::Scalar $ms1 => 31};
is $@ => '', 'Create a lexical scalar';
eval {Readonly::Scalar $s2 => undef};
is $@ => '', 'Create an undef global scalar';
#eval 'Readonly::Scalar $ms2';    # must be eval string because it's a compile-time error
#like $@ => qr/^Not enough arguments for Readonly::Scalar/, 'Try w/o args';

# fetching (4 tests)
is $s1  => 13, 'Fetch global';
is $ms1 => 31, 'Fetch lexical';
ok !defined $s2, 'Fetch undef global';
ok !defined $ms2, 'Fetch undef lexical';

# storing (2 tests)
eval {$s1 = 7};
is $@ => expected(__LINE__-1), 'Error setting global';
is $s1 => 13, 'Readonly global value unchanged';
#
done_testing;