This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
15d29a80e18d5f7e3455277b38b6fb7f1dd6bbe0
[perl5.git] / t / op / whereso.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     require './test.pl';
6     set_up_inc('../lib');
7 }
8
9 use strict;
10 use warnings;
11 no warnings 'experimental::smartmatch';
12
13 plan tests => 42;
14
15 foreach(3) {
16     CORE::whereso(3) {
17         pass "CORE::whereso without feature flag";
18     }
19 }
20
21 use feature 'switch';
22
23 foreach(3) {
24     CORE::whereso(3) {
25         pass "CORE::whereso with feature flag";
26     }
27 }
28
29 foreach(3) {
30     whereso(3) {
31         pass "whereso with feature flag";
32     }
33 }
34
35 foreach(0, 1) {
36     my $x = "foo";
37     is($x, "foo", "whereso lexical scope not started yet");
38     whereso(my $x = ($_ && "bar")) {
39         is($x, "bar", "whereso lexical scope starts");
40     }
41     is($x, "foo", "whereso lexical scope ends");
42 }
43
44 foreach(3) {
45     whereso($_ == 2) { fail; }
46     pass;
47 }
48
49 foreach(3) {
50     whereso($_ == 3) { pass; }
51     fail;
52 }
53
54 foreach(3) {
55     whereso($_ == 2) { fail; }
56     whereso($_ == 3) { pass; }
57     whereso($_ == 4) { fail; }
58     whereso($_ == 3) { fail; }
59 }
60
61 foreach(undef, 3) {
62     whereso(undef) { fail; }
63     pass;
64 }
65
66 foreach(undef, 1, 3) {
67     whereso(0) { fail; }
68     pass;
69 }
70
71 foreach(undef, 1, 3) {
72     whereso(1) { pass; }
73     fail;
74 }
75
76 sub is_list_context { wantarray }
77 sub is_scalar_context { !wantarray && defined(wantarray) }
78 sub is_void_context { !defined(wantarray) }
79 foreach(3) {
80     whereso(is_list_context()) { fail; }
81     pass;
82 }
83 foreach(3) {
84     whereso(is_scalar_context()) { pass; }
85     fail;
86 }
87 foreach(3) {
88     whereso(is_void_context()) { fail; }
89     pass;
90 }
91 foreach(3) {
92     whereso(is_list_context) { fail; }
93     pass;
94 }
95 foreach(3) {
96     whereso(is_scalar_context) { pass; }
97     fail;
98 }
99 foreach(3) {
100     whereso(is_void_context) { fail; }
101     pass;
102 }
103
104 my $ps = "foo";
105 foreach(3) {
106     whereso($ps) { pass; }
107     fail;
108 }
109 $ps = "";
110 foreach(3) {
111     whereso($ps) { fail; }
112     pass;
113 }
114 our $gs = "bar";
115 foreach(3) {
116     whereso($gs) { pass; }
117     fail;
118 }
119 $gs = "";
120 foreach(3) {
121     whereso($gs) { fail; }
122     pass;
123 }
124 my @pa = qw(a b c d e);
125 foreach(3) {
126     whereso(@pa) { pass; }
127     fail;
128 }
129 @pa = ();
130 foreach(3) {
131     whereso(@pa) { fail; }
132     pass;
133 }
134 our @ga = qw(a b c d e);
135 foreach(3) {
136     whereso(@ga) { pass; }
137     fail;
138 }
139 @ga = ();
140 foreach(3) {
141     whereso(@ga) { fail; }
142     pass;
143 }
144 my %ph = qw(a b c d e f g h i j);
145 foreach(3) {
146     whereso(%ph) { pass; }
147     fail;
148 }
149 %ph = ();
150 foreach(3) {
151     whereso(%ph) { fail; }
152     pass;
153 }
154 our %gh = qw(a b c d e f g h i j);
155 foreach(3) {
156     whereso(%gh) { pass; }
157     fail;
158 }
159 %gh = ();
160 foreach(3) {
161     whereso(%gh) { fail; }
162     pass;
163 }
164
165 my $one = 1;
166 foreach(3) {
167     whereso($one + 3) { pass; }
168     fail;
169 }
170 foreach(3) {
171     whereso($one - 1) { fail; }
172     pass;
173 }
174
175 foreach(3) {
176     whereso(()) { fail; }
177     pass;
178 }
179
180 foreach my $z (3) {
181     whereso(1) { pass; }
182     fail;
183 }
184
185 my @a = qw(x y z);
186 my $act = "";
187 while(@a) {
188     $act .= "[a@{[0+@a]}]";
189     whereso(shift(@a) eq "y") {
190         $act .= "[b]";
191     }
192     $act .= "[c]";
193 }
194 is $act, "[a3][c][a2][b][a1][c]";
195
196 $act = "";
197 {
198     $act .= "[a]";
199     whereso(0) { $act .= "[b]"; }
200     $act .= "[c]";
201     whereso(1) { $act .= "[d]"; }
202     $act .= "[e]";
203     whereso(1) { $act .= "[f]"; }
204 }
205 is $act, "[a][c][d]";
206
207 1;