Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

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