This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
assume cleared hv can't be re-blessed
[perl5.git] / ext / Hash-Util-FieldHash / t / 12_hashwarn.t
CommitLineData
1e73acc8 1#!./perl
1e73acc8
AS
2use Test::More;
3
4plan( tests => 12 );
5
6use strict;
7use warnings;
8use Hash::Util::FieldHash qw( :all);
9
10use vars qw{ @warnings };
11
12BEGIN {
13 $SIG{'__WARN__'} = sub { push @warnings, @_ };
14 $| = 1;
15}
16
17my $fail_odd = 'Odd number of elements in hash assignment at ';
18my $fail_odd_anon = 'Odd number of elements in anonymous hash at ';
19my $fail_ref = 'Reference found where even-sized list expected at ';
20my $fail_not_hr = 'Not a HASH reference at ';
21
22{
23 @warnings = ();
24 fieldhash my %hash;
25 %hash = (1..3);
26 cmp_ok(scalar(@warnings),'==',1,'odd count');
27 cmp_ok(substr($warnings[0],0,length($fail_odd)),'eq',$fail_odd,'odd msg');
28
29 @warnings = ();
30 %hash = 1;
31 cmp_ok(scalar(@warnings),'==',1,'scalar count');
32 cmp_ok(substr($warnings[0],0,length($fail_odd)),'eq',$fail_odd,'scalar msg');
33
34 @warnings = ();
35 %hash = { 1..3 };
36 cmp_ok(scalar(@warnings),'==',2,'odd hashref count');
37 cmp_ok(substr($warnings[0],0,length($fail_odd_anon)),'eq',$fail_odd_anon,'odd hashref msg 1');
38 cmp_ok(substr($warnings[1],0,length($fail_ref)),'eq',$fail_ref,'odd hashref msg 2');
39
40 @warnings = ();
41 %hash = [ 1..3 ];
42 cmp_ok(scalar(@warnings),'==',1,'arrayref count');
43 cmp_ok(substr($warnings[0],0,length($fail_ref)),'eq',$fail_ref,'arrayref msg');
44
45 @warnings = ();
46 %hash = sub { print "fenice" };
47 cmp_ok(scalar(@warnings),'==',1,'coderef count');
48 cmp_ok(substr($warnings[0],0,length($fail_odd)),'eq',$fail_odd,'coderef msg');
49
50 @warnings = ();
51 $_ = { 1..10 };
52 cmp_ok(scalar(@warnings),'==',0,'hashref assign');
53
54}