Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get coverage from Mojolicious templates #31

Open
jberger opened this issue Oct 23, 2012 · 8 comments
Open

Get coverage from Mojolicious templates #31

jberger opened this issue Oct 23, 2012 · 8 comments

Comments

@jberger
Copy link
Contributor

jberger commented Oct 23, 2012

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
Copy link
Contributor

dolmen commented Nov 12, 2012

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
Copy link

kraih commented Nov 12, 2012

There's a good chance the next version of Mojolicious will be using line directives. mojolicious/mojo@99b1521

@pjcj
Copy link
Owner

pjcj commented Nov 12, 2012

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

@jberger
Copy link
Contributor Author

jberger commented Nov 12, 2012

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
Copy link
Contributor

dolmen commented Nov 13, 2012

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 mojolicious/mojo@99b1521 is not enough.

@Eckankar
Copy link

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
Copy link
Contributor

haarg commented Apr 23, 2016

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.

ksmadsen added a commit to Jobindex/mojo that referenced this issue May 11, 2016
Add support for Devel::Cover in Mojo::Template, by monkey patching the compiled
template into its own namespace. This way the previously-anonymous sub exists
in the symbol table, and therefore Devel::Cover can see it.

As suggested by @haarg in pjcj/Devel--Cover#31

A path attribute is also added, to provide the path to the template instead of
using the name, which doesn't identify the template file.
@pjcj
Copy link
Owner

pjcj commented Sep 7, 2017

mojolicious/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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants