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