1#============================================================= -*-perl-*- 2# 3# t/vmethods/text.t 4# 5# Testing scalar (text) virtual variable methods. 6# 7# Written by Andy Wardley <abw@cpan.org> 8# 9# Copyright (C) 1996-2006 Andy Wardley. All Rights Reserved. 10# 11# This is free software; you can redistribute it and/or modify it 12# under the same terms as Perl itself. 13# 14# $Id$ 15# 16#======================================================================== 17 18use strict; 19use warnings; 20use lib qw( ./lib ../lib ../../lib ); 21use Template::Test; 22 23# make sure we're using the Perl stash 24$Template::Config::STASH = 'Template::Stash'; 25 26# define a new text method 27$Template::Stash::SCALAR_OPS->{ commify } = sub { 28 local $_ = shift; 29 my $c = shift || ","; 30 my $n = int(shift || 3); 31 return $_ if $n<1; 32 1 while s/^([-+]?\d+)(\d{$n})/$1$c$2/; 33 return $_; 34}; 35 36 37my $tt = Template->new(); 38my $tc = $tt->context(); 39 40# define vmethods using define_vmethod() interface. 41$tc->define_vmethod( item => 42 commas => 43 $Template::Stash::SCALAR_OPS->{ commify } ); 44 45my $params = { 46 undef => undef, 47 zero => 0, 48 one => 1, 49 animal => 'cat', 50 string => 'The cat sat on the mat', 51 spaced => ' The dog sat on the log', 52 word => 'bird', # The bird is the word 53 WORD => 'BIRD', 54 the_bird => "\n The bird\n is the word \n ", 55 quotable => "Tim O'Reilly said \"Oh really?\"", 56 markup => 'a < b > & c "d" \'e\'', 57}; 58 59test_expect(\*DATA, undef, $params); 60 61__DATA__ 62 63-- test -- 64-- name text.defined implicit undef -- 65[% notdef.defined ? 'def' : 'undef' %] 66-- expect -- 67undef 68 69-- test -- 70-- name text.defined explicit undef -- 71[% undef.defined ? 'def' : 'undef' %] 72-- expect -- 73undef 74 75-- test -- 76-- name text.defined zero -- 77[% zero.defined ? 'def' : 'undef' %] 78-- expect -- 79def 80 81-- test -- 82-- name text.defined one -- 83[% one.defined ? 'def' : 'undef' %] 84-- expect -- 85def 86 87-- test -- 88-- name string.length -- 89[% string.length %] 90-- expect -- 9122 92 93-- test -- 94-- name text.upper -- 95[% string.upper %] 96-- expect -- 97THE CAT SAT ON THE MAT 98 99-- test -- 100-- name text.lower -- 101[% string.lower %] 102-- expect -- 103the cat sat on the mat 104 105-- test -- 106-- name text.ucfirst -- 107[% word.ucfirst %] 108[% WORD.ucfirst %] 109[% WORD.lower.ucfirst %] 110-- expect -- 111Bird 112BIRD 113Bird 114 115-- test -- 116-- name text.lcfirst -- 117[% word.lcfirst %] 118[% WORD.lcfirst %] 119-- expect -- 120bird 121bIRD 122 123-- test -- 124-- name text.trim -- 125>[% the_bird.trim %]< 126-- expect -- 127>The bird 128 is the word< 129 130-- test -- 131-- name text.collapse -- 132>[% the_bird.collapse %]< 133-- expect -- 134>The bird is the word< 135 136-- test -- 137-- name text.sort.join -- 138[% string.sort.join %] 139-- expect -- 140The cat sat on the mat 141 142-- test -- 143-- name text.split.join a -- 144[% string.split.join('_') %] 145-- expect -- 146The_cat_sat_on_the_mat 147 148-- test -- 149-- name text.split.join b -- 150[% string.split(' ', 3).join('_') %] 151-- expect -- 152The_cat_sat on the mat 153 154-- test -- 155-- name text.split.join c -- 156[% spaced.split.join('_') %] 157-- expect -- 158The_dog_sat_on_the_log 159 160-- test -- 161-- name text.split.join d -- 162[% spaced.split(' ').join('_') %] 163-- expect -- 164__The_dog_sat_on_the_log 165 166-- test -- 167-- name text.list -- 168[% string.list.join %] 169-- expect -- 170The cat sat on the mat 171 172-- test -- 173-- name text.hash -- 174[% string.hash.value %] 175-- expect -- 176The cat sat on the mat 177 178-- test -- 179-- name text.size -- 180[% string.size %] 181-- expect -- 1821 183 184-- test -- 185-- name text.squote -- 186[% quotable %] 187[% quotable.squote %] 188-- expect -- 189Tim O'Reilly said "Oh really?" 190Tim O\'Reilly said "Oh really?" 191 192-- test -- 193-- name text.dquote -- 194[% quotable %] 195[% quotable.dquote %] 196-- expect -- 197Tim O'Reilly said "Oh really?" 198Tim O'Reilly said \"Oh really?\" 199 200-- test -- 201-- name text.html -- 202[% markup.html %] 203-- expect -- 204a < b > & c "d" 'e' 205 206-- test -- 207-- name text.xml -- 208[% markup.xml %] 209-- expect -- 210a < b > & c "d" 'e' 211 212 213-- test -- 214-- name text.repeat -- 215[% animal.repeat(3) %] 216-- expect -- 217catcatcat 218 219-- test -- 220-- name text.search -- 221[% animal.search('at$') ? "found 'at\$'" : "didn't find 'at\$'" %] 222-- expect -- 223found 'at$' 224 225-- test -- 226-- name text.search -- 227[% animal.search('^at') ? "found '^at'" : "didn't find '^at'" %] 228-- expect -- 229didn't find '^at' 230 231-- test -- 232-- name text.match an -- 233[% text = 'bandanna'; 234 text.match('an') ? 'match' : 'no match' 235%] 236-- expect -- 237match 238 239-- test -- 240-- name text.match on -- 241[% text = 'bandanna'; 242 text.match('on') ? 'match' : 'no match' 243%] 244-- expect -- 245no match 246 247-- test -- 248-- name text.match global an -- 249[% text = 'bandanna'; 250 text.match('an', 1).size %] matches 251-- expect -- 2522 matches 253 254-- test -- 255-- name text.match global an -- 256[% text = 'bandanna' -%] 257matches are [% text.match('an+', 1).join(', ') %] 258-- expect -- 259matches are an, ann 260 261-- test -- 262-- name text.match global on -- 263[% text = 'bandanna'; 264 text.match('on+', 1) ? 'match' : 'no match' 265%] 266-- expect -- 267no match 268 269-- test -- 270-- name: text substr method -- 271[% text = 'Hello World' -%] 272a: [% text.substr(6) %]! 273b: [% text.substr(0, 5) %]! 274c: [% text.substr(0, 5, 'Goodbye') %]! 275d: [% text %]! 276-- expect -- 277a: World! 278b: Hello! 279c: Goodbye World! 280d: Hello World! 281 282-- test -- 283-- name: another text substr method -- 284[% text = 'foo bar baz wiz waz woz' -%] 285a: [% text.substr(4, 3) %] 286b: [% text.substr(12) %] 287c: [% text.substr(0, 11, 'FOO') %] 288d: [% text %] 289-- expect -- 290a: bar 291b: wiz waz woz 292c: FOO wiz waz woz 293d: foo bar baz wiz waz woz 294 295 296-- test -- 297-- name: text.remove -- 298[% text = 'hello world!'; 299 text.remove('\s+world') 300%] 301-- expect -- 302hello! 303 304 305 306-- test -- 307-- name chunk left -- 308[% string = 'TheCatSatTheMat' -%] 309[% string.chunk(3).join(', ') %] 310-- expect -- 311The, Cat, Sat, The, Mat 312 313-- test -- 314-- name chunk leftover -- 315[% string = 'TheCatSatonTheMat' -%] 316[% string.chunk(3).join(', ') %] 317-- expect -- 318The, Cat, Sat, onT, heM, at 319 320-- test -- 321-- name chunk right -- 322[% string = 'TheCatSatTheMat' -%] 323[% string.chunk(-3).join(', ') %] 324-- expect -- 325The, Cat, Sat, The, Mat 326 327-- test -- 328-- name chunk rightover -- 329[% string = 'TheCatSatonTheMat' -%] 330[% string.chunk(-3).join(', ') %] 331-- expect -- 332Th, eCa, tSa, ton, The, Mat 333 334-- test -- 335-- name chunk ccard -- 336[% ccard_no = "1234567824683579"; 337 ccard_no.chunk(4).join 338%] 339-- expect -- 3401234 5678 2468 3579 341 342 343-- test -- 344[% string = 'foo' -%] 345[% string.repeat(3) %] 346-- expect -- 347foofoofoo 348 349-- test -- 350[% string1 = 'foobarfoobarfoo' 351 string2 = 'foobazfoobazfoo' 352-%] 353[% string1.search('bar') ? 'ok' : 'not ok' %] 354[% string2.search('bar') ? 'not ok' : 'ok' %] 355[% string1.replace('bar', 'baz') %] 356[% string2.replace('baz', 'qux') %] 357-- expect -- 358ok 359ok 360foobazfoobazfoo 361fooquxfooquxfoo 362 363-- test -- 364[% string1 = 'foobarfoobarfoo' 365 string2 = 'foobazfoobazfoo' 366-%] 367[% string1.match('bar') ? 'ok' : 'not ok' %] 368[% string2.match('bar') ? 'not ok' : 'ok' %] 369-- expect -- 370ok 371ok 372 373-- test -- 374[% string = 'foo bar ^%$ baz' -%] 375[% string.replace('\W+', '_') %] 376-- expect -- 377foo_bar_baz 378 379-- test -- 380[% var = 'value99' ; 381 var.replace('value', '') 382%] 383-- expect -- 38499 385 386-- test -- 387[% bob = "0" -%] 388bob: [% bob.replace('0','') %]. 389-- expect -- 390bob: . 391 392-- test -- 393[% string = 'The cat sat on the mat'; 394 match = string.match('The (\w+) (\w+) on the (\w+)'); 395-%] 396[% match.0 %].[% match.1 %]([% match.2 %]) 397-- expect -- 398cat.sat(mat) 399 400-- test -- 401[% string = 'The cat sat on the mat' -%] 402[% IF (match = string.match('The (\w+) sat on the (\w+)')) -%] 403matched animal: [% match.0 %] place: [% match.1 %] 404[% ELSE -%] 405no match 406[% END -%] 407[% IF (match = string.match('The (\w+) shat on the (\w+)')) -%] 408matched animal: [% match.0 %] place: [% match.1 %] 409[% ELSE -%] 410no match 411[% END -%] 412-- expect -- 413matched animal: cat place: mat 414no match 415 416 417-- test -- 418[% big_num = "1234567890"; big_num.commify %] 419-- expect -- 4201,234,567,890 421 422-- test -- 423[% big_num = "1234567890"; big_num.commify(":", 2) %] 424-- expect -- 42512:34:56:78:90 426 427-- test -- 428[% big_num = "1234567812345678"; big_num.commify(" ", 4) %] 429-- expect -- 4301234 5678 1234 5678 431 432-- test -- 433[% big_num = "hello world"; big_num.commify %] 434-- expect -- 435hello world 436 437-- test -- 438[% big_num = "1234567890"; big_num.commas %] 439-- expect -- 4401,234,567,890 441 442