Question about unit-testing templates

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Question about unit-testing templates

YoavShapira
Hi,
This may be another newbie question, but hopefully it's not as
clueless as my earlier question about hash keys ;)

Let's say I have a template, parent.ftl, that includes another
template, child.ftl, via <#include "child.ftl">.  Let's also say
child.ftl requires a variable, ${parentVar}, that is set in
parent.ftl, and
also sets some of its own variables, e.g. ${childVar}.  So far so good.

I'd like to be able to unit test both templates individually, i.e.
have a test (JUnit) class that does something like this:

testParent() {
  Map model = getParentModel(...)
  Template parent = ...
  Writer writer = ...
  template.process(model, writer);
}

testChild() {
  Map model = getChildModel(...)
  Template child = ...
  Writer writer = ...
  template.process(model, writer);
}

The parent model contains everything the parent and child need, so
testParent() passes without a problem.  The child model contains only
the child variables, not the parent variables, so testChild() fails.

Now I know I can get around this by using the unsafe operators in the
child template, child.ftl, e.g. ${parentVar!"some default"}.  But I
wonder, is there another way?  A setting perhaps that I can use only
to enable unit-testing of templates?

Any help is welcome, including pointers to best practices on
unit-testing template processing.  Thanks, and have a good weekend,

Yoav

-------------------------------------------------------------------------
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
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: Question about unit-testing templates

Daniel Dekany
Saturday, August 12, 2006, 12:38:30 AM, Yoav Shapira wrote:


> Hi,
> This may be another newbie question, but hopefully it's not as
> clueless as my earlier question about hash keys ;)
>
> Let's say I have a template, parent.ftl, that includes another
> template, child.ftl, via <#include "child.ftl">.  Let's also say
> child.ftl requires a variable, ${parentVar}, that is set in
> parent.ftl, and
> also sets some of its own variables, e.g. ${childVar}.  So far so good.
>
> I'd like to be able to unit test both templates individually, i.e.
> have a test (JUnit) class that does something like this:
>
> testParent() {
>   Map model = getParentModel(...)
>   Template parent = ...
>   Writer writer = ...
>   template.process(model, writer);
> }
>
> testChild() {
>   Map model = getChildModel(...)
>   Template child = ...
>   Writer writer = ...
>   template.process(model, writer);
> }
>
> The parent model contains everything the parent and child need, so
> testParent() passes without a problem.  The child model contains only
> the child variables, not the parent variables, so testChild() fails.
>
> Now I know I can get around this by using the unsafe operators in the
> child template, child.ftl, e.g. ${parentVar!"some default"}.

You hardly ever should change a templates just so that the JUnit test
will work. In this case, why don't just put the "parent variables"
into the data model too? This is how the child template will run in
reality after all.

> But I wonder, is there another way? A setting perhaps that I can use
> only to enable unit-testing of templates?

The question is if what do you want to test. By getting the template
but not executing it you can check if it contains syntactical errors.
For checking runtime errors (and the content of the data model is a
runtime thing) the template must be executed, and MVC templates
heavily depend on the data model, so usually you can't test them
without a complete data model.

> Any help is welcome, including pointers to best practices on
> unit-testing template processing.  Thanks, and have a good weekend,

--
Best regards,
 Daniel Dekany


-------------------------------------------------------------------------
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
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user
Reply | Threaded
Open this post in threaded view
|

Re: Question about unit-testing templates

YoavShapira
Daniel,
Thanks again!

> The question is if what do you want to test. By getting the template
> but not executing it you can check if it contains syntactical errors.

This hits the spot on the head: I really just wanted to check for
syntactical errors.  I didn't realize (I should have) that a simple
get(templateName) validates it syntactically: that's perfect for my
needs.

I have to say, as a long-time user of and contributor to a bunch of
open-source projects, I'm thoroughly impressed with Freemarker's docs
and mailing list so far.

Yoav

-------------------------------------------------------------------------
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
_______________________________________________
FreeMarker-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/freemarker-user