[stringtemplate-interest] Problem with checking length of string
Kenny Cockburn
kenny.cockburn at ntlworld.com
Thu Jun 1 16:18:31 PDT 2006
I had the following test script in c# 2.3b7 version
string TestIfStr = @"
group Base ;
TestStr(EmptyString) ::=<<
$if(!EmptyString)$
String Passed is NULL
$endif$
$if(!EmptyString.Length )$
String Passed has a Zero Length
$endif$
$if( EmptyString )$
Test for non null String Passed is The value ""$EmptyString$""
$endif$
$if( EmptyString.Length )$
String Has a Non Zero Length and the value ""$EmptyString$""
$endif$
>>
";
stg = new StringTemplateGroup(new StringReader(TestIfStr),
typeof(DefaultTemplateLexer));
sta3 = stg.GetInstanceOf("TestStr");
sta3.SetAttribute("EmptyString", "I have set a Value");
System.Console.WriteLine("EmptyString Passing In Value");
System.Console.WriteLine(sta3.ToString());
sta3 = stg.GetInstanceOf("TestStr");
System.Console.WriteLine("EmptyString Set To Empty String");
sta3.SetAttribute("EmptyString", string.Empty);
System.Console.WriteLine(sta3.ToString());
sta3 = stg.GetInstanceOf("TestStr");
System.Console.WriteLine("EmptyString To the null value");
sta3.SetAttribute("EmptyString", null);
System.Console.WriteLine(sta3.ToString());
-----
The only test that worked was when the string being passed contained a
Null reference ;
after some investigation in the c# I found the problem
the method
*public virtual bool TestAttributeTrue(object a) in file ASTExpr.cs
*
has a bug it did not deal with *numeric *values
The fix is to add the following lines at the end of the function
// KAC Can We convert this to a boolean
Type classType = typeof(System.Convert);
try
{
MethodInfo methodInfo =
classType.GetMethod(@"ToBoolean", new Type[] { a.GetType() });
// We should cache this relfection info
// Note It is possible methodInfo to be null
return (Boolean) methodInfo.Invoke(null, new
object[] { a });
}
catch
{
return true; // any other non-null object, return
true--it's present
}
------------
for best performance this reflection info should be cached in a hash
More information about the stringtemplate-interest
mailing list