14 # The behaviour of the feature pragma should be tested by lib/switch.t
15 # using the tests in t/lib/switch/*. This file tests the behaviour of
16 # the switch ops themselves.
21 like($@, qr/^Can't "continue" outside/, "continue outside");
24 like($@, qr/^Can't "break" outside/, "break outside");
30 given(my $x = "bar") {
31 is($x, "bar", "given scope starts");
33 is($x, "foo", "given scope ends");
38 given(my $x = "foo") {
39 when(be_true(my $x = "bar")) {
40 is($x, "bar", "given scope starts");
42 is($x, "foo", "given scope ends");
46 given("inside") { check_outside1() }
47 sub check_outside1 { is($_, "outside", "\$_ lexically scoped") }
51 given("inside") { check_outside2() }
53 is($_, "outside", "\$_ lexically scoped (lexical \$_)")
57 # Basic string/numeric comparisons and control flow
62 when(2) { $ok = 'two'; }
63 when(3) { $ok = 'three'; }
64 when(4) { $ok = 'four'; }
65 default { $ok = 'd'; }
67 is($ok, 'three', "numeric comparison");
74 when(2) { $ok = 'two'; }
75 when(3) { $ok = 'three'; }
76 when(4) { $ok = 'four'; }
77 default { $ok = 'd'; }
79 is($ok, 'three', "integer comparison");
85 when(3.1) { $ok1 = 'n'; }
86 when(3.0) { $ok1 = 'y'; continue }
87 when("3.0") { $ok2 = 'y'; }
88 default { $ok2 = 'n'; }
90 is($ok1, 'y', "more numeric (pt. 1)");
91 is($ok2, 'y', "more numeric (pt. 2)");
97 when("b") { $ok = 'B'; }
98 when("c") { $ok = 'C'; }
99 when("d") { $ok = 'D'; }
100 default { $ok = 'def'; }
102 is($ok, 'C', "string comparison");
108 when("b") { $ok = 'B'; }
109 when("c") { $ok = 'C'; continue }
110 when("c") { $ok = 'CC'; }
111 default { $ok = 'D'; }
113 is($ok, 'CC', "simple continue");
119 given (0) { when(undef) {$ok = 0} }
120 is($ok, 1, "Given(0) when(undef)");
125 given (0) { when($undef) {$ok = 0} }
126 is($ok, 1, 'Given(0) when($undef)');
131 given (0) { when($undef++) {$ok = 1} }
132 is($ok, 1, "Given(0) when($undef++)");
135 no warnings "uninitialized";
137 given (undef) { when(0) {$ok = 0} }
138 is($ok, 1, "Given(undef) when(0)");
141 no warnings "uninitialized";
144 given ($undef) { when(0) {$ok = 0} }
145 is($ok, 1, 'Given($undef) when(0)');
150 given ("") { when(undef) {$ok = 0} }
151 is($ok, 1, 'Given("") when(undef)');
156 given ("") { when($undef) {$ok = 0} }
157 is($ok, 1, 'Given("") when($undef)');
160 no warnings "uninitialized";
162 given (undef) { when("") {$ok = 0} }
163 is($ok, 1, 'Given(undef) when("")');
166 no warnings "uninitialized";
169 given ($undef) { when("") {$ok = 0} }
170 is($ok, 1, 'Given($undef) when("")');
175 given (undef) { when(undef) {$ok = 1} }
176 is($ok, 1, "Given(undef) when(undef)");
181 given (undef) { when($undef) {$ok = 1} }
182 is($ok, 1, 'Given(undef) when($undef)');
187 given ($undef) { when(undef) {$ok = 1} }
188 is($ok, 1, 'Given($undef) when(undef)');
193 given ($undef) { when($undef) {$ok = 1} }
194 is($ok, 1, 'Given($undef) when($undef)');
198 # Regular expressions
201 given("Hello, world!") {
203 { $ok1 = 'y'; continue}
205 { $ok1 = 'n'; continue}
206 when(/^(Hello,|Goodbye cruel) world[!.?]/)
207 { $ok2 = 'Y'; continue}
208 when(/^(Hello cruel|Goodbye,) world[!.?]/)
209 { $ok2 = 'n'; continue}
211 is($ok1, 'y', "regex 1");
212 is($ok2, 'Y', "regex 2");
217 my $test = "explicit numeric comparison (<)";
218 my $twenty_five = 25;
220 given($twenty_five) {
221 when ($_ < 10) { $ok = "ten" }
222 when ($_ < 20) { $ok = "twenty" }
223 when ($_ < 30) { $ok = "thirty" }
224 when ($_ < 40) { $ok = "forty" }
225 default { $ok = "default" }
227 is($ok, "thirty", $test);
232 my $test = "explicit numeric comparison (integer <)";
233 my $twenty_five = 25;
235 given($twenty_five) {
236 when ($_ < 10) { $ok = "ten" }
237 when ($_ < 20) { $ok = "twenty" }
238 when ($_ < 30) { $ok = "thirty" }
239 when ($_ < 40) { $ok = "forty" }
240 default { $ok = "default" }
242 is($ok, "thirty", $test);
246 my $test = "explicit numeric comparison (<=)";
247 my $twenty_five = 25;
249 given($twenty_five) {
250 when ($_ <= 10) { $ok = "ten" }
251 when ($_ <= 20) { $ok = "twenty" }
252 when ($_ <= 30) { $ok = "thirty" }
253 when ($_ <= 40) { $ok = "forty" }
254 default { $ok = "default" }
256 is($ok, "thirty", $test);
261 my $test = "explicit numeric comparison (integer <=)";
262 my $twenty_five = 25;
264 given($twenty_five) {
265 when ($_ <= 10) { $ok = "ten" }
266 when ($_ <= 20) { $ok = "twenty" }
267 when ($_ <= 30) { $ok = "thirty" }
268 when ($_ <= 40) { $ok = "forty" }
269 default { $ok = "default" }
271 is($ok, "thirty", $test);
276 my $test = "explicit numeric comparison (>)";
277 my $twenty_five = 25;
279 given($twenty_five) {
280 when ($_ > 40) { $ok = "forty" }
281 when ($_ > 30) { $ok = "thirty" }
282 when ($_ > 20) { $ok = "twenty" }
283 when ($_ > 10) { $ok = "ten" }
284 default { $ok = "default" }
286 is($ok, "twenty", $test);
290 my $test = "explicit numeric comparison (>=)";
291 my $twenty_five = 25;
293 given($twenty_five) {
294 when ($_ >= 40) { $ok = "forty" }
295 when ($_ >= 30) { $ok = "thirty" }
296 when ($_ >= 20) { $ok = "twenty" }
297 when ($_ >= 10) { $ok = "ten" }
298 default { $ok = "default" }
300 is($ok, "twenty", $test);
305 my $test = "explicit numeric comparison (integer >)";
306 my $twenty_five = 25;
308 given($twenty_five) {
309 when ($_ > 40) { $ok = "forty" }
310 when ($_ > 30) { $ok = "thirty" }
311 when ($_ > 20) { $ok = "twenty" }
312 when ($_ > 10) { $ok = "ten" }
313 default { $ok = "default" }
315 is($ok, "twenty", $test);
320 my $test = "explicit numeric comparison (integer >=)";
321 my $twenty_five = 25;
323 given($twenty_five) {
324 when ($_ >= 40) { $ok = "forty" }
325 when ($_ >= 30) { $ok = "thirty" }
326 when ($_ >= 20) { $ok = "twenty" }
327 when ($_ >= 10) { $ok = "ten" }
328 default { $ok = "default" }
330 is($ok, "twenty", $test);
335 my $test = "explicit string comparison (lt)";
336 my $twenty_five = "25";
338 given($twenty_five) {
339 when ($_ lt "10") { $ok = "ten" }
340 when ($_ lt "20") { $ok = "twenty" }
341 when ($_ lt "30") { $ok = "thirty" }
342 when ($_ lt "40") { $ok = "forty" }
343 default { $ok = "default" }
345 is($ok, "thirty", $test);
349 my $test = "explicit string comparison (le)";
350 my $twenty_five = "25";
352 given($twenty_five) {
353 when ($_ le "10") { $ok = "ten" }
354 when ($_ le "20") { $ok = "twenty" }
355 when ($_ le "30") { $ok = "thirty" }
356 when ($_ le "40") { $ok = "forty" }
357 default { $ok = "default" }
359 is($ok, "thirty", $test);
363 my $test = "explicit string comparison (gt)";
364 my $twenty_five = 25;
366 given($twenty_five) {
367 when ($_ ge "40") { $ok = "forty" }
368 when ($_ ge "30") { $ok = "thirty" }
369 when ($_ ge "20") { $ok = "twenty" }
370 when ($_ ge "10") { $ok = "ten" }
371 default { $ok = "default" }
373 is($ok, "twenty", $test);
377 my $test = "explicit string comparison (ge)";
378 my $twenty_five = 25;
380 given($twenty_five) {
381 when ($_ ge "40") { $ok = "forty" }
382 when ($_ ge "30") { $ok = "thirty" }
383 when ($_ ge "20") { $ok = "twenty" }
384 when ($_ ge "10") { $ok = "ten" }
385 default { $ok = "default" }
387 is($ok, "twenty", $test);
390 # Make sure it still works with a lexical $_:
393 my $test = "explicit comparison with lexical \$_";
394 my $twenty_five = 25;
396 given($twenty_five) {
397 when ($_ ge "40") { $ok = "forty" }
398 when ($_ ge "30") { $ok = "thirty" }
399 when ($_ ge "20") { $ok = "twenty" }
400 when ($_ ge "10") { $ok = "ten" }
401 default { $ok = "default" }
403 is($ok, "twenty", $test);
406 # Optimized-away comparisons
410 when (2 + 2 == 4) { $ok = 'y'; continue }
411 when (2 + 2 == 5) { $ok = 'n' }
413 is($ok, 'y', "Optimized-away comparison");
419 when (scalar 24) { $ok = 'n'; continue }
420 default { $ok = 'y' }
422 is($ok,'y','scalar()');
426 # (How to be both thorough and portable? Pinch a few ideas
427 # from t/op/filetest.t. We err on the side of portability for
431 my ($ok_d, $ok_f, $ok_r);
433 when(-d) {$ok_d = 1; continue}
434 when(!-f) {$ok_f = 1; continue}
435 when(-r) {$ok_r = 1; continue}
437 ok($ok_d, "Filetest -d");
438 ok($ok_f, "Filetest -f");
439 ok($ok_r, "Filetest -r");
442 # Sub and method calls
447 when(notfoo()) {$ok = 1}
449 ok($ok, "Sub call acts as boolean")
455 when(main->notfoo()) {$ok = 1}
457 ok($ok, "Class-method call acts as boolean")
464 when($obj->notfoo()) {$ok = 1}
466 ok($ok, "Object-method call acts as boolean")
469 # Other things that should not be smart matched
473 when( /(\d+)/ and ( 1 <= $1 and $1 <= 12 ) ) {
477 ok($ok, "bool not smartmatches");
487 ok($ok, "eof() not smartmatched");
492 my %foo = ("bar", 0);
494 when(exists $foo{bar}) {
498 ok($ok, "exists() not smartmatched");
508 ok($ok, "defined() not smartmatched");
514 when((1 == 1) && "bar") {
517 when((1 == 1) && $_ eq "foo") {
521 is($ok, 2, "((1 == 1) && \"bar\") not smartmatched");
526 for my $l (qw(a b c d)) {
528 when ($_ eq "b" .. $_ eq "c") { $n = 1 }
531 ok(($n xor $l =~ /[ad]/), 'when(E1..E2) evaluates in boolean context');
537 for my $l (qw(a b c d)) {
539 when ($_ eq "b" ... $_ eq "c") { $n = 1 }
542 ok(($n xor $l =~ /[ad]/), 'when(E1...E2) evaluates in boolean context');
549 when((1 == $ok) || "foo") {
553 ok($ok, '((1 == $ok) || "foo") smartmatched');
559 when((1 == $ok || undef) // "foo") {
563 ok($ok, '((1 == $ok || undef) // "foo") smartmatched');
566 # Make sure we aren't invoking the get-magic more than once
568 { # A helper class to count the number of accesses.
569 package FetchCounter;
572 bless {value => undef, count => 0}, $class;
575 my ($self, $val) = @_;
577 $self->{value} = $val;
581 # Avoid pre/post increment here
582 $self->{count} = 1 + $self->{count};
591 my $f = tie my $v, "FetchCounter";
593 { my $test_name = "Only one FETCH (in given)";
603 is($ok, 1, "precheck: $test_name");
604 is($f->count(), 1, $test_name);
607 { my $test_name = "Only one FETCH (numeric when)";
610 is($f->count(), 0, "Sanity check: $test_name");
619 is($ok, 1, "precheck: $test_name");
620 is($f->count(), 1, $test_name);
623 { my $test_name = "Only one FETCH (string when)";
626 is($f->count(), 0, "Sanity check: $test_name");
635 is($ok, 1, "precheck: $test_name");
636 is($f->count(), 1, $test_name);
639 { my $test_name = "Only one FETCH (undef)";
642 is($f->count(), 0, "Sanity check: $test_name");
643 no warnings "uninitialized";
649 when(undef) {$ok = 0}
651 is($ok, 1, "precheck: $test_name");
652 is($f->count(), 1, $test_name);
660 is($first, 0, "Loop: second");
662 like($@, qr/^Can't "break" in a loop topicalizer/,
663 q{Can't "break" in a loop topicalizer});
666 is($first, 1, "Loop: first");
668 # Implicit break is okay
677 is($first, 0, "Explicit \$_: second");
679 like($@, qr/^Can't "break" in a loop topicalizer/,
680 q{Can't "break" in a loop topicalizer});
683 is($first, 1, "Explicit \$_: first");
685 # Implicit break is okay
695 is($first, 0, "Implicitly lexical loop: second");
697 like($@, qr/^Can't "break" in a loop topicalizer/,
698 q{Can't "break" in a loop topicalizer});
701 is($first, 1, "Implicitly lexical loop: first");
703 # Implicit break is okay
713 is($first, 0, "Implicitly lexical, explicit \$_: second");
715 like($@, qr/^Can't "break" in a loop topicalizer/,
716 q{Can't "break" in a loop topicalizer});
719 is($first, 1, "Implicitly lexical, explicit \$_: first");
721 # Implicit break is okay
728 for my $_ (1, "two") {
730 is($first, 0, "Lexical loop: second");
732 like($@, qr/^Can't "break" in a loop topicalizer/,
733 q{Can't "break" in a loop topicalizer});
736 is($first, 1, "Lexical loop: first");
738 # Implicit break is okay
747 sub foo {$called_foo = 1; "@_" eq "foo"}
749 sub bar {$called_bar = 1; "@_" eq "bar"}
750 my ($matched_foo, $matched_bar) = (0, 0);
752 when(\&bar) {$matched_bar = 1}
753 when(\&foo) {$matched_foo = 1}
755 is($called_foo, 1, "foo() was called");
756 is($called_bar, 1, "bar() was called");
757 is($matched_bar, 0, "bar didn't match");
758 is($matched_foo, 1, "foo did match");
766 my ($ok1, $ok2) = (0,0);
769 { $ok1 = 1; continue }
771 { $ok2 = 1; continue }
773 is($ok1, 1, "Calling sub directly (true)");
774 is($ok2, 1, "Calling sub indirectly (true)");
778 { $ok1 = 2; continue }
780 { $ok2 = 2; continue }
782 is($ok1, 1, "Calling sub directly (false)");
783 is($ok2, 1, "Calling sub indirectly (false)");
787 skip_if_miniperl("no dynamic loading on miniperl, no Scalar::Util", 14);
789 { package OverloadTest;
791 use overload '""' => sub{"string value of obj"};
792 use overload 'eq' => sub{"$_[0]" eq "$_[1]"};
794 use overload "~~" => sub {
795 my ($self, $other, $reversed) = @_;
797 $self->{left} = $other;
798 $self->{right} = $self;
799 $self->{reversed} = 1;
801 $self->{left} = $self;
802 $self->{right} = $other;
803 $self->{reversed} = 0;
806 return $self->{retval};
810 my ($pkg, $retval) = @_;
819 my $test = "Overloaded obj in given (true)";
820 my $obj = OverloadTest->new(1);
823 when ("other arg") {$matched = 1}
824 default {$matched = 0}
827 is($obj->{called}, 1, "$test: called");
828 ok($matched, "$test: matched");
832 my $test = "Overloaded obj in given (false)";
833 my $obj = OverloadTest->new(0);
836 when ("other arg") {$matched = 1}
839 is($obj->{called}, 1, "$test: called");
840 ok(!$matched, "$test: not matched");
844 my $test = "Overloaded obj in when (true)";
845 my $obj = OverloadTest->new(1);
848 when ($obj) {$matched = 1}
849 default {$matched = 0}
852 is($obj->{called}, 1, "$test: called");
853 ok($matched, "$test: matched");
854 is($obj->{left}, "topic", "$test: left");
855 is($obj->{right}, "string value of obj", "$test: right");
856 ok($obj->{reversed}, "$test: reversed");
860 my $test = "Overloaded obj in when (false)";
861 my $obj = OverloadTest->new(0);
864 when ($obj) {$matched = 1}
865 default {$matched = 0}
868 is($obj->{called}, 1, "$test: called");
869 ok(!$matched, "$test: not matched");
870 is($obj->{left}, "topic", "$test: left");
871 is($obj->{right}, "string value of obj", "$test: right");
872 ok($obj->{reversed}, "$test: reversed");
882 is($ok, 1, "postfix undef");
888 $ok += 2 when 9.1685;
889 $ok += 4 when $_ > 4;
890 $ok += 8 when $_ < 2.5;
892 is($ok, 8, "postfix numeric");
897 $ok = 1, continue when $_ eq "apple";
899 $ok = 0 when "banana";
901 is($ok, 3, "postfix string");
906 do { $ok = 1; continue } when /pea/;
912 is($ok, 7, "postfix regex");
914 # be_true is defined at the beginning of the file
917 given(my $x = "foo") {
919 is($x, "foo", "scope inside ... when my \$x = ...");
921 } when be_true(my $x = "bar");
922 is($x, "bar", "scope after ... when my \$x = ...");
928 my $x = 2, continue when be_true();
929 is($x, undef, "scope after my \$x = ... when ...");
933 # Tests for last and next in when clauses
944 is($letter, "b", "last in when");
947 LETTER1: for ("a".."e") {
950 when ("b") { last LETTER1 }
954 is($letter, "b", "last LABEL in when");
959 when (/b|d/) { next }
964 is($letter, "a,c,e,", "next in when");
967 LETTER2: for ("a".."e") {
969 when (/b|d/) { next LETTER2 }
974 is($letter, "a,c,e,", "next LABEL in when");
976 # Test goto with given/when
981 GIVEN1: given ($flag) {
985 is($flag, 0, "goto GIVEN1");
990 when (0) { $flag = 1; }
995 is($flag, 1, "goto inside given");
1000 when (0) { $flag = 1; goto GIVEN3; $flag = 2; }
1004 is($flag, 1, "goto inside given and when");
1009 when (0) { $flag = 1; goto GIVEN4; $flag = 2; }
1013 is($flag, 1, "goto inside for and when");
1019 when (0) { $flag = 1; goto GIVEN5; $flag = 2; }
1023 is($flag, 1, "goto inside given and when to the given stmt");
1026 # test with unreified @_ in smart match [perl #71078]
1027 sub unreified_check { ok([@_] ~~ \@_) } # should always match
1028 unreified_check(1,2,"lala");
1029 unreified_check(1,2,undef);
1030 unreified_check(undef);
1031 unreified_check(undef,"");
1033 # Test do { given } as a rvalue
1038 my @things = (11 .. 26); # 16 elements
1039 my @exp = (5, 16, 9);
1042 my $scalar = do { given ($_) {
1043 when (0) { $lexical }
1044 when (2) { 'void'; 8, 9 }
1047 is($scalar, shift(@exp), "rvalue given - simple scalar [$_]");
1053 my @exp = (5, 7, 9);
1056 my $scalar = do { given ($_) {
1061 is($scalar, shift(@exp), "rvalue given - postfix scalar [$_]");
1066 my @exp = (5, 9, 9);
1068 my $scalar = do { given ($_) {
1074 is($scalar, shift(@exp), "rvalue given - default scalar [$_]");
1079 my @things = (11 .. 13);
1080 my @exp = ('3 4 5', '11 12 13', '8 9');
1082 my @list = do { given ($_) {
1084 when (2) { my $fake = 'void'; 8, 9 }
1087 is("@list", shift(@exp), "rvalue given - simple list [$_]");
1093 my @exp = ('3 4 5', '6 7', '12');
1095 my @list = do { given ($_) {
1100 is("@list", shift(@exp), "rvalue given - postfix list [$_]");
1105 my @things = (11 .. 20); # 10 elements
1106 my @exp = ('m o o', '8 10', '8 10');
1108 my @list = do { given ($_) {
1109 when (0) { "moo" =~ /(.)/g }
1110 default { 8, scalar(@things) }
1113 is("@list", shift(@exp), "rvalue given - default list [$_]");
1118 my @exp = ('6 7', '', '6 7');
1120 my @list = do { given ($_) {
1121 continue when $_ <= 1;
1126 is("@list", shift(@exp), "rvalue given - default list [$_]");
1130 # Context propagation
1131 my $smart_hash = sub {
1132 do { given ($_[0]) {
1134 when ([ 1 .. 3 ]) { 1 .. 3 }
1135 when (4) { my $fake; do { 4, 5 } }
1141 $scalar = $smart_hash->();
1142 is($scalar, 'undef', "rvalue given - scalar context propagation [undef]");
1144 $scalar = $smart_hash->(4);
1145 is($scalar, 5, "rvalue given - scalar context propagation [4]");
1147 $scalar = $smart_hash->(999);
1148 is($scalar, undef, "rvalue given - scalar context propagation [999]");
1152 @list = $smart_hash->();
1153 is("@list", 'undef', "rvalue given - list context propagation [undef]");
1155 @list = $smart_hash->(2);
1156 is("@list", '1 2 3', "rvalue given - list context propagation [2]");
1158 @list = $smart_hash->(4);
1159 is("@list", '4 5', "rvalue given - list context propagation [4]");
1161 @list = $smart_hash->(999);
1162 is("@list", '', "rvalue given - list context propagation [999]");
1166 my @list = 10 .. 15;
1175 when (@list[0..2]) {
1180 is("@in_list", "10 15", "when(array)");
1181 is("@in_slice", "10", "when(array slice)");
1185 my %list = map { $_ => $_ } "a" .. "f";
1188 for ("a", "e", "i") {
1194 when (@list{"a".."c"}) {
1199 is("@in_list", "a e", "when(hash)");
1200 is("@in_slice", "a", "when(hash slice)");
1203 { # RT#84526 - Handle magical TARG
1204 my $x = my $y = "aaa";
1207 is(pos, undef, "handle magical TARG");
1213 # Okay, that'll do for now. The intricacies of the smartmatch
1214 # semantics are tested in t/op/smartmatch.t