[Templates-cvs] cvs commit: TT3/t templates.t

cvs@template-toolkit.org cvs@template-toolkit.org
Fri, 26 Mar 2004 10:58:32 +0000


cvs         04/03/26 10:58:31

  Added:       t        templates.t
  Log:
  * added templates.t test script
  
  Revision  Changes    Path
  1.1                  TT3/t/templates.t
  
  Index: templates.t
  ===================================================================
  #============================================================= -*-perl-*-
  #
  # t/templates.t
  #
  # Test the Template::Templates module.
  #
  # 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: templates.t,v 1.1 2004/03/26 10:58:31 abw Exp $
  #
  #========================================================================
  
  use strict;
  use warnings;
  
  use lib qw( ./lib ../lib );
  use Template::Cache;
  use Template::Store;
  use Template::Provider;
  use Template::Component;
  use Template::Templates;
  use Template::Compilers;
  use Test::More tests => 14;
  
  my $DEBUG = 
  $Template::Templates::DEBUG = 
  $Template::Component::DEBUG = 
  $Template::Provider::DEBUG = 
  grep(/^--?d(ebug)?/, @ARGV);
  
  my $tpkg = 'Template::Templates';
  my $cpkg = 'Template::Component';
  
  my $cache = Template::Cache->new() 
      || die Template::Cache->error();
  
  ok( $cache, 'created cache' );
  
  my $templates = $tpkg->new( static => 1, 
                              cache => $cache,
                              walk => 'up' )
      || die $tpkg->error();
  
  ok( $templates, 'created templates resource' );
  
  my $component = $cpkg->new({
      name  => 'outer',
      path  => '/foo/bar/baz',
      templates => {
          'header' => "hello world",
          other => {
              id => 'src:/some/where/header',
              text => 'the header template',
          },
          'stubby' => sub { return "hello world" },
          '/footer' => {
              id => 'src:/some/where/footer',
              text => 'the footer template',
          },
      },
      compilers => 'Template::Compilers',
      resources => {
          templates => $templates,
      },
  }) || die $cpkg->error();
  
  ok( $component, 'created component' );
  
  my $header = $component->template('header') || die $component->error();
  
  ok( $header, 'got header');
  
  ok( $component->template( 'header' )
      || warn($component->error(), "\n") && 0, 'got header again' );
  
  ok( $component->template( 'footer' ) 
      || warn($component->error(), "\n") && 0, 'got footer' );
  
  ok( $component->template( 'header' ) 
      || warn($component->error(), "\n") && 0, 'got header once again' );
  
  ok( $component->template( '/footer' ) 
      || warn($component->error(), "\n") && 0, 'got /footer again' );
  
  
  my $c2 = $cpkg->new({
      name   => 'inner',
      path   => '/test',
      caller => $component,
  });
  
  
  ok( $c2, 'created nested context' );
  
  ok( $c2->template( 'header' ) 
      || die($c2->error(), "\n"), 'got header' );
  
  my $stubby = $c2->template('stubby') || die $c2->error();
  ok( $stubby, 'got stubby' );
  my $body = $stubby->{ body };
  ok( $body && ref $body eq 'CODE', 'got body' );
  is( &$body, 'hello world', 'hello world' );
  
  is( $stubby->run(), 'hello world', 'stubby says hi' );
  
  
  __END__
  
  #------------------------------------------------------------------------
  # test paths() method
  #------------------------------------------------------------------------
  
  my $root = '/foo/bar';
  my @paths = $resource->paths($root, '/one');
  is( scalar @paths, 1, 'one path' );
  is( $paths[0], '/one', '/one' );
  
  is( join(':', $resource->paths($root, './two')), 
      '/foo/bar/two', '/foo/bar/two' );
  
  is( join(':', $resource->paths($root, '../three')), 
      '/foo/three', '/foo/three' );
  
  is( join(':', $resource->paths($root, './../././four/five')), 
      '/foo/four/five', '/foo/four/five' );
  
  is( join(':', $resource->paths($root, 'four/twenty')), 
      'four/twenty:/foo/bar/four/twenty:/foo/four/twenty:/four/twenty', 
      '/four/twenty' );
  
  
  
  __END__
  
  # Local Variables:
  # mode: perl
  # perl-indent-level: 4
  # indent-tabs-mode: nil
  # End:
  #
  # vim: expandtab shiftwidth=4: