Commit 1ca5f025 authored by Matthew Dawson's avatar Matthew Dawson
Browse files

To test the PVS parser, make the MatlabParserOperatorParseTest generic.

Since any parser will have to test itself against a variety of operator, make
life simple and have one set of tests for all operators.  Also remove as much
code duplication as possible, by having one generic method that takes an operator
(as a String) and a parse function that handles calling the parse function
and compares the result as appropriate.

git-svn-id: https://groke.mcmaster.ca/svn/grad/colin/branches/TableTool_javization@10851 57e6efec-57d4-0310-aeb1-a6c144bb1a8b
parent fdabb422
Loading
Loading
Loading
Loading
+40 −24
Original line number Original line Diff line number Diff line
@@ -21,66 +21,82 @@ import ca.mcmaster.cas.tabularexpressiontoolbox.expression.ExpressionBinaryOpera
import ca.mcmaster.cas.tabularexpressiontoolbox.expression.ExpressionUnaryOperation;
import ca.mcmaster.cas.tabularexpressiontoolbox.expression.ExpressionUnaryOperation;
import ca.mcmaster.cas.tabularexpressiontoolbox.expression.UnaryOperation;
import ca.mcmaster.cas.tabularexpressiontoolbox.expression.UnaryOperation;
import ca.mcmaster.cas.tabularexpressiontoolbox.parsers.MatlabParser;
import ca.mcmaster.cas.tabularexpressiontoolbox.parsers.MatlabParser;
import ca.mcmaster.cas.tabularexpressiontoolbox.parsers.PVSSimpleParser;
import org.junit.Test;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;


import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collection;
import java.util.Map;


import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.CoreMatchers.is;
import static org.junit.Assert.*;
import static org.junit.Assert.assertThat;


/** This test simply tests that each operator parses correctly.
/** This test simply tests that each operator parses correctly.
 *
 *
 * This is a simply parametrized test set that only deals with the various operators MatlabParser deals with.  It avoids
 * This is a simply parametrized test set that only deals only with testing the individual operators.  It avoids
 * all the other details (it just uses literals for input).  It also avoids any dependency on generators.
 * all the other details (it just uses literals for input).  It also avoids any dependency on generators.
 *
 *
 * @author Matthew Dawson <matthew@mjdsystems.ca>
 * @author Matthew Dawson <matthew@mjdsystems.ca>
 */
 */
@RunWith(Parameterized.class)
@RunWith(Parameterized.class)
public class MatlabParserOperatorParseTest {
public class ParserOperatorParseTest {
    @Parameters
    @Parameters
    public static Collection<Object[]> data() {
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][]{
        return Arrays.asList(new Object[][]{
                {"+", BinaryOperation.Addition, null},
                {"+",  "+",   BinaryOperation.Addition,         null},
                {"-", BinaryOperation.Minus, null},
                {"-",  "-",   BinaryOperation.Minus,            null},
                {"*", BinaryOperation.Multiplication, null},
                {"*",  "*",   BinaryOperation.Multiplication,   null},
                {"/", BinaryOperation.Division, null},
                {"/",  "/",   BinaryOperation.Division,         null},
                {">", BinaryOperation.GreaterThen, null},
                {">",  ">",   BinaryOperation.GreaterThen,      null},
                {">=", BinaryOperation.GreaterThenEqual, null},
                {">=", ">=",  BinaryOperation.GreaterThenEqual, null},
                {"<", BinaryOperation.LessThen, null},
                {"<",  "<",   BinaryOperation.LessThen,         null},
                {"<=", BinaryOperation.LessThenEqual, null},
                {"<=", "<=",  BinaryOperation.LessThenEqual,    null},
                {"==", BinaryOperation.Equals, null},
                {"==", "=",   BinaryOperation.Equals,           null},
                {"&&", BinaryOperation.BooleanAnd, null},
                {"&&", "AND", BinaryOperation.BooleanAnd,       null},
                {"||", BinaryOperation.BooleanOr, null},
                {"||", "OR",  BinaryOperation.BooleanOr,        null},
                {"~", null, UnaryOperation.Negation},
                {"~",  "NOT", null,                             UnaryOperation.Negation},
                {"-", null, UnaryOperation.Minus}
                {"-",  "-",   null,                             UnaryOperation.Minus}
        });
        });
    }
    }
    @Parameter
    @Parameter
    public String inputSymbol;
    public String matlabInputSymbol;


    @Parameter(value = 1)
    @Parameter(value = 1)
    public BinaryOperation bop;
    public String pvsInputSymbol;


    @Parameter(value = 2)
    @Parameter(value = 2)
    public BinaryOperation bop;

    @Parameter(value = 3)
    public UnaryOperation uop;
    public UnaryOperation uop;


    @Test
    private void doTest(String expr, Method method) throws InvocationTargetException, IllegalAccessException {
    public void doTest() {
        if (bop != null) {
        if (bop != null) {
            // Binary op to test ...
            // Binary op to test ...
            ExpressionBinaryOperation exp = (ExpressionBinaryOperation)MatlabParser.parseMatlabCode(null, "1" + inputSymbol + "2\n");
            ExpressionBinaryOperation exp = (ExpressionBinaryOperation)method.invoke(null, null, "1 " + expr + " 2\n");


            assertThat(exp.toString(), is("(1 " + bop.toString() + " 2)"));
            assertThat(exp.toString(), is("(1 " + bop.toString() + " 2)"));
        } else {
        } else {
            // Unary op to test ...
            // Unary op to test ...
            ExpressionUnaryOperation exp = (ExpressionUnaryOperation)MatlabParser.parseMatlabCode(null, inputSymbol + "3\n");
            ExpressionUnaryOperation exp = (ExpressionUnaryOperation)method.invoke(null, null, expr + " 3\n");


            assertThat(exp.toString(), is("( " + uop.toString() + " 3)"));
            assertThat(exp.toString(), is("( " + uop.toString() + " 3)"));
        }
        }
    }
    }

    @Test
    public void testMatlabParser() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        doTest(matlabInputSymbol, MatlabParser.class.getMethod("parseMatlabCode", Map.class, String.class));
    }

    @Test
    public void testPVSSimpleParser() throws NoSuchMethodException, InvocationTargetException, IllegalAccessException {
        doTest(pvsInputSymbol, PVSSimpleParser.class.getMethod("parsePVSCode", Map.class, String.class));
    }
}
}