File Coverage

blib/lib/Mason/t/Filters.pm
Criterion Covered Total %
statement 74 74 100.0
branch n/a
condition n/a
subroutine 29 29 100.0
pod 0 13 0.0
total 103 116 88.7


line stmt bran cond sub pod time code
1             package Mason::t::Filters;
2             $Mason::t::Filters::VERSION = '2.24';
3 1     1   924 use Test::Warn;
  1         12870  
  1         59  
4 1     1   440 use Test::Class::Most parent => 'Mason::Test::Class';
  1         19465  
  1         6  
5              
6             sub test_basic : Tests {
7 1     1 0 219 my $self = shift;
8 1         7 $self->test_comp(
9             src => '
10             % sub { ucfirst(shift) } {{
11             <% "hello world?" %>
12             % }}
13             ',
14             expect => '
15             Hello world?
16             ',
17             );
18 1     1   138 }
  1         2  
  1         5  
19              
20             sub test_filters : Tests {
21 1     1 0 259 my $self = shift;
22 1         6 $self->test_comp(
23             src => '
24             <%class>
25             method Upper () { sub { uc(shift) } }
26             </%class>
27              
28             % $.Upper {{ # start Upper
29             Hello World.
30             % }} # end Upper
31              
32             % sub { ucfirst(shift) } {{
33             <% "hello world?" %>
34             % }}
35              
36             % sub { tr/A-Z/a-z/; $_ } {{
37             Hello World!
38             % }}
39             ',
40             expect => '
41             HELLO WORLD.
42             Hello world?
43             hello world!
44             ',
45             );
46 1     1   356 }
  1         2  
  1         5  
47              
48             sub test_filter_pipe : Tests {
49 1     1 0 267 my $self = shift;
50 1         8 $self->test_comp(
51             src => '
52             <%class>
53             method Upper () { sub { uc(shift) } }
54             method Lower () { sub { lc(shift) } }
55             method UpFirst () { sub { ucfirst(shift) } }
56             </%class>
57              
58             <% "HELLO" | Lower %>
59             <% "hello" | UpFirst %>
60             <% "HELLO" | UpFirst,Lower %>
61             <% "hello" | Lower, UpFirst %>
62             <% "HeLlO" | Lower, Upper %>
63             <% "HeLlO" | Upper, Lower %>
64             ',
65             expect => '
66             hello
67             Hello
68             Hello
69             hello
70             hello
71             HELLO
72             ',
73             );
74 1     1   304 }
  1         2  
  1         5  
75              
76             sub test_filter_block : Tests {
77 1     1 0 225 my $self = shift;
78 1         7 $self->test_comp(
79             src => '
80             <%filter MyRepeat ($count)>
81             % for (my $i=0; $i<$count; $i++) {
82             * <% $yield->() %>\
83             % }
84             </%filter>
85              
86             % my $count = 0;
87             % $.MyRepeat(3) {{
88             count = <% ++$count %>
89             % }}
90              
91             <%perl>
92             my $content = $m->filter($.MyRepeat(2), sub { "count == " . ++$count . "\n" });
93             print(uc($content));
94             </%perl>
95             ',
96             expect => '
97             * count = 1
98             * count = 2
99             * count = 3
100             * COUNT == 4
101             * COUNT == 5
102             ',
103             );
104 1     1   319 }
  1         3  
  1         4  
105              
106             sub test_lexical : Tests {
107 1     1 0 286 my $self = shift;
108 1         7 $self->test_comp(
109             src => <<'EOF',
110             % my $msg = "Hello World";
111             % sub { lc(shift) } {{
112             <% $msg %>
113             % }}
114             EOF
115             expect => 'hello world',
116             );
117 1     1   309 }
  1         2  
  1         5  
118              
119             sub test_repeat : Tests {
120 1     1 0 228 my $self = shift;
121 1         7 $self->test_comp(
122             src => <<'EOF',
123             % my $i = 1;
124             % $.Repeat(3) {{
125             i = <% $i++ %>
126             % }}
127             EOF
128             expect => <<'EOF',
129             i = 1
130             i = 2
131             i = 3
132             EOF
133             );
134 1     1   302 }
  1         3  
  1         4  
135              
136             sub test_nested : Tests {
137 1     1 0 231 my $self = shift;
138 1         8 $self->test_comp(
139             src => <<'EOF',
140             % sub { ucfirst(shift) } {{
141             % sub { tr/e/a/; $_ } {{
142             % sub { lc(shift) } {{
143             HELLO
144             % }}
145             % }}
146             % }}
147             goodbye
148              
149             % sub { ucfirst(shift) }, sub { tr/e/a/; $_ }, sub { lc(shift) } {{
150             HELLO
151             % }}
152             goodbye
153             EOF
154             expect => <<'EOF',
155             Hallo
156             goodbye
157              
158             Hallo
159             goodbye
160             EOF
161             );
162 1     1   306 }
  1         2  
  1         4  
