/home/.cpanm/work/1759492321.34807/Template-Toolkit-3.102/t
#============================================================= -*-perl-*- # # t/tiedhash.t # # Template script testing variable via a tied hash. # # Written by Andy Wardley <abw@wardley.org> # # Copyright (C) 1996-2001 Andy Wardley. All Rights Reserved. # Copyright (C) 1998-2001 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 warnings; # should not prove be responsible for this? use lib qw( blib/lib blib/arch lib ../blib/lib ../blib/arch ../lib ); use Template::Test; use Template::Stash; use Template::Config; use File::Spec; # MacOS Catalina won't allow Dynaloader to load from relative paths # Error: file system relative paths not allowed in hardened program @INC = map { File::Spec->rel2abs($_) } @INC; our $DEBUG = grep(/-d/, @ARGV); our $STORE_PREFIX = ''; our $FETCH_PREFIX = ''; # only run the test when compiled with Template::Stash if ( $Template::Config::STASH ne 'Template::Stash::XS' ) { skip_all('Template::Config is not using Template::Stash::XS'); } require Template::Stash::XS; #------------------------------------------------------------------------ package My::Tied::Hash; use Tie::Hash; use base 'Tie::StdHash'; sub FETCH { my ($hash, $key) = @_; print STDERR "FETCH($key)\n" if $main::DEBUG; my $val = $hash->{ $key }; return $val ? (ref $val ? $val : "$main::FETCH_PREFIX$val") : undef; } sub STORE { my ($hash, $key, $val) = @_; print STDERR "STORE($key, $val)\n" if $main::DEBUG; $hash->{ $key } = ref $val ? $val : "$main::STORE_PREFIX$val"; } #------------------------------------------------------------------------ package My::Tied::List; use Tie::Array; use base 'Tie::StdArray'; sub FETCH { my ($list, $n) = @_; print STDERR "FETCH from list [$n]\n" if $main::DEBUG; my $val = $list->[ $n ]; return $val ? (ref $val ? $val : "$main::FETCH_PREFIX$val") : undef; } sub STORE { my ($list, $n, $val) = @_; print STDERR "STORE to list [$n => $val]\n" if $main::DEBUG; $list->[$n] = ref $val ? $val : "$main::STORE_PREFIX$val"; } #------------------------------------------------------------------------ package main; # setup a tied hash and a tied list my @list; tie @list, 'My::Tied::List'; push(@list, 10, 20, 30); my %hash = (a => 'alpha'); tie %hash, 'My::Tied::Hash'; $hash{ a } = 'alpha'; $hash{ b } = 'bravo'; $hash{ zero } = 0; $hash{ one } = 1; # now turn on the prefixes so we can track items going in # and out of the tied hash/list $FETCH_PREFIX = 'FETCH:'; $STORE_PREFIX = 'STORE:'; my $data = { hash => \%hash, list => \@list, }; my $stash_perl = Template::Stash->new($data); my $stash_xs = Template::Stash::XS->new($data); my $tt = [ perl => Template->new( STASH => $stash_perl ), xs => Template->new( STASH => $stash_xs ), ]; test_expect(\*DATA, $tt); __DATA__ #------------------------------------------------------------------------ # first try with the Perl stash #------------------------------------------------------------------------ # hash tests -- test -- [% hash.a %] -- expect -- FETCH:alpha -- test -- [% hash.b %] -- expect -- FETCH:bravo -- test -- ready set:[% hash.c = 'cosmos' %] go:[% hash.c %] -- expect -- ready set: go:FETCH:STORE:cosmos -- test -- [% hash.foo.bar = 'one' -%] [% hash.foo.bar %] -- expect -- one # list tests -- test -- [% list.0 %] -- expect -- FETCH:10 -- test -- [% list.first %]-[% list.last %] -- expect -- FETCH:10-FETCH:30 -- test -- [% list.push(40); list.last %] -- expect -- FETCH:40 -- test -- [% list.4 = 50; list.4 %] -- expect -- FETCH:STORE:50 #------------------------------------------------------------------------ # now try using the XS stash #------------------------------------------------------------------------ # hash tests -- test -- -- use xs -- [% hash.a %] -- expect -- FETCH:alpha -- test -- [% hash.b %] -- expect -- FETCH:bravo -- test -- [% hash.c = 'crazy'; hash.c %] -- expect -- FETCH:STORE:crazy -- test -- [% DEFAULT hash.c = 'more crazy'; hash.c %] -- expect -- FETCH:STORE:crazy -- test -- [% hash.wiz = 'woz' -%] [% hash.wiz %] -- expect -- FETCH:STORE:woz -- test -- [% DEFAULT hash.zero = 'nothing'; hash.zero %] -- expect -- FETCH:STORE:nothing -- test -- before: [% hash.one %] after: [% DEFAULT hash.one = 'solitude'; hash.one %] -- expect -- before: FETCH:1 after: FETCH:1 -- test -- [% hash.foo = 10; hash.foo = 20; hash.foo %] -- expect -- FETCH:STORE:20 # this test should create an intermediate hash -- test -- [% DEFAULT hash.person = { }; hash.person.name = 'Arthur Dent'; hash.person.email = 'dent@tt2.org'; -%] name: [% hash.person.name %] email: [% hash.person.email %] -- expect -- name: Arthur Dent email: dent@tt2.org # list tests -- test -- [% list.0 %] -- expect -- FETCH:10 -- test -- [% list.first %]-[% list.last %] -- expect -- FETCH:10-FETCH:STORE:50 -- test -- [% list.push(60); list.last %] -- expect -- FETCH:60 -- test -- [% list.5 = 70; list.5 %] -- expect -- FETCH:STORE:70 -- test -- [% DEFAULT list.5 = 80; list.5 %] -- expect -- FETCH:STORE:70 -- test -- [% list.10 = 100; list.10 %] -- expect -- FETCH:STORE:100 -- test -- [% stuff = [ ]; stuff.0 = 'some stuff'; stuff.0 -%] -- expect -- some stuff
.
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