[stringtemplate-interest] Unit test framework for StringTemplate

Caleb Lyness caleb.lyness at ezswitch.net
Fri Feb 9 01:39:22 PST 2007


Jose San Leandro wrote:
> Hi,
>
> I'm making heavy use of StringTemplate in several projects, using a non-TDD 
> developing methodology: change template, check template filling logic, build 
> project, execute project, check template output.
>
> It should be much better if I could, from a template
> 1) Generate a "TemplateFiller" interface, with all the getters that the 
> template is expecting.
>   
That would be very cool. Then you could query the interface to find out
what
variables it has. But: the problem is that some templates are
referenced/included
via variables themselves. So its not known until final template
expansion what
variables are available...

What I have done is create a group with the following macro defined:

/*
 * Check if an attribute exists and render a comment if it is not, if
 * the "global" attribute "checkRequiredAttributes" is true. If this
attribute
 * is false or missing then the the failure comment is NOT generated.
 *
 * When the html comment is generated it will render the defined error
message
 * and the reference string. This failure  message can the be used by
automated
 * testing and reporting tools to catch problems.
 *
 * Parameters
 *   ref  - a reference string which is printed with the error message. It
 *          should at least hold the name of the failing attribute, and
 *          optionally the the template file name.
 *
 *   attr - the attribute to be checked
 *
 * Dont change the comment format, as it searched for during debugging.
 */
requiredAttribute(ref,attr) ::= <<
  $if (checkRequiredAttributes)$
  $if(!attr)$
      <!--$attr; null={ #FAILED ~ Missing required attribute: \$$ref$\$
}$-->
  $endif$
  $endif$
>>

Then when an attribute is required for the output in a template to work
I refer to this template:

$requiredAttribute(ref="code",attr=code)$

When in debug/junit mode I add the variable checkRequiredAttributes:
true to the template
parameters. The template above expands to a comment (for me its an HTML
comment) so
the final output can be tested further without problem. As part of the
junit test checks I look
in the output for the #FAILED ~ substring and report the message
following it if found
and fail the test.

Maybe this will help you....

I have some other techniques which I have developed to solve the
"correctly filled" template
check problem. But they can be described less easily and have
diminishing returns.

Cheers
     Caleb Lyness

> 2) Generate a junit test that uses a "<TemplateFiller>UnitTestImpl" instance 
> (which is the test itself) and compares the output of the template with the 
> contents of a provided file.
>
> Basically, I need a StringTemplate parser. I guess StringTemplate itself 
> defines an ANTLR-based one I could use.
>
> Do you think it would be possible to use it to extract all input parameters 
> from a template? It would have to be recursive itself, and probably indicate 
> at the end that the template input is an object graph, so I guess it will have 
> to traverse the AST for a template.
>
> I think a unit test framework for StringTemplate is highly desirable.
>
> What do you think?
> _______________________________________________
> stringtemplate-interest mailing list
> stringtemplate-interest at antlr.org
> http://www.antlr.org:8080/mailman/listinfo/stringtemplate-interest
>
>   

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20070209/d2762be9/attachment.html 
-------------- next part --------------
A non-text attachment was scrubbed...
Name: smime.p7s
Type: application/x-pkcs7-signature
Size: 3269 bytes
Desc: S/MIME Cryptographic Signature
Url : http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20070209/d2762be9/attachment.bin 


More information about the stringtemplate-interest mailing list