001: package org.hanseltest.stack;
002:
003: import junit.framework.Test;
004: import junit.framework.TestCase;
005:
006: import org.hansel.CoverageDecorator;
007: import org.hansel.stack.BinaryOperatorEntry;
008: import org.hansel.stack.HanselValue;
009: import org.hansel.stack.OperatorEntry;
010:
011: /**
012: * Test the BinaryOperatorEntry and class.
013: * @author Niklas Mehner
014: */
015: public class BinaryOperatorEntryTest extends TestCase {
016:
017: /**
018: * Create a new Test.
019: * @param name Name of the test.
020: */
021: public BinaryOperatorEntryTest(String name) {
022: super (name);
023: }
024:
025: /**
026: * Returns a new BinaryOperatorEntry.
027: * @param operator String representing the operator.
028: * @param precedence Precedence of the operator.
029: * @param type Type of the result.
030: * @param arg1 Name of the first argument.
031: * @param arg2 Name of the second argument.
032: * @return new BinaryOperatorEntry.
033: */
034: private HanselValue getOperatorEntry(String operator,
035: int precedence, String arg1, String arg2) {
036: HanselValue se1 = new HanselValue(arg1, false, 1);
037: HanselValue se2 = new HanselValue(arg2, false, 1);
038:
039: return new BinaryOperatorEntry(operator, precedence, se1, se2);
040: }
041:
042: /**
043: * Test simple addition.
044: */
045: public void testSimple() {
046: assertEquals("arg1 + arg2", getOperatorEntry("+", 3, "arg1",
047: "arg2").toString());
048: }
049:
050: /** Test the getOperator*() methods. */
051: public void testGetOperator() {
052: HanselValue se1 = new HanselValue("arg1", false, 1);
053: HanselValue se2 = new HanselValue("arg2", false, 1);
054: BinaryOperatorEntry boe = new BinaryOperatorEntry("+", 3, se1,
055: se2);
056:
057: assertSame(se1, boe.getOperator1());
058: assertSame(se2, boe.getOperator2());
059: }
060:
061: /** Test compress. */
062: public void testCompress() {
063: HanselValue se1 = new HanselValue("arg1", false, 1);
064: HanselValue se2 = new HanselValue("arg2", false, 1);
065: BinaryOperatorEntry boe = new BinaryOperatorEntry("+", 3, se1,
066: se2);
067:
068: assertSame(boe, boe.compress());
069: }
070:
071: /**
072: * Test precedence.
073: */
074: public void testPrecedence1() {
075: HanselValue entry1 = new HanselValue("arg1", false, 1);
076: HanselValue entry2 = getOperatorEntry("*", 2, "arg2", "arg3");
077: assertEquals("arg1 + arg2 * arg3", new BinaryOperatorEntry("+",
078: 3, entry1, entry2).toString());
079: }
080:
081: /**
082: * Test precedence.
083: */
084: public void testPrecedence2() {
085: HanselValue entry1 = new HanselValue("arg1", false, 1);
086: HanselValue entry2 = getOperatorEntry("+", 3, "arg2", "arg3");
087: assertEquals("arg1 * (arg2 + arg3)", new BinaryOperatorEntry(
088: "*", 2, entry1, entry2).toString());
089: }
090:
091: /**
092: * Test precedence.
093: */
094: public void testPrecedence3() {
095: HanselValue entry1 = new HanselValue("arg1", false, 1);
096: HanselValue entry2 = getOperatorEntry("+", 3, "arg2", "arg3");
097: assertEquals("arg1 + arg2 + arg3", new BinaryOperatorEntry("+",
098: 3, entry1, entry2).toString());
099: }
100:
101: /**
102: * Test precedence.
103: */
104: public void testPrecedence4() {
105: HanselValue entry1 = getOperatorEntry("+", 3, "arg1", "arg2");
106: HanselValue entry2 = getOperatorEntry("+", 3, "arg3", "arg4");
107: assertEquals("(arg1 + arg2) * (arg3 + arg4)",
108: new BinaryOperatorEntry("*", 2, entry1, entry2)
109: .toString());
110: }
111:
112: /**
113: * Returns a coverage decorator for this test.
114: * @return CoverageTest.
115: */
116: public static Test suite() {
117: return new CoverageDecorator(BinaryOperatorEntryTest.class,
118: new Class[] { OperatorEntry.class,
119: BinaryOperatorEntry.class });
120: }
121: }
|