Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 245 lines (176 sloc) 6.626 kb
3ebfa07 @abw Initial revision
authored
1 #============================================================= -*-perl-*-
2 #
3 # t/config.t
4 #
5 # Test the Template::Config 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 vars qw( $DEBUG );
22 use Template::Test;
23 use Template::Config;
24
f8656e4 @abw *** empty log message ***
authored
25 ntests(44);
3ebfa07 @abw Initial revision
authored
26 $DEBUG = 0;
b4f721c @abw *** empty log message ***
authored
27 $Template::Config::DEBUG = 0;
3ebfa07 @abw Initial revision
authored
28
29 my $factory = 'Template::Config';
30
31 #------------------------------------------------------------------------
32 # parser
33 #------------------------------------------------------------------------
34
35 print STDERR "Testing parser...\n" if $DEBUG;
36 my $parser;
37
38 $parser = $factory->parser(PRE_CHOMP => 1, INTERPOLATE => 1)
39 || print STDERR $factory->error(), "\n";
40
41 ok( $parser );
42 ok( $parser->{ PRE_CHOMP } == 1);
43 ok( $parser->{ INTERPOLATE } == 1);
44
45 $parser = $factory->parser({ POST_CHOMP => 1 })
46 || print STDERR $factory->error(), "\n";
47
48 ok( $parser );
49 ok( $parser->{ POST_CHOMP } == 1);
50
51
52 #------------------------------------------------------------------------
53 # provider
54 #------------------------------------------------------------------------
55
56 print STDERR "Testing provider...\n" if $DEBUG;
57 my $provider;
58
59 $provider = $factory->provider(INCLUDE_PATH => 'here:there',
60 PARSER => $parser)
61 || print STDERR $factory->error(), "\n";
62
63 ok( $provider );
64 ok( join('...', @{ $provider->{ INCLUDE_PATH } }) eq 'here...there' );
65 ok( $provider->{ PARSER }->{ POST_CHOMP } == 1);
66
67 $provider = $factory->provider({
68 INCLUDE_PATH => 'cat:mat',
d52480d @abw *** empty log message ***
authored
69 ANYCASE => 1,
3ebfa07 @abw Initial revision
authored
70 INTERPOLATE => 1
71 }) || print STDERR $factory->error(), "\n";
72
73 ok( $provider );
74 ok( join('...', @{ $provider->{ INCLUDE_PATH } }) eq 'cat...mat' );
b4f721c @abw *** empty log message ***
authored
75
76 # force provider to instantiate a parser and check it uses the correct
77 # parameters.
78 my $text = 'The cat sat on the mat';
79 ok( $provider->fetch(\$text) );
d52480d @abw *** empty log message ***
authored
80 ok( $provider->{ PARSER }->{ ANYCASE } == 1);
3ebfa07 @abw Initial revision
authored
81 ok( $provider->{ PARSER }->{ INTERPOLATE } == 1);
82
83
84 #------------------------------------------------------------------------
85 # plugins
86 #------------------------------------------------------------------------
87
88 print STDERR "Testing plugins...\n" if $DEBUG;
89 my $plugins;
90
91 $plugins = $factory->plugins(PLUGIN_BASE => 'MyPlugins')
92 || print STDERR $factory->error(), "\n";
93
94 ok( $plugins );
95 ok( join('+', @{$plugins->{ PLUGIN_BASE }}) eq 'MyPlugins+Template::Plugin' );
96
97 $plugins = $factory->plugins({
98 LOAD_PERL => 1,
99 PLUGIN_BASE => 'NewPlugins',
100 }) || print STDERR $factory->error(), "\n";
101
102 ok( $plugins );
103 ok( $plugins->{ LOAD_PERL } == 1 );
104 ok( join('+', @{$plugins->{ PLUGIN_BASE }}) eq 'NewPlugins+Template::Plugin' );
105
106
107 #------------------------------------------------------------------------
108 # filters
109 #------------------------------------------------------------------------
110
111 print STDERR "Testing filters...\n" if $DEBUG;
112 my $filters;
113
114 $filters = $factory->filters(TOLERANT => 1)
115 || print STDERR $factory->error(), "\n";
116
117 ok( $filters );
118 ok( $filters->{ TOLERANT } == 1);
119
120 $filters = $factory->filters({ TOLERANT => 1 })
121 || print STDERR $factory->error(), "\n";
122
123 ok( $filters );
124 ok( $filters->{ TOLERANT } == 1);
125
126
127
128 #------------------------------------------------------------------------
129 # stash
130 #------------------------------------------------------------------------
131
132 print STDERR "Testing stash...\n" if $DEBUG;
133 my $stash;
134
135 $stash = $factory->stash(foo => 10, bar => 20)
136 || print STDERR $factory->error(), "\n";
137
138 ok( $stash );
139 ok( $stash->get('foo') == 10);
140 ok( $stash->get('bar') == 20);
141
142 $stash = $factory->stash({
143 foo => 30,
144 bar => sub { 'forty' },
145 }) || print STDERR $factory->error(), "\n";
146
147 ok( $stash );
148 ok( $stash->get('foo') == 30);
149 ok( $stash->get('bar') eq 'forty' );
150
151
152 #------------------------------------------------------------------------
153 # context
154 #------------------------------------------------------------------------
155
156 print STDERR "Testing context...\n" if $DEBUG;
157 my $context;
158
159 $context = $factory->context()
160 || print STDERR $factory->error(), "\n";
161
162 ok( $context );
163
164 $context = $factory->context(INCLUDE_PATH => 'anywhere')
165 || print STDERR $factory->error(), "\n";
166
167 ok( $context );
e39913f @abw *** empty log message ***
authored
168 ok( $context->{ LOAD_TEMPLATES }->[0]->{ INCLUDE_PATH }->[0] eq 'anywhere' );
3ebfa07 @abw Initial revision
authored
169
170 $context = $factory->context({
e39913f @abw *** empty log message ***
authored
171 LOAD_TEMPLATES => [ $provider ],
172 LOAD_PLUGINS => [ $plugins ],
173 LOAD_FILTERS => [ $filters ],
174 STASH => $stash,
3ebfa07 @abw Initial revision
authored
175 }) || print STDERR $factory->error(), "\n";
176
177 ok( $context );
178 ok( $context->stash->get('foo') == 30 );
e39913f @abw *** empty log message ***
authored
179 ok( $context->{ LOAD_TEMPLATES }->[0]->{ PARSER }->{ INTERPOLATE } == 1);
180 ok( $context->{ LOAD_PLUGINS }->[0]->{ LOAD_PERL } == 1 );
181 ok( $context->{ LOAD_FILTERS }->[0]->{ TOLERANT } == 1 );
3ebfa07 @abw Initial revision
authored
182
183 #------------------------------------------------------------------------
184 # service
185 #------------------------------------------------------------------------
186
187 print STDERR "Testing service...\n" if $DEBUG;
188 my $service;
189
190 $service = $factory->service(INCLUDE_PATH => 'amsterdam')
191 || print STDERR $factory->error(), "\n";
192
193 ok( $service );
e39913f @abw *** empty log message ***
authored
194 ok( $service->{ CONTEXT }->{ LOAD_TEMPLATES }->[0]->{ INCLUDE_PATH }->[0]
3ebfa07 @abw Initial revision
authored
195 eq 'amsterdam' );
196
3e45060 @abw *** empty log message ***
authored
197
198 #------------------------------------------------------------------------
199 # iterator
200 #------------------------------------------------------------------------
201
202
203 print STDERR "Testing iterator...\n" if $DEBUG;
204
205 my ($iterator, $value, $error);
206
207 $iterator = $factory->iterator([qw(foo bar baz)])
208 || print STDERR $factory->error(), "\n";
209
210 ok( $iterator );
211
212 ($value, $error) = $iterator->get_first();
213 ok( $value eq 'foo' );
214
215 ($value, $error) = $iterator->get_next();
216 ok( $value eq 'bar' );
217
218 ($value, $error) = $iterator->get_next();
219 ok( $value eq 'baz' );
f8656e4 @abw *** empty log message ***
authored
220
221
222 #------------------------------------------------------------------------
223 # instdir
224 #------------------------------------------------------------------------
225
226 my $idir = Template::Config->instdir();
227
228 if ($Template::Config::INSTDIR) {
229 ok( $idir eq $Template::Config::INSTDIR );
230 }
231 else {
232 ok( ! defined($idir)
233 && $Template::Config::ERROR eq 'no installation directory' );
234 }
235
236 my $tdir = Template::Config->instdir('templates');
237
238 if ($Template::Config::INSTDIR) {
239 ok( $tdir eq "$Template::Config::INSTDIR/templates" );
240 }
241 else {
242 ok( ! defined($tdir)
243 && $Template::Config::ERROR eq 'no installation directory' );
244 }
Something went wrong with that request. Please try again.