1use strict; 2use warnings; 3use Test::More; 4use Hash::Util::FieldHash qw( :all); 5 6our @warnings; 7BEGIN { 8 $SIG{'__WARN__'} = sub { push @warnings, @_ }; 9 $| = 1; 10} 11 12my $fail_odd = 'Odd number of elements in hash assignment at '; 13my $fail_odd_anon = 'Odd number of elements in anonymous hash at '; 14my $fail_ref = 'Reference found where even-sized list expected at '; 15my $fail_not_hr = 'Not a HASH reference at '; 16 17{ 18 @warnings = (); 19 fieldhash my %hash; 20 %hash = (1..3); 21 cmp_ok(scalar(@warnings),'==',1,'odd count'); 22 cmp_ok(substr($warnings[0],0,length($fail_odd)),'eq',$fail_odd,'odd msg'); 23 24 @warnings = (); 25 %hash = 1; 26 cmp_ok(scalar(@warnings),'==',1,'scalar count'); 27 cmp_ok(substr($warnings[0],0,length($fail_odd)),'eq',$fail_odd,'scalar msg'); 28 29 @warnings = (); 30 %hash = { 1..3 }; 31 cmp_ok(scalar(@warnings),'==',2,'odd hashref count'); 32 cmp_ok(substr($warnings[0],0,length($fail_odd_anon)),'eq',$fail_odd_anon,'odd hashref msg 1'); 33 cmp_ok(substr($warnings[1],0,length($fail_ref)),'eq',$fail_ref,'odd hashref msg 2'); 34 35 @warnings = (); 36 %hash = [ 1..3 ]; 37 cmp_ok(scalar(@warnings),'==',1,'arrayref count'); 38 cmp_ok(substr($warnings[0],0,length($fail_ref)),'eq',$fail_ref,'arrayref msg'); 39 40 @warnings = (); 41 %hash = sub { print "fenice" }; 42 cmp_ok(scalar(@warnings),'==',1,'coderef count'); 43 cmp_ok(substr($warnings[0],0,length($fail_odd)),'eq',$fail_odd,'coderef msg'); 44 45 @warnings = (); 46 $_ = { 1..10 }; 47 cmp_ok(scalar(@warnings),'==',0,'hashref assign'); 48 49} 50 51done_testing; 52