[Templates-cvs] cvs commit: TT3/t/parser term.t

cvs@template-toolkit.org cvs@template-toolkit.org
Mon, 15 Nov 2004 19:17:42 +0000


cvs         04/11/15 19:17:42

  Modified:    t/parser term.t
  Log:
  * updated tests for the parser parse_term() method
  
  Revision  Changes    Path
  1.6       +192 -397  TT3/t/parser/term.t
  
  Index: term.t
  ===================================================================
  RCS file: /template-toolkit/TT3/t/parser/term.t,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- term.t	2004/11/10 18:28:27	1.5
  +++ term.t	2004/11/15 19:17:42	1.6
  @@ -10,7 +10,7 @@
   # This is free software; you can redistribute it and/or modify it
   # under the same terms as Perl itself.
   #
  -# $Id: term.t,v 1.5 2004/11/10 18:28:27 abw Exp $
  +# $Id: term.t,v 1.6 2004/11/15 19:17:42 abw Exp $
   #
   #========================================================================
   
  @@ -19,9 +19,9 @@
   
   use lib qw( ./lib ../lib ../../lib );
   use Template::Parser;
  -use Template::TT3::Generator::Debug;
  +use Template::Generator::Debug;
   use Template::Test qw( :all );
  -plan(56);
  +plan(55);
   
   our $DEBUG = 
   $Template::Parser::DEBUG =
  @@ -29,7 +29,7 @@
   grep(/^--?d(ebug)?/, @ARGV);
   
   my $ppkg = 'Template::Parser';
  -my $gpkg = 'Template::TT3::Generator::Debug';
  +my $gpkg = 'Template::Generator::Debug';
   
   my $parser = $ppkg->new() 
       || die $ppkg->error();
  @@ -39,25 +39,96 @@
       || die $gpkg->error();
   ok( $generator, "created a generator" );
   
  +
  +#------------------------------------------------------------------------
  +# parse_number()
  +#------------------------------------------------------------------------
  +
  +sub is_number {
  +    my ($text, $message) = @_;
  +    my $textref = \$text;
  +    $message ||= "number $text";
  +    my $result = $parser->parse_number($textref)
  +        || die "number ($text): ", $parser->error();
  +    is( $result->[0], 'number', "$message (type)" );
  +    is( $result->[1], $text, "$message (value)" );
  +}
  +
  +# just a few to check the method works - they're tested in full later
  +is_number('0x10');
  +is_number('+73.8');
  +is_number('7.3e4');
  +
  +
  +#------------------------------------------------------------------------
  +# parse_squote()
  +#------------------------------------------------------------------------
  +
  +sub is_squote {
  +    my ($text, $message) = @_;
  +    my $quoted = "'$text'";
  +    my $textref = \$quoted;
  +    $message ||= "squote $text";
  +    my $result = $parser->parse_squote($textref)
  +        || die "squote ($text): ", $parser->error();
  +    is( $result->[0], 'squote', "$message (type)" );
  +    is( $result->[1], $text, "$message (value)" );
  +}
  +
  +is_squote('hello world');
  +
  +
  +#------------------------------------------------------------------------
  +# parse_dquote()
  +#------------------------------------------------------------------------
  +
  +sub is_dquote {
  +    my ($text, $message) = @_;
  +    my $quoted = "\"$text\"";
  +    my $textref = \$quoted;
  +    $message ||= "dquote $text";
  +    my $result = $parser->parse_dquote($textref)
  +        || die "dquote ($text): ", $parser->error();
  +    is( $result->[0], 'dquote', "$message (type)" );
  +    is( $result->[1]->[0], $text, "$message (value)" );
  +}
  +
  +is_dquote("hello world");
  +
  +
  +#------------------------------------------------------------------------
  +# parse_term()
  +#------------------------------------------------------------------------
  +
   test_expect({
       handler => \&parse, 
  -    ok      => \&ok 
   });
   
   sub parse {
       my $test   = shift;
  -    my @lines  = split(/\n/, $test->{ input });
       my $result = '';
  +    my @lines;
  +
  +    # -- block -- flag indicates one single test, otherwise we 
  +    # split the block into separate lines
  +    if ($test->{ inflag }->{ block }) {
  +        @lines = $test->{ input };
  +    }
  +    else {
  +        @lines  = split(/\n/, $test->{ input });
  +    }
   
       foreach my $line (@lines) {
  -        $parser->error(undef);
  -        my $out = $parser->parse_term(\$line);
  +        my $out = eval {
  +            $parser->parse_term(\$line);
  +        };
           if ($out) {
               $out = $generator->generate($out) 
                   || die $generator->error();
           }
           else {
  -            $out = '<ERROR:' . $parser->error() . '>';
  +            my $error = $@ ? ref($@) ? $@->info() : $@ : $parser->error();
  +            $out = "<ERROR:$error>";
           }
           $result .= "$out\n";
       }
  @@ -179,6 +250,7 @@
   <number:+1.23e-45>
   
   
  +
   #========================================================================
   # strings
   #=======================================================================
  @@ -245,6 +317,7 @@
     <text:(99) baz>
   >
   
  +
   -- test double quoted embedded vars --
   "foo ${bar} baz"
   "foo ${ping.pong} bar"
  @@ -285,6 +358,39 @@
   >
   
   
  +
  +-- test embedded vars with complex args --
  +"foo ${ bar(99, ping(20).pong(30)) } baz"
  +-- expect --
  +<dquote:
  +  <text:foo >
  +  <variable:
  +    <node:
  +      <ident:bar>
  +      <args:
  +        <number:99>
  +        <variable:
  +          <node:
  +            <ident:ping>
  +            <args:
  +              <number:20>
  +            >
  +          >
  +          <node:
  +            <ident:pong>
  +            <args:
  +              <number:30>
  +            >
  +          >
  +        >
  +      >
  +    >
  +  >
  +  <text: baz>
  +>
  +
  +
  +
   #========================================================================
   # qw list
   #=======================================================================
  @@ -295,10 +401,10 @@
   qw( a b c d )
   qw( one two three four )
   -- expect --
  -<qwlist:>
  -<qwlist:>
  -<qwlist:a b c d>
  -<qwlist:one two three four>
  +<qwlist((, )):>
  +<qwlist((, )):>
  +<qwlist((, )):a b c d>
  +<qwlist((, )):one two three four>
   
   -- test qw[] list --
   qw[]
  @@ -306,10 +412,10 @@
   qw[ a b c d ]
   qw[ one two three four ]
   -- expect --
  -<qwlist:>
  -<qwlist:>
  -<qwlist:a b c d>
  -<qwlist:one two three four>
  +<qwlist([, ]):>
  +<qwlist([, ]):>
  +<qwlist([, ]):a b c d>
  +<qwlist([, ]):one two three four>
   
   -- test qw{} list --
   qw{}
  @@ -317,10 +423,10 @@
   qw{ a b c d}
   qw{ one two three four }
   -- expect --
  -<qwlist:>
  -<qwlist:>
  -<qwlist:a b c d>
  -<qwlist:one two three four>
  +<qwlist({, }):>
  +<qwlist({, }):>
  +<qwlist({, }):a b c d>
  +<qwlist({, }):one two three four>
   
   -- test qw<> list --
   qw<>
  @@ -328,10 +434,10 @@
   qw< a b c d>
   qw<one two three four >
   -- expect --
  -<qwlist:>
  -<qwlist:>
  -<qwlist:a b c d>
  -<qwlist:one two three four>
  +<qwlist(<, >):>
  +<qwlist(<, >):>
  +<qwlist(<, >):a b c d>
  +<qwlist(<, >):one two three four>
   
   
   
  @@ -356,6 +462,7 @@
     <number:8>
   >
   
  +
   -- test list unaryop expression 1 --
   [ -a ]
   -- expect --
  @@ -461,11 +568,12 @@
     >
   >
   
  +
   -- test list with number expression --
   [ 1 ? 2 : 3 ]
   -- expect --
   <list:
  -  <condition:
  +  <tertiary:
       <expression:
         <number:1>
       >
  @@ -482,7 +590,7 @@
   [ a ? b : c 10 20 ]
   -- expect --
   <list:
  -  <condition:
  +  <tertiary:
       <expression:
         <variable:
           <node:
  @@ -531,6 +639,7 @@
     >
   >
   
  +
   -- test list with range b --
   [ 1..3 5 7 9..11 13 ]
   -- expect --
  @@ -600,6 +709,18 @@
     >
   >
   
  +-- test list with embedded comments --
  +-- block --
  +[ 10 # foo
  +  20 # bar
  +  30 # baz 
  +]
  +-- expect --
  +<list:
  +  <number:10>
  +  <number:20>
  +  <number:30>
  +>
   
   -- test TODO more list with expression --
   #[ a + 2, b ? c : d, e or f ]
  @@ -610,9 +731,6 @@
   
   
   
  -
  -
  -
   #========================================================================
   # hash
   #=======================================================================
  @@ -662,6 +780,27 @@
     >
   >
   
  +
  +-- test hash with comments --
  +-- block --
  +{ # first key
  +  x => 20 # more comments
  +  # another comment
  +  y => 30,
  +}
  +-- expect --
  +<hash:
  +  <key:x>
  +  <value:
  +    <number:20>
  +  >
  +  <key:y>
  +  <value:
  +    <number:30>
  +  >
  +>
  +
  +
   -- test hash error --
   { "e" => 2.718 }
   { $bar => x }
  @@ -670,14 +809,15 @@
   { foo = }
   { foo => }
   -- expect --
  -<ERROR:unexpected '"' in hash definition>
  -<ERROR:unexpected '$' in hash definition>
  +<ERROR:unexpected '"' in hash definition { ... }>
  +<ERROR:unexpected '$' in hash definition { ... }>
   <ERROR:unexpected '[' after hash key 'qw' where '=' expected>
   <ERROR:unexpected '}' after hash key 'foo' where '=' expected>
   <ERROR:unexpected '}' after '=' where expression expected>
   <ERROR:unexpected '}' after '=>' where expression expected>
   
   
  +
   #========================================================================
   # parens
   #=======================================================================
  @@ -739,386 +879,41 @@
       >
     >
   >
  -
  -
  -
  -
  -
  -#========================================================================
  -# variables
  -#
  -# TODO: most of these can be moved into vars.t
  -# However, those that define literals as the first node in a dotted
  -# variable must remain here, e.g. "string".length and [1,2,3].join
  -#========================================================================
  -
  --- test var foo --
  -foo
  --- expect --
  -<variable:
  -  <node:
  -    <ident:foo>
  -  >
  ->
  -
  --- test var foo error --
  -foo.&
  --- expect --
  -<ERROR:missing item after '.' (got '&')> 
  -
  --- test foo with arg --
  -foo(10)
  --- expect --
  -<variable:
  -  <node:
  -    <ident:foo>
  -    <args:
  -      <number:10>
  -    >
  -  >
  ->
  -
  --- test foo with args --
  -foo(10, 20)
  --- expect --
  -<variable:
  -  <node:
  -    <ident:foo>
  -    <args:
  -      <number:10>
  -      <number:20>
  -    >
  -  >
  ->
  -
  --- test foo with named args --
  -foo(10, 20, thirty=30)
  --- expect --
  -<variable:
  -  <node:
  -    <ident:foo>
  -    <args:
  -      <number:10>
  -      <number:20>
  -      <named:
  -        <name:thirty>
  -        <value:
  -          <number:30>
  -        >
  -      >
  -    >
  -  >
  ->
  -
  --- test dotted --
  -foo.bar
  --- expect --
  -<variable:
  -  <node:
  -    <ident:foo>
  -  >
  -  <node:
  -    <ident:bar>
  -  >
  ->
  -
  --- test dotted number --
  -foo.3
  -foo.3.four
  -foo.3.14.four
  --- expect --
  -<variable:
  -  <node:
  -    <ident:foo>
  -  >
  -  <node:
  -    <integer:3>
  -  >
  ->
  -<variable:
  -  <node:
  -    <ident:foo>
  -  >
  -  <node:
  -    <integer:3>
  -  >
  -  <node:
  -    <ident:four>
  -  >
  ->
  -<variable:
  -  <node:
  -    <ident:foo>
  -  >
  -  <node:
  -    <integer:3>
  -  >
  -  <node:
  -    <integer:14>
  -  >
  -  <node:
  -    <ident:four>
  -  >
  ->
  -
  -
  --- test dotted literal --
  -foo.'bar'
  --- expect --
  -<variable:
  -  <node:
  -    <ident:foo>
  -  >
  -  <node:
  -    <squote:bar>
  -  >
  ->
  -
  --- test dotted interp --
  -foo.$bar
  -foo.$bar.baz
  -foo.$bar(10)
  -foo.$bar(10).baz(20)
  --- expect --
  -<variable:
  -  <node:
  -    <ident:foo>
  -  >
  -  <node:
  -    <variable:
  -      <node:
  -        <ident:bar>
  -      >
  -    >
  -  >
  ->
  -<variable:
  -  <node:
  -    <ident:foo>
  -  >
  -  <node:
  -    <variable:
  -      <node:
  -        <ident:bar>
  -      >
  -      <node:
  -        <ident:baz>
  -      >
  -    >
  -  >
  ->
  -<variable:
  -  <node:
  -    <ident:foo>
  -  >
  -  <node:
  -    <variable:
  -      <node:
  -        <ident:bar>
  -        <args:
  -          <number:10>
  -        >
  -      >
  -    >
  -  >
  ->
  -<variable:
  -  <node:
  -    <ident:foo>
  -  >
  -  <node:
  -    <variable:
  -      <node:
  -        <ident:bar>
  -        <args:
  -          <number:10>
  -        >
  -      >
  -      <node:
  -        <ident:baz>
  -        <args:
  -          <number:20>
  -        >
  -      >
  -    >
  -  >
  ->
   
  --- test dotted interp error --
  -foo.$
  -foo.$.baz
  --- expect --
  -<ERROR:missing item after '.' (got '$')>
  -<ERROR:missing item after '.' (got '$')>
  -
  --- test dotted embed --
  -foo.${bar}
  -foo.${bar}.baz
  -foo.${bar.baz}
  --- expect --
  -<variable:
  -  <node:
  -    <ident:foo>
  -  >
  -  <node:
  -    <variable:
  -      <node:
  -        <ident:bar>
  -      >
  -    >
  -  >
  ->
  -<variable:
  -  <node:
  -    <ident:foo>
  -  >
  -  <node:
  -    <variable:
  -      <node:
  -        <ident:bar>
  -      >
  -    >
  -  >
  -  <node:
  -    <ident:baz>
  -  >
  ->
  -<variable:
  -  <node:
  -    <ident:foo>
  -  >
  -  <node:
  -    <variable:
  -      <node:
  -        <ident:bar>
  -      >
  -      <node:
  -        <ident:baz>
  -      >
  -    >
  -  >
  ->
  -
  --- test dotted embed with args --
  -foo.${bar(10)}
  -foo.${ bar(10, 20) }.baz
  +-- test parens comments --
  +-- block --
  +(   a   # this is the variable a
  +        # which holds the first value
  +    +   # which we are going to add to
  +    b   # our second variable, b
  +)
   -- expect --
  -<variable:
  -  <node:
  -    <ident:foo>
  -  >
  -  <node:
  +<parens:
  +  <binops:
       <variable:
         <node:
  -        <ident:bar>
  -        <args:
  -          <number:10>
  -        >
  +        <ident:a>
         >
       >
  -  >
  ->
  -<variable:
  -  <node:
  -    <ident:foo>
  -  >
  -  <node:
  +    <op:+>
       <variable:
         <node:
  -        <ident:bar>
  -        <args:
  -          <number:10>
  -          <number:20>
  -        >
  -      >
  -    >
  -  >
  -  <node:
  -    <ident:baz>
  -  >
  ->
  -
  -
  --- test dotted ident error --
  --- skip --
  -foo.$
  -foo.$.baz
  --- expect --
  -<ERROR:missing item after '.' (got '$')>
  -<ERROR:missing item after '.' (got '$')>
  -
  -
  --- test gnarly --
  -foo(10, x.y(3.14), 30).bar(age=20, b.c(d.e)).baz
  --- expect --
  -<variable:
  -  <node:
  -    <ident:foo>
  -    <args:
  -      <number:10>
  -      <variable:
  -        <node:
  -          <ident:x>
  -        >
  -        <node:
  -          <ident:y>
  -          <args:
  -            <number:3.14>
  -          >
  -        >
  -      >
  -      <number:30>
  -    >
  -  >
  -  <node:
  -    <ident:bar>
  -    <args:
  -      <named:
  -        <name:age>
  -        <value:
  -          <number:20>
  -        >
  -      >
  -      <variable:
  -        <node:
  -          <ident:b>
  -        >
  -        <node:
  -          <ident:c>
  -          <args:
  -            <variable:
  -              <node:
  -                <ident:d>
  -              >
  -              <node:
  -                <ident:e>
  -              >
  -            >
  -          >
  -        >
  +        <ident:b>
         >
       >
     >
  -  <node:
  -    <ident:baz>
  -  >
   >
   
   
  +#========================================================================
  +# variables
  +#
  +# See also t/parser/vars.t which tests the full range of variables.
  +# The ones that we're particularly interested in here are those
  +# that define literals as the first node, e.g. "string".length and 
  +# [1,2,3].join.  
   
  --- test dotted quoted --
  -foo."bar"
  --- expect --
  -<variable:
  -  <node:
  -    <ident:foo>
  -  >
  -  <node:
  -    <dquote:
  -      <text:bar>
  -    >
  -  >
  ->
  +#========================================================================
   
   -- test number first --
   3.14.length