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
|
#!perl -w
use strict;
use FindBin qw($Bin);
use File::Spec::Functions qw(catdir catfile);
use Test::More tests => 28;
use HTML::Mason::Interp;
BEGIN { use_ok('MasonX::Interp::WithCallbacks') }
my $key = 'myCallbackTester';
my $cbs = [];
##############################################################################
# Set up callback functions.
##############################################################################
# Priority callback.
sub priority {
my $cb = shift;
isa_ok( $cb, 'Params::Callback');
my $params = $cb->params;
my $val = $cb->value;
$val = $cb->priority if $val eq 'def';
$params->{result} .= " $val";
}
sub chk_priority {
my $cb = shift;
isa_ok( $cb, 'Params::Callback');
my $val = $cb->value;
is( $cb->priority, $val, "Check priority value '$val'" );
}
sub def_priority {
my $cb = shift;
isa_ok( $cb, 'Params::Callback');
is( $cb->priority, 5, "Check default priority" );
}
push @$cbs, { pkg_key => $key,
cb_key => 'priority',
cb => \&priority,
priority => 6
},
{ pkg_key => $key,
cb_key => 'chk_priority',
cb => \&chk_priority,
priority => 2
},
{ pkg_key => $key,
cb_key => 'def_priority',
cb => \&def_priority,
};
##############################################################################
# Set up the Interp object.
##############################################################################
my $outbuf;
ok( my $interp = MasonX::Interp::WithCallbacks->new
( comp_root => catdir($Bin, qw(htdocs)),
callbacks => $cbs,
out_method => \$outbuf ),
"Construct interp object" );
isa_ok($interp, 'MasonX::Interp::WithCallbacks');
isa_ok($interp, 'HTML::Mason::Interp');
my $comp = '/dhandler';
##############################################################################
# Test the callbacks themselves.
##############################################################################
# Test the priority ordering.
$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 6 7 9", "Check simple result" );
$outbuf = '';
##############################################################################
# Test the default priority.
$interp->exec($comp, "$key|def_priority_cb" => 1);
##############################################################################
# Check various priority values.
$interp->exec($comp, "$key|chk_priority_cb0" => 0,
"$key|chk_priority_cb2" => 2,
"$key|chk_priority_cb9" => 9,
"$key|chk_priority_cb7" => 7,
"$key|chk_priority_cb1" => 1,
"$key|chk_priority_cb4" => 4,
"$key|chk_priority_cb" => 2 );
1;
__END__
|