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
|
#!perl -w
use strict;
use FindBin qw($Bin);
use File::Spec::Functions qw(catdir catfile);
use Test::More;
use HTML::Mason::Interp;
my $base_key = 'OOTester';
my $comp = '/dhandler';
##############################################################################
# Figure out if the current configuration can handle OO callbacks.
BEGIN {
plan skip_all => 'Object-oriented callbacks require Perl 5.6.0 or later'
if $] < 5.006;
plan skip_all => 'Attribute::Handlers and Class::ISA required for' .
' object-oriented callbacks'
unless eval { require Attribute::Handlers }
and eval { require Class::ISA };
plan tests => 16;
}
##############################################################################
# Set up the callback class.
##############################################################################
package Params::Callback::TestObjects;
use strict;
use base 'Params::Callback';
__PACKAGE__->register_subclass( class_key => $base_key);
use Params::CallbackRequest::Exceptions abbr => [qw(throw_cb_exec)];
sub simple : Callback {
my $self = shift;
main::isa_ok($self, 'Params::Callback');
main::isa_ok($self, __PACKAGE__);
my $params = $self->params;
$params->{result} = 'Simple Success';
}
sub lowerit : PostCallback {
my $self = shift;
my $params = $self->params;
if ($params->{do_lower}) {
main::isa_ok($self, 'Params::Callback');
main::isa_ok($self, __PACKAGE__);
$params->{result} = lc $params->{result};
}
}
1;
##############################################################################
# Set up another callback class to test the default class key.
package Params::Callback::TestKey;
use strict;
use base 'Params::Callback';
__PACKAGE__->register_subclass;
sub my_key : Callback {
my $self = shift;
main::is($self->pkg_key, __PACKAGE__, "Check package key" );
main::is($self->class_key, __PACKAGE__, "Check class key" );
}
##############################################################################
# Back in the real world...
##############################################################################
package main;
use strict;
use_ok('MasonX::Interp::WithCallbacks');
##############################################################################
# Set up a functional callback we can use.
sub another {
my $cb = shift;
main::isa_ok($cb, 'Params::Callback');
my $params = $cb->params;
$params->{result} = 'Another Success';
}
##############################################################################
# And a functional request callback.
sub presto {
my $cb = shift;
main::isa_ok($cb, 'Params::Callback');
my $params = $cb->params;
$params->{result} = 'PRESTO' if $params->{do_presto};
}
##############################################################################
# Construct the combined callback exec object.
my $outbuf;
ok( my $interp = MasonX::Interp::WithCallbacks->new
(comp_root => catdir($Bin, qw(htdocs)),
out_method => \$outbuf,
callbacks => [{ pkg_key => 'foo',
cb_key => 'another',
cb => \&another}],
cb_classes => 'ALL',
pre_callbacks => [\&presto] ),
"Construct combined CBExec object" );
##############################################################################
# Make sure the functional callback works.
$interp->exec($comp, 'foo|another_cb' => 1);
is( $outbuf, 'Another Success', "Check functional result" );
$outbuf = '';
##############################################################################
# Make sure OO callback works.
$interp->exec($comp, "$base_key|simple_cb" => 1);
is( $outbuf, 'Simple Success', "Check OO result" );
$outbuf = '';
##############################################################################
# Make sure that functional and OO request callbacks execute, too.
$interp->exec($comp,
do_lower => 1,
do_presto => 1);
is( $outbuf, 'presto', "Check request result" );
$outbuf = '';
##############################################################################
# Make sure that the default class key is the class name.
$interp->exec($comp, "Params::Callback::TestKey|my_key_cb" => 1);
$outbuf = '';
1;
__END__
|