1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203
|
#============================================================= -*-perl-*-
#
# t/vmethods/replace.t
#
# Testing the 'replace' scalar virtual method, and in particular the
# use of backreferences.
#
# Written by Andy Wardley <abw@cpan.org> and Sergey Martynoff
# <sergey@martynoff.info>
#
# 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 ../../lib );
use Template::Test;
use Template::Config;
use Template::Stash;
$^W = 1;
# make sure we're using the Perl stash
$Template::Config::STASH = 'Template::Stash';
test_expect(\*DATA);
__DATA__
-- test --
-- name: two backrefs --
[% text = 'The cat sat on the mat';
text.replace( '(\w+) sat on the (\w+)',
'dirty $1 shat on the filthy $2' )
%]
-- expect --
The dirty cat shat on the filthy mat
# test more than 9 captures to make sure $10, $11, etc., work ok
-- test --
-- name: ten+ backrefs --
[% text = 'one two three four five six seven eight nine ten eleven twelve thirteen';
text.replace(
'(\w+) (\w+) (\w+) (\w+) (\w+) (\w+) (\w+) (\w+) (\w+) (\w+) (\w+) (\w+)',
'[$12-$11-$10-$9-$8-$7-$6-$5-$4-$3-$2-$1]'
)
%]
-- expect --
[twelve-eleven-ten-nine-eight-seven-six-five-four-three-two-one] thirteen
-- test --
-- name: repeat backrefs --
[% text = 'one two three four five six seven eight nine ten eleven twelve thirteen';
text.replace(
'(\w+) ',
'[$1]-'
)
%]
-- expect --
[one]-[two]-[three]-[four]-[five]-[six]-[seven]-[eight]-[nine]-[ten]-[eleven]-[twelve]-thirteen
-- test --
-- name: one backref --
[% var = 'foo'; var.replace('f(o+)$', 'b$1') %]
-- expect --
boo
-- test --
-- name: three backrefs --
[% var = 'foo|bar/baz'; var.replace('(fo+)\|(bar)(.*)$', '[ $1, $2, $3 ]') %]
-- expect --
[ foo, bar, /baz ]
#------------------------------------------------------------------------
# tests based on Sergey's test script: http://martynoff.info/tt2/
#------------------------------------------------------------------------
-- test --
[% text = 'foo bar';
text.replace('foo', 'bar')
%]
-- expect --
bar bar
-- test --
[% text = 'foo bar';
text.replace('(f)(o+)', '$2$1')
%]
-- expect --
oof bar
-- test --
[% text = 'foo bar foo';
text.replace('(?i)FOO', 'zoo')
%]
-- expect --
zoo bar zoo
#------------------------------------------------------------------------
# references to $n vars that don't exists are ignored
#------------------------------------------------------------------------
-- test --
[% text = 'foo bar';
text.replace('(f)(o+)', '$20$1')
%]
-- expect --
f bar
-- test --
[% text = 'foo bar';
text.replace('(f)(o+)', '$2$10')
%]
-- expect --
oo bar
-- test --
[% text = 'foo fgoo foooo bar';
text.replace('((?:f([^o]*)(o+)\s)+)', '1=$1;2=$2;3=$3;')
%]
-- expect --
1=foo fgoo foooo ;2=;3=oooo;bar
#------------------------------------------------------------------------
# $n in source string should not be interpolated
#------------------------------------------------------------------------
-- test --
[% text = 'foo $1 bar';
text.replace('(foo)(.*)(bar)', '$1$2$3')
%]
-- expect --
foo $1 bar
-- test --
[% text = 'foo $1 bar';
text.replace('(foo)(.*)(bar)', '$3$2$1')
%]
-- expect --
bar $1 foo
-- test --
[% text = 'foo $200bar foobar';
text.replace('(f)(o+)', 'zoo')
%]
-- expect --
zoo $200bar zoobar
#------------------------------------------------------------------------
# escaped \$ in replacement string
#------------------------------------------------------------------------
-- test --
-- name: escape dollar --
[% text = 'foo bar';
text.replace('(f)(o+)', '\\$2$1')
%]
-- expect --
$2f bar
-- test --
-- name: escape backslash --
[% text = 'foo bar';
text.replace('(f)(o+)', 'x$1\\\\y$2'); # this is 'x$1\\y$2'
%]
-- expect --
xf\yoo bar
-- test --
-- name: backslash again --
[% text = 'foo bar';
text.replace('(f)(o+)', '$2\\\\$1'); # this is '$2\\$1'
%]
-- expect --
oo\f bar
-- test --
-- name: escape all over --
[% text = 'foo bar';
text.replace('(f)(o+)', '$2\\\\\\$1'); # this is '$2\\\$')
%]
-- expect --
oo\$1 bar
-- test --
[% text = 'foo bar foobar';
text.replace('(o)|([ar])', '$2!')
%]
-- expect --
f!! ba!r! f!!ba!r!
|