1#============================================================= -*-perl-*-
2#
3# t/chomp.t
4#
5# Test the PRE_CHOMP and POST_CHOMP options.
6#
7# Written by Andy Wardley <abw@wardley.org>
8#
9# Copyright (C) 1996-2009 Andy Wardley.  All Rights Reserved.
10#
11# This is free software; you can redistribute it and/or modify it
12# under the same terms as Perl itself.
13#
14#========================================================================
15
16use strict;
17use warnings;
18use lib qw( ./lib ../lib );
19use Template::Test;
20use Template::Constants qw( :chomp );
21
22# uncomment these lines for debugging the generated Perl code
23#$Template::Directive::PRETTY = 1;
24#$Template::Parser::DEBUG = 1;
25
26match( CHOMP_NONE, 0 );
27match( CHOMP_ONE, 1 );
28match( CHOMP_ALL, 1 );
29match( CHOMP_COLLAPSE, 2 );
30match( CHOMP_GREEDY, 3 );
31
32my $foo     = "\n[% foo %]\n";
33my $bar     = "\n[%- bar -%]\n";
34my $baz     = "\n[%+ baz +%]\n";
35my $ding    = "!\n\n[%~ ding ~%]\n\n!";
36my $dong    = "!\n\n[%= dong =%]\n\n!";
37my $dang    = "Hello[%# blah blah blah -%]\n!";
38my $winsux1 = "[% ding -%]\015\012[% dong %]";
39my $winsux2 = "[% ding -%]\015\012\015\012[% dong %]";
40my $winsux3 = "[% ding %]\015\012[%- dong %]";
41my $winsux4 = "[% ding %]\015\012\015\012[%- dong %]";
42
43my $blocks = {
44    foo     => $foo,
45    bar     => $bar,
46    baz     => $baz,
47    ding    => $ding,
48    dong    => $dong,
49    dang    => $dang,
50    winsux1 => $winsux1,
51    winsux2 => $winsux2,
52    winsux3 => $winsux3,
53    winsux4 => $winsux4,
54};
55
56# script may be being run in distribution root or 't' directory
57my $dir   = -d 't' ? 't/test/lib' : 'test/lib';
58
59
60#------------------------------------------------------------------------
61# tests without any CHOMP options set
62#------------------------------------------------------------------------
63
64my $tt2 = Template->new({
65    BLOCKS       => $blocks,
66    INCLUDE_PATH => $dir,
67});
68my $vars = {
69    foo  => 3.14,
70    bar  => 2.718,
71    baz  => 1.618,
72    ding => 'Hello',
73    dong => 'World'
74};
75
76my $out;
77ok( $tt2->process('foo', $vars, \$out), 'foo' );
78match( $out, "\n3.14\n", 'foo out' );
79
80$out = '';
81ok( $tt2->process('bar', $vars, \$out), 'bar' );
82match( $out, "2.718", 'bar out' );
83
84$out = '';
85ok( $tt2->process('baz', $vars, \$out), 'baz' );
86match( $out, "\n1.618\n", 'baz out' );
87
88$out = '';
89ok( $tt2->process('ding', $vars, \$out), 'ding' );
90match( $out, "!Hello!", 'ding out' );
91
92$out = '';
93ok( $tt2->process('dong', $vars, \$out), 'dong' );
94match( $out, "! World !", 'dong out' );
95
96$out = '';
97ok( $tt2->process('dang', $vars, \$out), 'dang' );
98match( $out, "Hello!", 'dang out' );
99
100$out = '';
101ok( $tt2->process('winsux1', $vars, \$out), 'winsux1' );
102match( od($out), "HelloWorld", 'winsux1 out' );
103
104$out = '';
105ok( $tt2->process('winsux2', $vars, \$out), 'winsux2' );
106match( od($out), 'Hello\015\012World', 'winsux2 out' );
107
108$out = '';
109ok( $tt2->process('winsux3', $vars, \$out), 'winsux3' );
110match( od($out), "HelloWorld", 'winsux3 out' );
111
112$out = '';
113ok( $tt2->process('winsux4', $vars, \$out), 'winsux4' );
114match( od($out), 'Hello\015\012World', 'winsux4 out' );
115
116$out = '';
117ok( $tt2->process('dos_newlines', $vars, \$out), 'dos_newlines' );
118match( $out, "HelloWorld", 'dos_newlines out' );
119
120sub od{
121    join(
122        '', 
123        map {
124            my $ord = ord($_);
125            ($ord > 127 || $ord < 32 )
126                ? sprintf '\0%lo', $ord
127                : $_
128        } 
129        split //, shift()
130    );
131}
132
133#------------------------------------------------------------------------
134# tests with the PRE_CHOMP option set
135#------------------------------------------------------------------------
136
137$tt2 = Template->new({
138    PRE_CHOMP => 1,
139    BLOCKS => $blocks,
140});
141
142$out = '';
143ok( $tt2->process('foo', $vars, \$out), 'pre pi' );
144match( $out, "3.14\n", 'pre pi match' );
145
146$out = '';
147ok( $tt2->process('bar', $vars, \$out), 'pre e' );
148match( $out, "2.718", 'pre e match' );
149
150$out = '';
151ok( $tt2->process('baz', $vars, \$out), 'pre phi' );
152match( $out, "\n1.618\n", 'pre phi match' );
153
154$out = '';
155ok( $tt2->process('ding', $vars, \$out), 'pre hello' );
156match( $out, "!Hello!", 'pre hello match' );
157
158$out = '';
159ok( $tt2->process('dong', $vars, \$out), 'pre world' );
160match( $out, "! World !", 'pre world match' );
161
162
163#------------------------------------------------------------------------
164# tests with the POST_CHOMP option set
165#------------------------------------------------------------------------
166
167$tt2 = Template->new({
168    POST_CHOMP => 1,
169    BLOCKS => $blocks,
170});
171
172$out = '';
173ok( $tt2->process('foo', $vars, \$out), 'post pi' );
174match( $out, "\n3.14", 'post pi match' );
175
176$out = '';
177ok( $tt2->process('bar', $vars, \$out), 'post e' );
178match( $out, "2.718", 'post e match' );
179
180$out = '';
181ok( $tt2->process('baz', $vars, \$out), 'post phi' );
182match( $out, "\n1.618\n", 'post phi match' );
183
184$out = '';
185ok( $tt2->process('ding', $vars, \$out), 'post hello' );
186match( $out, "!Hello!", 'post hello match' );
187
188$out = '';
189ok( $tt2->process('dong', $vars, \$out), 'post world' );
190match( $out, "! World !", 'post world match' );
191
192
193my $tt = [
194    tt_pre_none  => Template->new(PRE_CHOMP  => CHOMP_NONE),
195    tt_pre_one   => Template->new(PRE_CHOMP  => CHOMP_ONE),
196    tt_pre_all   => Template->new(PRE_CHOMP  => CHOMP_ALL),
197    tt_pre_coll  => Template->new(PRE_CHOMP  => CHOMP_COLLAPSE),
198    tt_post_none => Template->new(POST_CHOMP => CHOMP_NONE),
199    tt_post_one  => Template->new(POST_CHOMP => CHOMP_ONE),
200    tt_post_all  => Template->new(POST_CHOMP => CHOMP_ALL),
201    tt_post_coll => Template->new(POST_CHOMP => CHOMP_COLLAPSE),
202];
203
204test_expect(\*DATA, $tt);
205
206__DATA__
207#------------------------------------------------------------------------
208# tt_pre_none
209#------------------------------------------------------------------------
210-- test --
211begin[% a = 10; b = 20 %]
212     [% a %]
213     [% b %]
214end
215-- expect --
216begin
217     10
218     20
219end
220
221#------------------------------------------------------------------------
222# tt_pre_one
223#------------------------------------------------------------------------
224-- test --
225-- use tt_pre_one --
226-- test --
227begin[% a = 10; b = 20 %]
228     [% a %]
229     [% b %]
230end
231-- expect --
232begin1020
233end
234
235
236#------------------------------------------------------------------------
237# tt_pre_all
238#------------------------------------------------------------------------
239-- test --
240-- use tt_pre_all --
241-- test --
242begin[% a = 10; b = 20 %]
243     [% a %]
244     [% b %]
245end
246-- expect --
247begin1020
248end
249
250#------------------------------------------------------------------------
251# tt_pre_coll
252#------------------------------------------------------------------------
253-- test --
254-- use tt_pre_coll --
255-- test --
256begin[% a = 10; b = 20 %]
257     [% a %]
258     [% b %]
259end
260-- expect --
261begin 10 20
262end
263
264
265#------------------------------------------------------------------------
266# tt_post_none
267#------------------------------------------------------------------------
268-- test --
269-- use tt_post_none --
270begin[% a = 10; b = 20 %]
271     [% a %]
272     [% b %]
273end
274-- expect --
275begin
276     10
277     20
278end
279
280#------------------------------------------------------------------------
281# tt_post_all
282#------------------------------------------------------------------------
283-- test --
284-- use tt_post_all --
285-- test --
286begin[% a = 10; b = 20 %]
287     [% a %]
288     [% b %]
289end
290-- expect --
291begin     10     20end
292
293#------------------------------------------------------------------------
294# tt_post_one
295#------------------------------------------------------------------------
296-- test --
297-- use tt_post_one --
298-- test --
299begin[% a = 10; b = 20 %]
300     [% a %]
301     [% b %]
302end
303-- expect --
304begin     10     20end
305
306#------------------------------------------------------------------------
307# tt_post_coll
308#------------------------------------------------------------------------
309-- test --
310-- use tt_post_coll --
311-- test --
312begin[% a = 10; b = 20 %]     
313[% a %]     
314[% b %]     
315end
316-- expect --
317begin 10 20 end
318
319