Upgrade to the CGI.pm 2.93.
[p5sagit/p5-mst-13.2.git] / lib / CGI / t / carp.t
CommitLineData
fa8e8936 1# -*- Mode: cperl; coding: utf-8; cperl-indent-level: 2 -*-
2#!/usr/local/bin/perl -w
3
f0c07f2e 4use strict;
8f3ccfa2 5use lib qw(t/lib);
6
7# Due to a bug in older versions of MakeMaker & Test::Harness, we must
8# ensure the blib's are in @INC, else we might use the core CGI.pm
9use lib qw(blib/lib blib/arch);
ac734d8b 10
188ba755 11use Test::More tests => 47;
fa8e8936 12use IO::Handle;
13
14BEGIN { use_ok('CGI::Carp') };
15
16#-----------------------------------------------------------------------------
17# Test id
18#-----------------------------------------------------------------------------
19
20# directly invoked
21my $expect_f = __FILE__;
22my $expect_l = __LINE__ + 1;
23my ($file, $line, $id) = CGI::Carp::id(0);
24is($file, $expect_f, "file");
25is($line, $expect_l, "line");
26is($id, "carp.t", "id");
27
28# one level of indirection
29sub id1 { my $level = shift; return CGI::Carp::id($level); };
30
31$expect_l = __LINE__ + 1;
32($file, $line, $id) = id1(1);
33is($file, $expect_f, "file");
34is($line, $expect_l, "line");
35is($id, "carp.t", "id");
36
37# two levels of indirection
38sub id2 { my $level = shift; return id1($level); };
39
40$expect_l = __LINE__ + 1;
41($file, $line, $id) = id2(2);
42is($file, $expect_f, "file");
43is($line, $expect_l, "line");
44is($id, "carp.t", "id");
45
46#-----------------------------------------------------------------------------
47# Test stamp
48#-----------------------------------------------------------------------------
49
50my $stamp = "/^\\[
51 ([a-z]{3}\\s){2}\\s?
52 [\\s\\d:]+
53 \\]\\s$id:/ix";
54
55like(CGI::Carp::stamp(),
56 $stamp,
57 "Time in correct format");
58
59sub stamp1 {return CGI::Carp::stamp()};
60sub stamp2 {return stamp1()};
61
62like(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 = ();
72my $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;
82is(CGI::Carp::warn("There is a problem"),
83 "Called realwarn",
84 "CGI::Carp::warn calls CORE::warn");
85is(@CGI::Carp::WARNINGS, 0, "_warn not called");
86
87# Test that message is constructed correctly
88eval 'sub CGI::Carp::realwarn {my $mess = shift; return $mess};';
89
90$expect_l = __LINE__ + 1;
91like(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");
94is(@CGI::Carp::WARNINGS, 0, "_warn not called");
95
96# Test that _warn is called at the correct time
97$CGI::Carp::WARN = 1;
98
ac734d8b 99$expect_l = __LINE__ + 1;
fa8e8936 100like(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
104is(@CGI::Carp::WARNINGS, 1, "_warn now called");
105like($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
113ok(!CGI::Carp::ineval, 'ineval returns false when not in eval');
114eval {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;
124eval { CGI::Carp::die('There is a problem'); };
125like($@,
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
135like(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
143is(CGI::Carp::set_message('My new Message'),
144 'My new Message',
145 'CGI::Carp::set_message returns new message');
146
147is($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.
153CGI::Carp::set_message(''),
154
155#-----------------------------------------------------------------------------
188ba755 156# Test set_progname
157#-----------------------------------------------------------------------------
158
159import CGI::Carp qw(name=new_progname);
160is($CGI::Carp::PROGNAME,
161 'new_progname',
162 'CGI::Carp::import set program name correctly');
163
164is(CGI::Carp::set_progname('newer_progname'),
165 'newer_progname',
166 'CGI::Carp::set_progname returns new program name');
167
168is($CGI::Carp::PROGNAME,
169 'newer_progname',
170 'CGI::Carp::set_progname program name set correctly');
171
172# set the message back to the empty string so that the tests later
173# work properly.
174is (CGI::Carp::set_progname(undef),undef,"CGI::Carp::set_progname returns unset name correctly");
175is ($CGI::Carp::PROGNAME,undef,"CGI::Carp::set_progname program name unset correctly");
176
177#-----------------------------------------------------------------------------
fa8e8936 178# Test warnings_to_browser
179#-----------------------------------------------------------------------------
180
181CGI::Carp::warningsToBrowser(0);
182is($CGI::Carp::EMIT_WARNINGS, 0, "Warnings turned off");
183unless( is(@CGI::Carp::WARNINGS, 1, "_warn not called") ) {
184 print join "\n", map "'$_'", @CGI::Carp::WARNINGS;
185}
186
187# turn off STDOUT (prevents spurious warnings to screen
188tie *STDOUT, 'StoreStuff' or die "Can't tie STDOUT";
189CGI::Carp::warningsToBrowser(1);
190my $fake_out = join '', <STDOUT>;
191untie *STDOUT;
192
193open(STDOUT, ">&REAL_STDOUT");
0106e1e7 194my $fname = $0;
195$fname =~ tr/<>-/\253\273\255/; # _warn does this so we have to also
8f3ccfa2 196is( $fake_out, "<!-- warning: There is a problem at $fname line 100. -->\n",
fa8e8936 197 'warningsToBrowser() on' );
198
199is($CGI::Carp::EMIT_WARNINGS, 1, "Warnings turned off");
200is(@CGI::Carp::WARNINGS, 0, "_warn is called");
201
202#-----------------------------------------------------------------------------
203# Test fatals_to_browser
204#-----------------------------------------------------------------------------
205
206package StoreStuff;
207
208sub TIEHANDLE {
209 my $class = shift;
210 bless [], $class;
211}
212
213sub PRINT {
214 my $self = shift;
215 push @$self, @_;
216}
217
218sub READLINE {
219 my $self = shift;
220 shift @$self;
221}
222
223package main;
224
225tie *STDOUT, "StoreStuff";
226
227# do tests
228my @result;
229
230CGI::Carp::fatalsToBrowser();
231$result[0] .= $_ while (<STDOUT>);
232
233CGI::Carp::fatalsToBrowser('Message to the world');
234$result[1] .= $_ while (<STDOUT>);
235
236$ENV{SERVER_ADMIN} = 'foo@bar.com';
237CGI::Carp::fatalsToBrowser();
238$result[2] .= $_ while (<STDOUT>);
239
240CGI::Carp::set_message('Override the message passed in'),
241
242CGI::Carp::fatalsToBrowser('Message to the world');
243$result[3] .= $_ while (<STDOUT>);
244CGI::Carp::set_message(''),
245delete $ENV{SERVER_ADMIN};
246
247# now restore STDOUT
248untie *STDOUT;
249
250
251like($result[0],
252 '/Content-type: text/html/',
253 "Default string has header");
254
255ok($result[0] !~ /Message to the world/, "Custom message not in default string");
256
257like($result[1],
258 '/Message to the world/',
259 "Custom Message appears in output");
260
261ok($result[0] !~ /foo\@bar.com/, "Server Admin does not appear in default message");
262
263like($result[2],
264 '/foo@bar.com/',
265 "Server Admin appears in output");
266
267like($result[3],
268 '/Message to the world/',
269 "Custom message not in result");
270
271like($result[3],
272 '/Override the message passed in/',
273 "Correct message in string");
274
275#-----------------------------------------------------------------------------
276# Test to_filehandle
277#-----------------------------------------------------------------------------
278
279sub buffer {
280 CGI::Carp::to_filehandle (@_);
281}
282
283tie *STORE, "StoreStuff";
284
285require FileHandle;
286my $fh = FileHandle->new;
287
288ok( defined buffer(\*STORE), '\*STORE returns proper filehandle');
289ok( defined buffer( $fh ), '$fh returns proper filehandle');
290ok( defined buffer('::STDOUT'), 'STDIN returns proper filehandle');
291ok( defined buffer(*main::STDOUT), 'STDIN returns proper filehandle');
292ok(!defined buffer("WIBBLE"), '"WIBBLE" doesn\'t returns proper filehandle');