[Templates-cvs] cvs commit: TT3/t provider.t
cvs@template-toolkit.org
cvs@template-toolkit.org
Tue, 23 Mar 2004 14:15:17 +0000
cvs 04/03/23 14:15:17
Modified: t provider.t
Log:
* updated provider test to include files
Revision Changes Path
1.3 +54 -25 TT3/t/provider.t
Index: provider.t
===================================================================
RCS file: /template-toolkit/TT3/t/provider.t,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- provider.t 2004/03/23 13:34:58 1.2
+++ provider.t 2004/03/23 14:15:17 1.3
@@ -9,7 +9,7 @@
# This is free software; you can redistribute it and/or modify it
# under the same terms as Perl itself.
#
-# $Id: provider.t,v 1.2 2004/03/23 13:34:58 abw Exp $
+# $Id: provider.t,v 1.3 2004/03/23 14:15:17 abw Exp $
#
#========================================================================
@@ -19,13 +19,18 @@
use lib qw( ./lib ../lib );
use File::Spec;
use Template::Provider;
-use Test::More tests => 63;
+use Test::More tests => 70;
my $DEBUG =
$Template::Provider::DEBUG =
grep(/^--?d(ebug)?/, @ARGV);
+my $dir = -d 'testdata' ? 'testdata' :
+ -d 't/testdata' ? 't/testdata' :
+ -d '../testdata' ? '../testdata' :
+ die "cannot locate testdata directory";
+
#------------------------------------------------------------------------
# basic test of constructor
#------------------------------------------------------------------------
@@ -286,45 +291,69 @@
#------------------------------------------------------------------------
-# test join_path() method
+# test fetch() method with real files
#------------------------------------------------------------------------
-is( $prov->join_path('/foo', '/bar'), '/foo/bar', '/foo/bar path 1' );
-is( $prov->join_path('/foo/', '/bar'), '/foo/bar', '/foo/bar path 2' );
-is( $prov->join_path('/foo', 'bar'), '/foo/bar', '/foo/bar path 3' );
+my $a = File::Spec->catfile($dir, 'prova');
+my $b = File::Spec->catfile($dir, 'provb');
+my $c = File::Spec->catfile($dir, 'provb', 'provc');
+$prov = $pkg->new({ path => [ $a, $b, $c ] });
+ok( $prov, 'created file provider' );
-#------------------------------------------------------------------------
-# test fetch() and fetch_path() methods in base class
-#------------------------------------------------------------------------
-$prov = $pkg->new({
- path => ['/foo', '/bar'],
- templates => {
- '/foo/wiz' => 'Hello World',
- '/bar/waz' => 'Goodbye World',
- },
-}) || die $pkg->error();
+my $foo = $prov->fetch('foo') || die $prov->error();
+ok( $foo, 'provided file foo' );
+my $text = $foo->text()
+ || die $foo->error();
+ok( $text, 'got foo text' );
+ok( $$text =~ /^This is prova\/foo/, 'got foo text' );
+
+my $freshwoz = $prov->fetch('/wiz/waz/woz', fresh => 1) || die $prov->error();
+ok( $freshwoz, 'provided fresh file woz' );
+
+# check that fresh got set to 1
+is( $freshwoz->fresh(), 1, 'fresh woz file is always fresh' );
+
+my $woz = $prov->fetch('/wiz/waz/woz', life => 1) || die $prov->error();
+ok( $woz, 'provided woz file with life of 1 second' );
+
+# check that life got set to 1
+is( $woz->life(), 1, 'woz life is 1 second' );
+
+$text = $woz->text()
+ || die $woz->error();
+ok( $text, 'got foo text' );
+ok( $$text =~ /^This is the woz file/, 'got woz text' );
+
+# ah! minty fresh breath!
+ok( $woz->fresh(), 'file is fresh' );
+# mess with the file to update modification time
+my $wozfile = File::Spec->catfile($a, 'wiz/waz/woz');
+rename($wozfile, "$wozfile.old");
+open(FP, ">$wozfile") || die "$wozfile: $!\n";
+print FP $$text;
+close(FP);
-my $src = $prov->fetch('wiz') || die $prov->error();
+ok( 1, 'going to sleep for a second for freshness check...' );
+sleep(1);
-is( ${$src->text()}, 'Hello World', 'hello world' );
-ok( $src->fresh(), 'hello world source is fresh' );
+# not so fresh now
+ok( ! $woz->fresh(), 'file is stale' );
-$src = $prov->fetch('waz') || die $prov->error();
+# but freshwoz is always fresh
+ok( $freshwoz->fresh(), 'fresh woz is still fresh' );
-is( ${$src->text()}, 'Goodbye World', 'goodbye world' );
-ok( $src->fresh(), 'goodbye world source is fresh' );
#------------------------------------------------------------------------
# test subclass fetch() method
#------------------------------------------------------------------------
-ok( ! $prov->fetch('foo'), 'no fetch' );
-is( $prov->error(), 'foo not found',
- 'foo not found' );
+ok( ! $prov->fetch('nosuchfile'), 'no fetch' );
+is( $prov->error(), 'nosuchfile not found',
+ 'nosuchfile not found' );
package Provider1;
use base qw( Template::Provider );