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

cvs@template-toolkit.org cvs@template-toolkit.org
Fri, 03 Dec 2004 12:22:46 +0000


cvs         04/12/03 12:22:45

  Added:       t/parser path.t
  Log:
  * added parser tests for parse_path() and parse_paths() methods
  
  Revision  Changes    Path
  1.1                  TT3/t/parser/path.t
  
  Index: path.t
  ===================================================================
  #============================================================= -*-perl-*-
  #
  # t/parser/path.t
  #
  # Test the Template::Parser module and the methods to parse paths: 
  # parse_path(), parse_paths() and parse_static_path()
  #
  # Written by Andy Wardley <abw@wardley.org>
  #
  # This is free software; you can redistribute it and/or modify it
  # under the same terms as Perl itself.
  #
  # $Id: path.t,v 1.1 2004/12/03 12:22:45 abw Exp $
  #
  #========================================================================
  
  use strict;
  use warnings;
  
  use lib qw( ./lib ../lib ../../lib );
  use Template::Parser;
  use Template::Generator::Debug;
  use Template::Test tests => 41, import => ':all';
  
  our $DEBUG = 
  $Template::Parser::DEBUG =
  grep(/^--?d(ebug)?/, @ARGV);
  
  my $ppkg = 'Template::Parser';
  my $gpkg = 'Template::Generator::Debug';
  
  my $parser = $ppkg->new() 
      || die $ppkg->error();
  ok( $parser, "created a parser" );
  
  my $method   = 'parse_path';
  
  my $generator = $gpkg->new() 
      || die $gpkg->error();
  ok( $generator, "created a generator" );
  
  test_expect({
      handler => \&parse, 
      ok      => \&ok 
  });
  
  sub parse {
      my $test   = shift;
      my $input  = $test->{ input };
      my $result = '';
  
      if (my $new_method = (grep(/^parse_/, @{ $test->{ inflags } }))[0]) {
          if (UNIVERSAL::can($parser, $new_method)) {
              $method = $new_method;
          }
          else {
              die "invalid parser method: $new_method\n";
          }
      }
  
      my $expr = eval {
          $parser->$method(\$input);
      };
      if ($expr) {
          $expr = $generator->generate($expr);
          die "generator failed: ", $generator->error()
              unless defined $expr;
      }
      elsif ($@) {
          my $error = ref($@) ? $@->info() : $@;
          $expr = "<ERROR:$error>";
      }
      else {
          $expr = '<DECLINED:' . $parser->error() . '>';
      }
      return $expr;
  }
  
  
  
  __END__
  
  #------------------------------------------------------------------------
  # parse_path()
  #------------------------------------------------------------------------
  
  -- test empty path error --
  -- expect --
  <DECLINED:not a path>
  
  -- test not a path --
  ***
  -- expect --
  <DECLINED:not a path>
  
  -- test stop at invalid path characters --
  foo-bar
  -- expect --
  <path:foo>
  
  -- test unquoted path --
  header
  -- expect --
  <path:header>
  
  -- test unquoted path with dots --
  header.txt
  -- expect --
  <path:header.txt>
  
  -- test unquoted path with multiple dots --
  header.txt.old
  -- expect --
  <path:header.txt.old>
  
  -- test unquoted path with slashe --
  my/header
  -- expect --
  <path:my/header>
  
  -- test unquoted path with multiple slashes --
  my/header/file
  -- expect --
  <path:my/header/file>
  
  -- test unquoted path with colon --
  file:foo
  -- expect --
  <path:file:foo>
  
  -- test unquoted path with multiple colons --
  file:foo:bar
  -- expect --
  <path:file:foo:bar>
  
  -- test unquoted path with colon, slashes and dots --
  file:/my/files.old/foo.bar
  -- expect --
  <path:file:/my/files.old/foo.bar>
  
  -- test single quoted path --
  'header'
  -- expect --
  <squote:header>
  
  -- test double quoted path --
  "header"
  -- expect --
  <dquote:
    <text:header>
  >
  
  -- test double quoted path with interpolated variable --
  "header.$ext"
  -- expect --
  <dquote:
    <text:header.>
    <variable:
      <node:
        <ident:ext>
      >
    >
  >
  
  -- test double quoted path with embedded variable  --
  "${file}.html"
  -- expect --
  <dquote:
    <variable:
      <node:
        <ident:file>
      >
    >
    <text:.html>
  >
  
  -- test double quoted path with multiple variables  --
  "${file}.$ext"
  -- expect --
  <dquote:
    <variable:
      <node:
        <ident:file>
      >
    >
    <text:.>
    <variable:
      <node:
        <ident:ext>
      >
    >
  >
  
  -- test variable --
  $header
  -- expect --
  <variable:
    <node:
      <ident:header>
    >
  >
  
  -- test variable with args --
  $header(10, 20)
  -- expect --
  <variable:
    <node:
      <ident:header>
      <args:
        <number:10>
        <number:20>
      >
    >
  >
  
  
  -- test dotted variable --
  $foo.bar
  -- expect --
  <variable:
    <node:
      <ident:foo>
    >
    <node:
      <ident:bar>
    >
  >
  
  
  -- test dotted.variable with args --
  $foo(2, 3).bar(5, 7).baz(11, 13)
  -- expect --
  <variable:
    <node:
      <ident:foo>
      <args:
        <number:2>
        <number:3>
      >
    >
    <node:
      <ident:bar>
      <args:
        <number:5>
        <number:7>
      >
    >
    <node:
      <ident:baz>
      <args:
        <number:11>
        <number:13>
      >
    >
  >
  
  
  #------------------------------------------------------------------------
  # parse_static_path()
  #------------------------------------------------------------------------
  
  -- test static path unquoted --
  -- parse_static_path --
  header
  -- expect --
  <path:header>
  
  -- test static path single quoted --
  'header'
  -- expect --
  <squote:header>
  
  -- test static path double quoted --
  "header"
  -- expect --
  <dquote:
    <text:header>
  >
  
  -- test static path double quoted var error --
  "header.$ext"
  -- expect --
  <ERROR:variables are not allowed in static paths>
  
  -- test static path var error --
  $header
  -- expect --
  <ERROR:variables are not allowed in static paths>
  
  
  #------------------------------------------------------------------------
  # parse_paths()
  #------------------------------------------------------------------------
  
  -- test parse_paths unquoted --
  -- parse_paths --
  foo+bar
  -- expect --
  <paths:
    <path:foo>
    <path:bar>
  >
  
  -- test paths unquoted with whitespace --
  bar + baz
  -- expect --
  <paths:
    <path:bar>
    <path:baz>
  >
  
  -- test paths unquoted with newlines  --
    baz 
  + bam
  -- expect --
  <paths:
    <path:baz>
    <path:bam>
  >
  
  -- test paths unquoted with comment  --
    bam  # the bam file
  + wam  # the wam file
  -- expect --
  <paths:
    <path:bam>
    <path:wam>
  >
  
  -- test paths single quoted --
  'foo'+'bar'
  -- expect --
  <paths:
    <squote:foo>
    <squote:bar>
  >
  
  -- test paths single quoted with whitespace --
  'bar' + 'baz'
  -- expect --
  <paths:
    <squote:bar>
    <squote:baz>
  >
  
  -- test paths single quoted with newlines  --
    'baz'
  + 'bam'
  -- expect --
  <paths:
    <squote:baz>
    <squote:bam>
  >
  
  -- test paths single quoted with comment  --
    'bam'  # the bam file
  + 'wam'  # the wam file
  -- expect --
  <paths:
    <squote:bam>
    <squote:wam>
  >
  
  -- test paths double quoted --
  "foo"+"bar"
  -- expect --
  <paths:
    <dquote:
      <text:foo>
    >
    <dquote:
      <text:bar>
    >
  >
  
  -- test paths double quoted with whitespace --
  "bar" + "baz"
  -- expect --
  <paths:
    <dquote:
      <text:bar>
    >
    <dquote:
      <text:baz>
    >
  >
  
  -- test paths double quoted with newlines  --
    "baz"
  + "bam"
  -- expect --
  <paths:
    <dquote:
      <text:baz>
    >
    <dquote:
      <text:bam>
    >
  >
  
  -- test paths double quoted with comment  --
    "bam"  # the bam file
  + "wam"  # the wam file
  -- expect --
  <paths:
    <dquote:
      <text:bam>
    >
    <dquote:
      <text:wam>
    >
  >
  
  -- test paths with variables --
  $foo + $bar
  -- expect --
  <paths:
    <variable:
      <node:
        <ident:foo>
      >
    >
    <variable:
      <node:
        <ident:bar>
      >
    >
  >
  
  
  -- test mixture of multiple paths --
    foo
  + 'bar'
  + "baz"
  + $bam
  + $wam.bam
  -- expect --
  <paths:
    <path:foo>
    <squote:bar>
    <dquote:
      <text:baz>
    >
    <variable:
      <node:
        <ident:bam>
      >
    >
    <variable:
      <node:
        <ident:wam>
      >
      <node:
        <ident:bam>
      >
    >
  >
  
  
  
  __END__
  
  # Local Variables:
  # mode: perl
  # perl-indent-level: 4
  # indent-tabs-mode: nil
  # End:
  #
  # vim: expandtab shiftwidth=4: