1 # -*- Mode: cperl; coding: utf-8; cperl-indent-level: 2 -*-
2 #!/usr/local/bin/perl -w
9 # Due to a bug in older versions of MakeMaker & Test::Harness, we must
10 # ensure the blib's are in @INC, else we might use the core CGI.pm
11 unshift @INC, qw( ../blib/lib ../blib/arch lib );
17 use Test::More tests => 47;
20 BEGIN { use_ok('CGI::Carp') };
22 #-----------------------------------------------------------------------------
24 #-----------------------------------------------------------------------------
27 my $expect_f = __FILE__;
28 my $expect_l = __LINE__ + 1;
29 my ($file, $line, $id) = CGI::Carp::id(0);
30 is($file, $expect_f, "file");
31 is($line, $expect_l, "line");
32 is($id, "carp.t", "id");
34 # one level of indirection
35 sub id1 { my $level = shift; return CGI::Carp::id($level); };
37 $expect_l = __LINE__ + 1;
38 ($file, $line, $id) = id1(1);
39 is($file, $expect_f, "file");
40 is($line, $expect_l, "line");
41 is($id, "carp.t", "id");
43 # two levels of indirection
44 sub id2 { my $level = shift; return id1($level); };
46 $expect_l = __LINE__ + 1;
47 ($file, $line, $id) = id2(2);
48 is($file, $expect_f, "file");
49 is($line, $expect_l, "line");
50 is($id, "carp.t", "id");
52 #-----------------------------------------------------------------------------
54 #-----------------------------------------------------------------------------
61 like(CGI::Carp::stamp(),
63 "Time in correct format");
65 sub stamp1 {return CGI::Carp::stamp()};
66 sub stamp2 {return stamp1()};
68 like(stamp2(), $stamp, "Time in correct format");
70 #-----------------------------------------------------------------------------
72 #-----------------------------------------------------------------------------
74 # set some variables to control what's going on.
76 $CGI::Carp::EMIT_WARNINGS = 0;
77 @CGI::Carp::WARNINGS = ();
78 my $q_file = quotemeta($file);
81 # Test that realwarn is called
84 eval "sub CGI::Carp::realwarn {return 'Called realwarn'};";
87 $expect_l = __LINE__ + 1;
88 is(CGI::Carp::warn("There is a problem"),
90 "CGI::Carp::warn calls CORE::warn");
91 is(@CGI::Carp::WARNINGS, 0, "_warn not called");
93 # Test that message is constructed correctly
94 eval 'sub CGI::Carp::realwarn {my $mess = shift; return $mess};';
96 $expect_l = __LINE__ + 1;
97 like(CGI::Carp::warn("There is a problem"),
98 "/] $id: There is a problem at $q_file line $expect_l.".'$/',
99 "CGI::Carp::warn builds correct message");
100 is(@CGI::Carp::WARNINGS, 0, "_warn not called");
102 # Test that _warn is called at the correct time
103 $CGI::Carp::WARN = 1;
105 $expect_l = __LINE__ + 1;
106 like(CGI::Carp::warn("There is a problem"),
107 "/] $id: There is a problem at $q_file line $expect_l.".'$/',
108 "CGI::Carp::warn builds correct message");
110 is(@CGI::Carp::WARNINGS, 1, "_warn now called");
111 like($CGI::Carp::WARNINGS[0],
112 "/There is a problem at $q_file line $expect_l.".'$/',
113 "CGI::Carp::WARNINGS has correct message (without stamp)");
115 #-----------------------------------------------------------------------------
117 #-----------------------------------------------------------------------------
119 ok(!CGI::Carp::ineval, 'ineval returns false when not in eval');
120 eval {ok(CGI::Carp::ineval, 'ineval returns true when in eval');};
122 #-----------------------------------------------------------------------------
124 #-----------------------------------------------------------------------------
126 # set some variables to control what's going on.
127 $CGI::Carp::WRAP = 0;
129 $expect_l = __LINE__ + 1;
130 eval { CGI::Carp::die('There is a problem'); };
132 '/^There is a problem/',
133 'CGI::Carp::die calls CORE::die without altering argument in eval');
135 # Test that realwarn is called
138 eval 'sub CGI::Carp::realdie {my $mess = shift; return $mess};';
141 like(CGI::Carp::die('There is a problem'),
143 'CGI::Carp::die calls CORE::die, but adds stamp');
145 #-----------------------------------------------------------------------------
147 #-----------------------------------------------------------------------------
149 is(CGI::Carp::set_message('My new Message'),
151 'CGI::Carp::set_message returns new message');
153 is($CGI::Carp::CUSTOM_MSG,
155 'CGI::Carp::set_message message set correctly');
157 # set the message back to the empty string so that the tests later
159 CGI::Carp::set_message(''),
161 #-----------------------------------------------------------------------------
163 #-----------------------------------------------------------------------------
165 import CGI::Carp qw(name=new_progname);
166 is($CGI::Carp::PROGNAME,
168 'CGI::Carp::import set program name correctly');
170 is(CGI::Carp::set_progname('newer_progname'),
172 'CGI::Carp::set_progname returns new program name');
174 is($CGI::Carp::PROGNAME,
176 'CGI::Carp::set_progname program name set correctly');
178 # set the message back to the empty string so that the tests later
180 is (CGI::Carp::set_progname(undef),undef,"CGI::Carp::set_progname returns unset name correctly");
181 is ($CGI::Carp::PROGNAME,undef,"CGI::Carp::set_progname program name unset correctly");
183 #-----------------------------------------------------------------------------
184 # Test warnings_to_browser
185 #-----------------------------------------------------------------------------
187 CGI::Carp::warningsToBrowser(0);
188 is($CGI::Carp::EMIT_WARNINGS, 0, "Warnings turned off");
189 unless( is(@CGI::Carp::WARNINGS, 1, "_warn not called") ) {
190 print join "\n", map "'$_'", @CGI::Carp::WARNINGS;
193 # turn off STDOUT (prevents spurious warnings to screen
194 tie *STDOUT, 'StoreStuff' or die "Can't tie STDOUT";
195 CGI::Carp::warningsToBrowser(1);
196 my $fake_out = join '', <STDOUT>;
199 open(STDOUT, ">&REAL_STDOUT");
201 $fname =~ tr/<>-/\253\273\255/; # _warn does this so we have to also
202 is( $fake_out, "<!-- warning: There is a problem at $fname line 106. -->\n",
203 'warningsToBrowser() on' );
205 is($CGI::Carp::EMIT_WARNINGS, 1, "Warnings turned off");
206 is(@CGI::Carp::WARNINGS, 0, "_warn is called");
208 #-----------------------------------------------------------------------------
209 # Test fatals_to_browser
210 #-----------------------------------------------------------------------------
231 tie *STDOUT, "StoreStuff";
236 CGI::Carp::fatalsToBrowser();
237 $result[0] .= $_ while (<STDOUT>);
239 CGI::Carp::fatalsToBrowser('Message to the world');
240 $result[1] .= $_ while (<STDOUT>);
242 $ENV{SERVER_ADMIN} = 'foo@bar.com';
243 CGI::Carp::fatalsToBrowser();
244 $result[2] .= $_ while (<STDOUT>);
246 CGI::Carp::set_message('Override the message passed in'),
248 CGI::Carp::fatalsToBrowser('Message to the world');
249 $result[3] .= $_ while (<STDOUT>);
250 CGI::Carp::set_message(''),
251 delete $ENV{SERVER_ADMIN};
258 '/Content-type: text/html/',
259 "Default string has header");
261 ok($result[0] !~ /Message to the world/, "Custom message not in default string");
264 '/Message to the world/',
265 "Custom Message appears in output");
267 ok($result[0] !~ /foo\@bar.com/, "Server Admin does not appear in default message");
271 "Server Admin appears in output");
274 '/Message to the world/',
275 "Custom message not in result");
278 '/Override the message passed in/',
279 "Correct message in string");
281 #-----------------------------------------------------------------------------
283 #-----------------------------------------------------------------------------
286 CGI::Carp::to_filehandle (@_);
289 tie *STORE, "StoreStuff";
292 my $fh = FileHandle->new;
294 ok( defined buffer(\*STORE), '\*STORE returns proper filehandle');
295 ok( defined buffer( $fh ), '$fh returns proper filehandle');
296 ok( defined buffer('::STDOUT'), 'STDIN returns proper filehandle');
297 ok( defined buffer(*main::STDOUT), 'STDIN returns proper filehandle');
298 ok(!defined buffer("WIBBLE"), '"WIBBLE" doesn\'t returns proper filehandle');