pjcj / Devel--Cover

Code coverage metrics for Perl
http://www.pjcj.net/perl.html
93 stars 85 forks source link

Get coverage from Mojolicious templates #31

Open jberger opened 11 years ago

jberger commented 11 years ago

As threatened :-) I am formally requesting the ability to test Mojolicious templates. I have learned more about them and might even be able to get either text of the parsed template or the compiled form (as a sub ref) when needed. I am opening this ticket to be a point of communication along this task.

dolmen commented 11 years ago

To get proper line coverage of those templates, Mojo::Template should properly report the source (file name, line number) of the generated code. You can tell that to perl (and so to Devel::Cover) by using #line lines. See perlsyn for details.

So the task is not for Devel::Cover, but instead a patch for Mojo::Template->build is required.

Cc: @kraih

kraih commented 11 years ago

There's a good chance the next version of Mojolicious will be using line directives. https://github.com/kraih/mojo/commit/99b1521110cd75897c69371f2b6d1806823c67e2

pjcj commented 11 years ago

Excellent. Then I'll leave this until the next version is out.

jberger commented 11 years ago

After some nice coding by @kraih, the line numbers are placed correctly (the filename is something still being discussed). The problem now is that once D::C reads that template and has no idea what to do with it.

In Mojolicious the template is read in, preprocessed into a Perl form (which we could hand off, this is also where the line numbers come in if I understand correctly) then evaled into a sub. Care is taken so that if there was lacking coverage in the sub, it would relate to the line in the original template. The question is, how can we tell D::C that the pre-evaled string form is related to the template file? If there is such a mechanism, then we are nearly there. If not, there might be no hope.

dolmen commented 11 years ago

The question is, how can we tell D::C that the pre-evaled string form is related to the template file?

There is no other trick than #line lines. Fix that first. The added #line 1 in kraih/mojo@99b1521 is not enough.

Eckankar commented 8 years ago

Are the line annotations still problematic in Mojolicious? As far as I can see, they are generated correctly for the templates I have tried. For instance, the template

<ul>
% for my $l ($lines) {
    <li><%= $l %></li>
% }
</ul>

generates the code

#line 1 "tmpl/mojo/foo.html.ep"
package Mojo::Template::SandBox; use Mojo::Base -strict; sub { my $_O = ''; ; { $_O .= "\<ul\>\n";
 for my $l ($lines) { 
$_O .= "\ \ \ \ \<li\>";$_O .= scalar  $l ;$_O .= "\<\/li\>\n";
 } 
$_O .= "\<\/ul\>\n";
#line 5 "tmpl/mojo/foo.html.ep"
; } $_O };

As far as I can tell, this matches the lines up correctly with the corresponding source file.

haarg commented 8 years ago

The basic problem here is that Devel::Cover isn't seeing the code at all. A normal anonymous sub exists in the optree, so it will be found. And if you install your generated sub into the symbol table, it will be found. But an anonymous sub that doesn't exist at process end in any form will not be found.

pjcj commented 6 years ago

kraih/mojo#956 has been closed to move the discussion elsewhere, but is there any progress here? I'm happy to keep this open until we get a solution or until it comes back to a Devel::Cover problem. I'm also happy to close it if the problem has been fixed, or if it can be fixed entirely within Mojolicious.