Skip to content

Commit

Permalink
Parameterize operator precedence tests with JUnit
Browse files Browse the repository at this point in the history
Nicely separate test cases using JUnit runner logic
Previously this was run in one test in a gigantic for-loop but now we split it into different cases using JUnit 4's parameterized class functionality. This does increase the runtime a bit (now runs on the order of 1-2 seconds) but should be worth it for debugging failures.

[Refactor][Test][SANY]

Signed-off-by: Andrew Helwer <[email protected]>
  • Loading branch information
ahelwer authored and lemmy committed Feb 12, 2025
1 parent a1a9694 commit 5e1673e
Show file tree
Hide file tree
Showing 3 changed files with 618 additions and 445 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*******************************************************************************
* Copyright (c) 2024 Linux Foundation. All rights reserved.
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/
package tla2sany.parser;

import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

import util.UniqueString;

/**
* SANY has the concept of a "canonical" operator representation, since some
* operators have multiple possible symbols associated with them. This tests
* that the canonical operator is as expected.
*/
@RunWith(Parameterized.class)
public class CanonicalOperatorTests {

/**
* Generates all test cases.
*/
@Parameters(name = "{index}: {0}")
public static List<OperatorPrecedenceTests.Operator> getTests() {
final List<OperatorPrecedenceTests.Operator> testCases = new ArrayList<>();
for (final OperatorPrecedenceTests.Operator op : OperatorPrecedenceTests.OPERATORS) {
testCases.add(op);
}
return testCases;
}

/**
* The operator to test for canonical correctness.
*/
@Parameter
public OperatorPrecedenceTests.Operator op;

/**
* Tests that the {@link Operators} class resolves all operator synonyms
* to their correct canonical form.
*/
@Test
public void testCanonicalOperatorCorrectness() {
String expected = op.symbols[0];
for (String symbol : op.symbols) {
UniqueString symbolUnique = UniqueString.uniqueStringOf(symbol);
Assert.assertTrue(Operators.existsOperator(symbolUnique));
Assert.assertEquals(expected, Operators.resolveSynonym(symbolUnique).toString());
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
/*******************************************************************************
* Copyright (c) 2024 Linux Foundation. All rights reserved.
*
* The MIT License (MIT)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
* of the Software, and to permit persons to whom the Software is furnished to do
* so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
******************************************************************************/
package tla2sany.parser;

import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;

import util.ParserAPI;

import org.junit.Test;

/**
* Exhaustive tests of operator associativity. Iterates through all standard
* operators and tests whether expressions of the form "A + B + C" parse
* correctly; if operators are marked as (left-)associative then parsing
* should succeed, but if not it should fail.
*/
@RunWith(Parameterized.class)
public class OperatorAssociativityTests {

/**
* Generates all test cases.
*/
@Parameters(name = "{index}: A {1} B {2} C")
public static List<Object[]> getTests() {
final List<Object[]> testCases = new ArrayList<Object[]>();
for (final OperatorPrecedenceTests.Operator op : OperatorPrecedenceTests.OPERATORS) {
if (OperatorPrecedenceTests.FixKind.INFIX != op.fix) {
continue;
}
for (final String opSymbol1 : op.symbols) {
for (final String opSymbol2 : op.symbols) {
testCases.add(new Object[] { op, opSymbol1, opSymbol2 });
}
}
}
return testCases;
}

/**
* The operator to test for associativity parsing correctness.
*/
@Parameter(0)
public OperatorPrecedenceTests.Operator op;

/**
* The specific symbol to use leftmost in the expression.
*/
@Parameter(1)
public String opSymbol1;

/**
* The specific symbol to use rightmost in the expression.
*/
@Parameter(2)
public String opSymbol2;

/**
* Test to ensure associative operators associate and non-associative
* operators do not.
*/
@Test
public void testOperatorAssociativity() {
final String expr = String.format("A %s B %s C", opSymbol1, opSymbol2);
final String inputString = String.format(OperatorPrecedenceTests.PATTERN, expr);
final boolean parseSuccess = null != ParserAPI.processSyntax(inputString);
Assert.assertEquals(expr, op.associative, parseSuccess);
}
}
Loading

0 comments on commit 5e1673e

Please sign in to comment.