1#============================================================= -*-perl-*- 2# 3# t/vars.t 4# 5# Template script testing variable use. 6# 7# Written by Andy Wardley <abw@wardley.org> 8# 9# Copyright (C) 1996-2006 Andy Wardley. All Rights Reserved. 10# Copyright (C) 1998-2000 Canon Research Centre Europe Ltd. 11# 12# This is free software; you can redistribute it and/or modify it 13# under the same terms as Perl itself. 14# 15# $Id$ 16# 17#======================================================================== 18 19use strict; 20use warnings; 21use lib qw( ./lib ../lib ); 22use Template::Test; 23use Template::Stash; 24use Template::Constants qw( :status ); 25use Template::Directive; 26use Template::Parser; 27$Template::Test::DEBUG = 0; 28$Template::Parser::DEBUG = 0; 29 30# sample data 31my ($a, $b, $c, $d, $e, $f, $g, $h, $i, $j, $k, $l, $m, 32 $n, $o, $p, $q, $r, $s, $t, $u, $v, $w, $x, $y, $z) = 33 qw( alpha bravo charlie delta echo foxtrot golf hotel india 34 juliet kilo lima mike november oscar papa quebec romeo 35 sierra tango umbrella victor whisky x-ray yankee zulu ); 36 37my @days = qw( Monday Tuesday Wednesday Thursday Friday Saturday Sunday ); 38my $day = -1; 39my $count = 0; 40my $params = { 41 'a' => $a, 42 'b' => $b, 43 'c' => $c, 44 'd' => $d, 45 'e' => $e, 46 'f' => { 47 'g' => $g, 48 'h' => $h, 49 'i' => { 50 'j' => $j, 51 'k' => $k, 52 }, 53 }, 54 'g' => "solo $g", 55 'l' => $l, 56 'r' => $r, 57 's' => $s, 58 't' => $t, 59 'w' => $w, 60 'n' => sub { $count }, 61 'up' => sub { ++$count }, 62 'down' => sub { --$count }, 63 'reset' => sub { $count = shift(@_) || 0 }, 64 'undef' => sub { undef }, 65 'zero' => sub { 0 }, 66 'one' => sub { 'one' }, 67 'halt' => sub { die Template::Exception->new('stop', 'stopped') }, 68 'join' => sub { join(shift, @_) }, 69 'split' => sub { my $s = shift; $s = quotemeta($s); 70 my @r = split(/$s/, shift); \@r }, 71 'magic' => { 72 'chant' => 'Hocus Pocus', 73 'spell' => sub { join(" and a bit of ", @_) }, 74 }, 75 'day' => { 76 'prev' => \&yesterday, 77 'this' => \&today, 78 'next' => \&tomorrow, 79 }, 80 'belief' => \&belief, 81 'people' => sub { return qw( Tom Dick Larry ) }, 82 'gee' => 'g', 83 "letter$a" => "'$a'", 84 'yankee' => \&yankee, 85 '_private' => 123, 86 '_hidden' => 456, 87 expose => sub { undef $Template::Stash::PRIVATE }, 88 add => sub { $_[0] + $_[1] }, 89 90 # don't define a 'z' - DEFAULT test relies on its non-existance 91}; 92 93my $tt = [ default => Template->new({ INTERPOLATE => 1, ANYCASE => 1 }), 94 notcase => Template->new({ INTERPOLATE => 1, ANYCASE => 0 }) ]; 95 96test_expect(\*DATA, $tt, $params); 97 98 99#------------------------------------------------------------------------ 100# subs 101#------------------------------------------------------------------------ 102 103sub yesterday { 104 return "All my troubles seemed so far away..."; 105} 106 107sub today { 108 my $when = shift || 'Now'; 109 return "$when it looks as though they're here to stay."; 110} 111 112sub tomorrow { 113 my $dayno = shift; 114 unless (defined $dayno) { 115 $day++; 116 $day %= 7; 117 $dayno = $day; 118 } 119 return $days[$dayno]; 120} 121 122sub belief { 123 my @beliefs = @_; 124 my $b = join(' and ', @beliefs); 125 $b = '<nothing>' unless length $b; 126 return "Oh I believe in $b."; 127} 128 129sub yankee { 130 my $a = []; 131 $a->[1] = { a => 1 }; 132 $a->[3] = { a => 2 }; 133 return $a; 134} 135 136__DATA__ 137 138#------------------------------------------------------------------------ 139# GET 140#------------------------------------------------------------------------ 141 142-- test -- 143[[% nosuchvariable %]] 144[$nosuchvariable] 145-- expect -- 146[] 147[] 148 149-- test -- 150[% a %] 151[% GET b %] 152[% get c %] 153-- expect -- 154alpha 155bravo 156charlie 157 158-- test -- 159[% b %] [% GET b %] 160-- expect -- 161bravo bravo 162 163-- test -- 164$a $b ${c} ${d} [% e %] 165-- expect -- 166alpha bravo charlie delta echo 167 168-- test -- 169[% letteralpha %] 170[% ${"letter$a"} %] 171[% GET ${"letter$a"} %] 172-- expect -- 173'alpha' 174'alpha' 175'alpha' 176 177-- test -- 178[% f.g %] [% f.$gee %] [% f.${gee} %] 179-- expect -- 180golf golf golf 181 182-- test -- 183[% GET f.h %] [% get f.h %] [% f.${'h'} %] [% get f.${'h'} %] 184-- expect -- 185hotel hotel hotel hotel 186 187-- test -- 188$f.h ${f.g} ${f.h}.gif 189-- expect -- 190hotel golf hotel.gif 191 192-- test -- 193[% f.i.j %] [% GET f.i.j %] [% get f.i.k %] 194-- expect -- 195juliet juliet kilo 196 197-- test -- 198[% f.i.j %] $f.i.k [% f.${'i'}.${"j"} %] ${f.i.k}.gif 199-- expect -- 200juliet kilo juliet kilo.gif 201 202-- test -- 203[% 'this is literal text' %] 204[% GET 'so is this' %] 205[% "this is interpolated text containing $r and $f.i.j" %] 206[% GET "$t?" %] 207[% "<a href=\"${f.i.k}.html\">$f.i.k</a>" %] 208-- expect -- 209this is literal text 210so is this 211this is interpolated text containing romeo and juliet 212tango? 213<a href="kilo.html">kilo</a> 214 215-- test -- 216[% name = "$a $b $w" -%] 217Name: $name 218-- expect -- 219Name: alpha bravo whisky 220 221-- test -- 222[% join('--', a b, c, f.i.j) %] 223-- expect -- 224alpha--bravo--charlie--juliet 225 226-- test -- 227[% text = 'The cat sat on the mat' -%] 228[% FOREACH word = split(' ', text) -%]<$word> [% END %] 229-- expect -- 230<The> <cat> <sat> <on> <the> <mat> 231 232-- test -- 233[% magic.chant %] [% GET magic.chant %] 234[% magic.chant('foo') %] [% GET magic.chant('foo') %] 235-- expect -- 236Hocus Pocus Hocus Pocus 237Hocus Pocus Hocus Pocus 238 239-- test -- 240<<[% magic.spell %]>> 241[% magic.spell(a b c) %] 242-- expect -- 243<<>> 244alpha and a bit of bravo and a bit of charlie 245 246-- test -- 247[% one %] [% one('two', 'three') %] [% one(2 3) %] 248-- expect -- 249one one one 250 251-- test -- 252[% day.prev %] 253[% day.this %] 254[% belief('yesterday') %] 255-- expect -- 256All my troubles seemed so far away... 257Now it looks as though they're here to stay. 258Oh I believe in yesterday. 259 260-- test -- 261Yesterday, $day.prev 262$day.this 263${belief('yesterday')} 264-- expect -- 265Yesterday, All my troubles seemed so far away... 266Now it looks as though they're here to stay. 267Oh I believe in yesterday. 268 269-- test -- 270-- use notcase -- 271[% day.next %] 272$day.next 273-- expect -- 274Monday 275Tuesday 276 277-- test -- 278[% FOREACH [ 1 2 3 4 5 ] %]$day.next [% END %] 279-- expect -- 280Wednesday Thursday Friday Saturday Sunday 281 282-- test -- 283-- use default -- 284before 285[% halt %] 286after 287 288-- expect -- 289before 290 291-- test -- 292[% FOREACH k = yankee -%] 293[% loop.count %]. [% IF k; k.a; ELSE %]undef[% END %] 294[% END %] 295-- expect -- 2961. undef 2972. 1 2983. undef 2994. 2 300 301 302#------------------------------------------------------------------------ 303# CALL 304#------------------------------------------------------------------------ 305 306-- test -- 307before [% CALL a %]a[% CALL b %]n[% CALL c %]d[% CALL d %] after 308-- expect -- 309before and after 310 311-- test -- 312..[% CALL undef %].. 313-- expect -- 314.... 315 316-- test -- 317..[% CALL zero %].. 318-- expect -- 319.... 320 321-- test -- 322..[% n %]..[% CALL n %].. 323-- expect -- 324..0.... 325 326-- test -- 327..[% up %]..[% CALL up %]..[% n %] 328-- expect -- 329..1....2 330 331-- test -- 332[% CALL reset %][% n %] 333-- expect -- 3340 335 336-- test -- 337[% CALL reset(100) %][% n %] 338-- expect -- 339100 340 341#------------------------------------------------------------------------ 342# SET 343#------------------------------------------------------------------------ 344 345-- test -- 346[% a = a %] $a 347[% a = b %] $a 348-- expect -- 349 alpha 350 bravo 351 352-- test -- 353[% SET a = a %] $a 354[% SET a = b %] $a 355[% SET a = $c %] [$a] 356[% SET a = $gee %] $a 357[% SET a = ${gee} %] $a 358-- expect -- 359 alpha 360 bravo 361 [] 362 solo golf 363 solo golf 364 365-- test -- 366[% a = b 367 b = c 368 c = d 369 d = e 370%][% a %] [% b %] [% c %] [% d %] 371-- expect -- 372bravo charlie delta echo 373 374-- test -- 375[% SET 376 a = c 377 b = d 378 c = e 379%]$a $b $c 380-- expect -- 381charlie delta echo 382 383-- test -- 384[% 'a' = d 385 'include' = e 386 'INCLUDE' = f.g 387%][% a %]-[% ${'include'} %]-[% ${'INCLUDE'} %] 388-- expect -- 389delta-echo-golf 390 391-- test -- 392[% a = f.g %] $a 393[% a = f.i.j %] $a 394-- expect -- 395 golf 396 juliet 397 398-- test -- 399[% f.g = r %] $f.g 400[% f.i.j = s %] $f.i.j 401[% f.i.k = f.i.j %] ${f.i.k} 402-- expect -- 403 romeo 404 sierra 405 sierra 406 407-- test -- 408[% user = { 409 id = 'abw' 410 name = 'Andy Wardley' 411 callsign = "[-$a-$b-$w-]" 412 } 413-%] 414${user.id} ${ user.id } $user.id ${user.id}.gif 415[% message = "$b: ${ user.name } (${user.id}) ${ user.callsign }" -%] 416MSG: $message 417-- expect -- 418abw abw abw abw.gif 419MSG: bravo: Andy Wardley (abw) [-alpha-bravo-whisky-] 420 421-- test -- 422[% product = { 423 id => 'XYZ-2000', 424 desc => 'Bogon Generator', 425 cost => 678, 426 } 427-%] 428The $product.id $product.desc costs \$${product.cost}.00 429-- expect -- 430The XYZ-2000 Bogon Generator costs $678.00 431 432-- test -- 433[% data => { 434 g => 'my data' 435 } 436 complex = { 437 gee => 'g' 438 } 439-%] 440[% data.${complex.gee} %] 441-- expect -- 442my data 443 444 445#------------------------------------------------------------------------ 446# DEFAULT 447#------------------------------------------------------------------------ 448 449-- test -- 450[% a %] 451[% DEFAULT a = b -%] 452[% a %] 453-- expect -- 454alpha 455alpha 456 457-- test -- 458[% a = '' -%] 459[% DEFAULT a = b -%] 460[% a %] 461-- expect -- 462bravo 463 464-- test -- 465[% a = '' b = '' -%] 466[% DEFAULT 467 a = c 468 b = d 469 z = r 470-%] 471[% a %] [% b %] [% z %] 472-- expect -- 473charlie delta romeo 474 475 476#------------------------------------------------------------------------ 477# 'global' vars 478#------------------------------------------------------------------------ 479 480-- test -- 481[% global.version = '3.14' -%] 482Version: [% global.version %] 483-- expect -- 484Version: 3.14 485 486-- test -- 487Version: [% global.version %] 488-- expect -- 489Version: 3.14 490 491-- test -- 492[% global.newversion = global.version + 1 -%] 493Version: [% global.version %] 494Version: [% global.newversion %] 495-- expect -- 496Version: 3.14 497Version: 4.14 498 499-- test -- 500Version: [% global.version %] 501Version: [% global.newversion %] 502-- expect -- 503Version: 3.14 504Version: 4.14 505 506-- test -- 507[% hash1 = { 508 foo => 'Foo', 509 bar => 'Bar', 510 } 511 hash2 = { 512 wiz => 'Wiz', 513 woz => 'Woz', 514 } 515-%] 516[% hash1.import(hash2) -%] 517keys: [% hash1.keys.sort.join(', ') %] 518-- expect -- 519keys: bar, foo, wiz, woz 520 521-- test -- 522[% mage = { name => 'Gandalf', 523 aliases => [ 'Mithrandir', 'Olorin', 'Incanus' ] } 524-%] 525[% import(mage) -%] 526[% name %] 527[% aliases.join(', ') %] 528-- expect -- 529Gandalf 530Mithrandir, Olorin, Incanus 531 532 533# test private variables 534-- test -- 535[[% _private %]][[% _hidden %]] 536-- expect -- 537[][] 538 539# make them visible 540-- test -- 541[% CALL expose -%] 542[[% _private %]][[% _hidden %]] 543-- expect -- 544[123][456] 545 546 547 548# Stas reported a problem with spacing in expressions but I can't 549# seem to reproduce it... 550-- test -- 551[% a = 4 -%] 552[% b=6 -%] 553[% c = a + b -%] 554[% d=a+b -%] 555[% c %]/[% d %] 556-- expect -- 55710/10 558 559-- test -- 560[% a = 1 561 b = 2 562 c = 3 563-%] 564[% d = 1+1 %]d: [% d %] 565[% e = a+b %]e: [% e %] 566-- expect -- 567d: 2 568e: 3 569 570 571# these tests check that the incorrect precedence in the parser has now 572# been fixed, thanks to Craig Barrat. 573-- test -- 574[% 1 || 0 && 0 # should be 1 || (0&&0), not (1||0)&&0 %] 575-- expect -- 5761 577 578-- test -- 579[% 1 + !0 + 1 # should be 1 + (!0) + 0, not 1 + !(0 + 1) %] 580-- expect -- 5813 582 583-- test -- 584[% "x" _ "y" == "y"; ',' # should be ("x"_"y")=="y", not "x"_("y"=="y") %] 585-- expect -- 586, 587 588-- test -- 589[% "x" _ "y" == "xy" # should be ("x"_"y")=="xy", not "x"_("y"=="xy") %] 590-- expect -- 5911 592 593-- test -- 594[% add(3, 5) %] 595-- expect -- 5968 597 598-- test -- 599[% add(3 + 4, 5 + 7) %] 600-- expect -- 60119 602 603-- test -- 604[% a = 10; 605 b = 20; 606 c = 30; 607 add(add(a,b+1),c*3); 608%] 609-- expect -- 610121 611 612-- test -- 613[% a = 10; 614 b = 20; 615 c = 30; 616 d = 5; 617 e = 7; 618 add(a+5, b < 10 ? c : d + e*5); 619-%] 620-- expect -- 62155 622 623