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

cvs@template-toolkit.org cvs@template-toolkit.org
Fri, 26 Mar 2004 13:19:21 +0000


cvs         04/03/26 13:19:21

  Added:       t        plugins.t
  Log:
  * added tests for plugins
  
  Revision  Changes    Path
  1.1                  TT3/t/plugins.t
  
  Index: plugins.t
  ===================================================================
  #============================================================= -*-perl-*-
  #
  # t/plugins.t
  #
  # Test the Template::Plugins.pm 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: plugins.t,v 1.1 2004/03/26 13:19:21 abw Exp $
  #
  #========================================================================
  
  use strict;
  use warnings;
  
  use lib qw( ./lib ../lib );
  use Template::Plugins;
  use Test::More tests => 48;
  
  my $DEBUG = 
  $Template::Plugins::DEBUG = 
  grep(/^--?d(ebug)?/, @ARGV);
  
  
  #------------------------------------------------------------------------
  # object method, test default path set
  #------------------------------------------------------------------------
  
  my $plugins = Template::Plugins->new();
  ok( $plugins, 'created a plugins object' );
  
  my $path = $plugins->path();
  is( ref $path, 'ARRAY', 'got path list' );
  is( scalar @$path, 1, 'one item in path' );
  is( $path->[0], 'Template::Plugin', 'Template::Plugin in path' );
  
  my $plugs = $plugins->plugins();
  ok( $plugs, 'got plugins' );
  is( ref $plugs, 'HASH', 'got plugins hash' );
  is( scalar keys %$plugs, 1, 'one plugin in hash' );
  is( $plugs->{ hello }, 'Template::Plugin::Hello', 'hello plugin' );
  
  
  #------------------------------------------------------------------------
  # now try custom path
  #------------------------------------------------------------------------
  
  $plugins = Template::Plugins->new( path => 'Custom::Plugin' );
  ok( $plugins, 'created custom path plugins object' );
  
  $path = $plugins->path();
  is( ref $path, 'ARRAY', 'got custom path list' );
  is( scalar @$path, 1, 'one item in custom path' );
  is( $path->[0], 'Custom::Plugin', 'Custom::Plugin in path' );
  
  $plugins = Template::Plugins->new( path => [ 'One::Plugin', 'Two::Plugin' ] );
  ok( $plugins, 'created two path plugins object' );
  
  $path = $plugins->path();
  is( ref $path, 'ARRAY', 'got two path list' );
  is( scalar @$path, 2, 'two items in path' );
  is( $path->[0], 'One::Plugin', 'One::Plugin in path' );
  is( $path->[1], 'Two::Plugin', 'Two::Plugin in path' );
  
  $plugs = $plugins->plugins();
  ok( $plugs, 'still got plugins' );
  is( ref $plugs, 'HASH', 'still got plugins hash' );
  is( scalar keys %$plugs, 1, 'still one plugin in hash' );
  is( $plugs->{ hello }, 'Template::Plugin::Hello', 'still got hello plugin' );
  
  
  #------------------------------------------------------------------------
  # now try custom plugins
  #------------------------------------------------------------------------
  
  $plugins = Template::Plugins->new( plugins => {
      goodbye => 'Template::Plugin::Goodbye',
  } );
  
  ok( $plugins, 'created plugins with plugins' );
  
  $plugs = $plugins->plugins();
  ok( $plugs && ref $plugs eq 'HASH', 'got plugins plugins hash' );
  is( scalar keys %$plugs, 2, 'two plugins in hash' );
  is( $plugs->{ hello }, 'Template::Plugin::Hello', 
      'hello plugin is there' );
  is( $plugs->{ goodbye }, 'Template::Plugin::Goodbye', 
      'goodbye plugin is there' );
  
  
  
  #------------------------------------------------------------------------
  # subclass object, check new $PATH gets set
  #------------------------------------------------------------------------
  
  package My::Plugins;
  use base qw( Template::Plugins );
  use vars qw( $PATH );
  $PATH = 'My::Plugin';
  
  package main;
  $plugins = My::Plugins->new();
  ok( $plugins, 'created My::Plugins object' );
  
  $path = $plugins->path();
  is( ref $path, 'ARRAY', 'got my path list' );
  is( scalar @$path, 1, 'one item in my path' );
  is( $path->[0], 'My::Plugin', 'My::Plugin in path' );
  
  
  #------------------------------------------------------------------------
  # but we can still override it
  #------------------------------------------------------------------------
  
  $plugins = My::Plugins->new( path => 'Your::Plugin');
  ok( $plugins, 'created Your::Plugin object' );
  
  $path = $plugins->path();
  is( ref $path, 'ARRAY', 'got your path list' );
  is( scalar @$path, 1, 'one item in your path' );
  is( $path->[0], 'Your::Plugin', 'Your::Plugin in path' );
  
  
  #------------------------------------------------------------------------
  # and we can disable it altogether
  #------------------------------------------------------------------------
  
  $plugins = My::Plugins->new( path => 0 );
  ok( $plugins, 'created zero path plugins object' );
  
  $path = $plugins->path();
  is( ref $path, 'ARRAY', 'got zero path list' );
  is( scalar @$path, 0, 'zero items in path' );
  
  $plugins = My::Plugins->new( path => [] );
  ok( $plugins, 'created empty path plugins object' );
  
  $path = $plugins->path();
  is( ref $path, 'ARRAY', 'got empty path list' );
  is( scalar @$path, 0, 'no items in empty path' );
  
  
  #------------------------------------------------------------------------
  # package method
  #------------------------------------------------------------------------
  
  my $plugin = Template::Plugins->fetch('CONTEXT', 'hello') 
      || die Template::Plugins->error();
  
  ok( $plugin, 'got hello plugin' );
  
  is( $plugin->hello(), 'Hello World!', 'Hello World' );
  
  is( $plugin->hello( language => 'english'), 'Hello World!', 
      'Hello World! in english' );
  
  is( $plugin->hello( language => 'french' ), 'Salut le Monde!', 
      'Salut le Monde! in french' );
  
  
  $plugin = Template::Plugins->fetch('CONTEXT', 'hello', 
      { language => 'german' } ) 
      || die Template::Plugins->error();
  
  ok( $plugin, 'got german hello plugin' );
  
  is( $plugin->hello(), 'Hallo Welt!', 'Hallo Welt!' );
  
  is( $plugin->hello( language => 'english'), 'Hello World!', 
      'Hello World! in english again' );
  
  is( $plugin->hello( language => 'french' ), 'Salut le Monde!', 
      'Salut le Monde! in french again' );
  
  
  
  
  #------------------------------------------------------------------------
  # object method with extra plugins, path, etc.
  #------------------------------------------------------------------------
  
  
  __END__
  
  # Local Variables:
  # mode: perl
  # perl-indent-level: 4
  # indent-tabs-mode: nil
  # End:
  #
  # vim: expandtab shiftwidth=4: