1#============================================================= -*-perl-*- 2# 3# t/output.t 4# 5# Test the OUTPUT and OUTPUT_PATH options of the Template.pm module. 6# 7# Written by Andy Wardley <abw@kfs.org> 8# 9# Copyright (C) 1996-2000 Andy Wardley. All Rights Reserved. 10# Copyright (C) 1998-2000 Canon Research Centre Europe Ltd. 11# 12# This is free software; you can redistribute it and/or modify it 13# under the same terms as Perl itself. 14# 15# $Id$ 16# 17#======================================================================== 18 19use strict; 20use lib qw( ./lib ../lib ); 21use Template::Test; 22 23ntests(14); 24 25my $dir = -d 't' ? 't/test' : 'test'; 26my $f1 = 'foo.bar'; 27my $f2 = 'foo.baz'; 28my $file1 = "$dir/tmp/$f1"; 29my $file2 = "$dir/tmp/$f2"; 30 31#------------------------------------------------------------------------ 32 33my $tt = Template->new({ 34 INCLUDE_PATH => "$dir/src:$dir/lib", 35 OUTPUT_PATH => "$dir/tmp", 36}) || die Template->error(); 37 38unlink($file1) if -f $file1; 39 40ok( $tt->process('foo', &callsign, $f1) ); 41ok( -f $file1 ); 42 43open(FP, $file1) || die "$file1: $!\n"; 44local $/ = undef; 45my $out = <FP>; 46close(FP); 47 48ok( 1 ); 49 50match( $out, "This is the foo file, a is alpha" ); 51 52unlink($file1); 53 54#------------------------------------------------------------------------ 55 56$tt = Template->new({ 57 INCLUDE_PATH => "$dir/src:$dir/lib", 58 OUTPUT_PATH => "$dir/tmp", 59 OUTPUT => $f2, 60}) || die Template->error(); 61 62unlink($file2) if -f $file2; 63 64ok( $tt->process('foo', &callsign) ); 65ok( -f $file2 ); 66 67open(FP, $file2) || die "$file2: $!\n"; 68local $/ = undef; 69$out = <FP>; 70close(FP); 71 72ok( 1 ); 73 74match( $out, "This is the foo file, a is alpha" ); 75 76unlink($file2); 77 78 79#------------------------------------------------------------------------ 80# test passing options like 'binmode' to Template process() method to 81# ensure they get passed onto _output() subroutine. 82#------------------------------------------------------------------------ 83package My::Template; 84use Template; 85use base qw( Template ); 86use vars qw( $MESSAGE ); 87 88sub DEBUG { 89 my $self = shift; 90 $MESSAGE = join('', @_); 91} 92 93package main; 94 95$tt = My::Template->new({ 96 INCLUDE_PATH => "$dir/src:$dir/lib", 97 OUTPUT_PATH => "$dir/tmp", 98 OUTPUT => $f2, 99}) || die Template->error(); 100 101$Template::DEBUG = 1; 102 103ok( $tt->process('foo', &callsign, undef, { binmode => 1 }), 'processed' ); 104ok( -f $file2, 'output file exists' ); 105is( $My::Template::MESSAGE, "set binmode\n", 'set binmode via hashref' ); 106 107$My::Template::MESSAGE = 'reset'; 108 109ok( $tt->process('foo', &callsign, $f2, binmode => 1), 'processed again' ); 110ok( -f $file2, 'output file exists' ); 111is( $My::Template::MESSAGE, "set binmode\n", 'set binmode via arglist' ); 112 113unlink($file2); 114 115 116 117 118 119 120