diff --git a/t/mojo/template.t b/t/mojo/template.t index 6b55eec84a..65e24a3342 100644 --- a/t/mojo/template.t +++ b/t/mojo/template.t @@ -20,170 +20,202 @@ sub exception { die 'ohoh' } package main; -# Empty template -my $mt = Mojo::Template->new; -my $output = $mt->render(''); -is $output, '', 'empty string'; - -# Named template -$mt = Mojo::Template->new; -$output = $mt->name('foo/bar.mt')->render('<%= __FILE__ %>'); -is $output, "foo/bar.mt\n", 'template name'; - -# Consistent scalar context -$output = $mt->prepend('my @foo = (3, 4);')->render('<%= @foo %>:<%== @foo %>'); -is $output, "2:2\n", 'same context'; - -# Parentheses -$mt = Mojo::Template->new; -$output = $mt->render('<%= (1,2,3)[1] %><%== (1,2,3)[2] %>'); -is $output, "23\n", 'no ambiguity'; - -# String -$mt = Mojo::Template->new; -$output = $mt->render('Just a <%= "test" %>'); -is $output, "Just a test\n", 'rendered string'; - -# Trim tag -$mt = Mojo::Template->new; -$output = $mt->render(" ♥ <%= 'test♥' =%> \n"); -is $output, ' ♥test♥', 'tag trimmed'; - -# Trim expression -$mt = Mojo::Template->new; -$output = $mt->render("<%= '123' %><%= 'begin#test' =%>\n"); -is $output, '123begin#test', 'expression trimmed'; - -# Trim expression (multiple lines) -$mt = Mojo::Template->new; -$output = $mt->render(" foo \n <%= 'test' =%>\n foo\n"); -is $output, " foo \ntest foo\n", 'expression trimmed'; - -# Trim expression (at start of line) -$mt = Mojo::Template->new; -$output = $mt->render(" \n<%= 'test' =%>\n "); -is $output, " \ntest \n", 'expression trimmed'; - -# Trim expression (multiple lines) -$mt = Mojo::Template->new; -$output = $mt->render(" bar\n foo\n <%= 'test' =%>\n foo\n bar\n"); -is $output, " bar\n foo\ntest foo\n bar\n", 'expression trimmed'; - -# Trim expression (multiple empty lines) -$mt = Mojo::Template->new; -$output = $mt->render(" \n<%= 'test' =%>\n "); -is $output, " \ntest \n", 'expression trimmed'; - -# Trim expression tags -my $capture = 'no warnings "redefine"; sub capture { shift->(@_) }'; -$mt = Mojo::Template->new(prepend => $capture); -$output = $mt->render(' <%= capture begin =%><% end =%> '); -is $output, '', 'expression tags trimmed'; - -# Trim expression tags (relaxed expression end) -$mt = Mojo::Template->new(prepend => $capture); -$output = $mt->render(' <%= capture begin =%><%= end =%> '); -is $output, '', 'expression tags trimmed'; - -# Trim expression tags (relaxed escaped expression end) -$mt = Mojo::Template->new(prepend => $capture); -$output = $mt->render(' <%= capture begin =%><%== end =%> '); -is $output, '', 'expression tags trimmed'; - -# Trim expression tags (trim reset) -$mt = Mojo::Template->new; -$output = $mt->render(' <%= "one" =%><%= "two" %> three'); -is $output, "onetwo three\n", 'expression tags trimmed'; - -# Nothing to trim -$mt = Mojo::Template->new; -$output = $mt->render('<% =%>'); -is $output, '', 'nothing trimmed'; - -# Replace tag -$mt = Mojo::Template->new; -$output = $mt->render('<%% 1 + 1 %>'); -is $output, "<% 1 + 1 %>\n", 'tag has been replaced'; - -# Replace expression tag -$mt = Mojo::Template->new; -$output = $mt->render('<%%= 1 + 1 %>'); -is $output, "<%= 1 + 1 %>\n", 'expression tag has been replaced'; - -# Replace expression tag (alternative) -$mt = Mojo::Template->new; -$output = $mt->render(' lalala <%%= 1 + 1 %> 1234 '); -is $output, " lalala <%= 1 + 1 %> 1234 \n", 'expression tag has been replaced'; - -# Replace expression tag (another alternative) -$mt = Mojo::Template->new; -$output = $mt->render(< sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(''); + is $output, '', 'empty string'; +}; + +subtest 'Named template' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->name('foo/bar.mt')->render('<%= __FILE__ %>'); + is $output, "foo/bar.mt\n", 'template name'; +}; + +subtest 'Consistent scalar context' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->prepend('my @foo = (3, 4);')->render('<%= @foo %>:<%== @foo %>'); + is $output, "2:2\n", 'same context'; +}; + +subtest 'Parentheses' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render('<%= (1,2,3)[1] %><%== (1,2,3)[2] %>'); + is $output, "23\n", 'no ambiguity'; +}; + +subtest 'String' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render('Just a <%= "test" %>'); + is $output, "Just a test\n", 'rendered string'; +}; + +subtest 'Trim tag' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(" ♥ <%= 'test♥' =%> \n"); + is $output, ' ♥test♥', 'tag trimmed'; +}; + +subtest 'Trim expression' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render("<%= '123' %><%= 'begin#test' =%>\n"); + is $output, '123begin#test', 'expression trimmed'; +}; + +subtest 'Trim expression (multiple lines)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(" foo \n <%= 'test' =%>\n foo\n"); + is $output, " foo \ntest foo\n", 'expression trimmed'; +}; + +subtest 'Trim expression (at start of line)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(" \n<%= 'test' =%>\n "); + is $output, " \ntest \n", 'expression trimmed'; +}; + +subtest 'Trim expression (multiple lines)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(" bar\n foo\n <%= 'test' =%>\n foo\n bar\n"); + is $output, " bar\n foo\ntest foo\n bar\n", 'expression trimmed'; +}; + +subtest 'Trim expression (multiple empty lines)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(" \n<%= 'test' =%>\n "); + is $output, " \ntest \n", 'expression trimmed'; +}; + +subtest 'Trim expression tags' => sub { + my $capture = 'no warnings "redefine"; sub capture { shift->(@_) }'; + my $mt = Mojo::Template->new(prepend => $capture); + my $output = $mt->render(' <%= capture begin =%><% end =%> '); + is $output, '', 'expression tags trimmed'; +}; + +subtest 'Trim expression tags (relaxed expression end)' => sub { + my $capture = 'no warnings "redefine"; sub capture { shift->(@_) }'; + my $mt = Mojo::Template->new(prepend => $capture); + my $output = $mt->render(' <%= capture begin =%><%= end =%> '); + is $output, '', 'expression tags trimmed'; +}; + +subtest 'Trim expression tags (relaxed escaped expression end)' => sub { + my $capture = 'no warnings "redefine"; sub capture { shift->(@_) }'; + my $mt = Mojo::Template->new(prepend => $capture); + my $output = $mt->render(' <%= capture begin =%><%== end =%> '); + is $output, '', 'expression tags trimmed'; +}; + +subtest 'Trim expression tags (trim reset)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(' <%= "one" =%><%= "two" %> three'); + is $output, "onetwo three\n", 'expression tags trimmed'; +}; + +subtest 'Nothing to trim' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render('<% =%>'); + is $output, '', 'nothing trimmed'; +}; + +subtest 'Replace tag' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render('<%% 1 + 1 %>'); + is $output, "<% 1 + 1 %>\n", 'tag has been replaced'; +}; + +subtest 'Replace expression tag' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render('<%%= 1 + 1 %>'); + is $output, "<%= 1 + 1 %>\n", 'expression tag has been replaced'; +}; + +subtest 'Replace expression tag (alternative)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(' lalala <%%= 1 + 1 %> 1234 '); + is $output, " lalala <%= 1 + 1 %> 1234 \n", 'expression tag has been replaced'; +}; + +subtest 'Replace expression tag (another alternative)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(< 12 34 EOF -is $output, "lalala <%= 1 +\n 1 %> 12\n34\n", 'expression tag has been replaced'; + is $output, "lalala <%= 1 +\n 1 %> 12\n34\n", 'expression tag has been replaced'; +}; -# Replace comment tag -$mt = Mojo::Template->new; -$output = $mt->render('<%%# 1 + 1 %>'); -is $output, "<%# 1 + 1 %>\n", 'comment tag has been replaced'; +subtest 'Replace comment tag' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render('<%%# 1 + 1 %>'); + is $output, "<%# 1 + 1 %>\n", 'comment tag has been replaced'; +}; -# Replace line -$mt = Mojo::Template->new; -$output = $mt->render('%% my $foo = 23;'); -is $output, "% my \$foo = 23;\n", 'line has been replaced'; +subtest 'Replace line' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render('%% my $foo = 23;'); + is $output, "% my \$foo = 23;\n", 'line has been replaced'; +}; -# Replace expression line -$mt = Mojo::Template->new; -$output = $mt->render(' %%= 1 + 1'); -is $output, " %= 1 + 1\n", 'expression line has been replaced'; +subtest 'Replace expression line' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(' %%= 1 + 1'); + is $output, " %= 1 + 1\n", 'expression line has been replaced'; +}; -# Replace expression line (alternative) -$mt = Mojo::Template->new; -$output = $mt->render('%%= 1 + 1'); -is $output, "%= 1 + 1\n", 'expression line has been replaced'; +subtest 'Replace expression line (alternative)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render('%%= 1 + 1'); + is $output, "%= 1 + 1\n", 'expression line has been replaced'; +}; -# Replace comment line -$mt = Mojo::Template->new; -$output = $mt->render(' %%# 1 + 1'); -is $output, " %# 1 + 1\n", 'comment line has been replaced'; +subtest 'Replace comment line' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(' %%# 1 + 1'); + is $output, " %# 1 + 1\n", 'comment line has been replaced'; +}; -# Replace mixed -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Replace mixed' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); %% my $num = <%= 20 + 3%>; The number is <%%= <%= '$' %>num %>. EOF -is $output, "% my \$num = 23;\nThe number is <%= \$num %>.\n", 'mixed lines have been replaced'; + is $output, "% my \$num = 23;\nThe number is <%= \$num %>.\n", 'mixed lines have been replaced'; +}; -# Helper starting with "end" -$mt = Mojo::Template->new(prepend => 'sub endpoint { "works!" }'); -$output = $mt->render(<<'EOF'); +subtest 'Helper starting with "end"' => sub { + my $mt = Mojo::Template->new(prepend => 'sub endpoint { "works!" }'); + my $output = $mt->render(<<'EOF'); % endpoint; %= endpoint %== endpoint <% endpoint; %><%= endpoint %><%== endpoint =%> EOF -is $output, "works!\nworks!\nworks!works!", 'helper worked'; + is $output, "works!\nworks!\nworks!works!", 'helper worked'; +}; -# Helper ending with "begin" -$mt = Mojo::Template->new(prepend => 'sub funbegin { "works too!" }'); -$output = $mt->render(<<'EOF'); +subtest 'Helper ending with "begin"' => sub { + my $mt = Mojo::Template->new(prepend => 'sub funbegin { "works too!" }'); + my $output = $mt->render(<<'EOF'); % funbegin; %= funbegin %== funbegin <% funbegin; %><%= funbegin %><%== funbegin =%>\ EOF -is $output, "works too!\nworks too!\nworks too!works too!", 'helper worked'; + is $output, "works too!\nworks too!\nworks too!works too!", 'helper worked'; +}; -# Catched exception -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Catched exception' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); % eval { die {foo => 'bar'} }; %= $@->{foo} EOF -is $output, "bar\n", 'exception passed through'; + is $output, "bar\n", 'exception passed through'; +}; # Dummy exception object package MyException; @@ -194,17 +226,18 @@ has 'error'; package main; -# Catched exception object -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Catched exception object' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); % eval { die MyException->new(error => 'works!') }; %= $@->error EOF -is $output, "works!\n", 'exception object passed through'; + is $output, "works!\n", 'exception object passed through'; +}; -# Recursive block -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Recursive block' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); % my $block; <% $block = begin =%> % my $i = shift; @@ -213,11 +246,12 @@ $output = $mt->render(<<'EOF'); <% end =%> <%= $block->(2) %> EOF -is $output, "\n\n\n\n\n\n\n", 'recursive block'; + is $output, "\n\n\n\n\n\n\n", 'recursive block'; +}; -# Recursive block (perl lines) -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Recursive block (perl lines)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); % my $block; % $block = begin % my $i = shift; @@ -226,11 +260,12 @@ $output = $mt->render(<<'EOF'); % end %= $block->(2) EOF -is $output, "\n\n\n\n\n\n\n", 'recursive block'; + is $output, "\n\n\n\n\n\n\n", 'recursive block'; +}; -# Recursive block (indented perl lines) -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Recursive block (indented perl lines)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); % my $block; % $block = begin % my $i = shift; @@ -239,120 +274,138 @@ $output = $mt->render(<<'EOF'); % end %= $block->(2) EOF -is $output, " \n\n\n\n", 'recursive block'; + is $output, " \n\n\n\n", 'recursive block'; +}; -# Expression block (less whitespace) -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Expression block (less whitespace)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); <% my $block =begin=%> <%end=%> <%= $block->() %> EOF -is $output, "\n\n", 'expression block'; + is $output, "\n\n", 'expression block'; +}; -# Expression block (perl lines and less whitespace) -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Expression block (perl lines and less whitespace)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); % my $block =begin %end <%= $block->() %> EOF -is $output, "\n\n", 'expression block'; + is $output, "\n\n", 'expression block'; +}; -# Expression block (indented perl lines and less whitespace) -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Expression block (indented perl lines and less whitespace)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); % my $block =begin %end <%= $block->() %> EOF -is $output, "\n\n", 'expression block'; + is $output, "\n\n", 'expression block'; +}; -# Escaped expression block (passed through with extra whitespace) -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Escaped expression block (passed through with extra whitespace)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); <% my $block = begin %> <% end %> <%== $block->() %> EOF -is $output, "\n\n\n\n", 'escaped expression block'; + is $output, "\n\n\n\n", 'escaped expression block'; +}; -# Escaped expression block -# (passed through with perl lines and extra whitespace) -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Escaped expression block (passed through with perl lines and extra whitespace)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); % my $block = begin <% end %> <%== $block->() %> EOF -is $output, "\n\n\n", 'escaped expression block'; + is $output, "\n\n\n", 'escaped expression block'; +}; -# Escaped expression block -# (passed through with indented perl lines and extra whitespace) -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Escaped expression block (passed through with indented perl lines and extra whitespace)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); % my $block = begin % end <%== $block->() %> EOF -is $output, "\n\n", 'escaped expression block'; + is $output, "\n\n", 'escaped expression block'; +}; -# Capture lines (passed through with extra whitespace) -$mt = Mojo::Template->new(prepend => $capture); -$output = $mt->render(<<'EOF'); +subtest 'Capture lines (passed through with extra whitespace)' => sub { + my $capture = 'no warnings "redefine"; sub capture { shift->(@_) }'; + my $mt = Mojo::Template->new(prepend => $capture); + my $output = $mt->render(<<'EOF'); <% my $result = capture begin %> <% end %> %== $result EOF -is $output, "\n\n\n\n", 'captured lines'; + is $output, "\n\n\n\n", 'captured lines'; +}; -# Capture tags (passed through) -$mt = Mojo::Template->new(prepend => $capture); -$output = $mt->render(<<'EOF'); +subtest 'Capture tags (passed through)' => sub { + my $capture = 'no warnings "redefine"; sub capture { shift->(@_) }'; + my $mt = Mojo::Template->new(prepend => $capture); + my $output = $mt->render(<<'EOF'); <% my $result = capture begin %><% end %><%== $result %> EOF -is $output, "\n", 'capture tags'; + is $output, "\n", 'capture tags'; +}; -# Capture tags (passed through alternative) -$mt = Mojo::Template->new(prepend => $capture); -$output = $mt->render(<<'EOF'); +subtest 'Capture tags (passed through alternative)' => sub { + my $capture = 'no warnings "redefine"; sub capture { shift->(@_) }'; + my $mt = Mojo::Template->new(prepend => $capture); + my $output = $mt->render(<<'EOF'); <% my $result = capture begin %><% end %><%== $result %> EOF -is $output, "\n", 'capture tags'; + is $output, "\n", 'capture tags'; +}; -# Capture tags with appended code (passed through) -$mt = Mojo::Template->new(prepend => $capture); -$output = $mt->render(<<'EOF'); +subtest 'Capture tags with appended code (passed through)' => sub { + my $capture = 'no warnings "redefine"; sub capture { shift->(@_) }'; + my $mt = Mojo::Template->new(prepend => $capture); + my $output = $mt->render(<<'EOF'); <% my $result = +(capture begin %><% end); %><%== $result %> EOF -is $output, "\n", 'capture tags with appended code'; + is $output, "\n", 'capture tags with appended code'; +}; -# Capture tags with appended code (passed through alternative) -$mt = Mojo::Template->new(prepend => $capture); -$output = $mt->render(<<'EOF'); +subtest 'Capture tags with appended code (passed through alternative)' => sub { + my $capture = 'no warnings "redefine"; sub capture { shift->(@_) }'; + my $mt = Mojo::Template->new(prepend => $capture); + my $output = $mt->render(<<'EOF'); <% my $result = +( capture begin %><% end ); %><%= $result %> EOF -is $output, "\n", 'capture tags with appended code'; + is $output, "\n", 'capture tags with appended code'; +}; -# Nested capture tags (passed through) -$mt = Mojo::Template->new(prepend => $capture); -$output = $mt->render(<<'EOF'); +subtest 'Nested capture tags (passed through)' => sub { + my $capture = 'no warnings "redefine"; sub capture { shift->(@_) }'; + my $mt = Mojo::Template->new(prepend => $capture); + my $output = $mt->render(<<'EOF'); <% my $result = capture begin %><%= capture begin %><% end %><% end %><%== $result %> EOF -is $output, "\n", 'nested capture tags'; + is $output, "\n", 'nested capture tags'; +}; -# Nested capture tags (passed through alternative) -$mt = Mojo::Template->new(prepend => $capture); -$output = $mt->render(<<'EOF'); +subtest 'Nested capture tags (passed through alternative)' => sub { + my $capture = 'no warnings "redefine"; sub capture { shift->(@_) }'; + my $mt = Mojo::Template->new(prepend => $capture); + my $output = $mt->render(<<'EOF'); <% my $result = capture begin =%> <%== capture begin =%> @@ -360,11 +413,12 @@ $output = $mt->render(<<'EOF'); <% end =%> <%= $result =%> EOF -is $output, " \n", 'nested capture tags'; + is $output, " \n", 'nested capture tags'; +}; -# Advanced capturing (extra whitespace) -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Advanced capturing (extra whitespace)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); <% my $block = begin =%> <% my $name = shift; =%> Hello <%= $name %>. @@ -372,14 +426,15 @@ Hello <%= $name %>. <%= $block->('Baerbel') =%> <%= $block->('Wolfgang') =%> EOF -is $output, <new; -$output = $mt->render(<<'EOF'); +subtest 'Advanced capturing (perl lines extra whitespace)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); % my $block = begin <% my $name = shift; =%> Hello <%= $name %>. @@ -387,16 +442,17 @@ Hello <%= $name %>. <%= $block->('Baerbel') %> <%= $block->('Wolfgang') %> EOF -is $output, <new; -$output = $mt->render(<<'EOF'); +subtest 'Advanced capturing (indented perl lines extra whitespace)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); % my $block = begin <% my $name = shift; =%> Hello <%= $name %>. @@ -404,16 +460,17 @@ Hello <%= $name %>. <%= $block->('Baerbel') %> <%= $block->('Wolfgang') %> EOF -is $output, <new; -$output = $mt->render(<<'EOF'); +subtest 'Advanced capturing with tags' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); <% my $block = begin =%> <% my $name = shift; =%> Hello <%= $name %>. @@ -421,16 +478,17 @@ $output = $mt->render(<<'EOF'); <%= $block->('Sebastian') %> <%= $block->('Sara') %> EOF -is $output, <new; -$output = $mt->render(<<'EOF'); +subtest 'Advanced capturing with tags (perl lines)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); % my $block = begin <% my $name = shift; =%> Hello <%= $name %>. @@ -438,16 +496,17 @@ $output = $mt->render(<<'EOF'); %= $block->('Sebastian') %= $block->('Sara') EOF -is $output, <new; -$output = $mt->render(<<'EOF'); +subtest 'Advanced capturing with tags (indented perl lines)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); % my $block = begin % my $name = shift; Hello <%= $name %>. @@ -455,16 +514,17 @@ $output = $mt->render(<<'EOF'); %= $block->('Sebastian') %= $block->('Sara') EOF -is $output, <new; -$output = $mt->render(<<'EOF'); +subtest 'Advanced capturing with tags (alternative)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); <% my $block = begin =%> <% my $name = shift; =%> Hello <%= $name %>. @@ -472,16 +532,17 @@ $output = $mt->render(<<'EOF'); <%= $block->('Sebastian') %> <%= $block->('Sara') %> EOF -is $output, <new; -$output = $mt->render(<<'EOF'); +subtest 'Advanced capturing with tags (perl lines and alternative)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); % my $block = begin <% my $name = shift; =%> Hello <%= $name %>. @@ -489,16 +550,17 @@ $output = $mt->render(<<'EOF'); %= $block->('Sebastian') %= $block->('Sara') EOF -is $output, <new; -$output = $mt->render(<<'EOF'); +subtest 'Advanced capturing with tags (indented perl lines and alternative)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); % my $block = begin % my $name = shift; Hello <%= $name %>. @@ -506,16 +568,17 @@ $output = $mt->render(<<'EOF'); %= $block->('Sebastian') %= $block->('Sara') EOF -is $output, <new; -$output = $mt->render(<<'EOF'); +subtest 'More advanced capturing with tags (alternative)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); <% my $block1 = begin =%> <% my $name = shift; =%> @@ -530,22 +593,24 @@ begin =%> <%= $block1->('Sebastian') %> <%= $block2->('Sara') %> EOF -is $output, <new(prepend => $capture); -$output = $mt->render(<<'EOF'); +subtest 'Block loop' => sub { + my $capture = 'no warnings "redefine"; sub capture { shift->(@_) }'; + my $mt = Mojo::Template->new(prepend => $capture); + my $output = $mt->render(<<'EOF'); % my $i = 2; <%= capture begin %> <%= $i++ %> <% end for 1 .. 3; %> EOF -is $output, <new(prepend => $capture); -$output = $mt->render(<<'EOF'); +subtest 'Block loop (perl lines)' => sub { + my $capture = 'no warnings "redefine"; sub capture { shift->(@_) }'; + my $mt = Mojo::Template->new(prepend => $capture); + my $output = $mt->render(<<'EOF'); % my $i = 2; %= capture begin <%= $i++ =%> % end for 1 .. 3; EOF -is $output, "\n2\n3\n4", 'block loop'; + is $output, "\n2\n3\n4", 'block loop'; +}; -# Block loop (indented perl lines) -$mt = Mojo::Template->new(prepend => $capture); -$output = $mt->render(<<'EOF'); +subtest 'Block loop (indented perl lines)' => sub { + my $capture = 'no warnings "redefine"; sub capture { shift->(@_) }'; + my $mt = Mojo::Template->new(prepend => $capture); + my $output = $mt->render(<<'EOF'); % my $i = 2; %= capture begin %= $i++ % end for 1 .. 3; EOF -is $output, " \n 2\n\n 3\n\n 4\n", 'block loop'; + is $output, " \n 2\n\n 3\n\n 4\n", 'block loop'; +}; subtest 'End and begin in the same perl line' => sub { my $concat = 'no warnings "redefine"; sub concat { $_[0]->() . $_[1]->() }'; @@ -588,102 +658,107 @@ EOF is $output, " \n 1\n 2\n", 'end, begin'; }; -# Strict -$output = Mojo::Template->new->render('% $foo = 1;'); -isa_ok $output, 'Mojo::Exception', 'right exception'; -like $output->message, qr/^Global symbol "\$foo" requires/, 'right message'; +subtest 'Strict' => sub { + my $output = Mojo::Template->new->render('% $foo = 1;'); + isa_ok $output, 'Mojo::Exception', 'right exception'; + like $output->message, qr/^Global symbol "\$foo" requires/, 'right message'; +}; -# Importing into a template -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Importing into a template' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); % BEGIN { MyTemplateExporter->import } %= __PACKAGE__ %= foo EOF -is $output, "Mojo::Template::Sandbox\nworks!\n", 'right result'; -$output = $mt->render(<<'EOF'); + is $output, "Mojo::Template::Sandbox\nworks!\n", 'right result'; + $output = $mt->render(<<'EOF'); % BEGIN { MyTemplateExporter->import } %= __PACKAGE__ %= foo EOF -is $output, "Mojo::Template::Sandbox\nworks!\n", 'right result'; + is $output, "Mojo::Template::Sandbox\nworks!\n", 'right result'; +}; -# Unusable error message (stack trace required) -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Unusable error message (stack trace required)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); test 123 % die "x\n"; test EOF -isa_ok $output, 'Mojo::Exception', 'right exception'; -is $output->message, "x\n", 'right message'; -is $output->lines_before->[0][0], 1, 'right number'; -is $output->lines_before->[0][1], 'test', 'right line'; -is $output->lines_before->[1][0], 2, 'right number'; -is $output->lines_before->[1][1], '123', 'right line'; -ok $output->lines_before->[1][2], 'contains code'; -is $output->line->[0], 3, 'right number'; -is $output->line->[1], '% die "x\n";', 'right line'; -ok $output->line->[2], 'contains code'; -like "$output", qr/^x/, 'right result'; - -# Compile time exception -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); + isa_ok $output, 'Mojo::Exception', 'right exception'; + is $output->message, "x\n", 'right message'; + is $output->lines_before->[0][0], 1, 'right number'; + is $output->lines_before->[0][1], 'test', 'right line'; + is $output->lines_before->[1][0], 2, 'right number'; + is $output->lines_before->[1][1], '123', 'right line'; + ok $output->lines_before->[1][2], 'contains code'; + is $output->line->[0], 3, 'right number'; + is $output->line->[1], '% die "x\n";', 'right line'; + ok $output->line->[2], 'contains code'; + like "$output", qr/^x/, 'right result'; +}; + +subtest 'Compile time exception' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); test 123 % { %= 1 + 1 test EOF -isa_ok $output, 'Mojo::Exception', 'right exception'; -like $output->message, qr/Missing right curly/, 'right message'; -is $output->lines_before->[0][0], 1, 'right number'; -is $output->lines_before->[0][1], 'test', 'right line'; -is $output->lines_before->[1][0], 2, 'right number'; -is $output->lines_before->[1][1], '123', 'right line'; -is $output->lines_before->[2][0], 3, 'right number'; -is $output->lines_before->[2][1], '% {', 'right line'; -is $output->lines_before->[3][0], 4, 'right number'; -is $output->lines_before->[3][1], '%= 1 + 1', 'right line'; -is $output->line->[0], 5, 'right number'; -is $output->line->[1], 'test', 'right line'; -like "$output", qr/Missing right curly/, 'right result'; -like $output->frames->[0][1], qr/Template\.pm$/, 'right file'; - -# Exception in module -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); + isa_ok $output, 'Mojo::Exception', 'right exception'; + like $output->message, qr/Missing right curly/, 'right message'; + is $output->lines_before->[0][0], 1, 'right number'; + is $output->lines_before->[0][1], 'test', 'right line'; + is $output->lines_before->[1][0], 2, 'right number'; + is $output->lines_before->[1][1], '123', 'right line'; + is $output->lines_before->[2][0], 3, 'right number'; + is $output->lines_before->[2][1], '% {', 'right line'; + is $output->lines_before->[3][0], 4, 'right number'; + is $output->lines_before->[3][1], '%= 1 + 1', 'right line'; + is $output->line->[0], 5, 'right number'; + is $output->line->[1], 'test', 'right line'; + like "$output", qr/Missing right curly/, 'right result'; + like $output->frames->[0][1], qr/Template\.pm$/, 'right file'; +}; + +subtest 'Exception in module' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); test 123 %= MyTemplateException->exception %= 1 + 1 test EOF -isa_ok $output, 'Mojo::Exception', 'right exception'; -like $output->message, qr/ohoh/, 'right message'; -is $output->lines_before->[0][0], 14, 'right number'; -is $output->lines_before->[0][1], '}', 'right line'; -is $output->lines_before->[1][0], 15, 'right number'; -is $output->lines_before->[1][1], '', 'right line'; -is $output->lines_before->[2][0], 16, 'right number'; -is $output->lines_before->[2][1], 'package MyTemplateException;', 'right line'; -is $output->lines_before->[3][0], 17, 'right number'; -is $output->lines_before->[3][1], 'use Mojo::Base -strict;', 'right line'; -is $output->lines_before->[4][0], 18, 'right number'; -is $output->lines_before->[4][1], '', 'right line'; -is $output->line->[0], 19, 'right number'; -is $output->line->[1], "sub exception { die 'ohoh' }", 'right line'; -is $output->lines_after->[0][0], 20, 'right number'; -is $output->lines_after->[0][1], '', 'right line'; -is $output->lines_after->[1][0], 21, 'right number'; -is $output->lines_after->[1][1], 'package main;', 'right line'; -like "$output", qr/ohoh/, 'right result'; - -# Exception in template -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); + isa_ok $output, 'Mojo::Exception', 'right exception'; + like $output->message, qr/ohoh/, 'right message'; + is $output->lines_before->[0][0], 14, 'right number'; + is $output->lines_before->[0][1], '}', 'right line'; + is $output->lines_before->[1][0], 15, 'right number'; + is $output->lines_before->[1][1], '', 'right line'; + is $output->lines_before->[2][0], 16, 'right number'; + is $output->lines_before->[2][1], 'package MyTemplateException;', 'right line'; + is $output->lines_before->[3][0], 17, 'right number'; + is $output->lines_before->[3][1], 'use Mojo::Base -strict;', 'right line'; + is $output->lines_before->[4][0], 18, 'right number'; + is $output->lines_before->[4][1], '', 'right line'; + is $output->line->[0], 19, 'right number'; + is $output->line->[1], "sub exception { die 'ohoh' }", 'right line'; + is $output->lines_after->[0][0], 20, 'right number'; + is $output->lines_after->[0][1], '', 'right line'; + is $output->lines_after->[1][0], 21, 'right number'; + is $output->lines_after->[1][1], 'package main;', 'right line'; + like "$output", qr/ohoh/, 'right result'; +}; + +subtest 'Exception in template' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); test 123\ 456 @@ -692,24 +767,24 @@ test %= 1 + 1 test EOF -isa_ok $output, 'Mojo::Exception', 'right exception'; -like $output->message, qr/oops!/, 'right message'; -is $output->lines_before->[0][0], 1, 'right number'; -is $output->lines_before->[0][1], 'test', 'right line'; -is $output->lines_before->[1][0], 2, 'right number'; -is $output->lines_before->[1][1], '123\\', 'right line'; -is $output->lines_before->[2][0], 3, 'right number'; -is $output->lines_before->[2][1], '456', 'right line'; -is $output->lines_before->[3][0], 4, 'right number'; -is $output->lines_before->[3][1], ' %# This dies', 'right line'; -is $output->line->[0], 5, 'right number'; -is $output->line->[1], "% die 'oops!';", 'right line'; -is $output->lines_after->[0][0], 6, 'right number'; -is $output->lines_after->[0][1], '%= 1 + 1', 'right line'; -is $output->lines_after->[1][0], 7, 'right number'; -is $output->lines_after->[1][1], 'test', 'right line'; -$output->frames([['Sandbox', 'template', 5], ['main', 'template.t', 673]]); -is $output, <message, qr/oops!/, 'right message'; + is $output->lines_before->[0][0], 1, 'right number'; + is $output->lines_before->[0][1], 'test', 'right line'; + is $output->lines_before->[1][0], 2, 'right number'; + is $output->lines_before->[1][1], '123\\', 'right line'; + is $output->lines_before->[2][0], 3, 'right number'; + is $output->lines_before->[2][1], '456', 'right line'; + is $output->lines_before->[3][0], 4, 'right number'; + is $output->lines_before->[3][1], ' %# This dies', 'right line'; + is $output->line->[0], 5, 'right number'; + is $output->line->[1], "% die 'oops!';", 'right line'; + is $output->lines_after->[0][0], 6, 'right number'; + is $output->lines_after->[0][1], '%= 1 + 1', 'right line'; + is $output->lines_after->[1][0], 7, 'right number'; + is $output->lines_after->[1][1], 'test', 'right line'; + $output->frames([['Sandbox', 'template', 5], ['main', 'template.t', 673]]); + is $output, <new; -$output = $mt->render(<<'EOF'); +subtest 'Exception in template (empty perl lines)' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); test\\ 123 % @@ -737,36 +813,37 @@ test\\ %= 1 + 1 test EOF -isa_ok $output, 'Mojo::Exception', 'right exception'; -like $output->message, qr/oops!/, 'right message'; -is $output->lines_before->[0][0], 1, 'right number'; -is $output->lines_before->[0][1], 'test\\\\', 'right line'; -ok $output->lines_before->[0][2], 'contains code'; -is $output->lines_before->[1][0], 2, 'right number'; -is $output->lines_before->[1][1], '123', 'right line'; -ok $output->lines_before->[1][2], 'contains code'; -is $output->lines_before->[2][0], 3, 'right number'; -is $output->lines_before->[2][1], '%', 'right line'; -is $output->lines_before->[2][2], ' ', 'right code'; -is $output->line->[0], 4, 'right number'; -is $output->line->[1], "% die 'oops!';", 'right line'; -is $output->lines_after->[0][0], 5, 'right number'; -is $output->lines_after->[0][1], '%', 'right line'; -is $output->lines_after->[0][2], ' ', 'right code'; -is $output->lines_after->[1][0], 6, 'right number'; -is $output->lines_after->[1][1], ' %', 'right line'; -is $output->lines_after->[1][2], ' ', 'right code'; -is $output->lines_after->[2][0], 7, 'right number'; -is $output->lines_after->[2][1], '%', 'right line'; -is $output->lines_after->[2][2], ' ', 'right code'; -like "$output", qr/oops! at template line 4/, 'right result'; - -# Exception in nested template -$mt = Mojo::Template->new; -$mt->tag_start('[$-'); -$mt->tag_end('-$]'); -$mt->line_start('$-'); -$output = $mt->render(<<'EOF'); + isa_ok $output, 'Mojo::Exception', 'right exception'; + like $output->message, qr/oops!/, 'right message'; + is $output->lines_before->[0][0], 1, 'right number'; + is $output->lines_before->[0][1], 'test\\\\', 'right line'; + ok $output->lines_before->[0][2], 'contains code'; + is $output->lines_before->[1][0], 2, 'right number'; + is $output->lines_before->[1][1], '123', 'right line'; + ok $output->lines_before->[1][2], 'contains code'; + is $output->lines_before->[2][0], 3, 'right number'; + is $output->lines_before->[2][1], '%', 'right line'; + is $output->lines_before->[2][2], ' ', 'right code'; + is $output->line->[0], 4, 'right number'; + is $output->line->[1], "% die 'oops!';", 'right line'; + is $output->lines_after->[0][0], 5, 'right number'; + is $output->lines_after->[0][1], '%', 'right line'; + is $output->lines_after->[0][2], ' ', 'right code'; + is $output->lines_after->[1][0], 6, 'right number'; + is $output->lines_after->[1][1], ' %', 'right line'; + is $output->lines_after->[1][2], ' ', 'right code'; + is $output->lines_after->[2][0], 7, 'right number'; + is $output->lines_after->[2][1], '%', 'right line'; + is $output->lines_after->[2][2], ' ', 'right code'; + like "$output", qr/oops! at template line 4/, 'right result'; +}; + +subtest 'Exception in nested template' => sub { + my $mt = Mojo::Template->new; + $mt->tag_start('[$-'); + $mt->tag_end('-$]'); + $mt->line_start('$-'); + my $output = $mt->render(<<'EOF'); test $- my $mt = Mojo::Template->new; [$- my $output = $mt->render(<<'EOT'); @@ -775,11 +852,12 @@ EOT -$] $-= $output EOF -like $output, qr/test\n\nBareword "bar".+in use at template line 1\./, 'exception in nested template'; + like $output, qr/test\n\nBareword "bar".+in use at template line 1\./, 'exception in nested template'; +}; -# Control structures -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Control structures' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); % if (23 > 22) { foo % } @@ -793,11 +871,12 @@ bar foo % } EOF -is $output, "foo\nbar\n", 'control structure'; + is $output, "foo\nbar\n", 'control structure'; +}; -# Mixed tags -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF', 2); +subtest 'Mixed tags' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF', 2); <%= $_[0] + 1 %> test <%= 2 + 2 %> lala <%# comment lalala %> %# This is a comment! @@ -805,143 +884,157 @@ $output = $mt->render(<<'EOF', 2); %= $i * 2 EOF -is $output, "\n3 test 4 lala \n4\n\\n", 'all tags'; -like $mt->code, qr/lala/, 'right code'; -unlike $mt->code, qr/ comment lalala /, 'right code'; -is ref $mt->compiled, 'CODE', 'code compiled'; + is $output, "\n3 test 4 lala \n4\n\\n", 'all tags'; + like $mt->code, qr/lala/, 'right code'; + unlike $mt->code, qr/ comment lalala /, 'right code'; + is ref $mt->compiled, 'CODE', 'code compiled'; +}; -# Arguments -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF', 'test', {foo => 'bar'}); +subtest 'Arguments' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF', 'test', {foo => 'bar'}); % my $msg = shift; <% my $hash = $_[0]; %> %= $msg . ' ' . $hash->{foo} EOF -is $output, "\ntest bar\n\n", 'arguments'; -is $mt->process('tset', {foo => 'baz'}), "\ntset baz\n\n", 'arguments again'; -is $mt->process('tset', {foo => 'yada'}), "\ntset yada\n\n", 'arguments again'; + is $output, "\ntest bar\n\n", 'arguments'; + is $mt->process('tset', {foo => 'baz'}), "\ntset baz\n\n", 'arguments again'; + is $mt->process('tset', {foo => 'yada'}), "\ntset yada\n\n", 'arguments again'; +}; -# Variables -$mt = Mojo::Template->new; -$output = $mt->vars(1)->render('<%= $foo %><%= $bar %>', {foo => 'works', bar => '!'}); -is $output, "works!\n", 'variables'; +subtest 'Variables' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->vars(1)->render('<%= $foo %><%= $bar %>', {foo => 'works', bar => '!'}); + is $output, "works!\n", 'variables'; +}; -# No variables -$mt = Mojo::Template->new; -$output = $mt->vars(1)->render('works too!'); -is $output, "works too!\n", 'no variables'; +subtest 'No variables' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->vars(1)->render('works too!'); + is $output, "works too!\n", 'no variables'; +}; -# Bad variables -$mt = Mojo::Template->new; -$output = $mt->vars(1)->render('bad variables!', {'not good' => 23}); -is $output, "bad variables!\n", 'bad variables'; +subtest 'Bad variables' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->vars(1)->render('bad variables!', {'not good' => 23}); + is $output, "bad variables!\n", 'bad variables'; +}; -# Ugly multiline loop -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Ugly multiline loop' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); % my $nums = ''; <% for my $i (1..4) { $nums .= "$i"; } %><%= $nums%> EOF -is $output, "1234\n", 'ugly multiline loop'; + is $output, "1234\n", 'ugly multiline loop'; +}; -# Clean multiline loop -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Clean multiline loop' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); % for my $i (1..4) { %= $i % } EOF -is $output, "\n1\n2\n3\n4\n\n", 'clean multiline loop'; + is $output, "\n1\n2\n3\n4\n\n", 'clean multiline loop'; +}; -# Escaped line ending -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Escaped line ending' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); \ %= '2' x 4 \\\\ EOF -is $output, "2222\n\\\\\\\n", 'escaped line ending'; + is $output, "2222\n\\\\\\\n", 'escaped line ending'; +}; -# XML escape -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'XML escape' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); <%== '' %> %== '<' EOF -is $output, "<html>\n&lt;\n\n", 'XML escape'; + is $output, "<html>\n&lt;\n\n", 'XML escape'; +}; -# XML auto escape -$mt = Mojo::Template->new; -$mt->auto_escape(1); -$output = $mt->render(<<'EOF'); +subtest 'XML auto escape' => sub { + my $mt = Mojo::Template->new; + $mt->auto_escape(1); + my $output = $mt->render(<<'EOF'); <%= '' %> %= 'begin#<' %== 'begin#<' EOF -is $output, <<html> begin#&lt; begin#< EOF +}; -# Complicated XML auto escape -$mt = Mojo::Template->new; -$mt->auto_escape(1); -$output = $mt->render(<<'EOF', {foo => 23}); +subtest 'Complicated XML auto escape' => sub { + my $mt = Mojo::Template->new; + $mt->auto_escape(1); + my $output = $mt->render(<<'EOF', {foo => 23}); % use Data::Dumper; %= Data::Dumper->new([shift])->Maxdepth(2)->Indent(1)->Terse(1)->Dump EOF -is $output, <<'EOF', 'complicated XML auto escape'; + is $output, <<'EOF', 'complicated XML auto escape'; { 'foo' => 23 } EOF +}; -# Complicated XML auto escape -$mt = Mojo::Template->new; -$mt->auto_escape(1); -$output = $mt->render(<<'EOF'); +subtest 'Complicated XML auto escape' => sub { + my $mt = Mojo::Template->new; + $mt->auto_escape(1); + my $output = $mt->render(<<'EOF'); <%= '' for 1 .. 3 %> EOF -is $output, <<html><html><html> EOF +}; -# Prepending code -$mt = Mojo::Template->new; -$mt->prepend('my $foo = shift; my $bar = "something\nelse"'); -$output = $mt->render(<<'EOF', 23); +subtest 'Prepending code' => sub { + my $mt = Mojo::Template->new; + $mt->prepend('my $foo = shift; my $bar = "something\nelse"'); + my $output = $mt->render(<<'EOF', 23); <%= $foo %> %= $bar % my $bar = 23; %= $bar EOF -is $output, "23\nsomething\nelse\n23\n", 'prepending code'; -$mt = Mojo::Template->new; -$mt->prepend(q[{no warnings 'redefine'; no strict 'refs'; *foo = sub { 23 }}]); -$output = $mt->render('<%= foo() %>'); -is $output, "23\n", 'right result'; -$output = $mt->render('%= foo()'); -is $output, "23\n", 'right result'; - -# Appending code -$mt = Mojo::Template->new; -$mt->append('$_O = "FOO!"'); -$output = $mt->render('23'); -is $output, "FOO!", 'appending code'; - -# Multiline comment -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); + is $output, "23\nsomething\nelse\n23\n", 'prepending code'; + $mt = Mojo::Template->new; + $mt->prepend(q[{no warnings 'redefine'; no strict 'refs'; *foo = sub { 23 }}]); + $output = $mt->render('<%= foo() %>'); + is $output, "23\n", 'right result'; + $output = $mt->render('%= foo()'); + is $output, "23\n", 'right result'; +}; + +subtest 'Appending code' => sub { + my $mt = Mojo::Template->new; + $mt->append('$_O = "FOO!"'); + my $output = $mt->render('23'); + is $output, "FOO!", 'appending code'; +}; + +subtest 'Multiline comment' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); <%# this is a comment %>this not @@ -950,89 +1043,99 @@ comment %>this not % } EOF -is $output, "this not\n1\n2\n3\n4\n\n", 'multiline comment'; + is $output, "this not\n1\n2\n3\n4\n\n", 'multiline comment'; +}; -# Commented out tags -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Commented out tags' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); - %# <%= 23 %>test<%= 24 %> + %# '<%= 23 %>test<%= 24 %>' EOF -is $output, "\n\n", 'commented out tags'; + is $output, "\n\n", 'commented out tags'; +}; -# One-liner -$mt = Mojo::Template->new; -$output = $mt->render('<%= 3 * 3 %>\\'); -is $output, '9', 'one-liner'; +subtest 'One-liner' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render('<%= 3 * 3 %>\\'); + is $output, '9', 'one-liner'; +}; -# Different line start -$mt = Mojo::Template->new; -$mt->line_start('$'); -$output = $mt->render(<<'EOF'); +subtest 'Different line start' => sub { + my $mt = Mojo::Template->new; + $mt->line_start('$'); + my $output = $mt->render(<<'EOF'); \ $= '2' x 4 \\\\ EOF -is $output, "2222\n\\\\\\\n", 'different line start'; + is $output, "2222\n\\\\\\\n", 'different line start'; +}; -# Inline comments -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Inline comments' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); % if (1) { # test works! % } # tset great! EOF -is $output, "works!\ngreat!\n", 'comments did not affect the result'; + is $output, "works!\ngreat!\n", 'comments did not affect the result'; +}; -# Inline comment on last line -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Inline comment on last line' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); % if (1) { works! % } # tset EOF -is $output, "works!\n", 'comment did not affect the result'; + is $output, "works!\n", 'comment did not affect the result'; +}; -# Multiline expression -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Multiline expression' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); <%= do { my $i = '2'; $i x 4; }; %>\ \ EOF -is $output, '2222', 'multiline expression'; + is $output, '2222', 'multiline expression'; +}; -# Different multiline expression -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Different multiline expression' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); <%= do { my $i = '2'; $i x 4; }; %>\ EOF -is $output, '2222', 'multiline expression'; + is $output, '2222', 'multiline expression'; +}; -# Yet another multiline expression -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Yet another multiline expression' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); <%= 'hello' . ' world' %>\ EOF -is $output, 'hello world', 'multiline expression'; + is $output, 'hello world', 'multiline expression'; +}; -# And another multiline expression -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'And another multiline expression' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); <%= 'hello' . ' world' %>\ EOF -is $output, 'hello world', 'multiline expression'; + is $output, 'hello world', 'multiline expression'; +}; -# And another multiline expression -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'And another multiline expression' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); <%= 'hello' . ' wo' . @@ -1040,46 +1143,53 @@ $output = $mt->render(<<'EOF'); 'rld' %>\ EOF -is $output, 'hello world', 'multiline expression'; + is $output, 'hello world', 'multiline expression'; +}; -# Escaped multiline expression -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Escaped multiline expression' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); <%== 'hello ' .'world' %> EOF -is $output, "hello world\n", 'escaped multiline expression'; + is $output, "hello world\n", 'escaped multiline expression'; +}; -# Empty statement -$mt = Mojo::Template->new; -$output = $mt->render("test\n\n123\n\n<% %>456\n789"); -is $output, "test\n\n123\n\n456\n789\n", 'empty statement'; +subtest 'Empty statement' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render("test\n\n123\n\n<% %>456\n789"); + is $output, "test\n\n123\n\n456\n789\n", 'empty statement'; +}; -# No newline -$mt = Mojo::Template->new; -$output = $mt->render('test'); -is $output, "test\n", 'just one newline'; +subtest 'No newline' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render('test'); + is $output, "test\n", 'just one newline'; +}; -# Multiple newlines at the end -$mt = Mojo::Template->new; -$output = $mt->render("test\n\n\n\n"); -is $output, "test\n", 'just one newline'; +subtest 'Multiple newlines at the end' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render("test\n\n\n\n"); + is $output, "test\n", 'just one newline'; +}; -# Escaped newline at the end -$mt = Mojo::Template->new; -$output = $mt->render("test\\\n"); -is $output, 'test', 'no newline'; +subtest 'Escaped newline at the end' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render("test\\\n"); + is $output, 'test', 'no newline'; +}; -# Multiple escaped newlines at the end -$mt = Mojo::Template->new; -$output = $mt->render("test\\\n\n\n\n"); -is $output, 'test', 'no newline'; +subtest 'Multiple escaped newlines at the end' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render("test\\\n\n\n\n"); + is $output, 'test', 'no newline'; +}; -# Optimize successive text lines ending with newlines -$mt = Mojo::Template->new; -$mt->parse(<<'EOF'); +subtest 'Optimize successive text lines ending with newlines' => sub { + my $mt = Mojo::Template->new; + $mt->parse(<<'EOF'); test 123 456\ @@ -1088,108 +1198,120 @@ test 654 321 EOF -is $mt->tree->[0][1], "test\n123\n456", 'optimized text lines'; -$output = $mt->process; -is_deeply $mt->tree, [], 'has been consumed'; -is $output, "test\n123\n456789\\\n987\n654\n321\n", 'just text'; + is $mt->tree->[0][1], "test\n123\n456", 'optimized text lines'; + my $output = $mt->process; + is_deeply $mt->tree, [], 'has been consumed'; + is $output, "test\n123\n456789\\\n987\n654\n321\n", 'just text'; +}; -# Scoped scalar -$mt = Mojo::Template->new; -$output = $mt->render(<<'EOF'); +subtest 'Scoped scalar' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render(<<'EOF'); % my $foo = 'bar'; <%= $foo %> EOF -is $output, "bar\n", 'scoped scalar'; + is $output, "bar\n", 'scoped scalar'; +}; -# Different tags and line start -$mt = Mojo::Template->new; -$mt->tag_start('[$-'); -$mt->tag_end('-$]'); -$mt->line_start('$-'); -$output = $mt->render(<<'EOF', 'test', {foo => 'bar'}); +subtest 'Different tags and line start' => sub { + my $mt = Mojo::Template->new; + $mt->tag_start('[$-'); + $mt->tag_end('-$]'); + $mt->line_start('$-'); + my $output = $mt->render(<<'EOF', 'test', {foo => 'bar'}); $- my $msg = shift; [$- my $hash = $_[0]; -$] $-= $msg . ' ' . $hash->{foo} EOF -is $output, "\ntest bar\n\n", 'different tags and line start'; + is $output, "\ntest bar\n\n", 'different tags and line start'; +}; -# Different expression and comment marks -$mt = Mojo::Template->new; -$mt->comment_mark('@@@'); -$mt->expression_mark('---'); -$output = $mt->render(<<'EOF', 'test', {foo => 'bar'}); +subtest 'Different expression and comment marks' => sub { + my $mt = Mojo::Template->new; + $mt->comment_mark('@@@'); + $mt->expression_mark('---'); + my $output = $mt->render(<<'EOF', 'test', {foo => 'bar'}); % my $msg = shift; <% my $hash = $_[0]; %><%@@@ comment lalala %> %--- $msg . ' ' . $hash->{foo} EOF -is $output, < test bar EOF +}; + +subtest 'File' => sub { + my $mt = Mojo::Template->new; + my $file = curfile->sibling('templates', 'test.mt'); + my $output = $mt->render_file($file, 3); + like $output, qr/23\nHello World!/, 'file'; +}; -# File -$mt = Mojo::Template->new; -my $file = curfile->sibling('templates', 'test.mt'); -$output = $mt->render_file($file, 3); -like $output, qr/23\nHello World!/, 'file'; - -# Exception in file -$mt = Mojo::Template->new; -$file = curfile->sibling('templates', 'exception.mt'); -$output = $mt->render_file($file); -isa_ok $output, 'Mojo::Exception', 'right exception'; -like $output->message, qr/exception\.mt line 2/, 'message contains filename'; -is $output->lines_before->[0][0], 1, 'right number'; -is $output->lines_before->[0][1], 'test', 'right line'; -is $output->line->[0], 2, 'right number'; -is $output->line->[1], '% die;', 'right line'; -is $output->lines_after->[0][0], 3, 'right number'; -is $output->lines_after->[0][1], '123', 'right line'; -like "$output", qr/exception\.mt line 2/, 'right result'; - -# Exception in file (different name) -$mt = Mojo::Template->new; -$output = $mt->name('"foo.mt" from DATA section')->render_file($file); -isa_ok $output, 'Mojo::Exception', 'right exception'; -like $output->message, qr/foo\.mt from DATA section line 2/, 'message contains filename'; -is $output->lines_before->[0][0], 1, 'right number'; -is $output->lines_before->[0][1], 'test', 'right line'; -is $output->line->[0], 2, 'right number'; -is $output->line->[1], '% die;', 'right line'; -is $output->lines_after->[0][0], 3, 'right number'; -is $output->lines_after->[0][1], '123', 'right line'; -like "$output", qr/foo\.mt from DATA section line 2/, 'right result'; - -# Exception with UTF-8 context -$mt = Mojo::Template->new; -$file = curfile->sibling('templates', 'utf8_exception.mt'); -$output = $mt->render_file($file); -isa_ok $output, 'Mojo::Exception', 'right exception'; -is $output->lines_before->[0][1], '☃', 'right line'; -is $output->line->[1], '% die;♥', 'right line'; -is $output->lines_after->[0][1], '☃', 'right line'; - -# Exception in first line with bad message -$mt = Mojo::Template->new; -$output = $mt->render('<% die "Test at template line 99\n"; %>'); -isa_ok $output, 'Mojo::Exception', 'right exception'; -is $output->message, "Test at template line 99\n", 'right message'; -is $output->lines_before->[0], undef, 'no lines before'; -is $output->line->[0], 1, 'right number'; -is $output->line->[1], '<% die "Test at template line 99\n"; %>', 'right line'; -is $output->lines_after->[0], undef, 'no lines after'; - -# Different encodings -$mt = Mojo::Template->new(encoding => 'shift_jis'); -$file = curfile->sibling('templates', 'utf8_exception.mt'); -ok !eval { $mt->render_file($file) }, 'file not rendered'; -like $@, qr/invalid encoding/, 'right error'; - -# Custom escape function -$mt = Mojo::Template->new(escape => sub { '+' . $_[0] }); -is $mt->render('<%== "hi" =%>'), '+hi', 'right escaped string'; +subtest 'Exception in file' => sub { + my $mt = Mojo::Template->new; + my $file = curfile->sibling('templates', 'exception.mt'); + my $output = $mt->render_file($file); + isa_ok $output, 'Mojo::Exception', 'right exception'; + like $output->message, qr/exception\.mt line 2/, 'message contains filename'; + is $output->lines_before->[0][0], 1, 'right number'; + is $output->lines_before->[0][1], 'test', 'right line'; + is $output->line->[0], 2, 'right number'; + is $output->line->[1], '% die;', 'right line'; + is $output->lines_after->[0][0], 3, 'right number'; + is $output->lines_after->[0][1], '123', 'right line'; + like "$output", qr/exception\.mt line 2/, 'right result'; +}; + +subtest 'Exception in file (different name)' => sub { + my $mt = Mojo::Template->new; + my $file = curfile->sibling('templates', 'exception.mt'); + my $output = $mt->name('"foo.mt" from DATA section')->render_file($file); + isa_ok $output, 'Mojo::Exception', 'right exception'; + like $output->message, qr/foo\.mt from DATA section line 2/, 'message contains filename'; + is $output->lines_before->[0][0], 1, 'right number'; + is $output->lines_before->[0][1], 'test', 'right line'; + is $output->line->[0], 2, 'right number'; + is $output->line->[1], '% die;', 'right line'; + is $output->lines_after->[0][0], 3, 'right number'; + is $output->lines_after->[0][1], '123', 'right line'; + like "$output", qr/foo\.mt from DATA section line 2/, 'right result'; +}; + +subtest 'Exception with UTF-8 context' => sub { + my $mt = Mojo::Template->new; + my $file = curfile->sibling('templates', 'utf8_exception.mt'); + my $output = $mt->render_file($file); + isa_ok $output, 'Mojo::Exception', 'right exception'; + is $output->lines_before->[0][1], '☃', 'right line'; + is $output->line->[1], '% die;♥', 'right line'; + is $output->lines_after->[0][1], '☃', 'right line'; +}; + +subtest 'Exception in first line with bad message' => sub { + my $mt = Mojo::Template->new; + my $output = $mt->render('<% die "Test at template line 99\n"; %>'); + isa_ok $output, 'Mojo::Exception', 'right exception'; + is $output->message, "Test at template line 99\n", 'right message'; + is $output->lines_before->[0], undef, 'no lines before'; + is $output->line->[0], 1, 'right number'; + is $output->line->[1], '<% die "Test at template line 99\n"; %>', 'right line'; + is $output->lines_after->[0], undef, 'no lines after'; +}; + +subtest 'Different encodings' => sub { + my $mt = Mojo::Template->new(encoding => 'shift_jis'); + my $file = curfile->sibling('templates', 'utf8_exception.mt'); + ok !eval { $mt->render_file($file) }, 'file not rendered'; + like $@, qr/invalid encoding/, 'right error'; +}; + +subtest 'Custom escape function' => sub { + my $mt = Mojo::Template->new(escape => sub { '+' . $_[0] }); + is $mt->render('<%== "hi" =%>'), '+hi', 'right escaped string'; +}; done_testing();