-
Notifications
You must be signed in to change notification settings - Fork 135
/
if.t
222 lines (181 loc) · 5.83 KB
/
if.t
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
222
use v6;
use Test;
plan 43;
=begin kwid
Basic "if" tests.
=end kwid
# L<S04/Conditional statements>
my $x = 'test';
if $x eq $x { pass('if ($x eq $x) {} works'); } else { flunk('if ($x eq $x) {} failed'); }
if $x ne $x { flunk('if ($x ne $x) {} failed'); } else { pass('if ($x ne $x) {} works'); }
if 1 { pass('if (1) {} works'); } else { flunk('if (1) {} failed'); }
if 0 { flunk('if (0) {} failed'); } else { pass('if (0) {} works'); }
if Mu { flunk('if (Mu) {} failed'); } else { pass('if (Mu) {} works'); }
{
# die called in the condition part of an if statement should die immediately
# rather than being evaluated as true
my $foo = 1;
try { if die "should die" { $foo = 3 } else { $foo = 2; } };
#say '# $foo = ' ~ $foo;
is $foo, 1, "die should stop execution immediately.";
}
{
my $foo = 1; # just in case
if 1 > 2 { $foo = 2 } else { $foo = 3 };
is $foo, 3, 'if with no parens';
};
# if...elsif
{
my $foo = 1;
if 1 { $foo = 2 } elsif 1 { $foo = 3 };
is $foo, 2, 'if (1) {} elsif (1) {}';
}
{
my $foo = 1;
if 1 { $foo = 2 } elsif 0 { $foo = 3 };
is $foo, 2, 'if (1) {} elsif (0) {}';
}
{
my $foo = 1;
if 0 { $foo = 2 } elsif 1 { $foo = 3 };
is $foo, 3, 'if (0) {} elsif (1) {}';
}
{
my $foo = 1;
if 0 { $foo = 2 } elsif 0 { $foo = 3 };
is $foo, 1, 'if (0) {} elsif (0) {}';
}
# if...elsif...else
{
my $foo = 1;
if 0 { $foo = 2 } elsif 0 { $foo = 3 } else { $foo = 4 };
is $foo, 4;
}
{
my $foo = 1;
if 1 { $foo = 2 } elsif 0 { $foo = 3 } else { $foo = 4 };
is $foo, 2;
}
{
my $foo = 1;
if 1 { $foo = 2 } elsif 1 { $foo = 3 } else { $foo = 4 };
is $foo, 2;
}
{
my $foo = 1;
if 0 { $foo = 2 } elsif 1 { $foo = 3 } else { $foo = 4 };
is $foo, 3;
}
{
my $foo = 1;
if { 1 > 0 } { $foo = 2 } else { $foo = 3 };
is $foo, 2, 'if with parens, and closure as cond';
}
{
my $var = 9;
my sub func( $a, $b, $c ) { $var }; #OK not used
if func 1, 2, 3 { $var = 4 } else { $var = 5 };
is $var, 4, 'if with no parens, and call a function without parenthesis';
}
# I'm not sure where this should go
{
my $flag = 0;
if (my $x = 2) == 2 { $flag = $x }
is($flag, 2, "'my' variable within 'if' conditional");
}
{
throws-like 'if 1; 2', X::Syntax::Missing, '"if" requires a block';
}
# L<S04/"Conditional statements"/The value of the conditional expression may be optionally bound to a closure parameter>
{
my ($got, $a_val, $b_val);
my sub testa { $a_val };
my sub testb { $b_val };
$a_val = 'truea';
$b_val = 0;
if testa() -> $a { $got = $a }
elsif testb() -> $b { $got = $b }
else -> $c { $got = $c }
is $got, 'truea', 'if test() -> $a { } binding';
$a_val = 0;
$b_val = 'trueb';
if testa() -> $a { $got = $a }
elsif testb() -> $b { $got = $b }
else -> $c { $got = $c }
is $got, 'trueb', 'elsif test() -> $b { } binding';
$a_val = '';
$b_val = 0;
if testa() -> $a { $got = $a }
elsif testb() -> $b { $got = $b }
else -> $c { $got = $c }
is $got, 0, 'else -> $c { } binding previous elsif';
$a_val = '';
$b_val = 0;
if testa() -> $a { $got = $a }
else -> $c { $got = $c }
is $got, '', 'else -> $c { } binding previous if';
}
# Sing it again. This time with slurpy.
#?rakudo todo 'RT#105872'
{
my ($got, $a_val, $b_val);
my sub testa { $a_val };
my sub testb { $b_val };
$a_val = 'truea';
$b_val = 0;
if testa() -> *@a { $got = @a }
elsif testb() -> *@b { $got = @b }
else -> *@c { $got = @c }
is $got, ('truea',), 'if test() -> *@a { } binding';
$a_val = 0;
$b_val = 2;
if testa() -> *@a { $got = @a }
elsif testb() -> *@b { $got = @b }
else -> *@c { $got = @c }
is $got, 2, 'elsif test() -> *@b { } binding';
$a_val = '';
$b_val = 3 but False;
if testa() -> *@a { $got = @a }
elsif testb() -> *@b { $got = @b }
else -> *@c { $got = (@c, "yes") }
is $got, (3, "yes"), 'else -> *@c { } binding previous elsif';
$a_val = 4 but False;
$b_val = 0;
if testa() -> *@a { $got = @a }
else -> *@c { $got = (@c, "yes") }
is $got, (4, "yes"), 'else -> *@c { } binding previous if';
}
{
my $called = 0;
sub cond($when) {
is $called,$when,"condition is checked in correct order";
$called++;
0;
}
if cond(0) {
} elsif cond(1) {
} elsif cond(2) {
}
is $called,3,"all conditions are checked";
}
# L<S04/"Conditional statements"/The result of a conditional statement is the result of the block chosen to execute.>
is (if 1 { 42 }), (42), "if evaluates to executed block";
is (if 0 { 42 } else { 43 }), (43), "if+else else evaluates to executed block";
is (if 0 { 42 } else { 43 }), (43), "if+else if evaluates to executed block";
is (if 0 { 42 } elsif 0 { 43 } else { 44 }), (44), "if+elsif+else else evaluates to executed block";
is (if 0 { 42 } elsif 1 { 43 } else { 44 }), (43), "if+elsif+else elsif evaluates to executed block";
is (if 1 { 42 } elsif 0 { 43 } else { 44 }), (42), "if+elsif+else if evaluates to executed block";
is (if 0 { 42 } elsif 1 { 43 }), (43), "if+elsif elsif evaluates to executed block";
is (if 1 { 42 } elsif 0 { 43 }), (42), "if+elsif if evaluates to executed block";
is-deeply (if 0 { 42 } elsif 0 { 43 }), Empty, "if+elsif evaluates to () when no block chosen";
is-deeply (if 0 { 42 }), Empty, "if evaluates to () when no block chosen";
# L<S04/Statement parsing/keywords require whitespace>
throws-like 'if($x > 1) {}', X::Comp::Group, 'keyword needs at least one whitespace after it';
# RT #76174
# scoping of $_ in 'if' shouldn't break aliasing
{
my @a = 0, 1, 2;
for @a { if $_ { $_++ } };
is ~@a, '0 2 3', '"if" does not break lexical aliasing of $_'
}
# vim: ft=perl6