Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 178 lines (142 sloc) 4.071 kb
dfd54e2 [t] move statements/if.t to spec/
moritz authored
1 use v6;
2
3 use Test;
4
5 =begin kwid
6
7 Basic "if" tests.
8
9
10 =end kwid
11
94226b4 t/spec more smartlink fixes
szabgab authored
12 # L<S04/Conditional statements>
13
a4cde9f @pmurias tests if the conditions of an if are checked in order
pmurias authored
14 plan 29;
dfd54e2 [t] move statements/if.t to spec/
moritz authored
15
16 my $x = 'test';
4ad5e9c [t] use '' so {} doesn't get interpolated
pmurias authored
17 if ($x eq $x) { pass('if ($x eq $x) {} works'); } else { flunk('if ($x eq $x) {} failed'); }
18 if ($x ne $x) { flunk('if ($x ne $x) {} failed'); } else { pass('if ($x ne $x) {} works'); }
19 if (1) { pass('if (1) {} works'); } else { flunk('if (1) {} failed'); }
20 if (0) { flunk('if (0) {} failed'); } else { pass('if (0) {} works'); }
8f9a119 [t/] unify compartmentalized undef and Object concepts into Mu
lwall authored
21 if (Mu) { flunk('if (Mu) {} failed'); } else { pass('if (Mu) {} works'); }
dfd54e2 [t] move statements/if.t to spec/
moritz authored
22
fe6c53a [t] add block
pmurias authored
23 {
24 # die called in the condition part of an if statement should die immediately
25 # rather than being evaluated as true
26 my $foo = 1;
27 try { if (die "should die") { $foo = 3 } else { $foo = 2; } };
28 #say '# $foo = ' ~ $foo;
29 is $foo, 1, "die should stop execution immediately.";
30 }
dfd54e2 [t] move statements/if.t to spec/
moritz authored
31
32 {
33 my $foo = 1; # just in case
34 if 1 > 2 { $foo = 2 } else { $foo = 3 };
35 is $foo, 3, 'if with no parens';
36 };
37
38 # if...elsif
39 {
40 my $foo = 1;
41 if (1) { $foo = 2 } elsif (1) { $foo = 3 };
42 is $foo, 2, 'if (1) {} elsif (1) {}';
43 }
44
45 {
46 my $foo = 1;
47 if (1) { $foo = 2 } elsif (0) { $foo = 3 };
48 is $foo, 2, 'if (1) {} elsif (0) {}';
49 }
50
51 {
52 my $foo = 1;
53 if (0) { $foo = 2 } elsif (1) { $foo = 3 };
54 is $foo, 3, 'if (0) {} elsif (1) {}';
55 }
56
57 {
58 my $foo = 1;
59 if (0) { $foo = 2 } elsif (0) { $foo = 3 };
60 is $foo, 1, 'if (0) {} elsif (0) {}';
61 }
62
f256af3 @pmurias add fudge markers for niecza
pmurias authored
63
dfd54e2 [t] move statements/if.t to spec/
moritz authored
64 # if...elsif...else
65
66 {
67 my $foo = 1;
68 if (0) { $foo = 2 } elsif (0) { $foo = 3 } else { $foo = 4 };
69 is $foo, 4;
70 }
71
72 {
73 my $foo = 1;
74 if (1) { $foo = 2 } elsif (0) { $foo = 3 } else { $foo = 4 };
75 is $foo, 2;
76 }
77
78 {
79 my $foo = 1;
80 if (1) { $foo = 2 } elsif (1) { $foo = 3 } else { $foo = 4 };
81 is $foo, 2;
82 }
83
84 {
85 my $foo = 1;
86 if (0) { $foo = 2 } elsif (1) { $foo = 3 } else { $foo = 4 };
87 is $foo, 3;
88 }
89
90 {
91 my $foo = 1;
92 if ({ 1 > 0 }) { $foo = 2 } else { $foo = 3 };
261108c [t] fix test description
pmurias authored
93 is $foo, 2, 'if with parens, and closure as cond';
dfd54e2 [t] move statements/if.t to spec/
moritz authored
94 }
95
96 {
97 my $var = 9;
8dc7d4b [t/spec] mark various tests that intentionally declare things that are n...
lwall authored
98 my sub func( $a, $b, $c ) { $var }; #OK not used
dfd54e2 [t] move statements/if.t to spec/
moritz authored
99 if func 1, 2, 3 { $var = 4 } else { $var = 5 };
100 is $var, 4, 'if with no parens, and call a function without parenthesis';
101 }
102
103 # I'm not sure where this should go
104
105 {
6bbfbef @pmurias remove needless use of eval
pmurias authored
106 my $flag = 0;
107 if ( my $x = 2 ) == 2 { $flag = $x }
108 is($flag, 2, "'my' variable within 'if' conditional");
dfd54e2 [t] move statements/if.t to spec/
moritz authored
109 }
110
111 {
de12da2 [t/spec] fudged if.t for rakudo; cleared up one test
moritz authored
112 eval_dies_ok('if 1; 2', '"if" requires a block');
dfd54e2 [t] move statements/if.t to spec/
moritz authored
113 }
114
115 # L<S04/"Conditional statements"/The value of the conditional expression may be optionally bound to a closure parameter>
23a618d @coke pugs fudge
coke authored
116 #?pugs skip 'Cannot bind to non-existing variable: "$a"'
dfd54e2 [t] move statements/if.t to spec/
moritz authored
117 {
118 my ($got, $a_val, $b_val);
119 my sub testa { $a_val };
120 my sub testb { $b_val };
121
122 $a_val = 'truea';
123 $b_val = 0;
124 if testa() -> $a { $got = $a }
125 elsif testb() -> $b { $got = $b }
126 else -> $c { $got = $c }
127 is $got, 'truea', 'if test() -> $a { } binding';
128
129 $a_val = 0;
130 $b_val = 'trueb';
131 if testa() -> $a { $got = $a }
132 elsif testb() -> $b { $got = $b }
133 else -> $c { $got = $c }
134 is $got, 'trueb', 'elsif test() -> $b { } binding';
135
136 $a_val = '';
137 $b_val = 0;
138 if testa() -> $a { $got = $a }
139 elsif testb() -> $b { $got = $b }
140 else -> $c { $got = $c }
141 is $got, 0, 'else -> $c { } binding previous elsif';
142
143 $a_val = '';
144 $b_val = 0;
145 if testa() -> $a { $got = $a }
146 else -> $c { $got = $c }
147 is $got, '', 'else -> $c { } binding previous if';
148 }
149
a4cde9f @pmurias tests if the conditions of an if are checked in order
pmurias authored
150 {
151 my $called = 0;
152 sub cond($when) {
153 is $called,$when,"condition is checked in correct order";
154 $called++;
155 0;
156 }
157 if cond(0) {
158 } elsif cond(1) {
159 } elsif cond(2) {
160 }
161 is $called,3,"all conditions are checked";
162 }
163
164
94226b4 t/spec more smartlink fixes
szabgab authored
165 # L<S04/Statement parsing/keywords require whitespace>
23a618d @coke pugs fudge
coke authored
166 #?pugs todo
abbd86a [t/spec] Added test for required whitespace after a built-in statement (...
azawawi authored
167 eval_dies_ok('if($x > 1) {}','keyword needs at least one whitespace after it');
168
19422e1 [t/spec] test for RT #76174, "if" does not break lexical aliasing of $_ ...
moritz authored
169 # RT #76174
170 # scoping of $_ in 'if' shouldn't break aliasing
171 {
172 my @a = 0, 1, 2;
173 for @a { if $_ { $_++ } };
174 is ~@a, '0 2 3', '"if" does not break lexical aliasing of $_'
175 }
176
dfd54e2 [t] move statements/if.t to spec/
moritz authored
177 # vim: ft=perl6
Something went wrong with that request. Please try again.