package org.hanseltest.stack;
import junit.framework.Test;
import junit.framework.TestCase;
import org.hansel.CoverageDecorator;
import org.hansel.stack.BinaryOperatorEntry;
import org.hansel.stack.HanselValue;
import org.hansel.stack.OperatorEntry;
/**
* Test the BinaryOperatorEntry and class.
* @author Niklas Mehner
*/
public class BinaryOperatorEntryTest extends TestCase {
/**
* Create a new Test.
* @param name Name of the test.
*/
public BinaryOperatorEntryTest(String name) {
super(name);
}
/**
* Returns a new BinaryOperatorEntry.
* @param operator String representing the operator.
* @param precedence Precedence of the operator.
* @param type Type of the result.
* @param arg1 Name of the first argument.
* @param arg2 Name of the second argument.
* @return new BinaryOperatorEntry.
*/
private HanselValue getOperatorEntry(String operator,
int precedence,
String arg1,
String arg2) {
HanselValue se1 = new HanselValue(arg1, false, 1);
HanselValue se2 = new HanselValue(arg2, false, 1);
return new BinaryOperatorEntry(operator, precedence, se1, se2);
}
/**
* Test simple addition.
*/
public void testSimple() {
assertEquals("arg1 + arg2",
getOperatorEntry("+", 3,
"arg1", "arg2").toString());
}
/** Test the getOperator*() methods. */
public void testGetOperator() {
HanselValue se1 = new HanselValue("arg1", false, 1);
HanselValue se2 = new HanselValue("arg2", false, 1);
BinaryOperatorEntry boe = new BinaryOperatorEntry("+", 3, se1, se2);
assertSame(se1, boe.getOperator1());
assertSame(se2, boe.getOperator2());
}
/** Test compress. */
public void testCompress() {
HanselValue se1 = new HanselValue("arg1", false, 1);
HanselValue se2 = new HanselValue("arg2", false, 1);
BinaryOperatorEntry boe = new BinaryOperatorEntry("+", 3, se1, se2);
assertSame(boe, boe.compress());
}
/**
* Test precedence.
*/
public void testPrecedence1() {
HanselValue entry1 = new HanselValue("arg1", false, 1);
HanselValue entry2 = getOperatorEntry("*", 2,
"arg2", "arg3");
assertEquals("arg1 + arg2 * arg3",
new BinaryOperatorEntry("+", 3,
entry1, entry2).toString());
}
/**
* Test precedence.
*/
public void testPrecedence2() {
HanselValue entry1 = new HanselValue("arg1", false, 1);
HanselValue entry2 = getOperatorEntry("+", 3,
"arg2", "arg3");
assertEquals("arg1 * (arg2 + arg3)",
new BinaryOperatorEntry("*", 2,
entry1, entry2).toString());
}
/**
* Test precedence.
*/
public void testPrecedence3() {
HanselValue entry1 = new HanselValue("arg1", false, 1);
HanselValue entry2 = getOperatorEntry("+", 3,
"arg2", "arg3");
assertEquals("arg1 + arg2 + arg3",
new BinaryOperatorEntry("+", 3,
entry1, entry2).toString());
}
/**
* Test precedence.
*/
public void testPrecedence4() {
HanselValue entry1 = getOperatorEntry("+", 3,
"arg1", "arg2");
HanselValue entry2 = getOperatorEntry("+", 3,
"arg3", "arg4");
assertEquals("(arg1 + arg2) * (arg3 + arg4)",
new BinaryOperatorEntry("*", 2,
entry1, entry2).toString());
}
/**
* Returns a coverage decorator for this test.
* @return CoverageTest.
*/
public static Test suite() {
return new CoverageDecorator(BinaryOperatorEntryTest.class,
new Class[] {OperatorEntry.class,
BinaryOperatorEntry.class});
}
}
|