163              
164             sub test_misc_standard_filters : Tests {
165 1     1 0 236 my $self = shift;
166              
167 1         7 $self->test_comp(
168             src => 'the <% $m->filter($.Trim, " quick brown ") %> fox',
169             expect => 'the quick brown fox'
170             );
171 1         7 $self->test_comp(
172             src => '
173             % $.Capture(\my $buf) {{
174             2 + 2 = <% 2+2 %>
175             % }}
176             <% reverse($buf) %>
177              
178             % $.Tee(\my $buf2) {{
179             3 + 3 = <% 3+3 %>
180             % }}
181             <% reverse($buf2) %>
182              
183             ---
184             % $.NoBlankLines {{
185              
186             one
187              
188              
189              
190              
191             two
192              
193             % }}
194             ---
195             ',
196             expect => '
197             4 = 2 + 2
198              
199             3 + 3 = 6
200              
201             6 = 3 + 3
202              
203             ---
204             one
205             two
206             ---
207              
208             ',
209             );
210 1     1   317 }
  1         2  
  1         4  
211              
212             sub test_compcall_filter : Tests {
213 1     1 0 215 my $self = shift;
214              
215 1         7 $self->add_comp(
216             path => '/list_items.mi',
217             src => '
218             <%class>
219             has "items";
220             has "yield";
221             </%class>
222              
223             % foreach my $item (@{$.items}) {
224             <% $.yield->($item) %>
225             % }
226             ',
227             );
228 1         8 $self->test_comp(
229             src => '
230             % $.CompCall ("list_items.mi", items => [1,2,3]) {{
231             <li><% $_[0] %></li>
232             % }}
233             ',
234             expect => '
235             <li>1</li>
236              
237             <li>2</li>
238              
239             <li>3</li>
240             ',
241             );
242 1     1   414 }
  1         2  
  1         5  
243              
244             sub test_around : Tests {
245 1     1 0 2215 my $self = shift;
246 1         13 $self->test_comp(
247             src => <<'EOF',
248             hello
249              
250             <%around main>
251             % sub { uc($_[0]) } {{
252             % $self->$orig();
253             % }}
254             </%around>
255              
256             EOF
257             expect => <<'EOF',
258             HELLO
259             EOF
260             );
261 1     1   361 }
  1         3  
  1         5  
262              
263             # Test old filter syntax, still currently supported
264             #
265             sub test_old_syntax : Tests {
266 1     1 0 641 my $self = shift;
267 1         7 $self->test_comp(
268             src => '
269             <%class>
270             method Upper () { sub { uc(shift) } }
271             </%class>
272              
273             <% $.Upper { %>
274             Hello World.
275             </%>
276              
277             <% sub { ucfirst(shift) } { %>
278             <% "hello world?" %>
279             <% } %>
280              
281             <% sub { lc(shift) } { %>
282             Hello World!
283             </%>
284             ',
285             expect => '
286             HELLO WORLD.
287             Hello world?
288             hello world!
289             ',
290             );
291 1     1   378 }
  1         2  
  1         5  
292              
293             sub test_filter_comment : Tests {
294 1     1 0 213 my $self = shift;
295 1         8 $self->test_comp(
296             src => '
297             <%class>
298             method LESSp () { sub { uc(shift) } }
299             </%class>
300             % $.LESSp {{
301             #header {
302             text-align: left;
303             }
304             % }}
305             #footer {
306             text-align: right;
307             }
308             ',
309             expect => '
310             #HEADER {
311             TEXT-ALIGN: LEFT;
312             }
313             #footer {
314             text-align: right;
315             }
316             ',
317             );
318 1     1   345 }
  1         3  
  1         5  
319              
320             sub test_no_undef_warning : Tests {
321 1     1 0 220 my $self = shift;
322             warnings_are {
323 1     1   35 $self->test_comp(
324             src => '
325             <%class>
326             method Upper () { sub { uc(shift) } }
327             method Upper2 () {
328             return Mason::DynamicFilter->new(
329             filter => sub {
330             my $yield = $_[0];
331             return uc($yield->());
332             }
333             );
334             }
335             </%class>
336              
337             <%filter Upper3>
338             <% uc($yield->()) %>
339             </%filter>
340              
341             a = <% "a" | Upper %>.
342             undef = <% undef | Upper %>.
343              
344             a = <% "a" | Upper2 %>.
345             undef = <% undef | Upper2 %>.
346              
347             a = <% "a" | Upper3 %>.
348             undef = <% undef | Upper3 %>.
349             ',
350             expect => '
351             a = A.
352             undef = .
353              
354             a = A.
355             undef = .
356              
357             a = A.
358             undef = .
359             ',
360             );
361             }
362 1         6 [], "no warnings on undef";
363 1     1   349 }
  1         3  
  1         4  
364              
365             1;