Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 209 lines (167 sloc) 6.006 kB
31d816f @abw 2.04
authored
1 #!/usr/bin/perl -w # -*- perl -*-
3ebfa07 @abw Initial revision
authored
2 #============================================================= -*-perl-*-
3 #
4 # t/context.t
5 #
6 # Test the Template::Context.pm module.
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
20 use strict;
21 use lib qw( ./lib ../lib );
22 use Template::Test;
4e99583 @abw * various minor updates to accomodate new DEBUG options
authored
23 use Template::Constants qw( :debug );
3ebfa07 @abw Initial revision
authored
24
4e99583 @abw * various minor updates to accomodate new DEBUG options
authored
25 my $DEBUG = grep(/^--?d(debug)?$/, @ARGV);
82cbea8 @abw version 2.00
authored
26 #$Template::Test::DEBUG = 1;
3ebfa07 @abw Initial revision
authored
27
7a87144 @abw added Dumper plugin, fixed DOM, misc other fixes
authored
28 ntests(54);
b4f721c @abw *** empty log message ***
authored
29
30 # script may be being run in distribution root or 't' directory
31 my $dir = -d 't' ? 't/test' : 'test';
3ebfa07 @abw Initial revision
authored
32 my $tt = Template->new({
b4f721c @abw *** empty log message ***
authored
33 INCLUDE_PATH => "$dir/src:$dir/lib",
3ebfa07 @abw Initial revision
authored
34 TRIM => 1,
35 POST_CHOMP => 1,
4e99583 @abw * various minor updates to accomodate new DEBUG options
authored
36 DEBUG => $DEBUG ? DEBUG_CONTEXT : 0,
3ebfa07 @abw Initial revision
authored
37 });
38
39 my $ttperl = Template->new({
b4f721c @abw *** empty log message ***
authored
40 INCLUDE_PATH => "$dir/src:$dir/lib",
3ebfa07 @abw Initial revision
authored
41 TRIM => 1,
42 EVAL_PERL => 1,
43 POST_CHOMP => 1,
4e99583 @abw * various minor updates to accomodate new DEBUG options
authored
44 DEBUG => $DEBUG ? DEBUG_CONTEXT : 0,
3ebfa07 @abw Initial revision
authored
45 });
46
b4f721c @abw *** empty log message ***
authored
47 #------------------------------------------------------------------------
48 # misc
49 #------------------------------------------------------------------------
50
3ebfa07 @abw Initial revision
authored
51 # test we created a context object and check internal values
52 my $context = $tt->service->context();
53 ok( $context );
b4f721c @abw *** empty log message ***
authored
54 ok( $context eq $tt->context() );
3ebfa07 @abw Initial revision
authored
55 ok( $context->trim() );
56 ok( ! $context->eval_perl() );
57
58 ok( $context = $ttperl->service->context() );
59 ok( $context->trim() );
60 ok( $context->eval_perl() );
61
b4f721c @abw *** empty log message ***
authored
62 #------------------------------------------------------------------------
63 # template()
64 #------------------------------------------------------------------------
65
66 banner('testing template()');
67
3ebfa07 @abw Initial revision
authored
68 # test we can fetch a template via template()
69 my $template = $context->template('header');
70 ok( $template );
71 ok( UNIVERSAL::isa($template, 'Template::Document') );
72
73 # test that non-existance of a template is reported
82cbea8 @abw version 2.00
authored
74 eval { $template = $context->template('no_such_template') };
75 ok( $@ );
76 ok( "$@" eq 'file error - no_such_template: not found' );
3ebfa07 @abw Initial revision
authored
77
78 # check that template() returns CODE and Template::Document refs intact
79 my $code = sub { return "this is a hard-coded template" };
80 $template = $context->template($code);
81 ok( $template eq $code );
82
83 my $doc = "this is a document";
84 $doc = bless \$doc, 'Template::Document';
85 $template = $context->template($doc);
86 ok( $template eq $doc );
87 ok( $$doc = 'this is a document' );
88
89 # check the use of visit() and leave() to add temporary BLOCK lookup
90 # tables to the context's search space
91 my $blocks1 = {
92 some_block_1 => 'hello',
93 };
94 my $blocks2 = {
95 some_block_2 => 'world',
96 };
97
82cbea8 @abw version 2.00
authored
98 eval { $context->template('some_block_1') };
99 ok( $@ );
61ffc3f @abw * updated tests to pass document as first arg to visit()
authored
100 $context->visit('no doc', $blocks1);
82cbea8 @abw version 2.00
authored
101 ok( $context->template('some_block_1') eq 'hello' );
102 eval { $context->template('some_block_2') };
103 ok( $@ );
61ffc3f @abw * updated tests to pass document as first arg to visit()
authored
104 $context->visit('no doc', $blocks2);
3ebfa07 @abw Initial revision
authored
105 ok( $context->template('some_block_1') eq 'hello' );
106 ok( $context->template('some_block_2') eq 'world' );
107 $context->leave();
108 ok( $context->template('some_block_1') eq 'hello' );
82cbea8 @abw version 2.00
authored
109 eval { $context->template('some_block_2') };
110 ok( $@ );
3ebfa07 @abw Initial revision
authored
111 $context->leave();
82cbea8 @abw version 2.00
authored
112 eval { $context->template('some_block_1') };
113 ok( $@ );
114 eval { $context->template('some_block_2') };
115 ok( $@ );
116
3ebfa07 @abw Initial revision
authored
117
118 # test that reset() clears all blocks
61ffc3f @abw * updated tests to pass document as first arg to visit()
authored
119 $context->visit('no doc', $blocks1);
3ebfa07 @abw Initial revision
authored
120 ok( $context->template('some_block_1') eq 'hello' );
82cbea8 @abw version 2.00
authored
121 eval { $context->template('some_block_2') };
122 ok( $@ );
61ffc3f @abw * updated tests to pass document as first arg to visit()
authored
123 $context->visit('no doc', $blocks2);
3ebfa07 @abw Initial revision
authored
124 ok( $context->template('some_block_1') eq 'hello' );
125 ok( $context->template('some_block_2') eq 'world' );
126 $context->reset();
82cbea8 @abw version 2.00
authored
127 eval { $context->template('some_block_1') };
128 ok( $@ );
129 eval { $context->template('some_block_2') };
130 ok( $@ );
3ebfa07 @abw Initial revision
authored
131
b4f721c @abw *** empty log message ***
authored
132 #------------------------------------------------------------------------
133 # plugin()
134 #------------------------------------------------------------------------
135
136 banner('testing plugin()');
137
138 my $plugin = $context->plugin('Table', [ [1,2,3,4], { rows => 2 } ]);
139 ok( $plugin );
140 ok( ref $plugin eq 'Template::Plugin::Table' );
141
142 my $row = $plugin->row(0);
143 ok( $row && ref $row eq 'ARRAY' );
144 ok( $row->[0] == 1 );
145 ok( $row->[1] == 3 );
146
7a87144 @abw added Dumper plugin, fixed DOM, misc other fixes
authored
147 eval {
148 $plugin = $context->plugin('no_such_plugin');
149 };
150 ok( "$@" eq 'plugin error - no_such_plugin: plugin not found' );
b4f721c @abw *** empty log message ***
authored
151
152 #------------------------------------------------------------------------
153 # filter()
154 #------------------------------------------------------------------------
155
156 banner('testing filter()');
157
158 my $filter = $context->filter('html');
159 ok( $filter );
160 ok( ref $filter eq 'CODE' );
161 ok( &$filter('<input/>') eq '&lt;input/&gt;' );
162
31d816f @abw 2.04
authored
163 $filter = $context->filter('replace', [ 'foo', 'bar' ], 'repsave');
b4f721c @abw *** empty log message ***
authored
164 ok( $filter );
165 ok( ref $filter eq 'CODE' );
166 ok( &$filter('this is foo, so it is') eq 'this is bar, so it is' );
167
168 # check filter got cached
31d816f @abw 2.04
authored
169 $filter = $context->filter('repsave');
b4f721c @abw *** empty log message ***
authored
170 ok( $filter );
171 ok( ref $filter eq 'CODE' );
31d816f @abw 2.04
authored
172 match( &$filter('this is foo, so it is'), 'this is bar, so it is' );
b4f721c @abw *** empty log message ***
authored
173
174
175 #------------------------------------------------------------------------
176 # include() and process()
177 #------------------------------------------------------------------------
178
179 banner('testing include()');
180
181 $context = $tt->context();
182 ok( $context );
183
184 my $stash = $context->stash();
185 ok( $stash );
186
187 $stash->set('a', 'alpha');
188 ok( $stash->get('a') eq 'alpha' );
189
190 my $text = $context->include('baz');
191 ok( $text eq 'This is the baz file, a: alpha' );
192
193 $text = $context->include('baz', { a => 'bravo' });
194 ok( $text eq 'This is the baz file, a: bravo' );
195
196 # check stash hasn't been altered
197 ok( $stash->get('a') eq 'alpha' );
198
199 $text = $context->process('baz');
200 ok( $text eq 'This is the baz file, a: alpha' );
3ebfa07 @abw Initial revision
authored
201
b4f721c @abw *** empty log message ***
authored
202 # check stash *has* been altered
203 ok( $stash->get('a') eq 'charlie' );
3ebfa07 @abw Initial revision
authored
204
b4f721c @abw *** empty log message ***
authored
205 $text = $context->process('baz', { a => 'bravo' });
206 ok( $text eq 'This is the baz file, a: bravo' );
207 ok( $stash->get('a') eq 'charlie' );
3ebfa07 @abw Initial revision
authored
208
Something went wrong with that request. Please try again.