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