File: array.t

package info (click to toggle)
libreadonly-perl 1.03-2
  • links: PTS, VCS
  • area: main
  • in suites: lenny, squeeze
  • size: 124 kB
  • ctags: 36
  • sloc: perl: 727; makefile: 44
file content (90 lines) | stat: -rw-r--r-- 2,034 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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#!perl -I..

# Readonly array tests

use strict;
use Test::More tests => 23;

# Find the module (1 test)
BEGIN {use_ok('Readonly'); }

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/@a1 @a2/;
my @ma1;

# creation (3 tests)
eval 'Readonly::Array @a1;';
is $@ =>'', 'Create empty global array';
eval 'Readonly::Array @ma1 => ();';
is $@ => '', 'Create empty lexical array';
eval 'Readonly::Array @a2 => (1,2,3,4,5);';
is $@ => '', 'Create global array';

# fetching (3 tests)
ok !defined($a1[0]), 'Fetch global';
is $a2[0]  => 1, 'Fetch global';
is $a2[-1] => 5, 'Fetch global';

# fetch size (3 tests)
is scalar(@a1)  => 0, 'Global size (zero)';
is scalar(@ma1) => 0, 'Lexical size (zero)';
is $#a2 => 4, 'Global last element (nonzero)';

# store (2 tests)
eval {$ma1[0] = 5;};
is $@ => expected(__LINE__-1), 'Lexical store';
eval {$a2[3] = 4;};
is $@ => expected(__LINE__-1), 'Global store';

# storesize (1 test)
eval {$#a1 = 15;};
is $@ => expected(__LINE__-1), 'Change size';

# extend (1 test)
eval {$a1[77] = 88;};
is $@ => expected(__LINE__-1), 'Extend';

# exists (2 tests)
SKIP: {
	skip "Can't do exists on array until Perl 5.6", 2  if $] < 5.006;

	eval 'ok(exists $a2[4], "Global exists")';
	eval 'ok(!exists $ma1[4], "Lexical exists")';
	}

# clear (1 test)
eval {@a1 = ();};
is $@ => expected(__LINE__-1), 'Clear';

# push (1 test)
eval {push @ma1, -1;};
is $@ => expected(__LINE__-1), 'Push';

# unshift (1 test)
eval {unshift @a2, -1;};
is $@ => expected(__LINE__-1), 'Unshift';

# pop (1 test)
eval {pop (@a2);};
is $@ => expected(__LINE__-1), 'Pop';

# shift (1 test)
eval {shift (@a2);};
is $@ => expected(__LINE__-1), 'shift';

# splice (1 test)
eval {splice @a2, 0, 1;};
is $@ => expected(__LINE__-1), 'Splice';

# untie (1 test)
SKIP: {
	skip "Can't catch untie until Perl 5.6", 1  if $] <= 5.006;
	eval {untie @a2;};
	is $@ => expected(__LINE__-1), 'Untie';
	}