1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
|
#!perl -w
use strict;
use FindBin qw($Bin);
use File::Spec::Functions qw(catdir catfile);
use Test::More tests => 49;
use HTML::Mason::Interp;
BEGIN { use_ok('MasonX::Interp::WithCallbacks') }
my $key = 'myCallbackTester';
my $cbs = [];
##############################################################################
# Set up callback functions.
##############################################################################
# Simple callback.
sub simple {
my $cb = shift;
isa_ok( $cb, 'Params::Callback' );
isa_ok( $cb->cb_request, 'Params::CallbackRequest' );
my $params = $cb->params;
$params->{result} = 'Success';
}
push @$cbs, { pkg_key => $key,
cb_key => 'simple',
cb => \&simple
};
##############################################################################
# Priorty order checking.
sub priority {
my $cb = shift;
my $params = $cb->params;
my $val = $cb->value;
$val = '5' if $val eq 'def';
$params->{result} .= " $val";
}
push @$cbs, { pkg_key => $key,
cb_key => 'priority',
cb => \&priority
};
##############################################################################
# Hash value callback.
sub hash_check {
my $cb = shift;
isa_ok( $cb, 'Params::Callback');
my $params = $cb->params;
my $val = $cb->value;
# For some reason, if I don't eval this, then the code in the rest of
# the function doesn't run!
eval { isa_ok( $val, 'HASH' ) };
$params->{result} = "$val"
}
push @$cbs, { pkg_key => $key,
cb_key => 'hash_check',
cb => \&hash_check
};
##############################################################################
# Code value callback.
sub code_check {
my $cb = shift;
isa_ok( $cb, 'Params::Callback');
my $params = $cb->params;
my $val = $cb->value;
# For some reason, if I don't eval this, then the code in the rest of
# the function doesn't run!
eval { isa_ok( $val, 'CODE' ) };
$params->{result} = $val->();
}
push @$cbs, { pkg_key => $key,
cb_key => 'code_check',
cb => \&code_check
};
##############################################################################
# Count the number of times the callback executes.
sub count {
my $cb = shift;
isa_ok( $cb, 'Params::Callback');
my $params = $cb->params;
$params->{result}++;
}
push @$cbs, { pkg_key => $key,
cb_key => 'count',
cb => \&count
};
##############################################################################
# Abort callbacks.
sub test_abort {
my $cb = shift;
isa_ok( $cb, 'Params::Callback');
my $params = $cb->params;
$cb->abort(1);
}
push @$cbs, { pkg_key => $key,
cb_key => 'test_abort',
cb => \&test_abort
};
##############################################################################
# Check the aborted value.
sub test_aborted {
my $cb = shift;
isa_ok( $cb, 'Params::Callback');
my $params = $cb->params;
my $val = $cb->value;
eval { $cb->abort(1) } if $val;
$params->{result} = $cb->aborted($@) ? 'yes' : 'no';
}
push @$cbs, { pkg_key => $key,
cb_key => 'test_aborted',
cb => \&test_aborted
};
##############################################################################
# We'll use this callback just to grab the value of the "submit" parameter.
sub submit {
my $cb = shift;
isa_ok( $cb, 'Params::Callback');
my $params = $cb->params;
$params->{result} = $params->{submit};
}
push @$cbs, { pkg_key => $key,
cb_key => 'submit',
cb => \&submit
};
##############################################################################
# We'll use this callback to throw exceptions.
sub exception {
my $cb = shift;
my $params = $cb->params;
if ($cb->value) {
# Throw an exception object.
HTML::Mason::Exception->throw( error => "He's dead, Jim" );
} else {
# Just die.
die "He's dead, Jim";
}
}
push @$cbs, { pkg_key => $key,
cb_key => 'exception',
cb => \&exception
};
##############################################################################
# We'll use these callbacks to test notes().
sub add_note {
my $cb = shift;
$cb->notes($cb->value, $cb->params->{note});
}
sub get_note {
my $cb = shift;
$cb->params->{result} = $cb->notes($cb->value);
}
sub list_notes {
my $cb = shift;
my $params = $cb->params;
my $notes = $cb->notes;
for my $k (sort keys %$notes) {
$params->{result} .= "$k => $notes->{$k}\n";
}
}
sub clear {
my $cb = shift;
$cb->cb_request->clear_notes;
}
push @$cbs, { pkg_key => $key,
cb_key => 'add_note',
cb => \&add_note
},
{ pkg_key => $key,
cb_key => 'get_note',
cb => \&get_note
},
{ pkg_key => $key,
cb_key => 'list_notes',
cb => \&list_notes
},
{ pkg_key => $key,
cb_key => 'clear',
cb => \&clear
};
##############################################################################
# We'll use this callback to change the result to uppercase.
sub upper {
my $cb = shift;
my $params = $cb->params;
if ($params->{do_upper}) {
isa_ok( $cb, 'Params::Callback');
$params->{result} = uc $params->{result};
}
}
##############################################################################
# We'll use this callback to flip the characters of the "submit" parameter.
# The value of the "submit" parameter won't be "racecar!"
sub flip {
my $cb = shift;
my $params = $cb->params;
if ($params->{do_flip}) {
isa_ok( $cb, 'Params::Callback');
$params->{submit} = reverse $params->{submit};
}
}
##############################################################################
# Set up Mason objects.
##############################################################################
my $outbuf;
ok( my $interp = MasonX::Interp::WithCallbacks->new
( comp_root => catdir($Bin, qw(htdocs)),
callbacks => $cbs,
post_callbacks => [\&upper],
pre_callbacks => [\&flip],
out_method => \$outbuf ),
"Construct interp object" );
isa_ok($interp, 'MasonX::Interp::WithCallbacks');
isa_ok($interp, 'HTML::Mason::Interp');
isa_ok($interp->cb_request, 'Params::CallbackRequest');
my $comp = '/dhandler';
##############################################################################
# Try a simple callback.
$interp->exec($comp, "$key|simple_cb" => 1);
is( $outbuf, 'Success', "Check simple result" );
$outbuf = '';
##############################################################################
# Check that prioritized callbacks execute in the proper order.
$interp->exec($comp,
"$key|priority_cb0" => 0,
"$key|priority_cb2" => 2,
"$key|priority_cb9" => 9,
"$key|priority_cb7" => 7,
"$key|priority_cb1" => 1,
"$key|priority_cb4" => 4,
"$key|priority_cb" => 'def' );
is($outbuf, " 0 1 2 4 5 7 9", "Check priority order" );
$outbuf = '';
##############################################################################
# Emmulate the sumission of an <input type="image" /> button.
$interp->exec($comp,
"$key|simple_cb.x" => 18,
"$key|simple_cb.y" => 24 );
is( $outbuf, 'Success', "Check simple image result" );
$outbuf = '';
##############################################################################
# Make sure that an image submit doesn't cause the callback to be called
# twice.
$interp->exec($comp,
"$key|count_cb.x" => 18,
"$key|count_cb.y" => 24 );
is( $outbuf, '1', "Check image count result" );
$outbuf = '';
##############################################################################
# Just like the above, but make sure that different priorities execute
# at different times.
$interp->exec($comp,
"$key|count_cb1.x" => 18,
"$key|count_cb1.y" => 24,
"$key|count_cb2.x" => 18,
"$key|count_cb2.y" => 24 );
is( $outbuf, '2', "Check second image count result" );
$outbuf = '';
##############################################################################
# Test the abort functionality. The abort callback's higher priority should
# cause it to prevent simple from being called.
eval { $interp->exec($comp,
"$key|simple_cb" => 1,
"$key|test_abort_cb0" => 1 ) };
ok( my $err = $@, "Catch exception" );
isa_ok( $err, 'HTML::Mason::Exception::Abort' );
is( $err->aborted_value, 1, "Check aborted value" );
is( $outbuf, '', "Check abort result" );
$outbuf = '';
##############################################################################
# Test the aborted method.
$interp->exec($comp, "$key|test_aborted_cb" => 1 );
is( $outbuf, 'yes', "Check aborted result" );
$outbuf = '';
##############################################################################
# Test notes.
my $note_key = 'myNote';
my $note = 'Test note';
$interp->exec($comp,
"$key|add_note_cb1" => $note_key, # Executes first.
note => $note,
"$key|get_note_cb" => $note_key);
is( $outbuf, $note, "Check note result" );
$outbuf = '';
# Make sure the note isn't available on the next request.
$interp->exec($comp, "$key|get_note_cb" => $note_key );
is( $outbuf, '', "Check no note result" );
# Add multiple notes.
$interp->exec($comp,
"$key|add_note_cb1" => $note_key, # Executes first.
"$key|add_note_cb2" => $note_key . 1, # Executes second.
note => $note,
"$key|list_notes_cb" => 1);
is( $outbuf, "$note_key => $note\n${note_key}1 => $note\n",
"Check multiple note result" );
$outbuf = '';
# Make sure that notes percolate back to Mason.
$interp->exec($comp,
"$key|add_note_cb" => $note_key,
note => $note,
result => sub { shift->notes($note_key) } );
is( $outbuf, $note, "Check mason note result" );
$outbuf = '';
# Make sure that we can still get at the notes via the callback request object
# in Mason components.
$interp->exec($comp,
"$key|add_note_cb" => $note_key,
note => $note,
result => sub {
shift->interp->cb_request->notes($note_key)
} );
is( $outbuf, $note, "Check cb_request note result" );
$outbuf = '';
# Finally, make sure that if we clear it in callbacks, that no one gets it.
$interp->exec($comp,
"$key|add_note_cb1" => $note_key, # Executes first.
note => $note,
"$key|clear_cb" => 1,
result => sub { shift->notes($note_key) } );
is( $outbuf, '', "Check Mason cleared note result" );
$interp->exec($comp,
"$key|add_note_cb1" => $note_key, # Executes first.
note => $note,
"$key|clear_cb" => 1,
result => sub {
shift->interp->cb_request->notes($note_key)
} );
is( $outbuf, '', "Check cb_request cleared note result" );
##############################################################################
# Test the pre-execution callbacks.
my $string = 'yowza';
$interp->exec($comp,
"$key|submit_cb" => 1,
submit => $string,
do_flip => 1 );
is( $outbuf, reverse($string), "Check pre result" );
$outbuf = '';
##############################################################################
# Test the post-execution callbacks.
$interp->exec($comp,
"$key|simple_cb" => 1,
do_upper => 1 );
is( $outbuf, 'SUCCESS', "Check post result" );
$outbuf = '';
ok( $interp = MasonX::Interp::WithCallbacks->new
( comp_root => catdir($Bin, qw(htdocs)),
callbacks => $cbs,
ignore_nulls => 1,
out_method => \$outbuf ),
"Construct interp object that ignores nulls" );
$interp->exec($comp, "$key|simple_cb" => 1);
is( $outbuf, 'Success', "Check simple result" );
$outbuf = '';
# And try it with a null value.
$interp->exec($comp, "$key|simple_cb" => '');
is( $outbuf, '', "Check null result" );
$outbuf = '';
# And with undef.
$interp->exec($comp, "$key|simple_cb" => undef);
is( $outbuf, '', "Check undef result" );
$outbuf = '';
# But 0 should succeed.
$interp->exec($comp, "$key|simple_cb" => 0);
is( $outbuf, 'Success', "Check 0 result" );
$outbuf = '';
##############################################################################
# Test the exception handler.
ok( $interp = MasonX::Interp::WithCallbacks->new
( comp_root => catdir($Bin, qw(htdocs)),
callbacks => $cbs,
cb_exception_handler => sub {
like( $_[0], qr/^He's dead, Jim at/,
"Check our die message" );
},
out_method => \$outbuf ),
"Construct interp object that handles exceptions" );
$interp->exec($comp, "$key|exception_cb" => 0);
$outbuf = '';
__END__
|