BEGIN {
chdir 't' if -d 't';
@INC = '../lib';
+ require './test.pl';
}
use strict;
use warnings;
-use Test::More tests => 111;
+plan tests => 122;
# The behaviour of the feature pragma should be tested by lib/switch.t
# using the tests in t/lib/switch/*. This file tests the behaviour of
# the switch ops themselves.
-
use feature 'switch';
-no warnings "numeric";
eval { continue };
like($@, qr/^Can't "continue" outside/, "continue outside");
is($ok, 1, "Given(0) when($undef++)");
}
{
+ no warnings "uninitialized";
my $ok = 1;
given (undef) { when(0) {$ok = 0} }
is($ok, 1, "Given(undef) when(0)");
}
{
+ no warnings "uninitialized";
my $undef;
my $ok = 1;
given ($undef) { when(0) {$ok = 0} }
is($ok, 1, 'Given("") when($undef)');
}
{
+ no warnings "uninitialized";
my $ok = 1;
given (undef) { when("") {$ok = 0} }
is($ok, 1, 'Given(undef) when("")');
}
{
+ no warnings "uninitialized";
my $undef;
my $ok = 1;
given ($undef) { when("") {$ok = 0} }
}
# Sub and method calls
-sub bar {"bar"}
+sub notfoo {"bar"}
{
my $ok = 0;
given("foo") {
- when(bar()) {$ok = 1}
+ when(notfoo()) {$ok = 1}
}
ok($ok, "Sub call acts as boolean")
}
{
my $ok = 0;
given("foo") {
- when(main->bar()) {$ok = 1}
+ when(main->notfoo()) {$ok = 1}
}
ok($ok, "Class-method call acts as boolean")
}
my $ok = 0;
my $obj = bless [];
given("foo") {
- when($obj->bar()) {$ok = 1}
+ when($obj->notfoo()) {$ok = 1}
}
ok($ok, "Object-method call acts as boolean")
}
}
{
- my $ok = 0;
- given("foo") {
- when((1 == $ok) || "foo") {
- $ok = 1;
+ my $n = 0;
+ for my $l qw(a b c d) {
+ given ($l) {
+ when ($_ eq "b" .. $_ eq "c") { $n = 1 }
+ default { $n = 0 }
}
+ ok(($n xor $l =~ /[ad]/), 'when(E1..E2) evaluates in boolean context');
}
- ok($ok, '((1 == $ok) || "foo") smartmatched');
}
-TODO: {
- local $TODO = "RT #50538: when( \@n && \%n ) fails to smart match";
- { # this should smart match on each side of &&
- my @n = qw(fred barney betty);
- my @m = @n;
-
- my $ok = 0;
- given( "fred" ) {
- when( @n ) {
- $ok++; continue;
- }
- when( @m ) {
- $ok++; continue;
- }
- when( @m && @n ) {
- $ok++;
- }
+{
+ my $n = 0;
+ for my $l qw(a b c d) {
+ given ($l) {
+ when ($_ eq "b" ... $_ eq "c") { $n = 1 }
+ default { $n = 0 }
}
-
- is($ok, 3, '(@n && @m) smart-matched');
+ ok(($n xor $l =~ /[ad]/), 'when(E1...E2) evaluates in boolean context');
}
+}
- { # this should smart match on each side of &&
- my @n = qw(fred barney betty);
- my %n = map { $_, 1 } @n;
-
- my $ok = 0;
- given( "fred" ) {
- when( @n ) {
- $ok++; continue;
- }
- when( %n ) {
- $ok++; continue;
- }
- when( @n && %n ) {
- $ok++;
- }
+{
+ my $ok = 0;
+ given("foo") {
+ when((1 == $ok) || "foo") {
+ $ok = 1;
}
-
- is($ok, 3, '(@n && %n) smart-matched');
}
+ ok($ok, '((1 == $ok) || "foo") smartmatched');
+}
- { # this should smart match on each side of &&
- my %n = map { $_, 1 } qw(fred barney betty);
- my %m = %n;
-
- my $ok = 0;
- given( "fred" ) {
- when( %m ) {
- $ok++; continue;
- }
- when( %n ) {
- $ok++; continue;
- }
- when( %m && %n ) {
- $ok++;
- }
+{
+ my $ok = 0;
+ given("foo") {
+ when((1 == $ok || undef) // "foo") {
+ $ok = 1;
}
-
- is($ok, 3, '(%m && %n) smart-matched');
}
+ ok($ok, '((1 == $ok || undef) // "foo") smartmatched');
}
# Make sure we aren't invoking the get-magic more than once
my $ok;
$v = undef;
is($f->count(), 0, "Sanity check: $test_name");
+ no warnings "uninitialized";
given(my $undef) {
when(sub{0}->()) {}
when("21") {}
# Code references
{
- no warnings "redefine";
my $called_foo = 0;
- sub foo {$called_foo = 1}
+ sub foo {$called_foo = 1; "@_" eq "foo"}
my $called_bar = 0;
- sub bar {$called_bar = 1}
+ sub bar {$called_bar = 1; "@_" eq "bar"}
my ($matched_foo, $matched_bar) = (0, 0);
- given(\&foo) {
+ given("foo") {
when(\&bar) {$matched_bar = 1}
when(\&foo) {$matched_foo = 1}
}
- is($called_foo, 0, "Code ref comparison: foo not called");
- is($called_bar, 0, "Code ref comparison: bar not called");
- is($matched_bar, 0, "Code ref didn't match different one");
- is($matched_foo, 1, "Code ref did match itself");
+ is($called_foo, 1, "foo() was called");
+ is($called_bar, 1, "bar() was called");
+ is($matched_bar, 0, "bar didn't match");
+ is($matched_foo, 1, "foo did match");
}
sub contains_x {
is($ok2, 1, "Calling sub indirectly (false)");
}
-# Test overloading
-{ package OverloadTest;
+SKIP: {
+ skip "Scalar/Util.pm not yet available", 20
+ unless -r "$INC[0]/Scalar/Util.pm";
+ # Test overloading
+ { package OverloadTest;
+
+ use overload '""' => sub{"string value of obj"};
+ use overload 'eq' => sub{"$_[0]" eq "$_[1]"};
+
+ use overload "~~" => sub {
+ my ($self, $other, $reversed) = @_;
+ if ($reversed) {
+ $self->{left} = $other;
+ $self->{right} = $self;
+ $self->{reversed} = 1;
+ } else {
+ $self->{left} = $self;
+ $self->{right} = $other;
+ $self->{reversed} = 0;
+ }
+ $self->{called} = 1;
+ return $self->{retval};
+ };
+
+ sub new {
+ my ($pkg, $retval) = @_;
+ bless {
+ called => 0,
+ retval => $retval,
+ }, $pkg;
+ }
+ }
+
+ {
+ my $test = "Overloaded obj in given (true)";
+ my $obj = OverloadTest->new(1);
+ my $matched;
+ given($obj) {
+ when ("other arg") {$matched = 1}
+ default {$matched = 0}
+ }
+
+ is($obj->{called}, 1, "$test: called");
+ ok($matched, "$test: matched");
+ }
- use overload '""' => sub{"string value of obj"};
+ {
+ my $test = "Overloaded obj in given (false)";
+ my $obj = OverloadTest->new(0);
+ my $matched;
+ given($obj) {
+ when ("other arg") {$matched = 1}
+ }
+
+ is($obj->{called}, 1, "$test: called");
+ ok(!$matched, "$test: not matched");
+ }
- use overload "~~" => sub {
- my ($self, $other, $reversed) = @_;
- if ($reversed) {
- $self->{left} = $other;
- $self->{right} = $self;
- $self->{reversed} = 1;
- } else {
- $self->{left} = $self;
- $self->{right} = $other;
- $self->{reversed} = 0;
- }
- $self->{called} = 1;
- return $self->{retval};
- };
+ {
+ my $test = "Overloaded obj in when (true)";
+ my $obj = OverloadTest->new(1);
+ my $matched;
+ given("topic") {
+ when ($obj) {$matched = 1}
+ default {$matched = 0}
+ }
- sub new {
- my ($pkg, $retval) = @_;
- bless {
- called => 0,
- retval => $retval,
- }, $pkg;
+ is($obj->{called}, 1, "$test: called");
+ ok($matched, "$test: matched");
+ is($obj->{left}, "topic", "$test: left");
+ is($obj->{right}, "string value of obj", "$test: right");
+ ok($obj->{reversed}, "$test: reversed");
+ }
+
+ {
+ my $test = "Overloaded obj in when (false)";
+ my $obj = OverloadTest->new(0);
+ my $matched;
+ given("topic") {
+ when ($obj) {$matched = 1}
+ default {$matched = 0}
+ }
+
+ is($obj->{called}, 1, "$test: called");
+ ok(!$matched, "$test: not matched");
+ is($obj->{left}, "topic", "$test: left");
+ is($obj->{right}, "string value of obj", "$test: right");
+ ok($obj->{reversed}, "$test: reversed");
}
}
+# Postfix when
{
- my $test = "Overloaded obj in given (true)";
- my $obj = OverloadTest->new(1);
- my $matched;
- given($obj) {
- when ("other arg") {$matched = 1}
- default {$matched = 0}
+ my $ok;
+ given (undef) {
+ $ok = 1 when undef;
}
-
- is($obj->{called}, 1, "$test: called");
- ok($matched, "$test: matched");
- is($obj->{left}, "string value of obj", "$test: left");
- is($obj->{right}, "other arg", "$test: right");
- ok(!$obj->{reversed}, "$test: not reversed");
+ is($ok, 1, "postfix undef");
}
-
{
- my $test = "Overloaded obj in given (false)";
- my $obj = OverloadTest->new(0);
- my $matched;
- given($obj) {
- when ("other arg") {$matched = 1}
+ my $ok;
+ given (2) {
+ $ok += 1 when 7;
+ $ok += 2 when 9.1685;
+ $ok += 4 when $_ > 4;
+ $ok += 8 when $_ < 2.5;
}
-
- is($obj->{called}, 1, "$test: called");
- ok(!$matched, "$test: not matched");
- is($obj->{left}, "string value of obj", "$test: left");
- is($obj->{right}, "other arg", "$test: right");
- ok(!$obj->{reversed}, "$test: not reversed");
+ is($ok, 8, "postfix numeric");
}
-
{
- my $test = "Overloaded obj in when (true)";
- my $obj = OverloadTest->new(1);
- my $matched;
- given("topic") {
- when ($obj) {$matched = 1}
- default {$matched = 0}
+ my $ok;
+ given ("apple") {
+ $ok = 1, continue when $_ eq "apple";
+ $ok += 2;
+ $ok = 0 when "banana";
}
-
- is($obj->{called}, 1, "$test: called");
- ok($matched, "$test: matched");
- is($obj->{left}, "topic", "$test: left");
- is($obj->{right}, "string value of obj", "$test: right");
- ok($obj->{reversed}, "$test: reversed");
+ is($ok, 3, "postfix string");
}
-
{
- my $test = "Overloaded obj in when (false)";
- my $obj = OverloadTest->new(0);
- my $matched;
- given("topic") {
- when ($obj) {$matched = 1}
- default {$matched = 0}
+ my $ok;
+ given ("pear") {
+ do { $ok = 1; continue } when /pea/;
+ $ok += 2;
+ $ok = 0 when /pie/;
+ default { $ok += 4 }
+ $ok = 0;
}
-
- is($obj->{called}, 1, "$test: called");
- ok(!$matched, "$test: not matched");
- is($obj->{left}, "topic", "$test: left");
- is($obj->{right}, "string value of obj", "$test: right");
- ok($obj->{reversed}, "$test: reversed");
+ is($ok, 7, "postfix regex");
+}
+# be_true is defined at the beginning of the file
+{
+ my $x = "what";
+ given(my $x = "foo") {
+ do {
+ is($x, "foo", "scope inside ... when my \$x = ...");
+ continue;
+ } when be_true(my $x = "bar");
+ is($x, "bar", "scope after ... when my \$x = ...");
+ }
+}
+{
+ my $x = 0;
+ given(my $x = 1) {
+ my $x = 2, continue when be_true();
+ is($x, undef, "scope after my \$x = ... when ...");
+ }
+}
+
+# Tests for last and next in when clauses
+my $letter;
+
+$letter = '';
+for ("a".."e") {
+ given ($_) {
+ $letter = $_;
+ when ("b") { last }
+ }
+ $letter = "z";
+}
+is($letter, "b", "last in when");
+
+$letter = '';
+LETTER1: for ("a".."e") {
+ given ($_) {
+ $letter = $_;
+ when ("b") { last LETTER1 }
+ }
+ $letter = "z";
+}
+is($letter, "b", "last LABEL in when");
+
+$letter = '';
+for ("a".."e") {
+ given ($_) {
+ when (/b|d/) { next }
+ $letter .= $_;
+ }
+ $letter .= ',';
+}
+is($letter, "a,c,e,", "next in when");
+
+$letter = '';
+LETTER2: for ("a".."e") {
+ given ($_) {
+ when (/b|d/) { next LETTER2 }
+ $letter .= $_;
+ }
+ $letter .= ',';
}
+is($letter, "a,c,e,", "next LABEL in when");
# Okay, that'll do for now. The intricacies of the smartmatch
# semantics are tested in t/op/smartmatch.t