Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 158 lines (138 sloc) 3.648 kB
3ebfa07 @abw Initial revision
authored
1 #============================================================= -*-perl-*-
2 #
3 # t/document.t
4 #
5 # Test the Template::Document 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
19 use strict;
20 use lib qw( ./lib ../lib );
21 use Template::Test;
22 use Template::Config;
23 use Template::Document;
24
25 $^W = 1;
26 $Template::Test::DEBUG = 0;
27 $Template::Document::DEBUG = 0;
d52480d @abw *** empty log message ***
authored
28 #$Template::Parser::DEBUG = 1;
29 #$Template::Directive::PRETTY = 1;
3ebfa07 @abw Initial revision
authored
30 my $DEBUG = 0;
31
32
33 #------------------------------------------------------------------------
34 # define a dummy context object for runtime processing
35 #------------------------------------------------------------------------
36 package Template::DummyContext;
37 sub new { bless { }, shift }
38 sub visit { }
39 sub leave { }
40
41 package main;
42
43 #------------------------------------------------------------------------
44 # create a document and check accessor methods for blocks and metadata
45 #------------------------------------------------------------------------
7021422 @abw *** empty log message ***
authored
46 my $doc = Template::Document->new({
47 BLOCK => sub { my $c = shift; return "some output" },
48 DEFBLOCKS => {
3ebfa07 @abw Initial revision
authored
49 foo => sub { return 'the foo block' },
50 bar => sub { return 'the bar block' },
51 },
7021422 @abw *** empty log message ***
authored
52 METADATA => {
3ebfa07 @abw Initial revision
authored
53 author => 'Andy Wardley',
54 version => 3.14,
7021422 @abw *** empty log message ***
authored
55 },
56 });
3ebfa07 @abw Initial revision
authored
57
58 my $c = Template::DummyContext->new();
59
60 ok( $doc );
61 ok( $doc->author() eq 'Andy Wardley' );
62 ok( $doc->version() == 3.14 );
63 ok( $doc->process($c) eq 'some output' );
64 ok( ref($doc->block()) eq 'CODE' );
65 ok( ref($doc->blocks->{ foo }) eq 'CODE' );
66 ok( ref($doc->blocks->{ bar }) eq 'CODE' );
67 ok( &{ $doc->block } eq 'some output' );
68 ok( &{ $doc->blocks->{ foo } } eq 'the foo block' );
69 ok( &{ $doc->blocks->{ bar } } eq 'the bar block' );
70
fab54ce @abw version 2.13
authored
71 my $dir = -d 't' ? 't/test' : 'test';
72 my $tproc = Template->new({
73 INCLUDE_PATH => "$dir/src",
74 });
75
76 test_expect(\*DATA, $tproc, { mydoc => $doc });
3ebfa07 @abw Initial revision
authored
77
78 __END__
79 -- test --
80 # test metadata
81 [% META
82 author = 'Tom Smith'
83 version = 1.23
84 -%]
85 version [% template.version %] by [% template.author %]
86 -- expect --
87 version 1.23 by Tom Smith
88
89 # test local block definitions are accessible
90 -- test --
91 [% BLOCK foo -%]
92 This is block foo
93 [% INCLUDE bar -%]
94 This is the end of block foo
95 [% END -%]
96 [% BLOCK bar -%]
97 This is block bar
98 [% END -%]
99 [% PROCESS foo %]
100
101 -- expect --
102 This is block foo
103 This is block bar
104 This is the end of block foo
105
850d474 @abw *** empty log message ***
authored
106 -- test --
107 [% META title = 'My Template Title' -%]
108 [% BLOCK header -%]
109 title: [% template.title or title %]
110 [% END -%]
111 [% INCLUDE header %]
112 -- expect --
113 title: My Template Title
114
115 -- test --
d52480d @abw *** empty log message ***
authored
116 [% BLOCK header -%]
117 HEADER
118 component title: [% component.name %]
119 template title: [% template.name %]
120 [% END -%]
121 component title: [% component.name %]
122 template title: [% template.name %]
123 [% PROCESS header %]
124 -- expect --
125 component title: input text
126 template title: input text
127 HEADER
128 component title: header
129 template title: input text
130
131 -- test --
850d474 @abw *** empty log message ***
authored
132 [% META title = 'My Template Title' -%]
133 [% BLOCK header -%]
134 title: [% title or template.title %]
135 [% END -%]
136 [% INCLUDE header title = 'A New Title' %]
137 [% INCLUDE header %]
138 -- expect --
139 title: A New Title
140
141 title: My Template Title
7dc261c @abw *** empty log message ***
authored
142
143 -- test --
144 [% INCLUDE $mydoc %]
145 -- expect --
146 some output
3e45060 @abw *** empty log message ***
authored
147
fab54ce @abw version 2.13
authored
148 -- stop --
149 # test for component.caller and component.callers patch
150 -- test --
151 [% INCLUDE one;
152 INCLUDE two;
153 INCLUDE three;
154 %]
155 -- expect --
156 one, three
157 two, three
Something went wrong with that request. Please try again.