This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[perl #87708] use integer; $tied < $tied
[perl5.git] / t / op / hashwarn.t
1 #!./perl
2
3 BEGIN {
4     chdir 't' if -d 't';
5     @INC = qw(. ../lib);
6 }
7
8 require 'test.pl';
9 plan( tests => 16 );
10
11 use strict;
12 use warnings;
13
14 use vars qw{ @warnings };
15
16 BEGIN {
17     $SIG{'__WARN__'} = sub { push @warnings, @_ };
18     $| = 1;
19 }
20
21 my $fail_odd      = 'Odd number of elements in hash assignment at ';
22 my $fail_odd_anon = 'Odd number of elements in anonymous hash at ';
23 my $fail_ref      = 'Reference found where even-sized list expected at ';
24 my $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 }