Re: More 1.7 BC issues (porting from 1.5)
Boris Partensky <[email protected]>
| Newsgroups | gmane.comp.jakarta.velocity.user |
|---|---|
| Message-ID | <CABsrNP8ArsdF=-T1DcPLOr01FZGRok1yxu+N5vq71=mYeutszg@mail.gmail.com> |
> Ok, for my sanity, is this an accurate rewrite?
Yep.
> Do you get the same behavior in both with this?
Not quite same. *With* nested macros I get empty string in 1.7 and
foobaryokdar in 1.5. *Without* nested macros I get empty string in 1.5
and in 1.7. Below are the test cases for 1.5 and 1.7 that succeed.
1.5
public void testVelocityForEachNestedMacroInvokeMacroScope() throws Exception
{
VelocityEngine ve = new VelocityEngine();
ve.init();
String template = "#set($global_types=['foo', 'bar', 'yok',
'dar'])#macro( showBox $input)#set($type = '')$input#end"+
"#macro(showBoxes $types)#foreach($type in
$types)#showBox($type)#end#end#showBoxes($global_types)";
StringWriter eval = new StringWriter();
boolean b = ve.evaluate(new VelocityContext(), eval, "foo", template);
assertEquals(eval.toString(), "foobaryokdar", eval.toString());
}
public void testVelocityForEachNestedMacroInvokeMacroScopeNathan()
throws Exception
{
VelocityEngine ve = new VelocityEngine();
ve.init();
String template = "#macro( inner $arg )#set($ref =
'')$arg#end#macro( outer )#foreach( $ref in ['foo','bar','yok','dar']
)#inner( $ref )#end#end#outer()";
StringWriter eval = new StringWriter();
boolean b = ve.evaluate(new VelocityContext(), eval, "foo", template);
assertEquals(eval.toString(), "foobaryokdar", eval.toString());
}
public void testVelocityNoNestedMacroForEachInvokeMacroScope()
throws Exception
{
VelocityEngine ve = new VelocityEngine();
ve.init();
String template = "#macro( inner $arg )#set($ref =
'')$arg#end#foreach( $ref in ['foo','bar','yok','dar'] )#inner( $ref
)#end";
StringWriter eval = new StringWriter();
boolean b = ve.evaluate(new VelocityContext(), eval, "foo", template);
assertEquals(eval.toString(), "", eval.toString());
}
1.7
public void testVelocityForEachNestedMacroInvokeMacroScope()
{
String template = "#set($global_types=['foo', 'bar', 'yok',
'dar'])#macro( showBox $input)#set($type = '')$input#end"+
"#macro(showBoxes $types)#foreach($type in
$types)#showBox($type)#end#end#showBoxes($global_types)";
String eval = evaluate(template);
assertEquals(eval, "", eval);
}
public void testVelocityForEachNestedMacroInvokeMacroScopeNathan()
{
String template = "#macro( inner $arg )#set($ref =
'')$arg#end#macro( outer )#foreach( $ref in ['foo','bar','yok','dar']
)#inner( $ref )#end#end#outer()";
String eval = evaluate(template);
assertEquals(eval, "", eval);
}
public void testVelocityNoNestedMacroForEachInvokeMacroScope()
{
String template = "#macro( inner $arg )#set($ref =
'')$arg#end#foreach( $ref in ['foo','bar','yok','dar'] )#inner( $ref
)#end";
String eval = evaluate(template);
assertEquals(eval, "", eval);
}
On Thu, May 3, 2012 at 3:16 PM, Nathan Bubna <[email protected]> wrote:
> Ok, for my sanity, is this an accurate rewrite?
>
> #macro( inner $arg )
> #set($ref = '')$arg
> #end
> #macro( outer )
> #foreach( $ref in ['foo','bar','yok','dar'] )
> #inner( $ref )
> #end
> #end
> #outer()
>
> Do you get the same behavior in both with this?
>
> #macro( inner $arg )
> #set($ref = '')$arg
> #end
> #foreach( $ref in ['foo','bar','yok','dar'] )
> #inner( $ref )
> #end
>
> Or is it only when the macros are nested?
> Also, do you have localscope on for the macros?
>
> I know changes were made in the pass-by-name behavior between 1.5 and
> the trunk (2.0-SNAPSHOT), but my memory is failing as to the
> chronology of them. What i do recall (possibly incorrectly), makes me
> think this should have been reversed; blank in the older version,
> instead of the newer. I thought we stopped proxying #set calls on
> macro args, because while that was arguably a correct way to do
> pass-by-name, it was deemed surprising and not worth the
> implementation/performance costs. I seem to recall arguing with
> someone about this, maybe Byron? Well, i'm confused. I also haven't
> the time to fire up Velocity environment right now on this machine to
> test it. Perhaps someone else can step in.
>
> On Thu, May 3, 2012 at 11:51 AM, Boris Partensky
> <[email protected]> wrote:
>> Not seeing any errors in log. Corrected to 2 single quotes. Same
>> behavior. I was hoping that $input in each iteration will not be empty
>> string, but rather 'foo', 'bar' etc (that's the way 1.5 behaves). Why
>> would setting a value of $type in parent (foreach) scope would affect
>> the value of the local $input argument?
>>
>> String template = "#set($global_types=['foo', 'bar', 'yok',
>> 'dar'])#macro( showBox $input)#set($type = '')$input#end"+
>> "#macro(showBoxes $types)#foreach($type in
>> $types)#showBox($type)#end#end#showBoxes($global_types)";
>>
>>
>> On Thu, May 3, 2012 at 2:33 PM, Nathan Bubna <[email protected]> wrote:
>>> Anything in the log? #set($type = \"\") is not valid syntax, i think.
>>> I'm not even sure what value you want $type to have. Two double
>>> quotes or an empty string?
>>>
>>> On Thu, May 3, 2012 at 11:17 AM, Boris Partensky
>>> <[email protected]> wrote:
>>>> Hi, I have this use case which involves 2 nested macros and a foreach.
>>>> I am trying to understand why this template evaluates to empty string
>>>> on 1.7, and to "foobaryokdar" - on 1.5.
>>>>
>>>>
>>>> String template = "#set($global_types=['foo', 'bar', 'yok',
>>>> 'dar'])#macro( showBox $input)#set($type = \"\")$input#end"+
>>>> "#macro(showBoxes $types)#foreach($type in
>>>> $types)#showBox($type)#end#end#showBoxes($global_types)";
>>>>
>>>> ---------------------------------------------------------------------
>>>> To unsubscribe, e-mail: [email protected]
>>>> For additional commands, e-mail: [email protected]
>>>>
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: [email protected]
>>> For additional commands, e-mail: [email protected]
>>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [email protected]
>> For additional commands, e-mail: [email protected]
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [email protected]
> For additional commands, e-mail: [email protected]
>