11 use Test::More tests => 122;
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++)");
136 no warnings "uninitialized";
138 given (undef) { when(0) {$ok = 1} }
139 is($ok, 1, "Given(undef) when(0)");
142 no warnings "uninitialized";
145 given ($undef) { when(0) {$ok = 1} }
146 is($ok, 1, 'Given($undef) when(0)');
151 given ("") { when(undef) {$ok = 0} }
152 is($ok, 1, 'Given("") when(undef)');
157 given ("") { when($undef) {$ok = 0} }
158 is($ok, 1, 'Given("") when($undef)');
161 no warnings "uninitialized";
163 given (undef) { when("") {$ok = 1} }
164 is($ok, 1, 'Given(undef) when("")');
167 no warnings "uninitialized";
170 given ($undef) { when("") {$ok = 1} }
171 is($ok, 1, 'Given($undef) when("")');
176 given (undef) { when(undef) {$ok = 1} }
177 is($ok, 1, "Given(undef) when(undef)");
182 given (undef) { when($undef) {$ok = 1} }
183 is($ok, 1, 'Given(undef) when($undef)');
188 given ($undef) { when(undef) {$ok = 1} }
189 is($ok, 1, 'Given($undef) when(undef)');
194 given ($undef) { when($undef) {$ok = 1} }
195 is($ok, 1, 'Given($undef) when($undef)');
199 # Regular expressions
202 given("Hello, world!") {
204 { $ok1 = 'y'; continue}
206 { $ok1 = 'n'; continue}
207 when(/^(Hello,|Goodbye cruel) world[!.?]/)
208 { $ok2 = 'Y'; continue}
209 when(/^(Hello cruel|Goodbye,) world[!.?]/)
210 { $ok2 = 'n'; continue}
212 is($ok1, 'y', "regex 1");
213 is($ok2, 'Y', "regex 2");
218 my $test = "explicit numeric comparison (<)";
219 my $twenty_five = 25;
221 given($twenty_five) {
222 when ($_ < 10) { $ok = "ten" }
223 when ($_ < 20) { $ok = "twenty" }
224 when ($_ < 30) { $ok = "thirty" }
225 when ($_ < 40) { $ok = "forty" }
226 default { $ok = "default" }
228 is($ok, "thirty", $test);
233 my $test = "explicit numeric comparison (integer <)";
234 my $twenty_five = 25;
236 given($twenty_five) {
237 when ($_ < 10) { $ok = "ten" }
238 when ($_ < 20) { $ok = "twenty" }
239 when ($_ < 30) { $ok = "thirty" }
240 when ($_ < 40) { $ok = "forty" }
241 default { $ok = "default" }
243 is($ok, "thirty", $test);
247 my $test = "explicit numeric comparison (<=)";
248 my $twenty_five = 25;
250 given($twenty_five) {
251 when ($_ <= 10) { $ok = "ten" }
252 when ($_ <= 20) { $ok = "twenty" }
253 when ($_ <= 30) { $ok = "thirty" }
254 when ($_ <= 40) { $ok = "forty" }
255 default { $ok = "default" }
257 is($ok, "thirty", $test);
262 my $test = "explicit numeric comparison (integer <=)";
263 my $twenty_five = 25;
265 given($twenty_five) {
266 when ($_ <= 10) { $ok = "ten" }
267 when ($_ <= 20) { $ok = "twenty" }
268 when ($_ <= 30) { $ok = "thirty" }
269 when ($_ <= 40) { $ok = "forty" }
270 default { $ok = "default" }
272 is($ok, "thirty", $test);
277 my $test = "explicit numeric comparison (>)";
278 my $twenty_five = 25;
280 given($twenty_five) {
281 when ($_ > 40) { $ok = "forty" }
282 when ($_ > 30) { $ok = "thirty" }
283 when ($_ > 20) { $ok = "twenty" }
284 when ($_ > 10) { $ok = "ten" }
285 default { $ok = "default" }
287 is($ok, "twenty", $test);
291 my $test = "explicit numeric comparison (>=)";
292 my $twenty_five = 25;
294 given($twenty_five) {
295 when ($_ >= 40) { $ok = "forty" }
296 when ($_ >= 30) { $ok = "thirty" }
297 when ($_ >= 20) { $ok = "twenty" }
298 when ($_ >= 10) { $ok = "ten" }
299 default { $ok = "default" }
301 is($ok, "twenty", $test);
306 my $test = "explicit numeric comparison (integer >)";
307 my $twenty_five = 25;
309 given($twenty_five) {
310 when ($_ > 40) { $ok = "forty" }
311 when ($_ > 30) { $ok = "thirty" }
312 when ($_ > 20) { $ok = "twenty" }
313 when ($_ > 10) { $ok = "ten" }
314 default { $ok = "default" }
316 is($ok, "twenty", $test);
321 my $test = "explicit numeric comparison (integer >=)";
322 my $twenty_five = 25;
324 given($twenty_five) {
325 when ($_ >= 40) { $ok = "forty" }
326 when ($_ >= 30) { $ok = "thirty" }
327 when ($_ >= 20) { $ok = "twenty" }
328 when ($_ >= 10) { $ok = "ten" }
329 default { $ok = "default" }
331 is($ok, "twenty", $test);
336 my $test = "explicit string comparison (lt)";
337 my $twenty_five = "25";
339 given($twenty_five) {
340 when ($_ lt "10") { $ok = "ten" }
341 when ($_ lt "20") { $ok = "twenty" }
342 when ($_ lt "30") { $ok = "thirty" }
343 when ($_ lt "40") { $ok = "forty" }
344 default { $ok = "default" }
346 is($ok, "thirty", $test);
350 my $test = "explicit string comparison (le)";
351 my $twenty_five = "25";
353 given($twenty_five) {
354 when ($_ le "10") { $ok = "ten" }
355 when ($_ le "20") { $ok = "twenty" }
356 when ($_ le "30") { $ok = "thirty" }
357 when ($_ le "40") { $ok = "forty" }
358 default { $ok = "default" }
360 is($ok, "thirty", $test);
364 my $test = "explicit string comparison (gt)";
365 my $twenty_five = 25;
367 given($twenty_five) {
368 when ($_ ge "40") { $ok = "forty" }
369 when ($_ ge "30") { $ok = "thirty" }
370 when ($_ ge "20") { $ok = "twenty" }
371 when ($_ ge "10") { $ok = "ten" }
372 default { $ok = "default" }
374 is($ok, "twenty", $test);
378 my $test = "explicit string comparison (ge)";
379 my $twenty_five = 25;
381 given($twenty_five) {
382 when ($_ ge "40") { $ok = "forty" }
383 when ($_ ge "30") { $ok = "thirty" }
384 when ($_ ge "20") { $ok = "twenty" }
385 when ($_ ge "10") { $ok = "ten" }
386 default { $ok = "default" }
388 is($ok, "twenty", $test);
391 # Make sure it still works with a lexical $_:
394 my $test = "explicit comparison with lexical \$_";
395 my $twenty_five = 25;
397 given($twenty_five) {
398 when ($_ ge "40") { $ok = "forty" }
399 when ($_ ge "30") { $ok = "thirty" }
400 when ($_ ge "20") { $ok = "twenty" }
401 when ($_ ge "10") { $ok = "ten" }
402 default { $ok = "default" }
404 is($ok, "twenty", $test);
407 # Optimized-away comparisons
411 when (2 + 2 == 4) { $ok = 'y'; continue }
412 when (2 + 2 == 5) { $ok = 'n' }
414 is($ok, 'y', "Optimized-away comparison");
418 # (How to be both thorough and portable? Pinch a few ideas
419 # from t/op/filetest.t. We err on the side of portability for
423 my ($ok_d, $ok_f, $ok_r);
425 when(-d) {$ok_d = 1; continue}
426 when(!-f) {$ok_f = 1; continue}
427 when(-r) {$ok_r = 1; continue}
429 ok($ok_d, "Filetest -d");
430 ok($ok_f, "Filetest -f");
431 ok($ok_r, "Filetest -r");
434 # Sub and method calls
439 when(notfoo()) {$ok = 1}
441 ok($ok, "Sub call acts as boolean")
447 when(main->notfoo()) {$ok = 1}
449 ok($ok, "Class-method call acts as boolean")
456 when($obj->notfoo()) {$ok = 1}
458 ok($ok, "Object-method call acts as boolean")
461 # Other things that should not be smart matched
465 when( /(\d+)/ and ( 1 <= $1 and $1 <= 12 ) ) {
469 ok($ok, "bool not smartmatches");
479 ok($ok, "eof() not smartmatched");
484 my %foo = ("bar", 0);
486 when(exists $foo{bar}) {
490 ok($ok, "exists() not smartmatched");
500 ok($ok, "defined() not smartmatched");
506 when((1 == 1) && "bar") {
509 when((1 == 1) && $_ eq "foo") {
513 is($ok, 2, "((1 == 1) && \"bar\") not smartmatched");
518 for my $l qw(a b c d) {
520 when ($_ eq "b" .. $_ eq "c") { $n = 1 }
523 ok(($n xor $l =~ /[ad]/), 'when(E1..E2) evaluates in boolean context');
529 for my $l qw(a b c d) {
531 when ($_ eq "b" ... $_ eq "c") { $n = 1 }
534 ok(($n xor $l =~ /[ad]/), 'when(E1...E2) evaluates in boolean context');
541 when((1 == $ok) || "foo") {
545 ok($ok, '((1 == $ok) || "foo") smartmatched');
551 when((1 == $ok || undef) // "foo") {
555 ok($ok, '((1 == $ok || undef) // "foo") smartmatched');
558 # Make sure we aren't invoking the get-magic more than once
560 { # A helper class to count the number of accesses.
561 package FetchCounter;
564 bless {value => undef, count => 0}, $class;
567 my ($self, $val) = @_;
569 $self->{value} = $val;
573 # Avoid pre/post increment here
574 $self->{count} = 1 + $self->{count};
583 my $f = tie my $v, "FetchCounter";
585 { my $test_name = "Only one FETCH (in given)";
595 is($ok, 1, "precheck: $test_name");
596 is($f->count(), 1, $test_name);
599 { my $test_name = "Only one FETCH (numeric when)";
602 is($f->count(), 0, "Sanity check: $test_name");
611 is($ok, 1, "precheck: $test_name");
612 is($f->count(), 1, $test_name);
615 { my $test_name = "Only one FETCH (string when)";
618 is($f->count(), 0, "Sanity check: $test_name");
627 is($ok, 1, "precheck: $test_name");
628 is($f->count(), 1, $test_name);
631 { my $test_name = "Only one FETCH (undef)";
634 is($f->count(), 0, "Sanity check: $test_name");
635 no warnings "uninitialized";
641 when(undef) {$ok = 0}
643 is($ok, 1, "precheck: $test_name");
644 is($f->count(), 1, $test_name);
652 is($first, 0, "Loop: second");
654 like($@, qr/^Can't "break" in a loop topicalizer/,
655 q{Can't "break" in a loop topicalizer});
658 is($first, 1, "Loop: first");
660 # Implicit break is okay
669 is($first, 0, "Explicit \$_: second");
671 like($@, qr/^Can't "break" in a loop topicalizer/,
672 q{Can't "break" in a loop topicalizer});
675 is($first, 1, "Explicit \$_: first");
677 # Implicit break is okay
687 is($first, 0, "Implicitly lexical loop: second");
689 like($@, qr/^Can't "break" in a loop topicalizer/,
690 q{Can't "break" in a loop topicalizer});
693 is($first, 1, "Implicitly lexical loop: first");
695 # Implicit break is okay
705 is($first, 0, "Implicitly lexical, explicit \$_: second");
707 like($@, qr/^Can't "break" in a loop topicalizer/,
708 q{Can't "break" in a loop topicalizer});
711 is($first, 1, "Implicitly lexical, explicit \$_: first");
713 # Implicit break is okay
720 for my $_ (1, "two") {
722 is($first, 0, "Lexical loop: second");
724 like($@, qr/^Can't "break" in a loop topicalizer/,
725 q{Can't "break" in a loop topicalizer});
728 is($first, 1, "Lexical loop: first");
730 # Implicit break is okay
738 no warnings "redefine";
740 sub foo {$called_foo = 1; "@_" eq "foo"}
742 sub bar {$called_bar = 1; "@_" eq "bar"}
743 my ($matched_foo, $matched_bar) = (0, 0);
745 when(\&bar) {$matched_bar = 1}
746 when(\&foo) {$matched_foo = 1}
748 is($called_foo, 1, "foo() was called");
749 is($called_bar, 1, "bar() was called");
750 is($matched_bar, 0, "bar didn't match");
751 is($matched_foo, 1, "foo did match");
759 my ($ok1, $ok2) = (0,0);
762 { $ok1 = 1; continue }
764 { $ok2 = 1; continue }
766 is($ok1, 1, "Calling sub directly (true)");
767 is($ok2, 1, "Calling sub indirectly (true)");
771 { $ok1 = 2; continue }
773 { $ok2 = 2; continue }
775 is($ok1, 1, "Calling sub directly (false)");
776 is($ok2, 1, "Calling sub indirectly (false)");
780 skip "Scalar/Util.pm not yet available", 20
781 unless -r "$INC[0]/Scalar/Util.pm";
783 { package OverloadTest;
785 use overload '""' => sub{"string value of obj"};
786 use overload 'eq' => sub{"$_[0]" eq "$_[1]"};
788 use overload "~~" => sub {
789 my ($self, $other, $reversed) = @_;
791 $self->{left} = $other;
792 $self->{right} = $self;
793 $self->{reversed} = 1;
795 $self->{left} = $self;
796 $self->{right} = $other;
797 $self->{reversed} = 0;
800 return $self->{retval};
804 my ($pkg, $retval) = @_;
813 my $test = "Overloaded obj in given (true)";
814 my $obj = OverloadTest->new(1);
817 when ("other arg") {$matched = 1}
818 default {$matched = 0}
821 is($obj->{called}, 0, "$test: called");
822 ok(!$matched, "$test: not matched");
826 my $test = "Overloaded obj in given (false)";
827 my $obj = OverloadTest->new(0);
830 when ("other arg") {$matched = 1}
833 is($obj->{called}, 0, "$test: called");
834 ok(!$matched, "$test: not matched");
838 my $test = "Overloaded obj in when (true)";
839 my $obj = OverloadTest->new(1);
842 when ($obj) {$matched = 1}
843 default {$matched = 0}
846 is($obj->{called}, 1, "$test: called");
847 ok($matched, "$test: matched");
848 is($obj->{left}, "topic", "$test: left");
849 is($obj->{right}, "string value of obj", "$test: right");
850 ok($obj->{reversed}, "$test: reversed");
854 my $test = "Overloaded obj in when (false)";
855 my $obj = OverloadTest->new(0);
858 when ($obj) {$matched = 1}
859 default {$matched = 0}
862 is($obj->{called}, 1, "$test: called");
863 ok(!$matched, "$test: not matched");
864 is($obj->{left}, "topic", "$test: left");
865 is($obj->{right}, "string value of obj", "$test: right");
866 ok($obj->{reversed}, "$test: reversed");
876 is($ok, 1, "postfix undef");
882 $ok += 2 when 9.1685;
883 $ok += 4 when $_ > 4;
884 $ok += 8 when $_ < 2.5;
886 is($ok, 8, "postfix numeric");
891 $ok = 1, continue when $_ eq "apple";
893 $ok = 0 when "banana";
895 is($ok, 3, "postfix string");
900 do { $ok = 1; continue } when /pea/;
906 is($ok, 7, "postfix regex");
908 # be_true is defined at the beginning of the file
911 given(my $x = "foo") {
913 is($x, "foo", "scope inside ... when my \$x = ...");
915 } when be_true(my $x = "bar");
916 is($x, "bar", "scope after ... when my \$x = ...");
922 my $x = 2, continue when be_true();
923 is($x, undef, "scope after my \$x = ... when ...");
927 # Tests for last and next in when clauses
938 is($letter, "b", "last in when");
941 LETTER1: for ("a".."e") {
944 when ("b") { last LETTER1 }
948 is($letter, "b", "last LABEL in when");
953 when (/b|d/) { next }
958 is($letter, "a,c,e,", "next in when");
961 LETTER2: for ("a".."e") {
963 when (/b|d/) { next LETTER2 }
968 is($letter, "a,c,e,", "next LABEL in when");
970 # Okay, that'll do for now. The intricacies of the smartmatch
971 # semantics are tested in t/op/smartmatch.t