This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
t/op/hashwarn.t using test.pl
[perl5.git] / t / op / hashwarn.t
CommitLineData
1930e939
TP
1#!./perl
2
1930e939
TP
3BEGIN {
4 chdir 't' if -d 't';
6647cf4e 5 @INC = qw(. ../lib);
1930e939
TP
6}
7
6647cf4e
DL
8require 'test.pl';
9plan( tests => 16 );
10
90dc8f2b 11use strict;
9f1b1f2d 12use warnings;
90dc8f2b 13
1930e939
TP
14use vars qw{ @warnings };
15
16BEGIN {
1930e939
TP
17 $SIG{'__WARN__'} = sub { push @warnings, @_ };
18 $| = 1;
1930e939
TP
19}
20
6647cf4e
DL
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 ';
1930e939
TP
25
26{
6647cf4e 27 @warnings = ();
1930e939 28 my %hash = (1..3);
6647cf4e
DL
29 cmp_ok(scalar(@warnings),'==',1,'odd count');
30 cmp_ok(substr($warnings[0],0,length($fail_odd)),'eq',$fail_odd,'odd msg');
1930e939 31
6647cf4e 32 @warnings = ();
1930e939 33 %hash = 1;
6647cf4e
DL
34 cmp_ok(scalar(@warnings),'==',1,'scalar count');
35 cmp_ok(substr($warnings[0],0,length($fail_odd)),'eq',$fail_odd,'scalar msg');
1930e939 36
6647cf4e 37 @warnings = ();
1930e939 38 %hash = { 1..3 };
6647cf4e
DL
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');
1930e939 42
6647cf4e 43 @warnings = ();
1930e939 44 %hash = [ 1..3 ];
6647cf4e
DL
45 cmp_ok(scalar(@warnings),'==',1,'arrayref count');
46 cmp_ok(substr($warnings[0],0,length($fail_ref)),'eq',$fail_ref,'arrayref msg');
1930e939 47
6647cf4e
DL
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');
1930e939 56
6d822dc4 57 # Old pseudo-hash syntax, now removed.
6647cf4e
DL
58
59 @warnings = ();
10c8fecd 60 my $avhv = [{x=>1,y=>2}];
6d822dc4
MS
61 eval {
62 %$avhv = (x=>13,'y');
63 };
6647cf4e
DL
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');
6d822dc4 66
6647cf4e 67 @warnings = ();
6d822dc4
MS
68 eval {
69 %$avhv = 'x';
70 };
6647cf4e
DL
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');
1930e939 73}