1 # -*- Mode: cperl; coding: utf-8; cperl-indent-level: 2 -*-
2 #!/usr/local/bin/perl -w
6 use Test::More tests => 41;
9 BEGIN { use_ok('CGI::Carp') };
11 #-----------------------------------------------------------------------------
13 #-----------------------------------------------------------------------------
16 my $expect_f = __FILE__;
17 my $expect_l = __LINE__ + 1;
18 my ($file, $line, $id) = CGI::Carp::id(0);
19 is($file, $expect_f, "file");
20 is($line, $expect_l, "line");
21 is($id, "carp.t", "id");
23 # one level of indirection
24 sub id1 { my $level = shift; return CGI::Carp::id($level); };
26 $expect_l = __LINE__ + 1;
27 ($file, $line, $id) = id1(1);
28 is($file, $expect_f, "file");
29 is($line, $expect_l, "line");
30 is($id, "carp.t", "id");
32 # two levels of indirection
33 sub id2 { my $level = shift; return id1($level); };
35 $expect_l = __LINE__ + 1;
36 ($file, $line, $id) = id2(2);
37 is($file, $expect_f, "file");
38 is($line, $expect_l, "line");
39 is($id, "carp.t", "id");
41 #-----------------------------------------------------------------------------
43 #-----------------------------------------------------------------------------
50 like(CGI::Carp::stamp(),
52 "Time in correct format");
54 sub stamp1 {return CGI::Carp::stamp()};
55 sub stamp2 {return stamp1()};
57 like(stamp2(), $stamp, "Time in correct format");
59 #-----------------------------------------------------------------------------
61 #-----------------------------------------------------------------------------
63 # set some variables to control what's going on.
65 $CGI::Carp::EMIT_WARNINGS = 0;
66 my $q_file = quotemeta($file);
69 # Test that realwarn is called
72 eval "sub CGI::Carp::realwarn {return 'Called realwarn'};";
75 $expect_l = __LINE__ + 1;
76 is(CGI::Carp::warn("There is a problem"),
78 "CGI::Carp::warn calls CORE::warn");
80 # Test that message is constructed correctly
81 eval 'sub CGI::Carp::realwarn {my $mess = shift; return $mess};';
83 $expect_l = __LINE__ + 1;
84 like(CGI::Carp::warn("There is a problem"),
85 "/] $id: There is a problem at $q_file line $expect_l.".'$/',
86 "CGI::Carp::warn builds correct message");
88 # Test that _warn is called at the correct time
91 my $warn_expect_l = $expect_l = __LINE__ + 1;
92 like(CGI::Carp::warn("There is a problem"),
93 "/] $id: There is a problem at $q_file line $expect_l.".'$/',
94 "CGI::Carp::warn builds correct message");
96 #-----------------------------------------------------------------------------
98 #-----------------------------------------------------------------------------
100 ok(!CGI::Carp::ineval, 'ineval returns false when not in eval');
101 eval {ok(CGI::Carp::ineval, 'ineval returns true when in eval');};
103 #-----------------------------------------------------------------------------
105 #-----------------------------------------------------------------------------
107 # set some variables to control what's going on.
108 $CGI::Carp::WRAP = 0;
110 $expect_l = __LINE__ + 1;
111 eval { CGI::Carp::die('There is a problem'); };
113 '/^There is a problem/',
114 'CGI::Carp::die calls CORE::die without altering argument in eval');
116 # Test that realwarn is called
119 eval 'sub CGI::Carp::realdie {my $mess = shift; return $mess};';
122 like(CGI::Carp::die('There is a problem'),
124 'CGI::Carp::die calls CORE::die, but adds stamp');
126 #-----------------------------------------------------------------------------
128 #-----------------------------------------------------------------------------
130 is(CGI::Carp::set_message('My new Message'),
132 'CGI::Carp::set_message returns new message');
134 is($CGI::Carp::CUSTOM_MSG,
136 'CGI::Carp::set_message message set correctly');
138 # set the message back to the empty string so that the tests later
140 CGI::Carp::set_message(''),
142 #-----------------------------------------------------------------------------
144 #-----------------------------------------------------------------------------
146 import CGI::Carp qw(name=new_progname);
147 is($CGI::Carp::PROGNAME,
149 'CGI::Carp::import set program name correctly');
151 is(CGI::Carp::set_progname('newer_progname'),
153 'CGI::Carp::set_progname returns new program name');
155 is($CGI::Carp::PROGNAME,
157 'CGI::Carp::set_progname program name set correctly');
159 # set the message back to the empty string so that the tests later
161 is (CGI::Carp::set_progname(undef),undef,"CGI::Carp::set_progname returns unset name correctly");
162 is ($CGI::Carp::PROGNAME,undef,"CGI::Carp::set_progname program name unset correctly");
164 #-----------------------------------------------------------------------------
165 # Test warnings_to_browser
166 #-----------------------------------------------------------------------------
168 CGI::Carp::warningsToBrowser(0);
169 is($CGI::Carp::EMIT_WARNINGS, 0, "Warnings turned off");
171 # turn off STDOUT (prevents spurious warnings to screen
172 tie *STDOUT, 'StoreStuff' or die "Can't tie STDOUT";
173 CGI::Carp::warningsToBrowser(1);
174 my $fake_out = join '', <STDOUT>;
177 open(STDOUT, ">&REAL_STDOUT");
179 $fname =~ tr/<>-/\253\273\255/; # _warn does this so we have to also
180 is( $fake_out, "<!-- warning: There is a problem at $fname line $warn_expect_l. -->\n",
181 'warningsToBrowser() on' );
183 is($CGI::Carp::EMIT_WARNINGS, 1, "Warnings turned off");
185 #-----------------------------------------------------------------------------
186 # Test fatals_to_browser
187 #-----------------------------------------------------------------------------
208 tie *STDOUT, "StoreStuff";
213 CGI::Carp::fatalsToBrowser();
214 $result[0] .= $_ while (<STDOUT>);
216 CGI::Carp::fatalsToBrowser('Message to the world');
217 $result[1] .= $_ while (<STDOUT>);
219 $ENV{SERVER_ADMIN} = 'foo@bar.com';
220 CGI::Carp::fatalsToBrowser();
221 $result[2] .= $_ while (<STDOUT>);
223 CGI::Carp::set_message('Override the message passed in'),
225 CGI::Carp::fatalsToBrowser('Message to the world');
226 $result[3] .= $_ while (<STDOUT>);
227 CGI::Carp::set_message(''),
228 delete $ENV{SERVER_ADMIN};
235 '/Content-type: text/html/',
236 "Default string has header");
238 ok($result[0] !~ /Message to the world/, "Custom message not in default string");
241 '/Message to the world/',
242 "Custom Message appears in output");
244 ok($result[0] !~ /foo\@bar.com/, "Server Admin does not appear in default message");
248 "Server Admin appears in output");
251 '/Message to the world/',
252 "Custom message not in result");
255 '/Override the message passed in/',
256 "Correct message in string");
258 #-----------------------------------------------------------------------------
260 #-----------------------------------------------------------------------------
263 CGI::Carp::to_filehandle (@_);
266 tie *STORE, "StoreStuff";
269 my $fh = FileHandle->new;
271 ok( defined buffer(\*STORE), '\*STORE returns proper filehandle');
272 ok( defined buffer( $fh ), '$fh returns proper filehandle');
273 ok( defined buffer('::STDOUT'), 'STDIN returns proper filehandle');
274 ok( defined buffer(*main::STDOUT), 'STDIN returns proper filehandle');
275 ok(!defined buffer("WIBBLE"), '"WIBBLE" doesn\'t returns proper filehandle');