1#============================================================= -*-perl-*- 2# 3# t/list.t 4# 5# Tests list references as variables, including pseudo-methods such 6# as first(), last(), etc. 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 26use Template::Parser; 27$Template::Test::DEBUG = 0; 28#$Template::Parser::DEBUG = 1; 29#$Template::Directive::PRETTY = 1; 30 31# sample data 32my ($a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, 33 $n, $o, $p, $q, $r, $s, $t, $u, $v, $w, $x, $y, $z) = 34 qw( alpha bravo charlie delta echo foxtrot golf hotel india 35 juliet kilo lima mike november oscar papa quebec romeo 36 sierra tango umbrella victor whisky x-ray yankee zulu ); 37 38my $data = [ $r, $j, $s, $t, $y, $e, $f, $z ]; 39my $vars = { 40 'a' => $a, 41 'b' => $b, 42 'c' => $c, 43 'd' => $d, 44 'e' => $e, 45 data => $data, 46 days => [ qw( Mon Tue Wed Thu Fri Sat Sun ) ], 47 wxyz => [ { id => $z, name => 'Zebedee', rank => 'aa' }, 48 { id => $y, name => 'Yinyang', rank => 'ba' }, 49 { id => $x, name => 'Xeexeez', rank => 'ab' }, 50 { id => $w, name => 'Warlock', rank => 'bb' }, ], 51 inst => [ { name => 'piano', url => '/roses.html' }, 52 { name => 'flute', url => '/blow.html' }, 53 { name => 'organ', url => '/tulips.html' }, ], 54 nest => [ [ 3, 1, 4 ], [ 2, [ 7, 1, 8 ] ] ], 55}; 56 57my $config = {}; 58 59test_expect(\*DATA, $config, $vars); 60 61 62__DATA__ 63 64#------------------------------------------------------------------------ 65# GET 66#------------------------------------------------------------------------ 67-- test -- 68[% data.0 %] and [% data.1 %] 69-- expect -- 70romeo and juliet 71 72-- test -- 73[% data.first %] - [% data.last %] 74-- expect -- 75romeo - zulu 76 77-- test -- 78[% data.size %] [% data.max %] 79-- expect -- 808 7 81 82-- test -- 83[% data.join(', ') %] 84-- expect -- 85romeo, juliet, sierra, tango, yankee, echo, foxtrot, zulu 86 87-- test -- 88[% data.reverse.join(', ') %] 89-- expect -- 90zulu, foxtrot, echo, yankee, tango, sierra, juliet, romeo 91 92-- test -- 93[% data.sort.reverse.join(' - ') %] 94-- expect -- 95zulu - yankee - tango - sierra - romeo - juliet - foxtrot - echo 96 97-- test -- 98[% FOREACH item = wxyz.sort('id') -%] 99* [% item.name %] 100[% END %] 101-- expect -- 102* Warlock 103* Xeexeez 104* Yinyang 105* Zebedee 106 107-- test -- 108[% FOREACH item = wxyz.sort('rank') -%] 109* [% item.name %] 110[% END %] 111-- expect -- 112* Zebedee 113* Xeexeez 114* Yinyang 115* Warlock 116 117-- test -- 118[% FOREACH n = [0..6] -%] 119[% days.$n +%] 120[% END -%] 121-- expect -- 122Mon 123Tue 124Wed 125Thu 126Fri 127Sat 128Sun 129 130-- test -- 131[% data = [ 'one', 'two', data.first ] -%] 132[% data.join(', ') %] 133-- expect -- 134one, two, romeo 135 136-- test -- 137[% data = [ 90, 8, 70, 6, 1, 11, 10, 2, 5, 50, 52 ] -%] 138 sort: [% data.sort.join(', ') %] 139nsort: [% data.nsort.join(', ') %] 140-- expect -- 141 sort: 1, 10, 11, 2, 5, 50, 52, 6, 70, 8, 90 142nsort: 1, 2, 5, 6, 8, 10, 11, 50, 52, 70, 90 143 144-- test -- 145[% ilist = [] -%] 146[% ilist.push("<a href=\"$i.url\">$i.name</a>") FOREACH i = inst -%] 147[% ilist.join(",\n") -%] 148[% global.ilist = ilist -%] 149-- expect -- 150<a href="/roses.html">piano</a>, 151<a href="/blow.html">flute</a>, 152<a href="/tulips.html">organ</a> 153 154-- test -- 155[% global.ilist.pop %] 156-- expect -- 157<a href="/tulips.html">organ</a> 158 159-- test -- 160[% global.ilist.shift %] 161-- expect -- 162<a href="/roses.html">piano</a> 163 164-- test -- 165[% global.ilist.unshift('another') -%] 166[% global.ilist.join(', ') %] 167-- expect -- 168another, <a href="/blow.html">flute</a> 169 170-- test -- 171[% nest.0.0 %].[% nest.0.1 %][% nest.0.2 +%] 172[% nest.1.shift %].[% nest.1.0.join('') %] 173-- expect -- 1743.14 1752.718 176 177-- test -- 178[% # define some initial data 179 people => [ 180 { id => 'tom', name => 'Tom' }, 181 { id => 'dick', name => 'Richard' }, 182 { id => 'larry', name => 'Larry' }, 183 ] 184-%] 185[% folk = [] -%] 186[% folk.push("<a href=\"${person.id}.html\">$person.name</a>") 187 FOREACH person = people.sort('name') -%] 188[% folk.join(",\n") -%] 189-- expect -- 190<a href="larry.html">Larry</a>, 191<a href="dick.html">Richard</a>, 192<a href="tom.html">Tom</a> 193 194-- test -- 195[% data.grep('r').join(', ') %] 196-- expect -- 197romeo, sierra, foxtrot 198 199-- test -- 200[% data.grep('^r').join(', ') %] 201-- expect -- 202romeo 203