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