[stringtemplate-interest] Attribute not found exception? Revisited
John Snyders
jjsnyders at rcn.com
Thu Nov 9 07:41:17 PST 2006
-----Original Message-----
From: stringtemplate-interest-bounces at antlr.org
[mailto:stringtemplate-interest-bounces at antlr.org]On Behalf Of Caleb Lyness
Sent: Wednesday, November 08, 2006 3:47 PM
To: StringTemplate
Subject: [stringtemplate-interest] Attribute not found exception?
Revisited
Ahoy!
With ref to the following thread:
http://www.antlr.org:8080/pipermail/stringtemplate-interest/2006-March/00029
1.html
* I am looking for a way to ask a template what attributes it is
using/wanting.
[John Snyders] This is interesting and seems to imply that the template
(view) is in charge of defining the set of attributes. I usually think of
the model/controler as being the one that defines the set of attributes
available to the view. Perhaps a minor distinction since they both have to
work together. Are you asking this question to determine if the model is
generating stuff that the view does need? Like a good Java IDE will tell you
when you have unused variables.
So far I have overloaded a Map class which records which keys are
requested.
I pass this in as the attributes list. At the end of a template gen I know
what was
requested and what was not.
[John Snyders] Nice solution but how does it handle conditional logic in
the template? Also this would force a particular data model. Specifically
all access would need to go throught the map or something that looked like a
map. How could you tell that a scalar attribute ($foo$) was used?
Q: Can I do this with out actually rendering, (via toString())... i.e. is
there another way?
[John Snyders] In theory the parsed template should know about all the
attribute references. An alternate tree walk could report on all possible
attribute references. What it wouldn't know is if the references was to a
map key, property, field etc.
* Sometimes its useful to know (say for testing) that a "required" string
is missing. What is
required and what is not is quite specific to the template. Here is how I
do it for the HTML
I am generating:
[John Snyders] This is a seperate problem right? This is the case I think
is more interesting and important. The model/controler defines what
attributes will be available under what conditions. This contract should be
unit testable. Using a template to do this as you describe is a nice way to
unit test the contract without having to instrument the model/controler.
Your example has instrumented the actual template. I think it would be
better to somehow substitute a special unit test template. It should check
more than just what is required but what is expected for a given request.
Has anyone created extensions to JUnit to do this?
$if (checkAttributes)$
<!--
$reqAttr;null=missingAttribute("reqAttr")$
$serviceCode;null=missingAttribute("serviceCode")$
-->
$endif$
If the checkAttributes attribute is true/specified the html comment is
"rendered". If the
value is "required" by this template then it is listed in this comment.
When the comment
is rendered either the value is rendered or if the attribute is missing it
spits out a string
which can be easily found including the name of the missing attribute.
The template shown is defined in a group file as:
missingAttribute(attributeName) ::= "TEST:FAIL - Missing attribute:
\$$attributeName$\$"
The output will look something like this:
<p>
<!--
TEST:FAIL - Missing attribute: $reqAttr$
1
-->
You have recei
The unit tests can then search for these failure messages and make the
test fall over. The comments prevent
the test stuff from breaking the final output... :-)
For maps you can use a default to generate a string which can be caught in
the test too. Its a pity
the key of the map is not treated as an attribute. That would make this
possible:
[John Snyders] From my view I would not be as concerned with maps because
they are under the control of the template and therefore not part of the
boundary I would want to test.
amap ::= [
"2": "value 2",
"5": "Other",
default: "Unknown reason: $key$"
]
With this you could generate the fail message AND report the failing
value....
Perhaps someone else can think of a way to solve this one...
[John Snyders] Not off the top of my head
Cheers
Caleb
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org:8080/pipermail/stringtemplate-interest/attachments/20061109/726e4712/attachment.html
More information about the stringtemplate-interest
mailing list