1#============================================================= -*-perl-*-
2#
3# t/compile2.t
4#
5# Test that the compiled template files written by compile1.t can be 
6# loaded and used.
7#
8# Written by Andy Wardley <abw@kfs.org>
9#
10# Copyright (C) 1996-2000 Andy Wardley.  All Rights Reserved.
11# Copyright (C) 1998-2000 Canon Research Centre Europe Ltd.
12#
13# This is free software; you can redistribute it and/or modify it
14# under the same terms as Perl itself.
15#
16# $Id$
17#
18#========================================================================
19
20use strict;
21use lib qw( ./lib ../lib );
22use Template::Test;
23use File::Spec;
24$^W = 1;
25
26# script may be being run in distribution root or 't' directory
27my @dir   = -d 't' ? qw(t test src) : qw(test src);
28my $dir   = File::Spec->catfile(@dir);
29my $zero  = File::Spec->catfile(@dir, 'divisionbyzero');
30my $ttcfg = {
31    POST_CHOMP   => 1,
32    INCLUDE_PATH => $dir,
33    COMPILE_EXT  => '.ttc',
34    CONSTANTS    => {
35        zero => $zero,
36    },
37};
38
39my $compiled = "$dir/foo.ttc";
40
41# check compiled template files exist
42ok( -f $compiled );
43ok( -f "$dir/complex.ttc" );
44
45# ensure template metadata is saved in compiled file (bug fixed in v2.00)
46my $out = '';
47my $tt = Template->new($ttcfg);
48ok( $tt->process('baz', { showname => 1 }, \$out) );
49ok( scalar $out =~ /^name: baz/ );
50
51# we're going to hack on the foo.ttc file to change some key text.
52# this way we can tell that the template was loaded from the compiled
53# version and not the source.
54
55
56my @current_times = (stat $compiled)[8,9];
57
58open(FOO, $compiled) || die "$compiled: $!\n";
59local $/ = undef;
60my $foo = <FOO>;
61close(FOO);
62
63$foo =~ s/the foo file/the hacked foo file/;
64open(FOO, "> $compiled") || die "$compiled: $!\n";
65print FOO $foo;
66close(FOO);
67
68# Set mtime back to what it was
69utime( @current_times, $compiled );
70
71test_expect(\*DATA, $ttcfg);
72
73
74__DATA__
75-- test --
76[% INCLUDE foo a = 'any value' %]
77-- expect --
78This is the hacked foo file, a is any value
79
80-- test --
81[% META author => 'billg' version => 6.66  %]
82[% INCLUDE complex %]
83-- expect --
84This is the header, title: Yet Another Template Test
85This is a more complex file which includes some BLOCK definitions
86This is the footer, author: billg, version: 6.66
87- 3 - 2 - 1 
88
89-- test --
90[% META author => 'billg' version => 6.66  %]
91[% INCLUDE complex %]
92-- expect --
93This is the header, title: Yet Another Template Test
94This is a more complex file which includes some BLOCK definitions
95This is the footer, author: billg, version: 6.66
96- 3 - 2 - 1 
97
98
99-- test --
100[%- # second pass, reads the compiled code from cache -%]
101[% INCLUDE divisionbyzero -%]
102-- expect --
103-- process --
104undef error - Illegal division by zero at [% constants.zero %] line 1, <DATA> chunk 1.
105