/home/.cpanm/work/1759492321.34807/Template-Toolkit-3.102/t
#============================================================= -*-perl-*- # # t/constants.t # # Test constant folding via Template::Namespace::Constants # # Written by Andy Wardley <abw@andywardley.com> # # Copyright (C) 1996-2002 Andy Wardley. All Rights Reserved. # Copyright (C) 1998-2002 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$ # #======================================================================== use strict; use lib qw( ./lib ../lib ); use Template::Test; use Template::Stash; use Template::Directive; use Template::Parser; use Template::Namespace::Constants; my $DEBUG = grep(/-d/, @ARGV); $Template::Namespace::Constants::DEBUG = $DEBUG; my $n = 0; my $constants = { author => 'Andy \'Da Man\' Wardley', single => 'foo\'bar', double => "foo'bar", joint => ', ', col => { back => '#ffffff', text => '#000000', }, counter => sub { $n++ }, }; my $namespace = Template::Namespace::Constants->new( $constants ); ok( $namespace, 'created constants namespace' ); is( $namespace->ident([ 'constants', 0, "'author'", 0 ]), q{'Andy \'Da Man\' Wardley'}, 'author match' ); is( $namespace->ident([ 'constants', 0, "'single'", 0 ]), "'foo\\'bar'", 'single match' ); is( $namespace->ident([ 'constants', 0, "'double'", 0 ]), "'foo\\'bar'", 'double match' ); is( $namespace->ident([ 'constants', 0, "'col'", 0, "'back'", 0 ]), "'#ffffff'", 'col.back match' ); is( $namespace->ident([ 'constants', 0, "'col'", 0, "'text'", 0 ]), "'#000000'", 'col.text match' ); my $factory = Template::Directive->new({ NAMESPACE => { const => $namespace, } }); ok( $factory, 'created Template::Directive factory' ); my $parser = Template::Parser->new( FACTORY => $factory ); ok( $parser, 'created Template::Parser parser' ); my $parsed = $parser->parse(<<EOF); hello [% const.author %] [% "back is \$const.col.back" %] and text is [% const.col.text %] but a col is still a [% col.user %] EOF die "parser error: ", $parser->error(), "\n" unless $parsed; my $text = $parsed->{ BLOCK }; ok( scalar $text =~ /'Andy \\'Da Man\\' Wardley'/, 'author folded' ); ok( scalar $text =~ /'back is ' . '#ffffff'/, 'col.back folded' ); ok( scalar $text =~ /stash->get\(\['col', 0, 'user', 0\]\)/, 'col.user unfolded' ); $parser = Template::Parser->new({ NAMESPACE => { const => $namespace, } }); ok( $parser, 'created Template::Parser parser' ); $parsed = $parser->parse(<<EOF); hello [% const.author %] [% "back is \$const.col.back" %] and text is [% const.col.text %] but a col is still a [% col.user %] EOF die "parser error: ", $parser->error(), "\n" unless $parsed; $text = $parsed->{ BLOCK }; ok( scalar $text =~ /'Andy \\'Da Man\\' Wardley'/, 'author folded' ); ok( scalar $text =~ /'back is ' . '#ffffff'/, 'col.back folded' ); ok( scalar $text =~ /stash->get\(\['col', 0, 'user', 0\]\)/, 'col.user unfolded' ); #------------------------------------------------------------------------ my $tt1 = Template->new({ NAMESPACE => { const => $namespace, }, }); ok( $tt1, 'created tt1' ); my $const2 = { author => 'abw', joint => ' is the new ', col => { back => 'orange', text => 'black', }, fave => 'back', }; my $tt2 = Template->new({ CONSTANTS => $const2, }); ok( $tt2, 'created tt2' ); my $tt3 = Template->new({ CONSTANTS => $const2, CONSTANTS_NAMESPACE => 'const', }); ok( $tt3, 'created tt3' ); my $engines = [ tt1 => $tt1, tt2 => $tt2, tt3 => $tt3 ]; my $vars = { col => { user => 'red', luza => 'blue', }, constants => $constants, }; test_expect(\*DATA, $engines, $vars); __DATA__ -- test -- hello [% const.author %] [% "back is $const.col.back" %] and text is [% const.col.text %] col.user is [% col.user %] -- expect -- hello Andy 'Da Man' Wardley back is #ffffff and text is #000000 col.user is red -- test -- # look ma! I can even call virtual methods on contants! [% const.col.keys.sort.join(', ') %] -- expect -- back, text -- test -- # and even pass constant arguments to constant virtual methods! [% const.col.keys.sort.join(const.joint) %] -- expect -- back, text -- test -- # my constants can be subs, etc. zero [% const.counter %] one [% const.counter %] -- expect -- zero 0 one 1 -- test -- -- use tt2 -- [% "$constants.author thinks " %] [%- constants.col.values.sort.reverse.join(constants.joint) %] -- expect -- abw thinks orange is the new black -- test -- -- use tt3 -- [% "$const.author thinks " -%] [% const.col.values.sort.reverse.join(const.joint) %] -- expect -- abw thinks orange is the new black -- test -- -- name no const.foo -- no [% const.foo %]? -- expect -- no ? -- test -- fave [% const.fave %] col [% const.col.${const.fave} %] -- expect -- fave back col orange -- test -- -- use tt2 -- -- name defer references -- [% "$key\n" FOREACH key = constants.col.keys.sort %] -- expect -- back text -- test -- -- use tt3 -- a: [% const.author %] b: [% const.author = 'Fred Smith' %] c: [% const.author %] -- expect -- a: abw b: c: abw
.
Edit
..
Edit
README
Edit
anycase.t
Edit
args.t
Edit
assert.t
Edit
base.t
Edit
binop.t
Edit
block.t
Edit
block_duplicate.t
Edit
blocks.t
Edit
capture.t
Edit
case.t
Edit
chomp.t
Edit
compile1.t
Edit
compile2.t
Edit
compile3.t
Edit
compile4.t
Edit
compile5.t
Edit
config.t
Edit
constants.t
Edit
context.t
Edit
datafile.t
Edit
date.t
Edit
date_offset.t
Edit
date_utf8.t
Edit
debug.t
Edit
directive.t
Edit
directry.t
Edit
document.t
Edit
dumper.t
Edit
error.t
Edit
evalperl.t
Edit
exception.t
Edit
factory.t
Edit
file.t
Edit
fileline.t
Edit
filter.t
Edit
foreach.t
Edit
format.t
Edit
html.t
Edit
image.t
Edit
include.t
Edit
iterator.t
Edit
leak.t
Edit
lib
Edit
list.t
Edit
load_order_vmethods_stash.t
Edit
macro.t
Edit
math.t
Edit
math_rand.t
Edit
meta.t
Edit
mtime-zero.t
Edit
object.t
Edit
outline.t
Edit
outline_line.t
Edit
output.t
Edit
parser.t
Edit
parser2.t
Edit
plugins.t
Edit
plusfile.t
Edit
pod.t
Edit
prefix.t
Edit
proc.t
Edit
process-relative.t
Edit
process.t
Edit
process_dir.t
Edit
provider.t
Edit
ref.t
Edit
scalar.t
Edit
service.t
Edit
skel.t
Edit
stash-xs-unicode.t
Edit
stash-xs.t
Edit
stash.t
Edit
stashc.t
Edit
stop.t
Edit
strcat.t
Edit
strict.t
Edit
string.t
Edit
switch.t
Edit
table.t
Edit
tags.t
Edit
template.t
Edit
test
Edit
text.t
Edit
throw.t
Edit
tiedhash.t
Edit
trace_vars.t
Edit
try.t
Edit
unicode.t
Edit
url.t
Edit
vars.t
Edit
varsv1.t
Edit
view.t
Edit
vmethods
Edit
while.t
Edit
wrap.t
Edit
wrapper.t
Edit
zz-plugin-cycle.t
Edit
zz-plugin-leak-gh-213.t
Edit
zz-plugin-leak-rt-46691.t
Edit
zz-plugin-leak.t
Edit
zz-pmv.t
Edit
zz-pod-coverage.t
Edit
zz-pod-kwalitee.t
Edit
zz-stash-xs-leak.t
Edit
zz-url2.t
Edit