Commit | Line | Data |
fa8e8936 |
1 | # -*- Mode: cperl; coding: utf-8; cperl-indent-level: 2 -*- |
2 | #!/usr/local/bin/perl -w |
3 | |
88587957 |
4 | BEGIN { |
5 | chdir 't' if -d 't'; |
6 | @INC = '../lib'; |
7 | } |
8 | |
fa8e8936 |
9 | use strict; |
88587957 |
10 | #use lib qw(t/lib); |
fa8e8936 |
11 | use Test::More tests => 42; |
12 | use IO::Handle; |
13 | |
14 | BEGIN { use_ok('CGI::Carp') }; |
15 | |
16 | #----------------------------------------------------------------------------- |
17 | # Test id |
18 | #----------------------------------------------------------------------------- |
19 | |
20 | # directly invoked |
21 | my $expect_f = __FILE__; |
22 | my $expect_l = __LINE__ + 1; |
23 | my ($file, $line, $id) = CGI::Carp::id(0); |
24 | is($file, $expect_f, "file"); |
25 | is($line, $expect_l, "line"); |
26 | is($id, "carp.t", "id"); |
27 | |
28 | # one level of indirection |
29 | sub id1 { my $level = shift; return CGI::Carp::id($level); }; |
30 | |
31 | $expect_l = __LINE__ + 1; |
32 | ($file, $line, $id) = id1(1); |
33 | is($file, $expect_f, "file"); |
34 | is($line, $expect_l, "line"); |
35 | is($id, "carp.t", "id"); |
36 | |
37 | # two levels of indirection |
38 | sub id2 { my $level = shift; return id1($level); }; |
39 | |
40 | $expect_l = __LINE__ + 1; |
41 | ($file, $line, $id) = id2(2); |
42 | is($file, $expect_f, "file"); |
43 | is($line, $expect_l, "line"); |
44 | is($id, "carp.t", "id"); |
45 | |
46 | #----------------------------------------------------------------------------- |
47 | # Test stamp |
48 | #----------------------------------------------------------------------------- |
49 | |
50 | my $stamp = "/^\\[ |
51 | ([a-z]{3}\\s){2}\\s? |
52 | [\\s\\d:]+ |
53 | \\]\\s$id:/ix"; |
54 | |
55 | like(CGI::Carp::stamp(), |
56 | $stamp, |
57 | "Time in correct format"); |
58 | |
59 | sub stamp1 {return CGI::Carp::stamp()}; |
60 | sub stamp2 {return stamp1()}; |
61 | |
62 | like(stamp2(), $stamp, "Time in correct format"); |
63 | |
64 | #----------------------------------------------------------------------------- |
65 | # Test warn and _warn |
66 | #----------------------------------------------------------------------------- |
67 | |
68 | # set some variables to control what's going on. |
69 | $CGI::Carp::WARN = 0; |
70 | $CGI::Carp::EMIT_WARNINGS = 0; |
71 | @CGI::Carp::WARNINGS = (); |
72 | my $q_file = quotemeta($file); |
73 | |
74 | |
75 | # Test that realwarn is called |
76 | { |
77 | local $^W = 0; |
78 | eval "sub CGI::Carp::realwarn {return 'Called realwarn'};"; |
79 | } |
80 | |
81 | $expect_l = __LINE__ + 1; |
82 | is(CGI::Carp::warn("There is a problem"), |
83 | "Called realwarn", |
84 | "CGI::Carp::warn calls CORE::warn"); |
85 | is(@CGI::Carp::WARNINGS, 0, "_warn not called"); |
86 | |
87 | # Test that message is constructed correctly |
88 | eval 'sub CGI::Carp::realwarn {my $mess = shift; return $mess};'; |
89 | |
90 | $expect_l = __LINE__ + 1; |
91 | like(CGI::Carp::warn("There is a problem"), |
92 | "/] $id: There is a problem at $q_file line $expect_l.".'$/', |
93 | "CGI::Carp::warn builds correct message"); |
94 | is(@CGI::Carp::WARNINGS, 0, "_warn not called"); |
95 | |
96 | # Test that _warn is called at the correct time |
97 | $CGI::Carp::WARN = 1; |
98 | |
88587957 |
99 | my $save_expect_l = $expect_l = __LINE__ + 1; |
fa8e8936 |
100 | like(CGI::Carp::warn("There is a problem"), |
101 | "/] $id: There is a problem at $q_file line $expect_l.".'$/', |
102 | "CGI::Carp::warn builds correct message"); |
103 | |
104 | is(@CGI::Carp::WARNINGS, 1, "_warn now called"); |
105 | like($CGI::Carp::WARNINGS[0], |
106 | "/There is a problem at $q_file line $expect_l.".'$/', |
107 | "CGI::Carp::WARNINGS has correct message (without stamp)"); |
108 | |
109 | #----------------------------------------------------------------------------- |
110 | # Test ineval |
111 | #----------------------------------------------------------------------------- |
112 | |
113 | ok(!CGI::Carp::ineval, 'ineval returns false when not in eval'); |
114 | eval {ok(CGI::Carp::ineval, 'ineval returns true when in eval');}; |
115 | |
116 | #----------------------------------------------------------------------------- |
117 | # Test die |
118 | #----------------------------------------------------------------------------- |
119 | |
120 | # set some variables to control what's going on. |
121 | $CGI::Carp::WRAP = 0; |
122 | |
123 | $expect_l = __LINE__ + 1; |
124 | eval { CGI::Carp::die('There is a problem'); }; |
125 | like($@, |
126 | '/^There is a problem/', |
127 | 'CGI::Carp::die calls CORE::die without altering argument in eval'); |
128 | |
129 | # Test that realwarn is called |
130 | { |
131 | local $^W = 0; |
132 | eval 'sub CGI::Carp::realdie {my $mess = shift; return $mess};'; |
133 | } |
134 | |
135 | like(CGI::Carp::die('There is a problem'), |
136 | $stamp, |
137 | 'CGI::Carp::die calls CORE::die, but adds stamp'); |
138 | |
139 | #----------------------------------------------------------------------------- |
140 | # Test set_message |
141 | #----------------------------------------------------------------------------- |
142 | |
143 | is(CGI::Carp::set_message('My new Message'), |
144 | 'My new Message', |
145 | 'CGI::Carp::set_message returns new message'); |
146 | |
147 | is($CGI::Carp::CUSTOM_MSG, |
148 | 'My new Message', |
149 | 'CGI::Carp::set_message message set correctly'); |
150 | |
151 | # set the message back to the empty string so that the tests later |
152 | # work properly. |
153 | CGI::Carp::set_message(''), |
154 | |
155 | #----------------------------------------------------------------------------- |
156 | # Test warnings_to_browser |
157 | #----------------------------------------------------------------------------- |
158 | |
159 | CGI::Carp::warningsToBrowser(0); |
160 | is($CGI::Carp::EMIT_WARNINGS, 0, "Warnings turned off"); |
161 | unless( is(@CGI::Carp::WARNINGS, 1, "_warn not called") ) { |
162 | print join "\n", map "'$_'", @CGI::Carp::WARNINGS; |
163 | } |
164 | |
165 | # turn off STDOUT (prevents spurious warnings to screen |
166 | tie *STDOUT, 'StoreStuff' or die "Can't tie STDOUT"; |
167 | CGI::Carp::warningsToBrowser(1); |
168 | my $fake_out = join '', <STDOUT>; |
169 | untie *STDOUT; |
170 | |
171 | open(STDOUT, ">&REAL_STDOUT"); |
0106e1e7 |
172 | my $fname = $0; |
173 | $fname =~ tr/<>-/\253\273\255/; # _warn does this so we have to also |
88587957 |
174 | is( $fake_out, "<!-- warning: There is a problem at $fname line $save_expect_l. -->\n", |
fa8e8936 |
175 | 'warningsToBrowser() on' ); |
176 | |
177 | is($CGI::Carp::EMIT_WARNINGS, 1, "Warnings turned off"); |
178 | is(@CGI::Carp::WARNINGS, 0, "_warn is called"); |
179 | |
180 | #----------------------------------------------------------------------------- |
181 | # Test fatals_to_browser |
182 | #----------------------------------------------------------------------------- |
183 | |
184 | package StoreStuff; |
185 | |
186 | sub TIEHANDLE { |
187 | my $class = shift; |
188 | bless [], $class; |
189 | } |
190 | |
191 | sub PRINT { |
192 | my $self = shift; |
193 | push @$self, @_; |
194 | } |
195 | |
196 | sub READLINE { |
197 | my $self = shift; |
198 | shift @$self; |
199 | } |
200 | |
201 | package main; |
202 | |
203 | tie *STDOUT, "StoreStuff"; |
204 | |
205 | # do tests |
206 | my @result; |
207 | |
208 | CGI::Carp::fatalsToBrowser(); |
209 | $result[0] .= $_ while (<STDOUT>); |
210 | |
211 | CGI::Carp::fatalsToBrowser('Message to the world'); |
212 | $result[1] .= $_ while (<STDOUT>); |
213 | |
214 | $ENV{SERVER_ADMIN} = 'foo@bar.com'; |
215 | CGI::Carp::fatalsToBrowser(); |
216 | $result[2] .= $_ while (<STDOUT>); |
217 | |
218 | CGI::Carp::set_message('Override the message passed in'), |
219 | |
220 | CGI::Carp::fatalsToBrowser('Message to the world'); |
221 | $result[3] .= $_ while (<STDOUT>); |
222 | CGI::Carp::set_message(''), |
223 | delete $ENV{SERVER_ADMIN}; |
224 | |
225 | # now restore STDOUT |
226 | untie *STDOUT; |
227 | |
228 | |
229 | like($result[0], |
230 | '/Content-type: text/html/', |
231 | "Default string has header"); |
232 | |
233 | ok($result[0] !~ /Message to the world/, "Custom message not in default string"); |
234 | |
235 | like($result[1], |
236 | '/Message to the world/', |
237 | "Custom Message appears in output"); |
238 | |
239 | ok($result[0] !~ /foo\@bar.com/, "Server Admin does not appear in default message"); |
240 | |
241 | like($result[2], |
242 | '/foo@bar.com/', |
243 | "Server Admin appears in output"); |
244 | |
245 | like($result[3], |
246 | '/Message to the world/', |
247 | "Custom message not in result"); |
248 | |
249 | like($result[3], |
250 | '/Override the message passed in/', |
251 | "Correct message in string"); |
252 | |
253 | #----------------------------------------------------------------------------- |
254 | # Test to_filehandle |
255 | #----------------------------------------------------------------------------- |
256 | |
257 | sub buffer { |
258 | CGI::Carp::to_filehandle (@_); |
259 | } |
260 | |
261 | tie *STORE, "StoreStuff"; |
262 | |
263 | require FileHandle; |
264 | my $fh = FileHandle->new; |
265 | |
266 | ok( defined buffer(\*STORE), '\*STORE returns proper filehandle'); |
267 | ok( defined buffer( $fh ), '$fh returns proper filehandle'); |
268 | ok( defined buffer('::STDOUT'), 'STDIN returns proper filehandle'); |
269 | ok( defined buffer(*main::STDOUT), 'STDIN returns proper filehandle'); |
270 | ok(!defined buffer("WIBBLE"), '"WIBBLE" doesn\'t returns proper filehandle'); |