xref: /openbsd/gnu/usr.bin/perl/t/op/hashwarn.t (revision 5af055cd)
1#!./perl
2
3BEGIN {
4    chdir 't' if -d 't';
5    @INC = qw(. ../lib);
6}
7
8require 'test.pl';
9plan( tests => 16 );
10
11use strict;
12use warnings;
13
14use vars qw{ @warnings };
15
16BEGIN {
17    $SIG{'__WARN__'} = sub { push @warnings, @_ };
18    $| = 1;
19}
20
21my $fail_odd      = 'Odd number of elements in hash assignment at ';
22my $fail_odd_anon = 'Odd number of elements in anonymous hash at ';
23my $fail_ref      = 'Reference found where even-sized list expected at ';
24my $fail_not_hr   = 'Not a HASH reference at ';
25
26{
27    @warnings = ();
28    my %hash = (1..3);
29    cmp_ok(scalar(@warnings),'==',1,'odd count');
30    cmp_ok(substr($warnings[0],0,length($fail_odd)),'eq',$fail_odd,'odd msg');
31
32    @warnings = ();
33    %hash = 1;
34    cmp_ok(scalar(@warnings),'==',1,'scalar count');
35    cmp_ok(substr($warnings[0],0,length($fail_odd)),'eq',$fail_odd,'scalar msg');
36
37    @warnings = ();
38    %hash = { 1..3 };
39    cmp_ok(scalar(@warnings),'==',2,'odd hashref count');
40    cmp_ok(substr($warnings[0],0,length($fail_odd_anon)),'eq',$fail_odd_anon,'odd hashref msg 1');
41    cmp_ok(substr($warnings[1],0,length($fail_ref)),'eq',$fail_ref,'odd hashref msg 2');
42
43    @warnings = ();
44    %hash = [ 1..3 ];
45    cmp_ok(scalar(@warnings),'==',1,'arrayref count');
46    cmp_ok(substr($warnings[0],0,length($fail_ref)),'eq',$fail_ref,'arrayref msg');
47
48    @warnings = ();
49    %hash = sub { print "fenice" };
50    cmp_ok(scalar(@warnings),'==',1,'coderef count');
51    cmp_ok(substr($warnings[0],0,length($fail_odd)),'eq',$fail_odd,'coderef msg');
52
53    @warnings = ();
54    $_ = { 1..10 };
55    cmp_ok(scalar(@warnings),'==',0,'hashref assign');
56
57    # Old pseudo-hash syntax, now removed.
58
59    @warnings = ();
60    my $avhv = [{x=>1,y=>2}];
61    eval {
62        %$avhv = (x=>13,'y');
63    };
64    cmp_ok(scalar(@warnings),'==',0,'pseudo-hash 1 count');
65    cmp_ok(substr($@,0,length($fail_not_hr)),'eq',$fail_not_hr,'pseudo-hash 1 msg');
66
67    @warnings = ();
68    eval {
69        %$avhv = 'x';
70    };
71    cmp_ok(scalar(@warnings),'==',0,'pseudo-hash 2 count');
72    cmp_ok(substr($@,0,length($fail_not_hr)),'eq',$fail_not_hr,'pseudo-hash 2 msg');
73}
74