]> git.vpit.fr Git - perl/modules/indirect.git/blob - t/21-bad.t
Also pass the file and the line number to the hook
[perl/modules/indirect.git] / t / 21-bad.t
1 #!perl -T
2
3 package Dongs;
4
5 sub new;
6
7 package main;
8
9 use strict;
10 use warnings;
11
12 use Test::More tests => 50 * 6 + 2;
13
14 my ($obj, $x);
15 our ($y, $bloop);
16
17 sub expect {
18  my ($pkg) = @_;
19  qr/^warn:Indirect\s+call\s+of\s+method\s+
20      "(?:new|meh|$pkg$pkg)"
21      \s+on\s+object\s+
22      "(?:$pkg|newnew|\$(?:[xyz_\$]|(?:sploosh::)?sploosh|(?:main::)?bloop))"
23      \s+at\s+\(eval\s+\d+\)\s+line\s+\d+
24    /x
25 }
26
27 {
28  local $/ = "####";
29  while (<DATA>) {
30   chomp;
31   s/\s*$//;
32   s/(.*?)$//m;
33   my ($skip, $prefix) = split /#+/, $1;
34   $skip   = 0  unless defined $skip;
35   $prefix = '' unless defined $prefix;
36   s/\s*//;
37
38 SKIP:
39   {
40    skip "$_: $skip" => 6 if eval $skip;
41
42    local $SIG{__WARN__} = sub { die 'warn:' . join(' ', @_) };
43
44    eval "die qq{ok\\n}; $prefix; use indirect; $_";
45    is($@, "ok\n", "use indirect: $_");
46
47    eval "die qq{the code compiled but it shouldn't have\n}; $prefix; no indirect; $_";
48    like($@, expect('Hlagh'), "no indirect: $_");
49
50    s/Hlagh/Dongs/g;
51
52    eval "die qq{ok\\n}; $prefix; use indirect; $_";
53    is($@, "ok\n", "use indirect, defined: $_");
54
55    eval "die qq{the code compiled but it shouldn't have\n}; $prefix; no indirect; $_";
56    like($@, expect('Dongs'), "no indirect, defined: $_");
57
58    s/\$/\$ \n\t /g;
59    s/Dongs/Hlagh/g;
60
61    eval "die qq{ok\\n}; $prefix; use indirect; $_";
62    is($@, "ok\n", "use indirect, spaces: $_");
63
64    eval "die qq{the code compiled but it shouldn't have\n}; $prefix; no indirect; $_";
65    like($@, expect('Hlagh'), "no indirect, spaces: $_");
66   }
67  }
68 }
69
70 eval {
71  my $warn;
72  local $SIG{__WARN__} = sub { $warn = join ' ', @_ };
73  eval "die qq{ok\n}; no indirect 'hlagh'; \$obj = new Hlagh1;";
74  is($@, "ok\n", 'no indirect "hlagh" didn\'t croak');
75  like($warn, qr/^Indirect\s+call\s+of\s+method\s+"new"\s+on\s+object\s+"Hlagh1"/, 'no indirect "hlagh" enables the pragma');
76 }
77
78 __DATA__
79
80 $obj = new Hlagh;
81 ####
82 $obj = new Hlagh();
83 ####
84 $obj = new Hlagh(1);
85 ####
86 $obj = new Hlagh(1, 2);
87 ####
88 $obj = new        Hlagh            ;
89 ####
90 $obj = new        Hlagh     (      )      ;
91 ####
92 $obj = new        Hlagh     (      1        )     ;
93 ####
94 $obj = new        Hlagh     (      1        ,       2        )     ;
95 ####
96 $obj = new    
97                       Hlagh             
98         ;
99 ####
100 $obj = new   
101                                        Hlagh     (    
102                   )      ;
103 ####
104 $obj =
105               new    
106     Hlagh     (      1   
107             )     ;
108 ####
109 $obj =
110 new      
111 Hlagh    
112                    (      1        ,  
113                 2        )     ;
114 ####
115 $obj = new $x;
116 ####
117 $obj = new $x();
118 ####
119 $obj = new $x('foo');
120 ####
121 $obj = new $x qq{foo}, 1;
122 ####
123 $obj = new $x qr{foo\s+bar}, 1 .. 1;
124 ####
125 $obj = new $x(qw/bar baz/);
126 ####
127 $obj = new
128           $_;
129 ####
130 $obj = new
131              $_     (        );
132 ####
133 $obj = new $_      qr/foo/  ;
134 ####
135 $obj = new $_     qq(bar baz);
136 ####
137 meh $_;
138 ####
139 meh $_ 1, 2;
140 ####
141 meh $$;
142 ####
143 meh $$ 1, 2;
144 ####
145 meh $x;
146 ####
147 meh $x 1, 2;
148 ####
149 meh $x, 1, 2;
150 ####
151 meh $y;
152 ####
153 meh $y 1, 2;
154 ####
155 meh $y, 1, 2;
156 #### $] < 5.010 # use feature 'state'; state $z
157 meh $z;
158 #### $] < 5.010 # use feature 'state'; state $z
159 meh $z 1, 2;
160 #### $] < 5.010 # use feature 'state'; state $z
161 meh $z, 1, 2;
162 ####
163 package sploosh;
164 our $sploosh;
165 meh $sploosh::sploosh;
166 ####
167 package sploosh;
168 our $sploosh;
169 meh $sploosh;
170 ####
171 package sploosh;
172 meh $main::bloop;
173 ####
174 package sploosh;
175 meh $bloop;
176 ####
177 package ma;
178 meh $bloop;
179 ####
180 package sploosh;
181 our $sploosh;
182 package main;
183 meh $sploosh::sploosh;
184 ####
185 new Hlagh->wut;
186 ####
187 new Hlagh->wut();
188 ####
189 new Hlagh->wut, "Wut";
190 ####
191 $obj = HlaghHlagh Hlagh;
192 ####
193 $obj = HlaghHlagh Hlagh; # HlaghHlagh Hlagh
194 ####
195 $obj = new newnew;
196 ####
197 $obj = new newnew; # new newnew
198 ####
199 new Hlagh (meh $x)
200 ####
201 Hlagh->new(meh $x)