[Templates-cvs] cvs commit: TT3/t/vobject text.t
cvs@template-toolkit.org
cvs@template-toolkit.org
Mon, 29 Mar 2004 19:36:53 +0100
cvs 04/03/29 18:36:52
Modified: t/vobject text.t
Log:
* more tests for Text vmethod
Revision Changes Path
1.2 +240 -2 TT3/t/vobject/text.t
Index: text.t
===================================================================
RCS file: /template-toolkit/TT3/t/vobject/text.t,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- text.t 2004/03/29 16:41:31 1.1
+++ text.t 2004/03/29 18:36:52 1.2
@@ -9,7 +9,7 @@
# This is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
#
-# $Id: text.t,v 1.1 2004/03/29 16:41:31 abw Exp $
+# $Id: text.t,v 1.2 2004/03/29 18:36:52 abw Exp $
#
#========================================================================
@@ -18,7 +18,7 @@
use lib qw( ./lib ../lib ../../lib );
use Template::VObject::Text;
-use Test::More tests => 6;
+use Test::More tests =>111;
my $DEBUG =
$Template::VObject::Text::DEBUG =
@@ -37,6 +37,244 @@
my $upper = $text->can('upper');
my $source = 'Goodbye Cruel World';
is( &$upper(\$source), 'GOODBYE CRUEL WORLD', 'upper sub call' );
+
+
+
+#========================================================================
+
+
+sub tvm {
+ my ($vmeth, @args) = @_;
+ my $handler = Template::VObject::Text->can($vmeth) || return undef;
+ &$handler(@args);
+}
+
+ok( ! defined tvm( nonsuch => 'hello' ), 'undefined handler' );
+
+$text = 'The foo string';
+my ($list, $hash);
+
+
+#------------------------------------------------------------------------
+# ref, type
+#------------------------------------------------------------------------
+
+is( tvm( ref => $text), '', 'non-ref text' );
+is( tvm( type => $text), 'Text', ' type' );
+
+
+#------------------------------------------------------------------------
+# text, item, list, hash, copy
+#------------------------------------------------------------------------
+
+is( tvm( text => $text ), $text, 'text.text' );
+is( tvm( item => $text ), $text, 'text.item' );
+is( tvm( copy => $text ), $text, 'text.copy' );
+
+$list = tvm( list => $text );
+ok( $list, 'text.list returned something' );
+is( ref $list, 'ARRAY', 'text.list returned a list' );
+is( scalar @$list, 1, 'containing 1 item list' );
+is( $list->[0], $text, 'item 0 is $text' );
+
+$hash = tvm( hash => $text );
+ok( $hash, 'text.hash returned something' );
+is( ref $hash, 'HASH', 'text.hash returned a hash' );
+is( $hash->{ text }, $text, 'item is $text' );
+
+
+
+#------------------------------------------------------------------------
+# pop, push, shift, unshift, append, prepend
+#------------------------------------------------------------------------
+
+is( tvm( pop => $text, 'string' ),
+ 'The foo ', 'single pop' );
+
+is( tvm( pop => $text, ' ', 'foo', ' ', 'string' ),
+ 'The', 'multiple pop' );
+
+is( tvm( push => $text, ' more foo' ),
+ 'The foo string more foo', 'single push' );
+
+is( tvm( push => $text, ' more foo', ' even more foo' ),
+ 'The foo string more foo even more foo', 'multiple push' );
+
+is( tvm( suffix => $text, ' more foo' ),
+ 'The foo string more foo', 'single append' );
+is( tvm( suffix => $text, ' more foo', ' even more foo' ),
+ 'The foo string more foo even more foo', 'multiple append' );
+
+is( tvm( shift => $text, 'The ' ),
+ 'foo string', 'single shift' );
+is( tvm( shift => $text, 'The', ' ', 'foo', ' '),
+ 'string', 'multiple shift' );
+
+is( tvm( unshift => $text, 'This is ' ),
+ 'This is The foo string', 'single unshift' );
+is( tvm( unshift => $text, 'And this', ' ', 'still is', ' ' ),
+ 'And this still is The foo string', 'multiple unshift' );
+
+is( tvm( prefix => $text, 'This is ' ),
+ 'This is The foo string', 'single prepend' );
+is( tvm( prefix => $text, 'And this', ' ', 'still is', ' ' ),
+ 'And this still is The foo string', 'multiple prepend' );
+
+
+#------------------------------------------------------------------------
+# size, length, equals
+#------------------------------------------------------------------------
+
+is( tvm( size => $text ), 1, 'text.size is 1' );
+is( tvm( length => $text ), length $text,
+ 'text.length is ' . length $text );
+ok( tvm( equals => $text, $text ), 'equals' );
+
+
+#------------------------------------------------------------------------
+# centre, center, left, right, format
+#------------------------------------------------------------------------
+
+is( tvm( centre => 'ping', 10 ), ' ping ', 'ping centred' );
+is( tvm( center => 'ping', 10 ), ' ping ', 'ping centered' );
+is( tvm( left => 'pong', 10 ), 'pong ', 'pong left' );
+is( tvm( right => 'pong', 10 ), ' pong', 'pong right' );
+is( tvm( format => 'foo', '<img src="%s.png">' ),
+ '<img src="foo.png">', 'simple format' );
+is( tvm( format => 'foo', '<img src="%s.%s">', 'png' ),
+ '<img src="foo.png">', 'format with args' );
+
+
+#------------------------------------------------------------------------
+# upper, lower, capital, capitals
+#------------------------------------------------------------------------
+
+is( tvm( upper => $text ), 'THE FOO STRING', 'upper' );
+is( tvm( lower => $text ), 'the foo string', 'lower 1' );
+is( tvm( lower => 'UP and DOWN' ), 'up and down', 'lower 2' );
+is( tvm( capital => 'the foo bar' ), 'The foo bar', 'capital 1' );
+is( tvm( capital => '... the foo bar' ), '... The foo bar', 'capital 2' );
+is( tvm( capitals => $text ), 'The Foo String', 'capitals 1' );
+is( tvm( capitals => '... 10 20 once upon a time ...' ),
+ '... 10 20 Once Upon A Time ...', 'capitals 2' );
+
+
+#------------------------------------------------------------------------
+# chop, chomp, trim, collapse, truncate
+#------------------------------------------------------------------------
+
+is( tvm( chop => "foo bar\n" ), 'foo bar', 'chop 1' );
+is( tvm( chop => "foo bars" ), 'foo bar', 'chop 2' );
+is( tvm( chomp => "foo bar\n" ), 'foo bar', 'chomp 1' );
+is( tvm( chomp => "foo bars" ), 'foo bars', 'chomp 2' );
+is( tvm( trim => " \n foo bars \n\n " ), 'foo bars', 'trim 1' );
+is( tvm( trim => " \n foo \n bars \n\n " ), "foo \n bars", 'trim 2' );
+is( tvm( collapse => " \n foo bars \n\n " ), 'foo bars', 'collapse 1' );
+is( tvm( collapse => " \n foo \n bars \n\n " ), "foo bars", 'collapse 2' );
+is( tvm( truncate => $text, 20 ), "The foo string", 'truncate 0' );
+is( tvm( truncate => $text, 10 ), "The foo st", 'truncate 1' );
+is( tvm( truncate => $text, 10, '...' ), "The foo...", 'truncate 2' );
+
+
+#------------------------------------------------------------------------
+# chunk, repeat, remove, replace, match, search, split
+#------------------------------------------------------------------------
+
+$text = 'TheCatSatTheMat';
+$list = tvm( chunk => $text, 3 );
+ok( $list && ref $list eq 'ARRAY', 'text.chunk(3) returned a list' );
+is( scalar @$list, 5, 'list has 5 items' );
+is( $list->[0], 'The', 'The' );
+is( $list->[1], 'Cat', 'Cat' );
+is( $list->[2], 'Sat', 'Sat' );
+is( $list->[3], 'The', 'The' );
+is( $list->[4], 'Mat', 'Mat' );
+
+
+$text = 'TheCatSatonTheMat';
+$list = tvm( chunk => $text, 3 );
+ok( $list && ref $list eq 'ARRAY', 'text.chunk(3) returned a list' );
+is( scalar @$list, 6, 'list has 5 items' );
+is( $list->[0], 'The', 'The' );
+is( $list->[1], 'Cat', 'Cat' );
+is( $list->[2], 'Sat', 'Sat' );
+is( $list->[3], 'onT', 'onT' );
+is( $list->[4], 'heM', 'heM' );
+is( $list->[5], 'at', 'at' );
+
+$text = 'TheCatSatTheMat';
+$list = tvm( chunk => $text, -3 );
+ok( $list && ref $list eq 'ARRAY', 'text.chunk(-3) returned a list' );
+is( scalar @$list, 5, 'list has 5 items' );
+is( $list->[0], 'The', 'The' );
+is( $list->[1], 'Cat', 'Cat' );
+is( $list->[2], 'Sat', 'Sat' );
+is( $list->[3], 'The', 'The' );
+is( $list->[4], 'Mat', 'Mat' );
+
+$text = 'TheCatSatonTheMat';
+$list = tvm( chunk => $text, -3 );
+ok( $list && ref $list eq 'ARRAY', 'text.chunk(-3) returned a list' );
+is( scalar @$list, 6, 'list has 6 items' );
+is( $list->[0], 'Th', 'Th' );
+is( $list->[1], 'eCa', 'eCa' );
+is( $list->[2], 'tSa', 'tSa' );
+is( $list->[3], 'ton', 'ton' );
+is( $list->[4], 'The', 'The' );
+is( $list->[5], 'Mat', 'Mat' );
+
+$text = '1234567824683579';
+$list = tvm( chunk => $text, 4 );
+ok( $list && ref $list eq 'ARRAY', 'text.chunk(4) returned a list' );
+is( scalar @$list, 4, 'list has 4 items' );
+is( $list->[0], '1234', 'got 1234' );
+is( $list->[1], '5678', 'got 5678' );
+is( $list->[2], '2468', 'got 2468' );
+is( $list->[3], '3579', 'got 3579' );
+
+
+$text = 'The foo string';
+is( tvm( repeat => $text, 2 ), 'The foo stringThe foo string', 'repeat' );
+is( tvm( remove => $text, '\s+foo' ), 'The string', 'remove' );
+is( tvm( replace => $text, '\s+foo', ' bar' ), 'The bar string', 'replace' );
+ok( tvm( match => $text, 'foo'), 'match 1' );
+my $matches = tvm( match => $text, 'o');
+is( scalar @$matches, 2, 'match 2' );
+
+ok( tvm( search => $text, 'foo'), 'search 1' );
+my $searches = tvm( search => $text, 'o');
+is( scalar @$searches, 2, 'search 2' );
+
+my $splits = tvm( split => $text );
+is( scalar @$splits, 3, 'split 1' );
+is( $splits->[0], 'The', 'The' );
+is( $splits->[1], 'foo', 'foo' );
+is( $splits->[2], 'string', 'string' );
+
+$splits = tvm( split => $text, '\s+foo\s+' );
+is( scalar @$splits, 2, 'split 2' );
+is( $splits->[0], 'The', 'The' );
+is( $splits->[1], 'string', 'string' );
+
+
+
+#------------------------------------------------------------------------
+# vtable() and vmethods()
+#------------------------------------------------------------------------
+
+my $vtable = Template::VObject::Text->methods();
+ok( $vtable, 'vtable returned' );
+is( ref $vtable, 'HASH', 'vtable is a hash' );
+ok( defined $vtable->{ length }, 'length is defined' );
+
+#my $vmeths = Template::VObject->vtable();
+#ok( $vmeths, 'vtable returned' );
+#$vtable = $vmeths->{ text };
+#ok( $vtable, 'text vtable returned' );
+#is( ref $vtable, 'HASH', 'vtable is a hash' );
+#ok( defined $vtable->{ length }, 'length is defined' );
+
+
__END__