1#!/usr/bin/perl
2
3use warnings;
4use strict;
5
6use Template;
7use Test::More tests => 13;
8
9use lib '/home/nik/svk/local/jc/CPAN/Template-Plugin-Subst/trunk/lib';
10my $tt = Template->new();
11
12my $template;
13my $output;
14my $expected_output;
15
16$template = <<'EOTEMPLATE';
17hello, world
18EOTEMPLATE
19
20$expected_output = "hello, world\n";
21
22$tt->process(\$template, undef, \$output);
23
24is($output, $expected_output, 'Basic template processing');
25
26$template = <<'EOTEMPLATE';
27[%- USE Subst -%]
28[%- FILTER $Subst pattern = 'foo' replacement = 'bar' -%]
29foo
30[% END -%]
31EOTEMPLATE
32
33$expected_output = "bar\n";
34
35undef $output;
36$tt->process(\$template, undef, \$output);
37
38is($output, $expected_output, 'Simple replacement');
39
40$template = <<'EOTEMPLATE';
41[%- USE Subst -%]
42[%- FILTER $Subst pattern = 'foo' replacement = 'bar' -%]
43This is a 'foo' test
44[% END -%]
45EOTEMPLATE
46
47$expected_output = "This is a 'bar' test\n";
48
49undef $output;
50$tt->process(\$template, undef, \$output);
51
52is($output, $expected_output, 'Simple replacement (2)');
53
54$template = <<'EOTEMPLATE';
55[%- USE Subst -%]
56[%- FILTER $Subst pattern = 'foo' replacement = 'bar' -%]
57This is a 'foo' test.
58
59Another line of foo.
60
61Here's (foo) another one.
62[% END -%]
63EOTEMPLATE
64
65$expected_output = <<'EOT';
66This is a 'bar' test.
67
68Another line of bar.
69
70Here's (bar) another one.
71EOT
72
73undef $output;
74$tt->process(\$template, undef, \$output);
75
76is($output, $expected_output, 'Search/replace across multiple lines');
77
78$template = <<'EOTEMPLATE';
79[%- USE Subst -%]
80[%- FILTER $Subst pattern = '(foo)(bar)' replacement = '$2$1' -%]
81foobar
82[% END -%]
83EOTEMPLATE
84
85$expected_output = "barfoo\n";
86undef $output;
87$tt->process(\$template, undef, \$output);
88is($output, $expected_output, 'Backrefs work');
89
90$template = <<'EOTEMPLATE';
91[%- USE f = Subst pattern = '(foo)(bar)' replacement = '$2$1' -%]
92[%- str = 'foobar' -%]
93[% str | $f %]
94EOTEMPLATE
95
96$expected_output = "barfoo\n";
97undef $output;
98$tt->process(\$template, undef, \$output);
99is($output, $expected_output, 'Works with | notation');
100
101$template = <<'EOTEMPLATE';
102[%- USE f = Subst pattern = '(foo)(bar)' replacement = '[$2][$1]' -%]
103[%- str = 'foobar' -%]
104[% str | $f %]
105EOTEMPLATE
106
107$expected_output = "[bar][foo]\n";
108undef $output;
109$tt->process(\$template, undef, \$output);
110is($output, $expected_output, 'Adding extra content works');
111
112$template = <<'EOTEMPLATE';
113[%- USE f = Subst pattern = 'foo' replacement = 'bar' -%]
114[%- str = 'A foo, and another foo, and yet another foo' -%]
115[% str | $f %]
116EOTEMPLATE
117
118$expected_output = "A bar, and another bar, and yet another bar\n";
119undef $output;
120$tt->process(\$template, undef, \$output);
121is($output, $expected_output, 'Default behaviour is global');
122
123$template = <<'EOTEMPLATE';
124[%- USE f = Subst pattern = 'foo' replacement = 'bar' global = 0 -%]
125[%- str = 'A foo, and another foo, and yet another foo' -%]
126[% str | $f %]
127EOTEMPLATE
128
129$expected_output = "A bar, and another foo, and yet another foo\n";
130undef $output;
131$tt->process(\$template, undef, \$output);
132is($output, $expected_output, '\'global = 0\' works');
133
134$template = <<'EOTEMPLATE';
135[%- USE f = Subst pattern = '(foo)' replacement = '[$1]' global = 0 -%]
136[%- str = 'A foo, and another foo, and yet another foo' -%]
137[% str | $f %]
138EOTEMPLATE
139
140$expected_output = "A [foo], and another foo, and yet another foo\n";
141undef $output;
142$tt->process(\$template, undef, \$output);
143is($output, $expected_output, '\'global = 0\' works with backrefs');
144
145$template = <<'EOTEMPLATE';
146[%- USE f = Subst pattern = 'foo' replacement = 'bar' -%]
147[%- str = 'A foo, and a FOO' -%]
148[% str | $f %]
149EOTEMPLATE
150
151$expected_output = "A bar, and a FOO\n";
152undef $output;
153$tt->process(\$template, undef, \$output);
154is($output, $expected_output, 'Matches are case sensitive by default');
155
156$template = <<'EOTEMPLATE';
157[%- USE f = Subst pattern = '(?i)foo' replacement = 'bar' -%]
158[%- str = 'A foo, and a FOO' -%]
159[% str | $f %]
160EOTEMPLATE
161
162$expected_output = "A bar, and a bar\n";
163undef $output;
164$tt->process(\$template, undef, \$output);
165is($output, $expected_output, '  but (?i) works in the pattern');
166
167$template = <<'EOTEMPLATE';
168[%- USE f = Subst pattern = '\$(\d)' replacement = 'USD$1' -%]
169[%- str = 'How much money do you want:  $1 or $2 ?' -%]
170[% str | $f %]
171EOTEMPLATE
172
173$expected_output = "How much money do you want:  USD1 or USD2 ?\n";
174undef $output;
175$tt->process(\$template, undef, \$output);
176is($output, $expected_output, '$ in pattern seems to work');
177
178