This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Create subdirectory t/opbasic. Move 5 test files there.
[perl5.git] / t / op / assignwarn.t
1 #!./perl -w
2
3 #
4 # Verify which OP= operators warn if their targets are undefined.
5 # Based on redef.t, contributed by Graham Barr <Graham.Barr@tiuk.ti.com>
6 #       -- Robin Barker 
7 #
8 # Now almost completely rewritten.
9
10 BEGIN {
11     chdir 't' if -d 't';
12     @INC = '../lib';
13     require './test.pl';
14 }
15
16 use strict;
17
18 my (%should_warn, %should_not);
19 ++$should_warn{$_} foreach qw(* / x & ** << >>);
20 ++$should_not{$_} foreach qw(+ - . | ^ && ||);
21
22 my %integer;
23 $integer{$_} = 0 foreach qw(* / % + -);
24
25 sub TIESCALAR { my $x; bless \$x }
26 sub FETCH { ${$_[0]} }
27 sub STORE { ${$_[0]} = $_[1] }
28
29 sub test_op {
30     my ($tie, $int, $op_seq, $warn) = @_;
31     my $code = "sub {\n";
32     $code .= "use integer;" if $int;
33     $code .= "my \$x;\n";
34     $code .= "tie \$x, 'main';\n" if $tie;
35     $code .= "$op_seq;\n}\n";
36
37     my $sub = eval $code;
38     is($@, '', "Can eval code for $op_seq");
39     if ($warn) {
40         warning_like($sub, qr/^Use of uninitialized value/,
41                      "$op_seq$tie$int warns");
42     } else {
43         warning_is($sub, undef, "$op_seq$tie$int does not warn");
44     }
45 }
46
47 # go through all tests once normally and once with tied $x
48 for my $tie ("", ", tied") {
49     foreach my $integer ('', ', int') {
50         test_op($tie, $integer, $_, 0) foreach qw($x++ $x-- ++$x --$x);
51     }
52
53     foreach (keys %should_warn, keys %should_not) {
54         test_op($tie, '', "\$x $_= 1", $should_warn{$_});
55         next unless exists $integer{$_};
56         test_op($tie, ', int', "\$x $_= 1", $should_warn{$_});
57     }
58
59     foreach (qw(| ^ &)) {
60         test_op($tie, '', "\$x $_= 'x'", $should_warn{$_});
61     }
62 }
63
64 done_testing();