11 use Test::More tests => 107;
13 # The behaviour of the feature pragma should be tested by lib/switch.t
14 # using the tests in t/lib/switch/*. This file tests the behaviour of
15 # the switch ops themselves.
19 no warnings "numeric";
22 like($@, qr/^Can't "continue" outside/, "continue outside");
25 like($@, qr/^Can't "break" outside/, "break outside");
31 given(my $x = "bar") {
32 is($x, "bar", "given scope starts");
34 is($x, "foo", "given scope ends");
39 given(my $x = "foo") {
40 when(be_true(my $x = "bar")) {
41 is($x, "bar", "given scope starts");
43 is($x, "foo", "given scope ends");
47 given("inside") { check_outside1() }
48 sub check_outside1 { is($_, "outside", "\$_ lexically scoped") }
52 given("inside") { check_outside2() }
54 is($_, "outside", "\$_ lexically scoped (lexical \$_)")
58 # Basic string/numeric comparisons and control flow
63 when(2) { $ok = 'two'; }
64 when(3) { $ok = 'three'; }
65 when(4) { $ok = 'four'; }
66 default { $ok = 'd'; }
68 is($ok, 'three', "numeric comparison");
75 when(2) { $ok = 'two'; }
76 when(3) { $ok = 'three'; }
77 when(4) { $ok = 'four'; }
78 default { $ok = 'd'; }
80 is($ok, 'three', "integer comparison");
86 when(3.1) { $ok1 = 'n'; }
87 when(3.0) { $ok1 = 'y'; continue }
88 when("3.0") { $ok2 = 'y'; }
89 default { $ok2 = 'n'; }
91 is($ok1, 'y', "more numeric (pt. 1)");
92 is($ok2, 'y', "more numeric (pt. 2)");
98 when("b") { $ok = 'B'; }
99 when("c") { $ok = 'C'; }
100 when("d") { $ok = 'D'; }
101 default { $ok = 'def'; }
103 is($ok, 'C', "string comparison");
109 when("b") { $ok = 'B'; }
110 when("c") { $ok = 'C'; continue }
111 when("c") { $ok = 'CC'; }
112 default { $ok = 'D'; }
114 is($ok, 'CC', "simple continue");
120 given (0) { when(undef) {$ok = 0} }
121 is($ok, 1, "Given(0) when(undef)");
126 given (0) { when($undef) {$ok = 0} }
127 is($ok, 1, 'Given(0) when($undef)');
132 given (0) { when($undef++) {$ok = 1} }
133 is($ok, 1, "Given(0) when($undef++)");
137 given (undef) { when(0) {$ok = 0} }
138 is($ok, 1, "Given(undef) when(0)");
143 given ($undef) { when(0) {$ok = 0} }
144 is($ok, 1, 'Given($undef) when(0)');
149 given ("") { when(undef) {$ok = 0} }
150 is($ok, 1, 'Given("") when(undef)');
155 given ("") { when($undef) {$ok = 0} }
156 is($ok, 1, 'Given("") when($undef)');
160 given (undef) { when("") {$ok = 0} }
161 is($ok, 1, 'Given(undef) when("")');
166 given ($undef) { when("") {$ok = 0} }
167 is($ok, 1, 'Given($undef) when("")');
172 given (undef) { when(undef) {$ok = 1} }
173 is($ok, 1, "Given(undef) when(undef)");
178 given (undef) { when($undef) {$ok = 1} }
179 is($ok, 1, 'Given(undef) when($undef)');
184 given ($undef) { when(undef) {$ok = 1} }
185 is($ok, 1, 'Given($undef) when(undef)');
190 given ($undef) { when($undef) {$ok = 1} }
191 is($ok, 1, 'Given($undef) when($undef)');
195 # Regular expressions
198 given("Hello, world!") {
200 { $ok1 = 'y'; continue}
202 { $ok1 = 'n'; continue}
203 when(/^(Hello,|Goodbye cruel) world[!.?]/)
204 { $ok2 = 'Y'; continue}
205 when(/^(Hello cruel|Goodbye,) world[!.?]/)
206 { $ok2 = 'n'; continue}
208 is($ok1, 'y', "regex 1");
209 is($ok2, 'Y', "regex 2");
214 my $test = "explicit numeric comparison (<)";
215 my $twenty_five = 25;
217 given($twenty_five) {
218 when ($_ < 10) { $ok = "ten" }
219 when ($_ < 20) { $ok = "twenty" }
220 when ($_ < 30) { $ok = "thirty" }
221 when ($_ < 40) { $ok = "forty" }
222 default { $ok = "default" }
224 is($ok, "thirty", $test);
229 my $test = "explicit numeric comparison (integer <)";
230 my $twenty_five = 25;
232 given($twenty_five) {
233 when ($_ < 10) { $ok = "ten" }
234 when ($_ < 20) { $ok = "twenty" }
235 when ($_ < 30) { $ok = "thirty" }
236 when ($_ < 40) { $ok = "forty" }
237 default { $ok = "default" }
239 is($ok, "thirty", $test);
243 my $test = "explicit numeric comparison (<=)";
244 my $twenty_five = 25;
246 given($twenty_five) {
247 when ($_ <= 10) { $ok = "ten" }
248 when ($_ <= 20) { $ok = "twenty" }
249 when ($_ <= 30) { $ok = "thirty" }
250 when ($_ <= 40) { $ok = "forty" }
251 default { $ok = "default" }
253 is($ok, "thirty", $test);
258 my $test = "explicit numeric comparison (integer <=)";
259 my $twenty_five = 25;
261 given($twenty_five) {
262 when ($_ <= 10) { $ok = "ten" }
263 when ($_ <= 20) { $ok = "twenty" }
264 when ($_ <= 30) { $ok = "thirty" }
265 when ($_ <= 40) { $ok = "forty" }
266 default { $ok = "default" }
268 is($ok, "thirty", $test);
273 my $test = "explicit numeric comparison (>)";
274 my $twenty_five = 25;
276 given($twenty_five) {
277 when ($_ > 40) { $ok = "forty" }
278 when ($_ > 30) { $ok = "thirty" }
279 when ($_ > 20) { $ok = "twenty" }
280 when ($_ > 10) { $ok = "ten" }
281 default { $ok = "default" }
283 is($ok, "twenty", $test);
287 my $test = "explicit numeric comparison (>=)";
288 my $twenty_five = 25;
290 given($twenty_five) {
291 when ($_ >= 40) { $ok = "forty" }
292 when ($_ >= 30) { $ok = "thirty" }
293 when ($_ >= 20) { $ok = "twenty" }
294 when ($_ >= 10) { $ok = "ten" }
295 default { $ok = "default" }
297 is($ok, "twenty", $test);
302 my $test = "explicit numeric comparison (integer >)";
303 my $twenty_five = 25;
305 given($twenty_five) {
306 when ($_ > 40) { $ok = "forty" }
307 when ($_ > 30) { $ok = "thirty" }
308 when ($_ > 20) { $ok = "twenty" }
309 when ($_ > 10) { $ok = "ten" }
310 default { $ok = "default" }
312 is($ok, "twenty", $test);
317 my $test = "explicit numeric comparison (integer >=)";
318 my $twenty_five = 25;
320 given($twenty_five) {
321 when ($_ >= 40) { $ok = "forty" }
322 when ($_ >= 30) { $ok = "thirty" }
323 when ($_ >= 20) { $ok = "twenty" }
324 when ($_ >= 10) { $ok = "ten" }
325 default { $ok = "default" }
327 is($ok, "twenty", $test);
332 my $test = "explicit string comparison (lt)";
333 my $twenty_five = "25";
335 given($twenty_five) {
336 when ($_ lt "10") { $ok = "ten" }
337 when ($_ lt "20") { $ok = "twenty" }
338 when ($_ lt "30") { $ok = "thirty" }
339 when ($_ lt "40") { $ok = "forty" }
340 default { $ok = "default" }
342 is($ok, "thirty", $test);
346 my $test = "explicit string comparison (le)";
347 my $twenty_five = "25";
349 given($twenty_five) {
350 when ($_ le "10") { $ok = "ten" }
351 when ($_ le "20") { $ok = "twenty" }
352 when ($_ le "30") { $ok = "thirty" }
353 when ($_ le "40") { $ok = "forty" }
354 default { $ok = "default" }
356 is($ok, "thirty", $test);
360 my $test = "explicit string comparison (gt)";
361 my $twenty_five = 25;
363 given($twenty_five) {
364 when ($_ ge "40") { $ok = "forty" }
365 when ($_ ge "30") { $ok = "thirty" }
366 when ($_ ge "20") { $ok = "twenty" }
367 when ($_ ge "10") { $ok = "ten" }
368 default { $ok = "default" }
370 is($ok, "twenty", $test);
374 my $test = "explicit string comparison (ge)";
375 my $twenty_five = 25;
377 given($twenty_five) {
378 when ($_ ge "40") { $ok = "forty" }
379 when ($_ ge "30") { $ok = "thirty" }
380 when ($_ ge "20") { $ok = "twenty" }
381 when ($_ ge "10") { $ok = "ten" }
382 default { $ok = "default" }
384 is($ok, "twenty", $test);
387 # Make sure it still works with a lexical $_:
390 my $test = "explicit comparison with lexical \$_";
391 my $twenty_five = 25;
393 given($twenty_five) {
394 when ($_ ge "40") { $ok = "forty" }
395 when ($_ ge "30") { $ok = "thirty" }
396 when ($_ ge "20") { $ok = "twenty" }
397 when ($_ ge "10") { $ok = "ten" }
398 default { $ok = "default" }
400 is($ok, "twenty", $test);
403 # Optimized-away comparisons
407 when (2 + 2 == 4) { $ok = 'y'; continue }
408 when (2 + 2 == 5) { $ok = 'n' }
410 is($ok, 'y', "Optimized-away comparison");
414 # (How to be both thorough and portable? Pinch a few ideas
415 # from t/op/filetest.t. We err on the side of portability for
419 my ($ok_d, $ok_f, $ok_r);
421 when(-d) {$ok_d = 1; continue}
422 when(!-f) {$ok_f = 1; continue}
423 when(-r) {$ok_r = 1; continue}
425 ok($ok_d, "Filetest -d");
426 ok($ok_f, "Filetest -f");
427 ok($ok_r, "Filetest -r");
430 # Sub and method calls
435 when(bar()) {$ok = 1}
437 ok($ok, "Sub call acts as boolean")
443 when(main->bar()) {$ok = 1}
445 ok($ok, "Class-method call acts as boolean")
452 when($obj->bar()) {$ok = 1}
454 ok($ok, "Object-method call acts as boolean")
457 # Other things that should not be smart matched
465 ok($ok, "eof() not smartmatched");
470 my %foo = ("bar", 0);
472 when(exists $foo{bar}) {
476 ok($ok, "exists() not smartmatched");
486 ok($ok, "defined() not smartmatched");
492 when((1 == 1) && "bar") {
495 when((1 == 1) && $_ eq "foo") {
499 is($ok, 2, "((1 == 1) && \"bar\") not smartmatched");
505 when((1 == $ok) || "foo") {
509 ok($ok, '((1 == $ok) || "foo") not smartmatched');
513 # Make sure we aren't invoking the get-magic more than once
515 { # A helper class to count the number of accesses.
516 package FetchCounter;
519 bless {value => undef, count => 0}, $class;
522 my ($self, $val) = @_;
524 $self->{value} = $val;
528 # Avoid pre/post increment here
529 $self->{count} = 1 + $self->{count};
538 my $f = tie my $v, "FetchCounter";
540 { my $test_name = "Only one FETCH (in given)";
550 is($ok, 1, "precheck: $test_name");
551 is($f->count(), 1, $test_name);
554 { my $test_name = "Only one FETCH (numeric when)";
557 is($f->count(), 0, "Sanity check: $test_name");
566 is($ok, 1, "precheck: $test_name");
567 is($f->count(), 1, $test_name);
570 { my $test_name = "Only one FETCH (string when)";
573 is($f->count(), 0, "Sanity check: $test_name");
582 is($ok, 1, "precheck: $test_name");
583 is($f->count(), 1, $test_name);
586 { my $test_name = "Only one FETCH (undef)";
589 is($f->count(), 0, "Sanity check: $test_name");
595 when(undef) {$ok = 0}
597 is($ok, 1, "precheck: $test_name");
598 is($f->count(), 1, $test_name);
606 is($first, 0, "Loop: second");
608 like($@, qr/^Can't "break" in a loop topicalizer/,
609 q{Can't "break" in a loop topicalizer});
612 is($first, 1, "Loop: first");
614 # Implicit break is okay
623 is($first, 0, "Explicit \$_: second");
625 like($@, qr/^Can't "break" in a loop topicalizer/,
626 q{Can't "break" in a loop topicalizer});
629 is($first, 1, "Explicit \$_: first");
631 # Implicit break is okay
641 is($first, 0, "Implicitly lexical loop: second");
643 like($@, qr/^Can't "break" in a loop topicalizer/,
644 q{Can't "break" in a loop topicalizer});
647 is($first, 1, "Implicitly lexical loop: first");
649 # Implicit break is okay
659 is($first, 0, "Implicitly lexical, explicit \$_: second");
661 like($@, qr/^Can't "break" in a loop topicalizer/,
662 q{Can't "break" in a loop topicalizer});
665 is($first, 1, "Implicitly lexical, explicit \$_: first");
667 # Implicit break is okay
674 for my $_ (1, "two") {
676 is($first, 0, "Lexical loop: second");
678 like($@, qr/^Can't "break" in a loop topicalizer/,
679 q{Can't "break" in a loop topicalizer});
682 is($first, 1, "Lecical loop: first");
684 # Implicit break is okay
692 no warnings "redefine";
694 sub foo {$called_foo = 1}
696 sub bar {$called_bar = 1}
697 my ($matched_foo, $matched_bar) = (0, 0);
699 when(\&bar) {$matched_bar = 1}
700 when(\&foo) {$matched_foo = 1}
702 is($called_foo, 0, "Code ref comparison: foo not called");
703 is($called_bar, 0, "Code ref comparison: bar not called");
704 is($matched_bar, 0, "Code ref didn't match different one");
705 is($matched_foo, 1, "Code ref did match itself");
713 my ($ok1, $ok2) = (0,0);
716 { $ok1 = 1; continue }
718 { $ok2 = 1; continue }
720 is($ok1, 1, "Calling sub directly (true)");
721 is($ok2, 1, "Calling sub indirectly (true)");
725 { $ok1 = 2; continue }
727 { $ok2 = 2; continue }
729 is($ok1, 1, "Calling sub directly (false)");
730 is($ok2, 1, "Calling sub indirectly (false)");
734 { package OverloadTest;
736 use overload '""' => sub{"string value of obj"};
738 use overload "~~" => sub {
739 my ($self, $other, $reversed) = @_;
741 $self->{left} = $other;
742 $self->{right} = $self;
743 $self->{reversed} = 1;
745 $self->{left} = $self;
746 $self->{right} = $other;
747 $self->{reversed} = 0;
750 return $self->{retval};
754 my ($pkg, $retval) = @_;
763 my $test = "Overloaded obj in given (true)";
764 my $obj = OverloadTest->new(1);
767 when ("other arg") {$matched = 1}
768 default {$matched = 0}
771 is($obj->{called}, 1, "$test: called");
772 ok($matched, "$test: matched");
773 is($obj->{left}, "string value of obj", "$test: left");
774 is($obj->{right}, "other arg", "$test: right");
775 ok(!$obj->{reversed}, "$test: not reversed");
779 my $test = "Overloaded obj in given (false)";
780 my $obj = OverloadTest->new(0);
783 when ("other arg") {$matched = 1}
786 is($obj->{called}, 1, "$test: called");
787 ok(!$matched, "$test: not matched");
788 is($obj->{left}, "string value of obj", "$test: left");
789 is($obj->{right}, "other arg", "$test: right");
790 ok(!$obj->{reversed}, "$test: not reversed");
794 my $test = "Overloaded obj in when (true)";
795 my $obj = OverloadTest->new(1);
798 when ($obj) {$matched = 1}
799 default {$matched = 0}
802 is($obj->{called}, 1, "$test: called");
803 ok($matched, "$test: matched");
804 is($obj->{left}, "topic", "$test: left");
805 is($obj->{right}, "string value of obj", "$test: right");
806 ok($obj->{reversed}, "$test: reversed");
810 my $test = "Overloaded obj in when (false)";
811 my $obj = OverloadTest->new(0);
814 when ($obj) {$matched = 1}
815 default {$matched = 0}
818 is($obj->{called}, 1, "$test: called");
819 ok(!$matched, "$test: not matched");
820 is($obj->{left}, "topic", "$test: left");
821 is($obj->{right}, "string value of obj", "$test: right");
822 ok($obj->{reversed}, "$test: reversed");
825 # Okay, that'll do for now. The intricacies of the smartmatch
826 # semantics are tested in t/op/smartmatch.t