dcsimg
junit test for expression like "if a+b c-d/3 then 1 else if ab & cd then 2 else 3"
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   lavanya_naidu
Posted On:   Wednesday, May 16, 2007 05:55 AM

i need junit test case for the class which is using expression like "if a+b > c-d/3 then 1 else if a>b & c>d then 2 else 3" .terms,factors and soon. this method is calling a ExpressionParserLookup interface which is implemented by some class. public static double evaluate(String expression, ExpressionParserLookup lookup) throws ExpressionParserException { return (new ExpressionParser2(expression,lookup)).evaluate(); } the evaluate method is using using the :- public double evaluate(String name) throws Exception { //name as to be either characname ,userfield name or //a characvalue(presided by its characname and a dot(.)) TRChara   More>>

i need junit test case for the class which is using expression like "if a+b > c-d/3 then 1 else if a>b & c>d then 2 else 3" .terms,factors and soon.




this method is calling a ExpressionParserLookup interface which is implemented by some class.



			
public static double evaluate(String expression, ExpressionParserLookup lookup)
throws ExpressionParserException {
return (new ExpressionParser2(expression,lookup)).evaluate();
}

the evaluate method is using using the :-

public double evaluate(String name) throws Exception {
//name as to be either characname ,userfield name or
//a characvalue(presided by its characname and a dot(.))
TRCharacValue characValue = null;
TRUserValue userValue = null;
name=removeBraces(name);//if name is enclosed by square brackets
if (name != null) {
characValue = getCharacValueByName(name);
if (characValue != null) {
//1.given name is a charac name
//characValue.
TRCharac charac = getCharac(characValue.getCharacID());
if(charac.getIsMulti())
{
return getMultiValuedCharacMetric(charac);

}
if(characValue.getMetric()!=TRObject.TR_NUMBER_NULL)
characValue = (new TRCharacValueSQL()).getCharacValue(m_oCon,characValue.getKey());

return characValue.getMetric();
/* if (TRCharacValue.DATATYPE_INT == characValue.getPartialCharac().getDataType())
return characValue.getIntValue();
else
return characValue.getMetric();*/

}
userValue = getUserValueByName(name);
if (userValue != null) {
//2.given name is a userfield name
char dataType = userValue.getDataType();
if (TRUserValue.DATATYPE_INT == dataType) {
return userValue.getIntValue();

}else if (TRUserValue.DATATYPE_DECIMAL == dataType) {
long value = userValue.getDecimalValue();
return value/Math.pow(10,TRUserField.DECIMALSCALE);

}
}

//3.it might be a charac value
//characvalue must be presided by its characname and dot(.)
String splitArray[] = name.split("\.");//'\.'escape sequence for '.'
for (int i = 1; i <= splitArray.length; i++) {
//but characname can have a dot .so we need to check different combinations of splited array
//to get exact charac name and characvalue
String[] result = getNextCombination(splitArray, i, ".");
if (result != null) {
String characName = result[0];
String characValueName = result[1];
//sarath:if the charac value ends with .
if(name.endsWith("."))
characValueName+=".";
TRCharac charac = null;
//TRCharacValueSQL valueSQL = new TRCharacValueSQL();
charac =( new TRCharacSQL()).getCharac(m_oCon,characName,TRObject.TR_NUMBER_NULL);
if (charac != null) {
//if the charac is a multi-valued then 2 cases arise
//1)if it is non summed multivalued then a) return TRObject.TR_NUMBER_NULL
// if given charac value one of the values.else b) return the metric
// value of that given charac value
//2)If it is summed multivalued then calucalate accordingly and return
//the calculated metric value. presently this code wont work this case

if(charac.getIsMulti()&& TRCharac.NONE.equals(charac.getMetricType()))
return getMultiMetricValue(charac,characValueName);
///if the charac is not multi-valued then
///return the metric value of that charac value.
return getSingleMetricValue(charac,characValueName);
}//if characvalue!=null
}//if result!=null
}//for
}
throw new IllegalArgumentException( "c_DM_CEXPRLOOKUP_EVALERROR" );
}



how can i write a junit test case for this method.Please help me URGENT.i am new to Junit.

   <<Less

Re: junit test for expression like "if a+b c-d/3 then 1 else if ab & cd then 2 else 3"

Posted By:   Robert_Lybarger  
Posted On:   Wednesday, May 16, 2007 09:07 AM

I am not going to bother to read that much code, so I will just try to respond from what I understand of the question statement itself: If you have a method which takes parameters and produces output based on three different states (relative to the parameter values), then write three different test methods, and use parameters in each test that you know a priori will trigger those different responses.

public void testConditionOne() {
result = callMethod(1, 1, 0, 0);
//assertions and checks on the result here
}

public void testConditionTwo() {
result = callMethod(1, 0, 1, 0);
//assertions and checks on the result here
}

public void testConditionThree() {
result = callMethod(0, 0, 1, 1);
//assertions and checks on the result here
}

You cannot test a continuous spectrum of parameter combinations, and you don't really need to. Pick some combinations that exercises those different portions, and then be finished with it.
About | Sitemap | Contact