Thread: [htmltmpl] Testing HTML::Template templates
Brought to you by:
samtregar
From: Justin S. <ju...@sk...> - 2006-09-10 22:31:10
|
Heya, I've thus caught the Test::More and Co bug and find myself wanting to add tests to almost every part of my proggy. One part of the proggy that I'd love to test are my template files. It seems of good value to test the structure of the templates, as user contributions from people who may not be quite familiar with the templates, but still want to add something, may introduce new formatting bugs. I'm hoping there's a already existing strategy for doing this. I've tried something like this test: #!/usr/bin/perl use Test::HTML::Lint tests => 1; my $html = q{ <table> <!-- tmpl_loop foo --> <tr <!-- tmpl_if __odd__ -->style="background-color:#ccf;"<!--/ tmpl_if-->> <td> Something. </td> </tr> <!--/tmpl_loop--> </table> }; html_ok($html, "We're good."); Which, when run, returns something like: Justins-Computer:/t justin$ perl HTML_Template_test.t 1..1 not ok 1 - We're good. # Failed test 'We're good.' # in HTML_Template_test.t at line 19. # Errors: We're good. # (8:5) </tr> with no opening <tr> # Looks like you failed 1 test of 1. Removing the, "<!-- tmpl_if __odd__ -->style="background- color:#ccf;"<!--/tmpl_if-->" from the HTML snippet seems to make the test pass. Obviously, this isn't a bug in HTML::Template, if anything, it's a bug in HTML::Lint - although I'll have to see if HTML comments are valid markup inside other HTML tags. Even if they are, the test is returning an incorrect error. HTML::Lint hasn't been updated in a while and has a few open bugs, so I don't know if this is the best solution to my needs. Just passing the template to HTML::Template will tell me if the template is accepted and there's no problems with the <tmpl_*> tags, but it's not going to tell me anything about the rest of the HTML. Does anyone else have a better strategy for testing the structure of their HTML::Template templates? I'm wondering what the potential of the block tags making the template itself have inconsistent HTML structure. Probably fairly great, although it may be an interesting thing to enforce; (most) any template could be included without problem in (most) any other template. Justin Simoni -- :: is an eccentric artist, living and working in Denver, Colorado :: URL: http://justinsimoni.com :: Mailing List - http://justinsimoni.com/mailing_list.html |
From: Michael P. <mp...@pl...> - 2006-09-11 13:55:37
|
Justin Simoni wrote: > Does anyone else have a better strategy for testing the structure of > their HTML::Template templates? I've done 2 things in the past. 1) Load each template, use the query() method to find out what the vars and loops are and fill them in with fake data. Then test the output. PROS: * you can make sure your tmpl_* syntax isn't fatally wrong * you don't need to worry about templates being valid HTML, just the resulting output. CONS: * you're still at the mercy of something like HTML::Lint and it's bugs * you're not testing whether or not someone mispelled a variable name, etc 2) Test the modules that use the templates, not the templates themselves. PROS: * Tests not only the logic in the controller classes, but also that the vars are being passed correctly to the template with the right names. CONS: * Not as easy to write. You can't just write one test that tests all your templates. Plus you need to make sure you exercise every case so that every template is used. I prefer #2. -- Michael Peters Developer Plus Three, LP |
From: Justin S. <ju...@sk...> - 2006-09-12 05:30:25
|
On Sep 11, 2006, at 3:51 AM, Gabor Szabo wrote: > that's good, are you also aware of the perl-qa mailing list? I had a hunch there'd be a Kult of Kwality Assurancers for Perl :) > Why not test the result, after processing using HTML::Template? > That's what is really important. The problem with testing them after they're parsed is that, depending on the return values of any and every <tmpl_if> and friends block, the return'd HTML would be different. I'm thinking that most every HTML::Template template is actually valid HTML structurally, even with the block statements and thus can be tested for valid structure. HTML::Lint at least barfs on the embedded comments inside HTML tags. That, of itself may not be valid HTML, in which case I've gotten a dead end. Justin Simoni -- :: is an eccentric artist, living and working in Denver, Colorado :: URL: http://justinsimoni.com :: Mailing List - http://justinsimoni.com/mailing_list.html On Sep 11, 2006, at 3:51 AM, Gabor Szabo wrote: > On 9/11/06, Justin Simoni <ju...@sk...> wrote: >> Heya, >> >> I've thus caught the Test::More and Co bug and find myself wanting to >> add tests to almost every part of my proggy. > > that's good, are you also aware of the perl-qa mailing list? > >> html_ok($html, "We're good."); > > I am not sure why do you want your templates to be Lint-able? > It might be "nice" but it does not seem to add business value. > > Why not test the result, after processing using HTML::Template? > That's what is really important. > > Gabor > > |
From: Justin S. <ju...@sk...> - 2006-09-12 05:52:31
|
Ok, well, cludged my own solution to this problem: On Sep 11, 2006, at 11:30 PM, Justin Simoni wrote: > HTML::Lint at least barfs on the embedded comments inside HTML tags. > That, of itself may not be valid HTML, in which case I've gotten a > dead end. Before I send the template file to HTML::Lint, I can just strip out the HTML comments, and now I'm just testing the HTML, sans the HTML::Template comments. The only downside to this, is that the line numbers may be different between my templates and what the tests are returning as the line where a problem may lay. Template files aren't usually thousands of lines long, so this shouldn't be the biggest problem in the world. And, from the small set of files I've tested, looks like they're all structurally sound HTML files (except for the problems the tests did find), which was a hunch of mine that HTML::Template templates would be (sans the embedded comments in tags), since HTML::Template sort of mandates a clean separation between design and code. Justin Simoni -- :: is an eccentric artist, living and working in Denver, Colorado :: URL: http://justinsimoni.com :: Mailing List - http://justinsimoni.com/mailing_list.html On Sep 11, 2006, at 11:30 PM, Justin Simoni wrote: > > > On Sep 11, 2006, at 3:51 AM, Gabor Szabo wrote: >> that's good, are you also aware of the perl-qa mailing list? > > I had a hunch there'd be a Kult of Kwality Assurancers for Perl :) > >> Why not test the result, after processing using HTML::Template? >> That's what is really important. > > The problem with testing them after they're parsed is that, depending > on the return values of any and every <tmpl_if> and friends block, > the return'd HTML would be different. I'm thinking that most every > HTML::Template template is actually valid HTML structurally, even > with the block statements and thus can be tested for valid structure. > HTML::Lint at least barfs on the embedded comments inside HTML tags. > That, of itself may not be valid HTML, in which case I've gotten a > dead end. > > Justin Simoni > -- > :: is an eccentric artist, living and working in Denver, Colorado > :: URL: http://justinsimoni.com > :: Mailing List - http://justinsimoni.com/mailing_list.html > > > On Sep 11, 2006, at 3:51 AM, Gabor Szabo wrote: >> On 9/11/06, Justin Simoni <ju...@sk...> wrote: >>> Heya, >>> >>> I've thus caught the Test::More and Co bug and find myself >>> wanting to >>> add tests to almost every part of my proggy. >> >> that's good, are you also aware of the perl-qa mailing list? >> >>> html_ok($html, "We're good."); >> >> I am not sure why do you want your templates to be Lint-able? >> It might be "nice" but it does not seem to add business value. >> >> Why not test the result, after processing using HTML::Template? >> That's what is really important. >> >> Gabor >> >> > > ---------------------------------------------------------------------- > --- > Using Tomcat but need to do more? Need to support web services, > security? > Get stuff done quickly with pre-integrated technology to make your > job easier > Download IBM WebSphere Application Server v.1.0.1 based on Apache > Geronimo > http://sel.as-us.falkag.net/sel? > cmd=lnk&kid=120709&bid=263057&dat=121642 > _______________________________________________ > Html-template-users mailing list > Htm...@li... > https://lists.sourceforge.net/lists/listinfo/html-template-users > |
From: Gabor S. <sz...@gm...> - 2006-09-12 05:58:23
|
On 9/12/06, Justin Simoni <ju...@sk...> wrote: > > And, from the small set of files I've tested, looks like they're all > structurally sound HTML files (except for the problems the tests did > find), which was a hunch of mine that HTML::Template templates would > be (sans the embedded comments in tags), since HTML::Template sort of > mandates a clean separation between design and code. Returning to my soapbox, what if one of the values pasted into the template is an invalid piece of HTML code. e.g. '23 < 42'. The templates are still valid but the generated HTML is not. Gabor |
From: Justin S. <ju...@sk...> - 2006-09-12 06:49:01
|
On Sep 11, 2006, at 11:58 PM, Gabor Szabo wrote: > Returning to my soapbox, what if one of the values pasted into the > template is an invalid piece of HTML code. e.g. '23 < 42'. > > The templates are still valid but the generated HTML is not. Very much true, although what I'm attempting to test is the structure of the HTML in the templates themselves. I think testing the validity of output of HTML::Template templates a whole different cookie - and wouldn't be hurt by valid HTML in the template to begin with. I'm not sure what the solution is to that, since each template wants a different set of variables with different values being different for each value. Perhaps a solution to that would be to create a WWW::Mechanize robot that visits all the screens (known) to exist in the webapp and report of any problems with the HTML. I *guess* that would be fun, although it still doesn't test every instance of what gets passed to HTML::Template. Another solution would be to filter all the variables that are sent to HTML::Template before they are. HTML::Template is only called directly in one place in the app, so adding that wouldn't be a problem. Another may be just to enforce the various ESCAPE parameters in the HTML::Templates where they'd do the most good. That would be as easy as appending the program's style guide. I may worry about that later (and funny, I can't find anything about validating those conditions). At the moment, I'm fixing the errors my fairly simple test is turning up in my templates. I'm fairly pleased of the results I've gotten back, most of the errors reported are, in fact, goofs in the HTML of the HTML::Template templates. Justin Simoni -- :: is an eccentric artist, living and working in Denver, Colorado :: URL: http://justinsimoni.com :: Mailing List - http://justinsimoni.com/mailing_list.html On Sep 11, 2006, at 11:58 PM, Gabor Szabo wrote: > On 9/12/06, Justin Simoni <ju...@sk...> wrote: >> >> And, from the small set of files I've tested, looks like they're all >> structurally sound HTML files (except for the problems the tests did >> find), which was a hunch of mine that HTML::Template templates would >> be (sans the embedded comments in tags), since HTML::Template sort of >> mandates a clean separation between design and code. > > Returning to my soapbox, what if one of the values pasted into the > template is an invalid piece of HTML code. e.g. '23 < 42'. > > The templates are still valid but the generated HTML is not. > > Gabor > > ---------------------------------------------------------------------- > --- > Using Tomcat but need to do more? Need to support web services, > security? > Get stuff done quickly with pre-integrated technology to make your > job easier > Download IBM WebSphere Application Server v.1.0.1 based on Apache > Geronimo > http://sel.as-us.falkag.net/sel? > cmd=lnk&kid=120709&bid=263057&dat=121642 > _______________________________________________ > Html-template-users mailing list > Htm...@li... > https://lists.sourceforge.net/lists/listinfo/html-template-users > |
From: Gabor S. <sz...@gm...> - 2006-09-12 07:03:39
|
On 9/12/06, Justin Simoni <ju...@sk...> wrote: > At the moment, I'm fixing the errors my fairly simple test is turning > up in my templates. I'm fairly pleased of the results I've gotten > back, most of the errors reported are, in fact, goofs in the HTML of > the HTML::Template templates. > Yeah that would be a nice addition to the testing modules. In addition I guess there is some code in HTML::Template that validates if a given template is a valid template. Using that could be part of your tests. Gabor |