From 3e125adaaf6d41df6a78e92e6682685224048aba Mon Sep 17 00:00:00 2001 From: Ruslan Zakirov Date: Mon, 8 Oct 2012 02:35:22 +0400 Subject: [PATCH] test "Odd number of elements in hash assignment" --- t/op/hashassign.t | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/t/op/hashassign.t b/t/op/hashassign.t index 906e7ca..a0b8cf9 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" ); +} + + -- 1.8.3.1