1#============================================================= -*-perl-*- 2# 3# t/args.t 4# 5# Testing the passing of positional and named arguments to sub-routine and 6# object methods. 7# 8# Written by Andy Wardley <abw@kfs.org> 9# 10# Copyright (C) 1996-2000 Andy Wardley. All Rights Reserved. 11# Copyright (C) 1998-2000 Canon Research Centre Europe Ltd. 12# 13# This is free software; you can redistribute it and/or modify it 14# under the same terms as Perl itself. 15# 16# $Id$ 17# 18#======================================================================== 19 20use strict; 21use lib qw( ./lib ../lib ); 22use Template::Test; 23use Template::Constants qw( :status ); 24$^W = 1; 25 26#------------------------------------------------------------------------ 27# define simple object and package sub for reporting arguments passed 28#------------------------------------------------------------------------ 29 30package MyObj; 31use base qw( Template::Base ); 32 33sub foo { 34 my $self = shift; 35 return "object:\n" . args(@_); 36} 37 38sub args { 39 my @args = @_; 40 my $named = ref $args[$#args] eq 'HASH' ? pop @args : { }; 41 local $" = ', '; 42 43 return " ARGS: [ @args ]\n NAMED: { " 44 . join(', ', map { "$_ => $named->{ $_ }" } sort keys %$named) 45 . " }\n"; 46} 47 48 49#------------------------------------------------------------------------ 50# main tests 51#------------------------------------------------------------------------ 52 53package main; 54 55use Template::Parser; 56$Template::Test::DEBUG = 0; 57$Template::Parser::DEBUG = 0; 58 59my $replace = callsign(); 60$replace->{ args } = \&MyObj::args; 61$replace->{ obj } = MyObj->new(); 62 63test_expect(\*DATA, { INTERPOLATE => 1 }, $replace); 64 65 66__DATA__ 67-- test -- 68[% args(a b c) %] 69-- expect -- 70 ARGS: [ alpha, bravo, charlie ] 71 NAMED: { } 72 73-- test -- 74[% args(a b c d=e f=g) %] 75-- expect -- 76 ARGS: [ alpha, bravo, charlie ] 77 NAMED: { d => echo, f => golf } 78 79-- test -- 80[% args(a, b, c, d=e, f=g) %] 81-- expect -- 82 ARGS: [ alpha, bravo, charlie ] 83 NAMED: { d => echo, f => golf } 84 85-- test -- 86[% args(a, b, c, d=e, f=g,) %] 87-- expect -- 88 ARGS: [ alpha, bravo, charlie ] 89 NAMED: { d => echo, f => golf } 90 91-- test -- 92[% args(d=e, a, b, f=g, c) %] 93-- expect -- 94 ARGS: [ alpha, bravo, charlie ] 95 NAMED: { d => echo, f => golf } 96 97-- test -- 98[% obj.foo(d=e, a, b, f=g, c) %] 99-- expect -- 100object: 101 ARGS: [ alpha, bravo, charlie ] 102 NAMED: { d => echo, f => golf } 103 104-- test -- 105[% obj.foo(d=e, a, b, f=g, c).split("\n").1 %] 106-- expect -- 107 ARGS: [ alpha, bravo, charlie ] 108 109