1#============================================================= -*-perl-*- 2# 3# t/parser.t 4# 5# Test the Template::Parser module. 6# 7# Written by Andy Wardley <abw@kfs.org> 8# 9# Copyright (C) 1996-2000 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 lib qw( . ../lib ); 21use Template::Test; 22use Template::Config; 23use Template::Parser; 24$^W = 1; 25 26#$Template::Test::DEBUG = 0; 27#$Template::Test::PRESERVE = 1; 28#$Template::Stash::DEBUG = 1; 29#$Template::Parser::DEBUG = 1; 30#$Template::Directive::PRETTY = 1; 31 32my $p2 = Template::Parser->new({ 33 START_TAG => '\[\*', 34 END_TAG => '\*\]', 35 ANYCASE => 1, 36 PRE_CHOMP => 1, 37 V1DOLLAR => 1, 38}); 39 40# test new/old styles 41my $s1 = $p2->new_style( { TAG_STYLE => 'metatext', PRE_CHOMP => 0, POST_CHOMP => 1 } ) 42 || die $p2->error(); 43ok( $s1 ); 44match( $s1->{ START_TAG }, '%%' ); 45match( $s1->{ PRE_CHOMP }, '0' ); 46match( $s1->{ POST_CHOMP }, '1' ); 47 48#print STDERR "style: { ", join(', ', map { "$_ => $s1->{ $_ }" } keys %$s1), " }\n"; 49 50my $s2 = $p2->old_style() 51 || die $p2->error(); 52ok( $s2 ); 53match( $s2->{ START_TAG }, '\[\*' ); 54match( $s2->{ PRE_CHOMP }, '1' ); 55match( $s2->{ POST_CHOMP }, '0' ); 56 57#print STDERR "style: { ", join(', ', map { "$_ => $s2->{ $_ }" } keys %$s2), " }\n"; 58 59my $p3 = Template::Config->parser({ 60 TAG_STYLE => 'html', 61 POST_CHOMP => 1, 62 ANYCASE => 1, 63 INTERPOLATE => 1, 64}); 65 66my $p4 = Template::Config->parser({ 67 ANYCASE => 0, 68}); 69 70my $tt = [ 71 tt1 => Template->new(ANYCASE => 1), 72 tt2 => Template->new(PARSER => $p2), 73 tt3 => Template->new(PARSER => $p3), 74 tt4 => Template->new(PARSER => $p4), 75]; 76 77my $replace = &callsign; 78$replace->{ alist } = [ 'foo', 0, 'bar', 0 ]; 79$replace->{ wintxt } = "foo\r\n\r\nbar\r\n\r\nbaz"; 80$replace->{ data } = { first => 11, last => 42 }; 81 82test_expect(\*DATA, $tt, $replace); 83 84__DATA__ 85#------------------------------------------------------------------------ 86# tt1 87#------------------------------------------------------------------------ 88-- test -- 89start $a 90[% BLOCK a %] 91this is a 92[% END %] 93=[% INCLUDE a %]= 94=[% include a %]= 95end 96-- expect -- 97start $a 98 99= 100this is a 101= 102= 103this is a 104= 105end 106 107-- test -- 108[% data.first; ' to '; data.last %] 109-- expect -- 11011 to 42 111 112 113#------------------------------------------------------------------------ 114# tt2 115#------------------------------------------------------------------------ 116-- test -- 117-- use tt2 -- 118begin 119[% this will be ignored %] 120[* a *] 121end 122-- expect -- 123begin 124[% this will be ignored %]alpha 125end 126 127-- test -- 128$b does nothing: 129[* c = 'b'; 'hello' *] 130stuff: 131[* $c *] 132-- expect -- 133$b does nothing: hello 134stuff: b 135 136#------------------------------------------------------------------------ 137# tt3 138#------------------------------------------------------------------------ 139-- test -- 140-- use tt3 -- 141begin 142[% this will be ignored %] 143<!-- a --> 144end 145 146-- expect -- 147begin 148[% this will be ignored %] 149alphaend 150 151-- test -- 152$b does something: 153<!-- c = 'b'; 'hello' --> 154stuff: 155<!-- $c --> 156end 157-- expect -- 158bravo does something: 159hellostuff: 160bravoend 161 162 163#------------------------------------------------------------------------ 164# tt4 165#------------------------------------------------------------------------ 166-- test -- 167-- use tt4 -- 168start $a[% 'include' = 'hello world' %] 169[% BLOCK a -%] 170this is a 171[%- END %] 172=[% INCLUDE a %]= 173=[% include %]= 174end 175-- expect -- 176start $a 177 178=this is a= 179=hello world= 180end 181 182 183#------------------------------------------------------------------------ 184-- test -- 185[% sql = " 186 SELECT * 187 FROM table" 188-%] 189SQL: [% sql %] 190-- expect -- 191SQL: 192 SELECT * 193 FROM table 194 195-- test -- 196[% a = "\a\b\c\ndef" -%] 197a: [% a %] 198-- expect -- 199a: abc 200def 201 202-- test -- 203[% a = "\f\o\o" 204 b = "a is '$a'" 205 c = "b is \$100" 206-%] 207a: [% a %] b: [% b %] c: [% c %] 208 209-- expect -- 210a: foo b: a is 'foo' c: b is $100 211 212-- test -- 213[% tag = { 214 a => "[\%" 215 z => "%\]" 216 } 217 quoted = "[\% INSERT foo %\]" 218-%] 219A directive looks like: [% tag.a %] INCLUDE foo [% tag.z %] 220The quoted value is [% quoted %] 221 222-- expect -- 223A directive looks like: [% INCLUDE foo %] 224The quoted value is [% INSERT foo %] 225 226-- test -- 227=[% wintxt | replace("(\r\n){2,}", "\n<break>\n") %] 228 229-- expect -- 230=foo 231<break> 232bar 233<break> 234baz 235 236-- test -- 237[% nl = "\n" 238 tab = "\t" 239-%] 240blah blah[% nl %][% tab %]x[% nl; tab %]y[% nl %]end 241-- expect -- 242blah blah 243 x 244 y 245end 246 247 248#------------------------------------------------------------------------ 249# STOP RIGHT HERE! 250#------------------------------------------------------------------------ 251 252-- stop -- 253 254-- test -- 255alist: [% $alist %] 256-- expect -- 257alist: ?? 258 259-- test -- 260[% foo.bar.baz %] 261