Commit 98223593 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 9f25430d
Loading
Loading
Loading
Loading
+40 −24
Original line number 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.UnaryOperation;
import ca.mcmaster.cas.tabularexpressiontoolbox.parsers.MatlabParser;
import ca.mcmaster.cas.tabularexpressiontoolbox.parsers.PVSSimpleParser;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
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.Collection;
import java.util.Map;

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

/** 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.
 *
 * @author Matthew Dawson <matthew@mjdsystems.ca>
 */
@RunWith(Parameterized.class)
public class MatlabParserOperatorParseTest {
public class ParserOperatorParseTest {
    @Parameters
    public static Collection<Object[]> data() {
        return Arrays.asList(new Object[][]{
                {"+", BinaryOperation.Addition, null},
                {"-", BinaryOperation.Minus, null},
                {"*", BinaryOperation.Multiplication, null},
                {"/", BinaryOperation.Division, null},
                {">", BinaryOperation.GreaterThen, null},
                {">=", BinaryOperation.GreaterThenEqual, null},
                {"<", BinaryOperation.LessThen, null},
                {"<=", BinaryOperation.LessThenEqual, null},
                {"==", BinaryOperation.Equals, null},
                {"&&", BinaryOperation.BooleanAnd, null},
                {"||", BinaryOperation.BooleanOr, null},
                {"~", null, UnaryOperation.Negation},
                {"-", null, UnaryOperation.Minus}
                {"+",  "+",   BinaryOperation.Addition,         null},
                {"-",  "-",   BinaryOperation.Minus,            null},
                {"*",  "*",   BinaryOperation.Multiplication,   null},
                {"/",  "/",   BinaryOperation.Division,         null},
                {">",  ">",   BinaryOperation.GreaterThen,      null},
                {">=", ">=",  BinaryOperation.GreaterThenEqual, null},
                {"<",  "<",   BinaryOperation.LessThen,         null},
                {"<=", "<=",  BinaryOperation.LessThenEqual,    null},
                {"==", "=",   BinaryOperation.Equals,           null},
                {"&&", "AND", BinaryOperation.BooleanAnd,       null},
                {"||", "OR",  BinaryOperation.BooleanOr,        null},
                {"~",  "NOT", null,                             UnaryOperation.Negation},
                {"-",  "-",   null,                             UnaryOperation.Minus}
        });
    }
    @Parameter
    public String inputSymbol;
    public String matlabInputSymbol;

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

    @Parameter(value = 2)
    public BinaryOperation bop;

    @Parameter(value = 3)
    public UnaryOperation uop;

    @Test
    public void doTest() {
    private void doTest(String expr, Method method) throws InvocationTargetException, IllegalAccessException {
        if (bop != null) {
            // 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)"));
        } else {
            // 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)"));
        }
    }

    @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));
    }
}