summaryrefslogtreecommitdiff
path: root/t/op/attrs.t
blob: 1ed92a1a8d274c472d3aeebca52cb2578d10415e (plain)
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!./perl -w

# Regression tests for attributes.pm and the C< : attrs> syntax.

BEGIN {
    chdir 't' if -d 't';
    @INC = '../lib';
}

sub NTESTS () ;

my ($test, $ntests);
BEGIN {$ntests=0}
$test=0;
my $failed = 0;

print "1..".NTESTS."\n";

$SIG{__WARN__} = sub { die @_ };

sub mytest {
    my $bad = '';
    if (!$@ ne !$_[0] || $_[0] && $@ !~ $_[0]) {
	if ($@) {
	    my $x = $@;
	    $x =~ s/\n.*\z//s;
	    print "# Got: $x\n"
	}
	else {
	    print "# Got unexpected success\n";
	}
	if ($_[0]) {
	    print "# Expected: $_[0]\n";
	}
	else {
	    print "# Expected success\n";
	}
	$failed = 1;
	$bad = 'not ';
    }
    elsif (@_ == 3 && $_[1] ne $_[2]) {
	print "# Got: $_[1]\n";
	print "# Expected: $_[2]\n";
	$failed = 1;
	$bad = 'not ';
    }
    print $bad."ok ".++$test."\n";
}

eval 'sub t1 ($) : locked { $_[0]++ }';
mytest;
BEGIN {++$ntests}

eval 'sub t2 : locked { $_[0]++ }';
mytest;
BEGIN {++$ntests}

eval 'sub t3 ($) : locked ;';
mytest;
BEGIN {++$ntests}

eval 'sub t4 : locked ;';
mytest;
BEGIN {++$ntests}

my $anon1;
eval '$anon1 = sub ($) : locked:method { $_[0]++ }';
mytest;
BEGIN {++$ntests}

my $anon2;
eval '$anon2 = sub : locked : method { $_[0]++ }';
mytest;
BEGIN {++$ntests}

my $anon3;
eval '$anon3 = sub : method { $_[0]->[1] }';
mytest;
BEGIN {++$ntests}

eval 'sub e1 ($) : plugh ;';
mytest qr/^Invalid CODE attributes?: ["']?plugh["']? at/;
BEGIN {++$ntests}

eval 'sub e2 ($) : plugh(0,0) xyzzy ;';
mytest qr/^Invalid CODE attributes: ["']?plugh\(0,0\)["']? /;
BEGIN {++$ntests}

eval 'sub e3 ($) : plugh(0,0 xyzzy ;';
mytest qr/Unterminated attribute parameter in attribute list at/;
BEGIN {++$ntests}

eval 'sub e4 ($) : plugh + xyzzy ;';
mytest qr/Invalid separator character '[+]' in attribute list at/;
BEGIN {++$ntests}

eval 'my main $x : = 0;';
mytest;
BEGIN {++$ntests}

eval 'my $x : = 0;';
mytest;
BEGIN {++$ntests}

eval 'my $x ;';
mytest;
BEGIN {++$ntests}

eval 'my ($x) : = 0;';
mytest;
BEGIN {++$ntests}

eval 'my ($x) ;';
mytest;
BEGIN {++$ntests}

eval 'my ($x) : ;';
mytest;
BEGIN {++$ntests}

eval 'my ($x,$y) : = 0;';
mytest;
BEGIN {++$ntests}

eval 'my ($x,$y) ;';
mytest;
BEGIN {++$ntests}

eval 'my ($x,$y) : ;';
mytest;
BEGIN {++$ntests}

eval 'my ($x,$y) : plugh;';
mytest qr/^Invalid SCALAR attribute: ["']?plugh["']? at/;
BEGIN {++$ntests}

sub A::MODIFY_SCALAR_ATTRIBUTES { return }
eval 'my A $x : plugh;';
mytest qr/^SCALAR package attribute may clash with future reserved word: ["']?plugh["']? at/;
BEGIN {++$ntests}

eval 'my A $x : plugh plover;';
mytest qr/^SCALAR package attributes may clash with future reserved words: ["']?plugh["']? /;
BEGIN {++$ntests}

eval 'package Cat; my Cat @socks;';
mytest qr/^Can't declare class for non-scalar \@socks in "my"/;
BEGIN {++$ntests}

sub X::MODIFY_CODE_ATTRIBUTES { die "$_[0]" }
sub X::foo { 1 }
*Y::bar = \&X::foo;
*Y::bar = \&X::foo;	# second time for -w
eval 'package Z; sub Y::bar : foo';
mytest qr/^X at /;
BEGIN {++$ntests}

eval 'package Z; sub Y::baz : locked {}';
my @attrs = eval 'attributes::get \&Y::baz';
mytest '', "@attrs", "locked";
BEGIN {++$ntests}

@attrs = eval 'attributes::get $anon1';
mytest '', "@attrs", "locked method";
BEGIN {++$ntests}

sub Z::DESTROY { }
sub Z::FETCH_CODE_ATTRIBUTES { return 'Z' }
my $thunk = eval 'bless +sub : method locked { 1 }, "Z"';
mytest '', ref($thunk), "Z";
BEGIN {++$ntests}

@attrs = eval 'attributes::get $thunk';
mytest '', "@attrs", "locked method Z";
BEGIN {++$ntests}

# Test ability to modify existing sub's (or XSUB's) attributes.
eval 'package A; sub X { $_[0] } sub X : lvalue';
@attrs = eval 'attributes::get \&A::X';
mytest '', "@attrs", "lvalue";
BEGIN {++$ntests}

# Above not with just 'pure' built-in attributes.
sub Z::MODIFY_CODE_ATTRIBUTES { (); }
eval 'package Z; sub L { $_[0] } sub L : Z lvalue';
@attrs = eval 'attributes::get \&Z::L';
mytest '', "@attrs", "lvalue Z";
BEGIN {++$ntests}


# Begin testing attributes that tie

{
    package Ttie;
    sub DESTROY {}
    sub TIESCALAR { my $x = $_[1]; bless \$x, $_[0]; }
    sub FETCH { ${$_[0]} }
    sub STORE {
	#print "# In Ttie::STORE\n";
	::mytest '';
	${$_[0]} = $_[1]*2;
    }
    package Tloop;
    sub MODIFY_SCALAR_ATTRIBUTES { tie ${$_[1]}, 'Ttie', -1; (); }
}

eval '
    package Tloop;
    for my $i (0..2) {
	my $x : TieLoop = $i;
	$x != $i*2 and ::mytest "", $x, $i*2;
    }
';
mytest;
BEGIN {$ntests += 4}

# Other tests should be added above this line

sub NTESTS () { $ntests }

exit $failed;