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
|
#============================================================= -*-perl-*-
#
# t/args.t
#
# Testing the passing of positional and named arguments to sub-routine and
# object methods.
#
# Written by Andy Wardley <abw@kfs.org>
#
# Copyright (C) 1996-2000 Andy Wardley. All Rights Reserved.
# Copyright (C) 1998-2000 Canon Research Centre Europe Ltd.
#
# This is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
#
# $Id: args.t,v 2.0 2000/08/10 14:56:14 abw Exp $
#
#========================================================================
use strict;
use lib qw( ./lib ../lib );
use Template::Test;
use Template::Constants qw( :status );
$^W = 1;
#------------------------------------------------------------------------
# define simple object and package sub for reporting arguments passed
#------------------------------------------------------------------------
package MyObj;
use base qw( Template::Base );
sub foo {
my $self = shift;
return "object:\n" . args(@_);
}
sub args {
my @args = @_;
my $named = ref $args[$#args] eq 'HASH' ? pop @args : { };
local $" = ', ';
return " ARGS: [ @args ]\n NAMED: { "
. join(', ', map { "$_ => $named->{ $_ }" } sort keys %$named)
. " }\n";
}
#------------------------------------------------------------------------
# main tests
#------------------------------------------------------------------------
package main;
use Template::Parser;
$Template::Test::DEBUG = 0;
$Template::Parser::DEBUG = 0;
my $replace = callsign();
$replace->{ args } = \&MyObj::args;
$replace->{ obj } = MyObj->new();
test_expect(\*DATA, { INTERPOLATE => 1 }, $replace);
__DATA__
-- test --
[% args(a b c) %]
-- expect --
ARGS: [ alpha, bravo, charlie ]
NAMED: { }
-- test --
[% args(a b c d=e f=g) %]
-- expect --
ARGS: [ alpha, bravo, charlie ]
NAMED: { d => echo, f => golf }
-- test --
[% args(a, b, c, d=e, f=g) %]
-- expect --
ARGS: [ alpha, bravo, charlie ]
NAMED: { d => echo, f => golf }
-- test --
[% args(a, b, c, d=e, f=g,) %]
-- expect --
ARGS: [ alpha, bravo, charlie ]
NAMED: { d => echo, f => golf }
-- test --
[% args(d=e, a, b, f=g, c) %]
-- expect --
ARGS: [ alpha, bravo, charlie ]
NAMED: { d => echo, f => golf }
-- test --
[% obj.foo(d=e, a, b, f=g, c) %]
-- expect --
object:
ARGS: [ alpha, bravo, charlie ]
NAMED: { d => echo, f => golf }
-- test --
[% obj.foo(d=e, a, b, f=g, c).split("\n").1 %]
-- expect --
ARGS: [ alpha, bravo, charlie ]
|