]> git.vpit.fr Git - perl/modules/indirect.git/blob - t/20-bad.t
Support variables with spaces after the sigil
[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 * 6 + 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   s/\$/\$ \n\t /g;
49   s/Dongs/Hlagh/g;
50   {
51    use indirect;
52    eval "die qq{ok\\n}; $_";
53   }
54   is($@, "ok\n", "use indirect, spaces: $_");
55   {
56    no indirect;
57    eval "die qq{the code compiled but it shouldn't have\n}; $_";
58   }
59   like($@, expect('Hlagh'), "no indirect, spaces: $_");
60  }
61 }
62
63 eval {
64  no indirect 'hlagh';
65  my $warn;
66  local $SIG{__WARN__} = sub { $warn = join ' ', @_ };
67  eval "die qq{the code compiled but it shouldn't have\n}; \$obj = new Hlagh1;";
68  like($warn, qr/^Indirect\s+call\s+of\s+method\s+"new"\s+on\s+object\s+"Hlagh1"/, 'no indirect "hlagh" enables the pragma');
69  eval "die qq{the code compiled but it shouldn't have\n}; \$obj = new Hlagh2;";
70  like($warn, qr/^Indirect\s+call\s+of\s+method\s+"new"\s+on\s+object\s+"Hlagh2"/, 'no indirect "hlagh" doesn\'t croak');
71 }
72
73 __DATA__
74 $obj = new Hlagh;
75 ####
76 $obj = new Hlagh();
77 ####
78 $obj = new Hlagh(1);
79 ####
80 $obj = new Hlagh(1, 2);
81 ####
82 $obj = new        Hlagh            ;
83 ####
84 $obj = new        Hlagh     (      )      ;
85 ####
86 $obj = new        Hlagh     (      1        )     ;
87 ####
88 $obj = new        Hlagh     (      1        ,       2        )     ;
89 ####
90 $obj = new    
91                       Hlagh             
92         ;
93 ####
94 $obj = new   
95                                        Hlagh     (    
96                   )      ;
97 ####
98 $obj =
99               new    
100     Hlagh     (      1   
101             )     ;
102 ####
103 $obj =
104 new      
105 Hlagh    
106                    (      1        ,  
107                 2        )     ;
108 ####
109 $obj = new $x;
110 ####
111 $obj = new $x();
112 ####
113 $obj = new $x('foo');
114 ####
115 $obj = new $x qq{foo}, 1;
116 ####
117 $obj = new $x qr{foo\s+bar}, 1 .. 1;
118 ####
119 $obj = new $x(qw/bar baz/);
120 ####
121 $obj = new
122           $_;
123 ####
124 $obj = new
125              $_     (        );
126 ####
127 $obj = new $_      qr/foo/  ;
128 ####
129 $obj = new $_     qq(bar baz);
130 ####
131 meh $_;
132 ####
133 meh $_ 1, 2;
134 ####
135 meh $x;
136 ####
137 meh $x 1, 2;
138 ####
139 meh $x, 1, 2;
140 ####
141 meh $y;
142 ####
143 meh $y 1, 2;
144 ####
145 meh $y, 1, 2;
146 ####
147 package sploosh;
148 our $sploosh;
149 meh $sploosh::sploosh;
150 ####
151 package sploosh;
152 our $sploosh;
153 meh $sploosh;
154 ####
155 package sploosh;
156 meh $main::bloop;
157 ####
158 package sploosh;
159 meh $bloop;
160 ####
161 package sploosh;
162 our $sploosh;
163 package main;
164 meh $sploosh::sploosh;
165 ####
166 new Hlagh->wut;
167 ####
168 new Hlagh->wut();
169 ####
170 new Hlagh->wut, "Wut";
171 ####
172 $obj = HlaghHlagh Hlagh;
173 ####
174 $obj = HlaghHlagh Hlagh; # HlaghHlagh Hlagh
175 ####
176 $obj = new newnew;
177 ####
178 $obj = new newnew; # new newnew
179 ####
180 new Hlagh (meh $x)
181 ####
182 Hlagh->new(meh $x)