1#!/usr/bin/perl -w 2#============================================================= -*-perl-*- 3# 4# t/string.t 5# 6# Test the String plugin 7# 8# Written by Andy Wardley <abw@kfs.org> 9# 10# Copyright (C) 1996-2000 Andy Wardley. All Rights Reserved. 11# Copyright (C) 1998-2000 Canon Research Centre Europe Ltd. 12# 13# This is free software; you can redistribute it and/or modify it 14# under the same terms as Perl itself. 15# 16# $Id$ 17# 18#======================================================================== 19 20use strict; 21use lib qw( ./lib ../lib ); 22use Template::Test; 23use Template::Plugin::String; 24 25my $DEBUG = grep /-d/, @ARGV; 26#$Template::Parser::DEBUG = $DEBUG; 27#$Template::Directive::PRETTY = $DEBUG; 28 29test_expect(\*DATA); 30 31__DATA__ 32-- test -- 33[% USE String -%] 34string: [[% String.text %]] 35-- expect -- 36string: [] 37 38-- test -- 39[% USE String 'hello world' -%] 40string: [[% String.text %]] 41-- expect -- 42string: [hello world] 43 44-- test -- 45[% USE String text='hello world' -%] 46string: [[% String.text %]] 47-- expect -- 48string: [hello world] 49 50-- test -- 51[% USE String -%] 52string: [[% String %]] 53-- expect -- 54string: [] 55 56-- test -- 57[% USE String 'hello world' -%] 58string: [[% String %]] 59-- expect -- 60string: [hello world] 61 62-- test -- 63[% USE String text='hello world' -%] 64string: [[% String %]] 65-- expect -- 66string: [hello world] 67 68-- test -- 69[% USE String text='hello' -%] 70string: [[% String.append(' world') %]] 71string: [[% String %]] 72-- expect -- 73string: [hello world] 74string: [hello world] 75 76-- test -- 77[% USE String text='hello' -%] 78[% copy = String.copy -%] 79string: [[% String %]] 80string: [[% copy %]] 81-- expect -- 82string: [hello] 83string: [hello] 84 85-- test -- 86[% USE String -%] 87[% hi = String.new('hello') -%] 88[% lo = String.new('world') -%] 89[% hw = String.new(text="$hi $lo") -%] 90hi: [[% hi %]] 91lo: [[% lo %]] 92hw: [[% hw %]] 93-- expect -- 94hi: [hello] 95lo: [world] 96hw: [hello world] 97 98-- test -- 99[% USE hi = String 'hello' -%] 100[% lo = hi.new('world') -%] 101hi: [[% hi %]] 102lo: [[% lo %]] 103-- expect -- 104hi: [hello] 105lo: [world] 106 107-- test -- 108[% USE hi = String 'hello' -%] 109[% lo = hi.copy -%] 110hi: [[% hi %]] 111lo: [[% lo %]] 112-- expect -- 113hi: [hello] 114lo: [hello] 115 116-- test -- 117[% USE hi = String 'hello' -%] 118[% lo = hi.copy.append(' world') -%] 119hi: [[% hi %]] 120lo: [[% lo %]] 121-- expect -- 122hi: [hello] 123lo: [hello world] 124 125-- test -- 126[% USE hi = String 'hello' -%] 127[% lo = hi.new('hey').append(' world') -%] 128hi: [[% hi %]] 129lo: [[% lo %]] 130-- expect -- 131hi: [hello] 132lo: [hey world] 133 134-- test -- 135[% USE hi=String "hello world\n" -%] 136hi: [[% hi %]] 137[% lo = hi.chomp -%] 138hi: [[% hi %]] 139lo: [[% lo %]] 140-- expect -- 141hi: [hello world 142] 143hi: [hello world] 144lo: [hello world] 145 146-- test -- 147[% USE foo=String "foop" -%] 148[[% foo.chop %]] 149[[% foo.chop %]] 150-- expect -- 151[foo] 152[fo] 153 154-- test -- 155[% USE hi=String "hello" -%] 156 left: [[% hi.copy.left(11) %]] 157 right: [[% hi.copy.right(11) %]] 158center: [[% hi.copy.center(11) %]] 159centre: [[% hi.copy.centre(12) %]] 160-- expect -- 161 left: [hello ] 162 right: [ hello] 163center: [ hello ] 164centre: [ hello ] 165 166-- test -- 167[% USE str=String('hello world') -%] 168 hi: [[% str.upper %]] 169 hi: [[% str %]] 170 lo: [[% str.lower %]] 171cap: [[% str.capital %]] 172-- expect -- 173 hi: [HELLO WORLD] 174 hi: [HELLO WORLD] 175 lo: [hello world] 176cap: [Hello world] 177 178-- test -- 179[% USE str=String('hello world') -%] 180len: [[% str.length %]] 181-- expect -- 182len: [11] 183 184-- test -- 185[% USE str=String(" \n\n\t\r hello\nworld\n\r \n \r") -%] 186[[% str.trim %]] 187-- expect -- 188[hello 189world] 190 191-- test -- 192[% USE str=String(" \n\n\t\r hello \n \n\r world\n\r \n \r") -%] 193[[% str.collapse %]] 194-- expect -- 195[hello world] 196 197-- test -- 198[% USE str=String("hello") -%] 199[[% str.append(' world') %]] 200[[% str.prepend('well, ') %]] 201-- expect -- 202[hello world] 203[well, hello world] 204 205-- test -- 206[% USE str=String("hello") -%] 207[[% str.push(' world') %]] 208[[% str.unshift('well, ') %]] 209-- expect -- 210[hello world] 211[well, hello world] 212 213-- test -- 214[% USE str=String('foo bar') -%] 215[[% str.copy.pop(' bar') %]] 216[[% str.copy.shift('foo ') %]] 217-- expect -- 218[foo] 219[bar] 220 221-- test -- 222[% USE str=String('Hello World') -%] 223[[% str.copy.truncate(5) %]] 224[[% str.copy.truncate(8, '...') %]] 225[[% str.copy.truncate(20, '...') %]] 226-- expect -- 227[Hello] 228[Hello...] 229[Hello World] 230 231-- test -- 232[% USE String('foo') -%] 233[[% String.append(' ').repeat(4) %]] 234-- expect -- 235[foo foo foo foo ] 236 237-- test -- 238[% USE String('foo') -%] 239[% String.format("[%s]") %] 240-- expect -- 241[foo] 242 243-- test -- 244[% USE String('foo bar foo baz') -%] 245[[% String.replace('foo', 'oof') %]] 246-- expect -- 247[oof bar oof baz] 248 249-- test -- 250[% USE String('foo bar foo baz') -%] 251[[% String.copy.remove('foo\s*') %]] 252[[% String.copy.remove('ba[rz]\s*') %]] 253-- expect -- 254[bar baz] 255[foo foo ] 256 257-- test -- 258[% USE String('foo bar foo baz') -%] 259[[% String.split.join(', ') %]] 260-- expect -- 261[foo, bar, foo, baz] 262 263-- test -- 264[% USE String('foo bar foo baz') -%] 265[[% String.split(' bar ').join(', ') %]] 266-- expect -- 267[foo, foo baz] 268 269-- test -- 270[% USE String('foo bar foo baz') -%] 271[[% String.split(' bar ').join(', ') %]] 272-- expect -- 273[foo, foo baz] 274 275-- test -- 276[% USE String('foo bar foo baz') -%] 277[[% String.split('\s+').join(', ') %]] 278-- expect -- 279[foo, bar, foo, baz] 280 281-- test -- 282[% USE String('foo bar foo baz') -%] 283[[% String.split('\s+', 2).join(', ') %]] 284-- expect -- 285[foo, bar foo baz] 286 287 288-- test -- 289[% USE String('foo bar foo baz') -%] 290[% String.search('foo') ? 'ok' : 'not ok' %] 291[% String.search('fooz') ? 'not ok' : 'ok' %] 292[% String.search('^foo') ? 'ok' : 'not ok' %] 293[% String.search('^bar') ? 'not ok' : 'ok' %] 294-- expect -- 295ok 296ok 297ok 298ok 299 300 301-- test -- 302[% USE String 'foo < bar' filter='html' -%] 303[% String %] 304-- expect -- 305foo < bar 306 307-- test -- 308[% USE String 'foo bar' filter='uri' -%] 309[% String %] 310-- expect -- 311foo%20bar 312 313-- test -- 314[% USE String 'foo bar' filters='uri' -%] 315[% String %] 316-- expect -- 317foo%20bar 318 319-- test -- 320[% USE String ' foo bar ' filters=['trim' 'uri'] -%] 321[[% String %]] 322-- expect -- 323[foo%20bar] 324 325-- test -- 326[% USE String ' foo bar ' filter='trim, uri' -%] 327[[% String %]] 328-- expect -- 329[foo%20bar] 330 331-- test -- 332[% USE String ' foo bar ' filters='trim, uri' -%] 333[[% String %]] 334-- expect -- 335[foo%20bar] 336 337-- test -- 338[% USE String 'foo bar' filters={ replace=['bar', 'baz'], 339 trim='', uri='' } -%] 340[[% String %]] 341-- expect -- 342[foo%20baz] 343 344-- test -- 345[% USE String 'foo bar' filters=[ 'replace', ['bar', 'baz'], 346 'trim', 'uri' ] -%] 347[[% String %]] 348-- expect -- 349[foo%20baz] 350 351-- test -- 352[% USE String 'foo bar' -%] 353[% String %] 354[% String.filter('uri') %] 355[% String.filter('replace', 'bar', 'baz') %] 356[% String.output_filter('uri') -%] 357[% String %] 358[% String.output_filter({ repeat => [3] }) -%] 359[% String %] 360-- expect -- 361foo bar 362foo%20bar 363foo baz 364foo%20bar 365foo%20barfoo%20barfoo%20bar 366 367-- test -- 368[% USE String; 369 a = 'HeLLo'; 370 b = 'hEllO'; 371 a == b ? "not ok 0\n" : "ok 0\n"; 372 String.new(a) == String.new(b) ? "not ok 1\n" : "ok 1\n"; 373 String.new(a).lower == String.new(b).lower ? "ok 2\n" : "not ok 2\n"; 374 String.new(a).lower.equals(String.new(b).lower) ? "ok 3\n" : "not ok 3\n"; 375 a.search("(?i)^$b\$") ? "ok 4\n" : "not ok 4\n"; 376-%] 377-- expect -- 378ok 0 379ok 1 380ok 2 381ok 3 382ok 4 383 384-- test -- 385[% USE String('Hello World') -%] 386a: [% String.substr(6) %]! 387b: [% String.substr(0, 5) %]! 388c: [% String.substr(0, 5, 'Goodbye') %]! 389d: [% String %]! 390-- expect -- 391a: World! 392b: Hello! 393c: Hello! 394d: Goodbye World! 395 396-- test -- 397[% USE str = String('foo bar baz wiz waz woz') -%] 398a: [% str.substr(4, 3) %] 399b: [% str.substr(12) %] 400c: [% str.substr(0, 11, 'FOO') %] 401d: [% str %] 402-- expect -- 403a: bar 404b: wiz waz woz 405c: foo bar baz 406d: FOO wiz waz woz 407 408