[Templates-cvs] cvs commit: TT3/t scanner.t
cvs@template-toolkit.org
cvs@template-toolkit.org
Wed, 10 Nov 2004 18:27:18 +0000
cvs 04/11/10 18:27:18
Modified: t scanner.t
Log:
* new tests for scanner updates
Revision Changes Path
1.9 +106 -90 TT3/t/scanner.t
Index: scanner.t
===================================================================
RCS file: /template-toolkit/TT3/t/scanner.t,v
retrieving revision 1.8
retrieving revision 1.9
diff -u -r1.8 -r1.9
--- scanner.t 2004/03/29 16:41:16 1.8
+++ scanner.t 2004/11/10 18:27:18 1.9
@@ -9,44 +9,50 @@
# This is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
#
-# $Id: scanner.t,v 1.8 2004/03/29 16:41:16 abw Exp $
+# $Id: scanner.t,v 1.9 2004/11/10 18:27:18 abw Exp $
#
+# TODO
+# - fix for new scanner module
+# - test comment option
+#
#========================================================================
use strict;
use warnings;
use lib qw( ./lib ../lib );
-use Test::More skip_all => 'all tests: sorry, these tests are broken';
-use Template::TT3::Base;
-use Template::TT3::Scanner;
-use Template::TT3::Handler;
-use Template::TT3::Tag;
-use Template::TT3::Tagset::TT3;
-use Test::More tests => 53;
+use Template::Base;
+use Template::Scanner;
+use Template::Handler;
+use Template::Tag;
+#use Template::Tagset::TT3;
+use Template::Test;
+
+plan(53);
my $DEBUG =
-$Template::TT3::Scanner::DEBUG =
-$Template::TT3::Handler::DEBUG =
-$Template::TT3::Tag::DEBUG =
+$Template::Scanner::DEBUG =
+$Template::Handler::DEBUG =
+$Template::Tag::DEBUG =
grep /^--?d(ebug)?$/, @ARGV;
-my $handpkg = 'Template::TT3::Handler';
-my $scanpkg = 'Template::TT3::Scanner';
+my $handpkg = 'Template::Handler';
+my $scanpkg = 'Template::Scanner';
+
#------------------------------------------------------------------------
# define a subclass tag for testing the scanner scan() method
#------------------------------------------------------------------------
package Template::Test::Tag;
-use base qw( Template::TT3::Tag::Closed );
+use base qw( Template::Tag::Closed );
sub parse {
my ($self, $textref, $handler, $match) = @_;
my $text = $$textref;
# $text =~ s/\n/\\n/g;
- $handler->directive( tag => $self->location(), $text );
+ $handler->expr( tag => $self->location(), $text );
return $handler;
}
@@ -55,13 +61,11 @@
#------------------------------------------------------------------------
package Template::Test::Text;
-use base qw( Template::TT3::Tag::Text );
+use base qw( Template::Tag::Text );
sub parse {
my ($self, $textref, $handler, $match) = @_;
- my $text = $$textref;
-# $text =~ s/\n/\\n/g;
- $handler->directive( text => $self->location(), $text );
+ $handler->expr( text => $self->location(), $textref );
}
@@ -75,21 +79,18 @@
my $textpkg = 'Template::Test::Text';
my $footag = $tagpkg->new({
- name => 'foo',
start => '<foo:',
end => '>',
});
ok( $footag, 'created foo tag' );
my $bartag = $tagpkg->new({
- name => 'bar',
start => '<bar:',
end => '>',
});
ok( $bartag, 'created bar tag' );
my $baztag = $tagpkg->new({
- name => 'bar',
start => qr/(?i:<ba(z|m):)/,
end => '>',
});
@@ -99,12 +100,12 @@
ok( $texttag, 'created text tag');
my $scanner = $scanpkg->new(
- tags => [ $footag, $bartag, $baztag ],
- text => $texttag
+ tags => [ foo => $footag, bar => $bartag, baz => $baztag ],
+ text => $texttag,
);
ok( $scanner, 'created scanner' );
-my $handler = $handpkg->new( );
+my $handler = $handpkg->new( type => 'test' );
ok( $handler, 'created handler' );
my $doctext = "This is the first line of text
@@ -118,85 +119,100 @@
# TODO: we shouldn't have to start() the handler should we?
-ok( $handler->start(), 'started handler' );
+# ok( $handler->start(), 'started handler' );
$handler = $scanner->scan(\$doctext, $handler)
|| die $scanner->error();
-# TODO: or end()?
my $body = $handler->end()
|| die $handler->error();
ok( $body, 'got a result' );
print $scanner->dump_item($body) if $DEBUG;
+
+
+#------------------------------------------------------------------------
+# check result (will depend on planned changes to handler)
+#------------------------------------------------------------------------
+
+sub is_text_expr {
+ my ($expr, $lines, $text, $message) = @_;
+ $message ||= $text;
+ is( $expr->[0], 'text', "$message (text expr)" );
+ is( $expr->[1], $lines, "$message ($lines)" );
+ is( ${$expr->[2]}, $text, "$message (text matches)" );
+}
+
+sub is_tag_expr {
+ my ($expr, $lines, $text, $message) = @_;
+ $message ||= $text;
+ is( $expr->[0], 'tag', "$message (tag expr)" );
+ is( $expr->[1], $lines, "$message ($lines)" );
+ is( $expr->[2], $text, "$message (text matches)" );
+}
-is( shift @$body, 'template', 'body is a template' );
+is( $body->[0], 'test', 'body is a test' );
+$body = $body->[1];
is( scalar @$body, 9, 'nine elements in body' );
+
+
+is_text_expr(
+ $body->[0],
+ 'lines 1-2',
+ "This is the first line of text\nThe second line has ",
+ 'first body'
+);
+
+
+is_text_expr( $body->[2],
+ 'lines 2-3',
+ " and\nthe next line has an ",
+ 'third body'
+);
+
+is_tag_expr(
+ $body->[3],
+ 'lines 3-6',
+ "embedded bar directive\nthat spans\nseveral\nlines",
+ 'fourth body'
+);
+
+is_text_expr( $body->[4],
+ 'lines 6-7',
+ " before going back to text.\n",
+ 'fifth body'
+);
+
+is_tag_expr(
+ $body->[5],
+ 'line 7',
+ 'at the start',
+ 'sixth body'
+);
+
+is_tag_expr(
+ $body->[6],
+ 'line 7',
+ 'in the middle',
+ 'seventh body'
+);
+
+is_tag_expr(
+ $body->[7],
+ 'line 7',
+ 'at the end',
+ 'eighth body'
+);
+
+is_text_expr( $body->[8],
+ 'lines 7-8',
+ "\nthe end",
+ 'ninth body'
+);
-is( $body->[0]->[0], 'text',
- 'first body type' );
-is( $body->[0]->[1], 'lines 1-2',
- 'first body line' );
-is( $body->[0]->[2], "This is the first line of text\nThe second line has ",
- 'first body text' );
-
-is( $body->[1]->[0], 'tag',
- 'second body type' );
-is( $body->[1]->[1], 'line 2',
- 'second body line' );
-is( $body->[1]->[2], 'embedded foo directive',
- 'second body text' );
-
-is( $body->[2]->[0], 'text',
- 'third body type' );
-is( $body->[2]->[1], 'lines 2-3',
- 'third body line' );
-is( $body->[2]->[2], " and\nthe next line has an ",
- 'third body text' );
-
-is( $body->[3]->[0], 'tag',
- 'fourth body type' );
-is( $body->[3]->[1], 'lines 3-6',
- 'fourth body line' );
-is( $body->[3]->[2], "embedded bar directive\nthat spans\nseveral\nlines",
- 'fourth body text' );
-
-is( $body->[4]->[0], 'text',
- 'fifth body type' );
-is( $body->[4]->[1], 'lines 6-7',
- 'fifth body line' );
-is( $body->[4]->[2], " before going back to text.\n",
- 'fifth body text' );
-
-is( $body->[5]->[0], 'tag',
- 'sixth body type' );
-is( $body->[5]->[1], 'line 7',
- 'sixth body line' );
-is( $body->[5]->[2], 'at the start',
- 'sixth body text' );
-
-is( $body->[6]->[0], 'tag',
- 'seventh body type' );
-is( $body->[6]->[1], 'line 7',
- 'seventh body line' );
-is( $body->[6]->[2], 'in the middle',
- 'seventh body text' );
-
-is( $body->[7]->[0], 'tag',
- 'eighth body type' );
-is( $body->[7]->[1], 'line 7',
- 'eighth body line' );
-is( $body->[7]->[2], 'at the end',
- 'eighth body text' );
-
-is( $body->[8]->[0], 'text',
- 'ninth body type' );
-is( $body->[8]->[1], 'lines 7-8',
- 'ninth body line' );
-is( $body->[8]->[2], "\nthe end",
- 'ninth body text' );
+__END__
#------------------------------------------------------------------------
# test the tagset() method