summaryrefslogtreecommitdiff
path: root/t
diff options
context:
space:
mode:
authorRuslan Zakirov <ruz@bestpractical.com>2012-10-08 02:35:22 +0400
committerFather Chrysostomos <sprout@cpan.org>2012-12-11 08:59:39 -0800
commit3e125adaaf6d41df6a78e92e6682685224048aba (patch)
tree482c7d4ad85702d7f8cbb020dcd87add08526e17 /t
parent231cbeb24ba077cbde643fc4d5178055c1464f5c (diff)
downloadperl-3e125adaaf6d41df6a78e92e6682685224048aba.tar.gz
test "Odd number of elements in hash assignment"
Diffstat (limited to 't')
-rw-r--r--t/op/hashassign.t51
1 files changed, 50 insertions, 1 deletions
diff --git a/t/op/hashassign.t b/t/op/hashassign.t
index 906e7ca7df..a0b8cf9b88 100644
--- a/t/op/hashassign.t
+++ b/t/op/hashassign.t
@@ -8,7 +8,7 @@ BEGIN {
# use strict;
-plan tests => 218;
+plan tests => 238;
my @comma = ("key", "value");
@@ -320,3 +320,52 @@ SKIP: {
undef %tb;
is $p, \%tb, "hash undef should not zap weak refs";
}
+
+# test odd hash assignment warnings
+{
+ my ($s, %h);
+ warning_like(sub {%h = (1..3)}, qr/^Odd number of elements in hash assignment/);
+ warning_like(sub {%h = ({})}, qr/^Reference found where even-sized list expected/);
+
+ warning_like(sub { ($s, %h) = (1..4)}, qr/^Odd number of elements in hash assignment/);
+ warning_like(sub { ($s, %h) = (1, {})}, qr/^Reference found where even-sized list expected/);
+}
+
+# hash assignment in scalar and list context with odd number of elements
+{
+ no warnings 'misc', 'uninitialized';
+ my %h; my $x;
+ is( join( ':', %h = (1..3)), '1:2:3:',
+ 'odd hash assignment in list context' );
+ ok( eq_hash( \%h, {1 => 2, 3 => undef} ), "correct value stored" );
+ is( scalar( %h = (1..3) ), 3,
+ 'odd hash assignment in scalar context' );
+ ok( eq_hash( \%h, {1 => 2, 3 => undef} ), "correct value stored" );
+ is( join(':', ($x,%h) = (0,1,2,3) ), '0:1:2:3:',
+ 'scalar + odd hash assignment in list context' );
+ ok( eq_hash( \%h, {1 => 2, 3 => undef} ), "correct value stored" );
+ is( scalar( ($x,%h) = (0,1,2,3) ), 4,
+ 'scalar + odd hash assignment in scalar context' );
+ ok( eq_hash( \%h, {1 => 2, 3 => undef} ), "correct value stored" );
+}
+
+# hash assignment in scalar and list context with odd number of elements
+# and duplicates
+{
+ no warnings 'misc', 'uninitialized';
+ my %h; my $x;
+ is( (join ':', %h = (1,1,1)), '1:',
+ 'odd hash assignment in list context with duplicates' );
+ ok( eq_hash( \%h, {1 => undef} ), "correct value stored" );
+ is( scalar(%h = (1,1,1)), 3,
+ 'odd hash assignment in scalar context with duplicates' );
+ ok( eq_hash( \%h, {1 => undef} ), "correct value stored" );
+ is( join(':', ($x,%h) = (0,1,1,1) ), '0:1:',
+ 'scalar + odd hash assignment in list context with duplicates' );
+ ok( eq_hash( \%h, {1 => undef} ), "correct value stored" );
+ is( scalar( ($x,%h) = (0,1,1,1) ), 4,
+ 'scalar + odd hash assignment in scalar context with duplicates' );
+ ok( eq_hash( \%h, {1 => undef} ), "correct value stored" );
+}
+
+