Skip to content

Commit c227876

Browse files
committed
Add tests for aliasing, or not, of $_ in various block syntaxes
1 parent b070ea7 commit c227876

File tree

1 file changed

+64
-5
lines changed

1 file changed

+64
-5
lines changed

S02-magicals/dollar-underscore.t

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,72 @@
11
use v6;
22

3-
# Tests for a bug uncovered when Jesse Vincent was testing
4-
# functionality for Patrick Michaud
5-
# TODO: add smartlinks, more tests
6-
73
use Test;
84

9-
plan 11;
5+
plan 23;
106

7+
{
8+
my $a;
9+
10+
$_ = 42;
11+
{ $_ := 43 };
12+
is $_, 42, 'bare block aliases $_';
13+
14+
$_ = 42;
15+
if 1 { $_ := 43 };
16+
is $_, 42, 'conditional block (if) aliases $_';
17+
18+
$_ = 42;
19+
if 0 { }
20+
else { $_ := 43 };
21+
is $_, 42, 'conditional block (else) aliases $_';
22+
23+
$_ = 42;
24+
if 0 { }
25+
elsif 1 { $_ := 43 };
26+
is $_, 42, 'conditional block (elsif) aliases $_';
27+
28+
$_ = 42; $a = 1;
29+
while $a-- { $_ := 43 };
30+
is $_, 42, 'conditional loop block (while) aliases $_';
31+
32+
$_ = 42; $a = 0;
33+
until $a++ { $_ := 43 };
34+
is $_, 42, 'conditional loop block (while) aliases $_';
35+
36+
my $c = 43;
37+
my $d = 42;
38+
39+
$_ := $d;
40+
if $_ := $c { };
41+
is $_, 43, 'condition of conditional block (if) does not alias $_';
42+
43+
$_ := $d;
44+
if 0 { }
45+
elsif $_ := $c { };
46+
is $_, 43, 'condition of conditional block (elsif) does not alias $_';
47+
48+
$_ := $d; $a = 0;
49+
while ($_ := $c) + $a++ < 44 { };
50+
is $_, 43, 'condition of loop block (while) does not alias $_';
51+
52+
$_ := $d; $a = 0;
53+
until ($_ := $c) + $a++ > 43 { };
54+
is $_, 43, 'condition of loop block (until) does not alias $_';
55+
56+
$_ := $d; $a = 0;
57+
while $_++ < 44 { my $b = 44; $_ := $b; $a++ };
58+
is $a, 2, 'no interaction between $_s from conditional and body in loop (while)';
59+
60+
$d = 42;
61+
$_ := $d; $a = 0;
62+
until $_++ >= 44 { my $b = 44; $_ := $b; $a++ };
63+
is $a, 2, 'no interaction between $_s from conditional and body in loop (until)';
64+
}
65+
66+
67+
# Tests for a bug uncovered when Jesse Vincent was testing
68+
# functionality for Patrick Michaud
69+
# TODO: add smartlinks, more tests
1170

1271
my @list = ('a');
1372

0 commit comments

Comments
 (0)