Migrate from JUnit 4 to 5

This commit is contained in:
Riccardo Azzolini 2019-08-12 17:45:20 +02:00
parent fc119efedc
commit b3f2ad82d0
10 changed files with 121 additions and 173 deletions

View File

@ -46,8 +46,8 @@
<artifactId>commons-lang3</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

View File

@ -29,18 +29,4 @@ public class DslAggregateException extends Exception {
public List<DslError> getErrors() {
return errors;
}
@Override
public boolean equals(final Object o) {
if (!(o instanceof DslAggregateException)) {
return false;
}
final DslAggregateException other = (DslAggregateException) o;
return this.errors.equals(other.errors);
}
@Override
public int hashCode() {
return Objects.hash(errors);
}
}

View File

@ -1,35 +0,0 @@
package it.cavallium.warppi;
import junit.framework.Assert;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
/**
* Unit test for simple App.
*/
public class AppTest extends TestCase {
/**
* Create the test case
*
* @param testName
* name of the test case
*/
public AppTest(final String testName) {
super(testName);
}
/**
* @return the suite of tests being tested
*/
public static Test suite() {
return new TestSuite(AppTest.class);
}
/**
* Rigourous Test :-)
*/
public void testApp() {
Assert.assertTrue(true);
}
}

View File

@ -10,13 +10,13 @@ import it.cavallium.warppi.math.rules.RuleType;
import it.cavallium.warppi.math.rules.dsl.patterns.*;
import it.cavallium.warppi.util.Error;
import it.unimi.dsi.fastutil.objects.ObjectArrayList;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
public class PatternRuleTest {
class PatternRuleTest {
private final MathContext mathContext = new MathContext();
private final Pattern x = new SubFunctionPattern("x");
@ -26,7 +26,7 @@ public class PatternRuleTest {
);
@Test
public void testNonMatching() throws InterruptedException, Error {
void testNonMatching() throws InterruptedException, Error {
final Function func = new Sum(
mathContext,
new Number(mathContext, 1),
@ -38,7 +38,7 @@ public class PatternRuleTest {
}
@Test
public void testMatching() throws InterruptedException, Error {
void testMatching() throws InterruptedException, Error {
final Function func = new Sum(
mathContext,
new Number(mathContext, 1),
@ -57,7 +57,7 @@ public class PatternRuleTest {
}
@Test
public void testMatchingRecursive() throws InterruptedException, Error {
void testMatchingRecursive() throws InterruptedException, Error {
final Function func = new Sum(
mathContext,
new Number(mathContext, 3),
@ -85,13 +85,14 @@ public class PatternRuleTest {
}
@Test
public void testMultipleReplacements() throws InterruptedException, Error {
void testMultipleReplacements() throws InterruptedException, Error {
final Number one = new Number(mathContext, 1);
final Number two = new Number(mathContext, 2);
final Function func = new SumSubtraction(mathContext, one, two);
final Pattern x = new SubFunctionPattern("x");
final Pattern y = new SubFunctionPattern("y");
@SuppressWarnings("SuspiciousNameCombination")
final PatternRule rule = new PatternRule(
"TestRule",
RuleType.EXPANSION,
@ -108,7 +109,7 @@ public class PatternRuleTest {
}
@Test
public void testNoReplacements() throws InterruptedException, Error {
void testNoReplacements() throws InterruptedException, Error {
final Function func = new Sum(
mathContext,
new Number(mathContext, 1),

View File

@ -10,18 +10,18 @@ import it.cavallium.warppi.math.functions.equations.EquationsSystem;
import it.cavallium.warppi.math.functions.trigonometry.*;
import it.cavallium.warppi.math.rules.dsl.patterns.*;
import org.apache.commons.lang3.tuple.ImmutablePair;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import java.util.*;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
public class PatternTest {
class PatternTest {
private final MathContext mathContext = new MathContext();
@Test
public void subFunctionPattern() {
void subFunctionPattern() {
final Pattern pattern = new SubFunctionPattern("x");
final Function func = new Sum(
@ -36,16 +36,19 @@ public class PatternTest {
assertEquals(func, pattern.replace(mathContext, subFunctions.get()));
}
@Test(expected = UndefinedSubFunctionException.class) // TODO assert exception.getSubFunctionName().equals("x")
public void undefinedSubFunction() {
@Test
void undefinedSubFunction() {
final Pattern pattern = new SubFunctionPattern("x");
final Map<String, Function> subFunctions = Collections.singletonMap("y", new Number(mathContext, 1));
pattern.replace(mathContext, subFunctions);
final var exception = assertThrows(UndefinedSubFunctionException.class, () ->
pattern.replace(mathContext, subFunctions)
);
assertEquals("x", exception.getSubFunctionName());
}
@Test
public void sumPattern() {
void sumPattern() {
final Pattern pattern = new SumPattern(
new SubFunctionPattern("x"),
new SubFunctionPattern("y")
@ -69,7 +72,7 @@ public class PatternTest {
}
@Test
public void repeatedSubFunction() {
void repeatedSubFunction() {
final Pattern pattern = new SumPattern(
new SubFunctionPattern("x"),
new SubFunctionPattern("x")
@ -93,7 +96,7 @@ public class PatternTest {
}
@Test
public void numberPattern() {
void numberPattern() {
final Pattern pattern = new NumberPattern(BigDecimal.valueOf(Math.PI));
final Function shouldNotMatch = new Number(mathContext, 2);
@ -106,7 +109,7 @@ public class PatternTest {
}
@Test
public void negativePattern() {
void negativePattern() {
final Pattern pattern = new NegativePattern(
new SubFunctionPattern("x")
);
@ -124,7 +127,7 @@ public class PatternTest {
}
@Test
public void negativePatternForNumber() {
void negativePatternForNumber() {
final Pattern pattern = new NegativePattern(
new NumberPattern(new BigDecimal(1))
);
@ -139,7 +142,7 @@ public class PatternTest {
}
@Test
public void undefinedPattern() {
void undefinedPattern() {
final Pattern pattern = new UndefinedPattern();
final Function shouldNotMatch = new Number(mathContext, 0);
@ -152,7 +155,7 @@ public class PatternTest {
}
@Test
public void equationsSystemPattern() {
void equationsSystemPattern() {
final Pattern pattern = new EquationsSystemPattern(new Pattern[]{
new SubFunctionPattern("x"),
new SubFunctionPattern("y"),
@ -182,7 +185,7 @@ public class PatternTest {
}
@Test
public void rootPatternForRootSquare() {
void rootPatternForRootSquare() {
final Pattern pattern = new RootPattern(
new SubFunctionPattern("x"),
new SubFunctionPattern("y")
@ -210,7 +213,7 @@ public class PatternTest {
}
@Test
public void constantPattern() {
void constantPattern() {
final Pattern pattern = new ConstantPattern(MathematicalSymbols.PI);
final Function shouldNotMatch = new Variable(
@ -231,7 +234,7 @@ public class PatternTest {
}
@Test
public void otherBinaryPatterns() {
void otherBinaryPatterns() {
final Number one = new Number(mathContext, 1);
final Number two = new Number(mathContext, 2);
final SubFunctionPattern x = new SubFunctionPattern("x");
@ -239,6 +242,7 @@ public class PatternTest {
final Function shouldNotMatch = new Sum(mathContext, one, two);
@SuppressWarnings("SuspiciousNameCombination")
final List<ImmutablePair<Pattern, Function>> patternsAndMatchingFunctions = Arrays.asList(
new ImmutablePair<>(
new DivisionPattern(x, y),
@ -278,7 +282,7 @@ public class PatternTest {
}
@Test
public void otherUnaryPatterns() {
void otherUnaryPatterns() {
final Number one = new Number(mathContext, 1);
final SubFunctionPattern x = new SubFunctionPattern("x");

View File

@ -10,23 +10,18 @@ import it.cavallium.warppi.math.rules.dsl.patterns.NegativePattern;
import it.cavallium.warppi.math.rules.dsl.patterns.NumberPattern;
import it.cavallium.warppi.math.rules.dsl.patterns.SubFunctionPattern;
import it.cavallium.warppi.math.rules.dsl.patterns.SumPattern;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.junit.Assert.*;
public class RulesDslTest {
@org.junit.Rule
public ExpectedException thrown = ExpectedException.none();
import static org.junit.jupiter.api.Assertions.*;
class RulesDslTest {
@Test
public void validRules() throws DslAggregateException {
void validRules() throws DslAggregateException {
final List<Rule> rules = RulesDsl.makeRules(
"reduction test1: x -> x\n" +
"expansion test2:\n" +
@ -63,38 +58,35 @@ public class RulesDslTest {
}
@Test
public void lexerError() throws DslAggregateException {
thrown.expect(DslAggregateException.class);
thrown.expect(equalTo(
new DslAggregateException(Collections.singletonList(
void lexerError() {
final var exception = assertThrows(DslAggregateException.class, () ->
RulesDsl.makeRules("reduction test: 2. 5 -> 1")
);
final var expectedErrors = Collections.singletonList(
new IncompleteNumberLiteral(16, "2.")
))
));
RulesDsl.makeRules("reduction test: 2. 5 -> 1");
);
assertEquals(expectedErrors, exception.getErrors());
}
@Test
public void parserError() throws DslAggregateException {
thrown.expect(DslAggregateException.class);
thrown.expect(equalTo(
new DslAggregateException(Collections.singletonList(
void parserError() {
final var exception = assertThrows(DslAggregateException.class, () ->
RulesDsl.makeRules("existence test: x + y ->")
);
final var expectedErrors = Collections.singletonList(
new UnexpectedToken(new Token(TokenType.EOF, "", 24))
))
));
RulesDsl.makeRules("existence test: x + y ->");
);
assertEquals(expectedErrors, exception.getErrors());
}
@Test
public void undefinedSubFunction() throws DslAggregateException {
thrown.expect(DslAggregateException.class);
thrown.expect(equalTo(
new DslAggregateException(Collections.singletonList(
void undefinedSubFunction() {
final var exception = assertThrows(DslAggregateException.class, () ->
RulesDsl.makeRules("expansion test: x -> x + y")
);
final var expectedErrors = Collections.singletonList(
new UndefinedSubFunction(new Token(TokenType.IDENTIFIER, "y", 25))
))
));
RulesDsl.makeRules("expansion test: x -> x + y");
);
assertEquals(expectedErrors, exception.getErrors());
}
}

View File

@ -1,16 +1,16 @@
package it.cavallium.warppi.math.rules.dsl.errorutils;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
public class LineMapTest {
class LineMapTest {
@Test
public void emptyText() {
void emptyText() {
String text = "";
LineMap map = new LineMap(text);
@ -18,7 +18,7 @@ public class LineMapTest {
}
@Test
public void noLineSeparators() {
void noLineSeparators() {
String text = "single line";
LineMap map = new LineMap(text);
@ -29,7 +29,7 @@ public class LineMapTest {
}
@Test
public void trailingLf() {
void trailingLf() {
String text = "single line\n";
LineMap map = new LineMap(text);
@ -40,7 +40,7 @@ public class LineMapTest {
}
@Test
public void trailingCr() {
void trailingCr() {
String text = "single line\r";
LineMap map = new LineMap(text);
@ -51,7 +51,7 @@ public class LineMapTest {
}
@Test
public void trailingCrLf() {
void trailingCrLf() {
String text = "single line\r\n";
LineMap map = new LineMap(text);
@ -62,7 +62,7 @@ public class LineMapTest {
}
@Test
public void multipleNonEmptyLines() {
void multipleNonEmptyLines() {
String text = "line 1\nline 2\rline 3\r\nline 4";
LineMap map = new LineMap(text);
@ -76,7 +76,7 @@ public class LineMapTest {
}
@Test
public void singleEmptyLine() {
void singleEmptyLine() {
String text = "\n";
LineMap map = new LineMap(text);
@ -87,7 +87,7 @@ public class LineMapTest {
}
@Test
public void multipleEmptyLines() {
void multipleEmptyLines() {
String text = "\r\n\n\r";
LineMap map = new LineMap(text);
@ -100,7 +100,7 @@ public class LineMapTest {
}
@Test
public void mixedEmptyAndNonEmptyLines() {
void mixedEmptyAndNonEmptyLines() {
String text = "line 1\nline 2\r\r\nline 4\n\n";
LineMap map = new LineMap(text);
@ -115,7 +115,7 @@ public class LineMapTest {
}
@Test
public void emptySubstrings() {
void emptySubstrings() {
String text = "single line\n";
LineMap map = new LineMap(text);
@ -128,7 +128,7 @@ public class LineMapTest {
}
@Test
public void substringIsJustLineSeparator() {
void substringIsJustLineSeparator() {
String separator = "\n";
String text = "line 1" + separator + "line 2";
LineMap map = new LineMap(text);

View File

@ -1,8 +1,8 @@
package it.cavallium.warppi.math.rules.dsl.frontend;
import it.cavallium.warppi.math.rules.dsl.DslError;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.util.ArrayList;
import java.util.Arrays;
@ -10,18 +10,18 @@ import java.util.Collections;
import java.util.List;
import static it.cavallium.warppi.math.rules.dsl.frontend.TokenType.*;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
public class LexerTest {
class LexerTest {
private final List<DslError> errors = new ArrayList<>();
@Before
public void setUp() {
@BeforeEach
void setUp() {
errors.clear();
}
@Test
public void emptyInput() {
void emptyInput() {
final Lexer lexer = new Lexer("", errors::add);
final List<Token> expected = Collections.singletonList(
new Token(EOF, "", 0)
@ -30,7 +30,7 @@ public class LexerTest {
}
@Test
public void validRule() {
void validRule() {
final Lexer lexer = new Lexer(
"reduction TestRule_123:\n" +
" x + y * z = -(a_123 +- 3 / 2.2) -> [\n" +
@ -85,7 +85,7 @@ public class LexerTest {
}
@Test
public void incompleteNumberOtherChar() {
void incompleteNumberOtherChar() {
final Lexer lexer = new Lexer("2. 5 + 3", errors::add);
final List<Token> expectedTokens = Arrays.asList(
@ -103,7 +103,7 @@ public class LexerTest {
}
@Test
public void incompleteNumberEof() {
void incompleteNumberEof() {
final Lexer lexer = new Lexer("2.", errors::add);
final List<Token> expectedTokens = Collections.singletonList(
@ -118,7 +118,7 @@ public class LexerTest {
}
@Test
public void unexpectedCharacters() {
void unexpectedCharacters() {
final Lexer lexer = new Lexer("reduction @| .: {}", errors::add);
final List<Token> expectedTokens = Arrays.asList(
@ -137,7 +137,7 @@ public class LexerTest {
}
@Test
public void unterminatedComment() {
void unterminatedComment() {
final Lexer lexer = new Lexer("reduction /* test:\n x -> x", errors::add);
final List<Token> expectedTokens = Arrays.asList(
@ -153,7 +153,7 @@ public class LexerTest {
}
@Test
public void errorOrder() {
void errorOrder() {
final Lexer lexer = new Lexer(".2. @", errors::add);
final List<Token> expectedTokens = Collections.singletonList(

View File

@ -7,8 +7,8 @@ import it.cavallium.warppi.math.rules.dsl.Pattern;
import it.cavallium.warppi.math.rules.dsl.PatternRule;
import it.cavallium.warppi.math.rules.dsl.patterns.*;
import org.apache.commons.lang3.ObjectUtils;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import java.util.ArrayList;
@ -19,18 +19,18 @@ import java.util.stream.Collectors;
import java.util.stream.Stream;
import static it.cavallium.warppi.math.rules.dsl.frontend.TokenType.*;
import static org.junit.Assert.*;
import static org.junit.jupiter.api.Assertions.*;
public class ParserTest {
class ParserTest {
private final List<DslError> errors = new ArrayList<>();
@Before
public void setUp() {
@BeforeEach
void setUp() {
errors.clear();
}
@Test
public void noRules() {
void noRules() {
final List<Token> tokens = Collections.singletonList(
new Token(EOF, "", 0)
);
@ -39,7 +39,7 @@ public class ParserTest {
}
@Test
public void validRuleMultipleReplacements() {
void validRuleMultipleReplacements() {
final List<Token> tokens = Arrays.asList(
new Token(REDUCTION, "reduction", 0),
new Token(IDENTIFIER, "TestRule_123", 10),
@ -127,7 +127,7 @@ public class ParserTest {
}
@Test
public void validRuleNoReplacements() {
void validRuleNoReplacements() {
final List<Token> tokens = Arrays.asList(
new Token(EXISTENCE, "existence", 0),
new Token(IDENTIFIER, "test", 0),
@ -154,7 +154,7 @@ public class ParserTest {
}
@Test
public void validRuleOneReplacement() {
void validRuleOneReplacement() {
final List<Token> tokens = Arrays.asList(
new Token(REDUCTION, "reduction", 0),
new Token(IDENTIFIER, "test", 0),
@ -188,7 +188,7 @@ public class ParserTest {
}
@Test
public void validRuleOneReplacementBrackets() {
void validRuleOneReplacementBrackets() {
final List<Token> tokens = Arrays.asList(
new Token(REDUCTION, "reduction", 0),
new Token(IDENTIFIER, "test", 0),
@ -232,7 +232,7 @@ public class ParserTest {
}
@Test
public void multipleValidRules() {
void multipleValidRules() {
final List<Token> tokens = Arrays.asList(
new Token(REDUCTION, "reduction", 0),
new Token(IDENTIFIER, "test1", 0),
@ -291,7 +291,7 @@ public class ParserTest {
}
@Test
public void subFunctionIdentifiers() {
void subFunctionIdentifiers() {
final List<ReferenceEqualityToken> rule0x = new ArrayList<>();
final List<ReferenceEqualityToken> rule1x = new ArrayList<>();
final List<ReferenceEqualityToken> rule1y = new ArrayList<>();
@ -400,8 +400,8 @@ public class ParserTest {
// The EOF token is inserted by the lexer, therefore it can only be missing
// in case of programming errors, and not directly because of user input.
@Test(expected = RuntimeException.class)
public void missingEof() {
@Test
void missingEof() {
final List<Token> tokens = Arrays.asList(
new Token(EXISTENCE, "existence", 0),
new Token(IDENTIFIER, "test", 0),
@ -414,11 +414,11 @@ public class ParserTest {
new Token(RIGHT_BRACKET, "]", 0)
);
final Parser parser = new Parser(tokens, errors::add);
parser.parse();
assertThrows(RuntimeException.class, parser::parse);
}
@Test
public void incompleteRule() {
void incompleteRule() {
final List<Token> tokens = Arrays.asList(
new Token(EXISTENCE, "existence", 0),
new Token(IDENTIFIER, "test", 0),
@ -440,7 +440,7 @@ public class ParserTest {
}
@Test
public void missingRuleType() {
void missingRuleType() {
final List<Token> tokens = Arrays.asList(
new Token(IDENTIFIER, "test", 0),
new Token(EOF, "", 0)
@ -457,7 +457,7 @@ public class ParserTest {
}
@Test
public void unexpectedTokenPrimary() {
void unexpectedTokenPrimary() {
final List<Token> tokens = Arrays.asList(
new Token(EXISTENCE, "existence", 0),
new Token(IDENTIFIER, "test", 0),
@ -481,7 +481,7 @@ public class ParserTest {
}
@Test
public void missingRuleName() {
void missingRuleName() {
final List<Token> tokens = Arrays.asList(
new Token(REDUCTION, "reduction", 0),
new Token(COLON, ":", 0),
@ -499,7 +499,7 @@ public class ParserTest {
}
@Test
public void missingColon() {
void missingColon() {
final List<Token> tokens = Arrays.asList(
new Token(REDUCTION, "reduction", 0),
new Token(IDENTIFIER, "test", 0),
@ -520,7 +520,7 @@ public class ParserTest {
}
@Test
public void missingArrow() {
void missingArrow() {
final List<Token> tokens = Arrays.asList(
new Token(REDUCTION, "reduction", 0),
new Token(IDENTIFIER, "test", 0),
@ -541,7 +541,7 @@ public class ParserTest {
}
@Test
public void missingRightBracket() {
void missingRightBracket() {
final List<Token> tokens = Arrays.asList(
new Token(REDUCTION, "reduction", 0),
new Token(IDENTIFIER, "test", 0),
@ -564,7 +564,7 @@ public class ParserTest {
}
@Test
public void missingOneArgFunctionLeftParen() {
void missingOneArgFunctionLeftParen() {
final List<Token> tokens = Arrays.asList(
new Token(EXISTENCE, "existence", 0),
new Token(IDENTIFIER, "test", 0),
@ -589,7 +589,7 @@ public class ParserTest {
}
@Test
public void missingOneArgFunctionRightParen() {
void missingOneArgFunctionRightParen() {
final List<Token> tokens = Arrays.asList(
new Token(EXISTENCE, "existence", 0),
new Token(IDENTIFIER, "test", 0),
@ -614,7 +614,7 @@ public class ParserTest {
}
@Test
public void missingTwoArgFunctionLeftParen() {
void missingTwoArgFunctionLeftParen() {
final List<Token> tokens = Arrays.asList(
new Token(EXISTENCE, "existence", 0),
new Token(IDENTIFIER, "test", 0),
@ -641,7 +641,7 @@ public class ParserTest {
}
@Test
public void missingTwoArgFunctionComma() {
void missingTwoArgFunctionComma() {
final List<Token> tokens = Arrays.asList(
new Token(EXISTENCE, "existence", 0),
new Token(IDENTIFIER, "test", 0),
@ -668,7 +668,7 @@ public class ParserTest {
}
@Test
public void missingTwoArgFunctionRightParen() {
void missingTwoArgFunctionRightParen() {
final List<Token> tokens = Arrays.asList(
new Token(EXISTENCE, "existence", 0),
new Token(IDENTIFIER, "test", 0),
@ -695,7 +695,7 @@ public class ParserTest {
}
@Test
public void missingExpressionRightParen() {
void missingExpressionRightParen() {
final List<Token> tokens = Arrays.asList(
new Token(EXISTENCE, "existence", 0),
new Token(IDENTIFIER, "test", 0),
@ -719,7 +719,7 @@ public class ParserTest {
}
@Test
public void recoveryToNextRule() {
void recoveryToNextRule() {
final List<Token> tokens = Arrays.asList(
new Token(REDUCTION, "reduction", 0),
new Token(IDENTIFIER, "test1", 0),

View File

@ -92,9 +92,9 @@
<dependencyManagement>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.5.1</version>
<scope>test</scope>
</dependency>
<dependency>