Replace try-fail-catch-assert with assertThrows

This commit is contained in:
Kenzie Togami 2019-07-15 16:19:28 -07:00
parent 3b157b67c3
commit 5c5c822f4b
No known key found for this signature in database
GPG Key ID: 5D200B325E157A81

View File

@ -32,9 +32,10 @@ import org.mockito.Mockito;
import static java.lang.Math.atan2; import static java.lang.Math.atan2;
import static java.lang.Math.sin; import static java.lang.Math.sin;
import static org.junit.jupiter.api.Assertions.assertAll;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public class ExpressionTest { public class ExpressionTest {
@BeforeEach @BeforeEach
@ -67,56 +68,46 @@ public class ExpressionTest {
} }
@Test @Test
public void testErrors() throws ExpressionException { public void testErrors() {
assertAll(
// test lexer errors // test lexer errors
try { () -> {
compile("#"); LexerException e = assertThrows(LexerException.class,
fail("Error expected"); () -> compile("#"));
} catch (LexerException e) {
assertEquals(0, e.getPosition(), "Error position"); assertEquals(0, e.getPosition(), "Error position");
} },
// test parser errors // test parser errors
try { () -> {
compile("x"); ParserException e = assertThrows(ParserException.class,
fail("Error expected"); () -> compile("x"));
} catch (ParserException e) {
assertEquals(0, e.getPosition(), "Error position"); assertEquals(0, e.getPosition(), "Error position");
} },
try { () -> {
compile("x()"); ParserException e = assertThrows(ParserException.class,
fail("Error expected"); () -> compile("x()"));
} catch (ParserException e) {
assertEquals(0, e.getPosition(), "Error position"); assertEquals(0, e.getPosition(), "Error position");
} },
try { () -> assertThrows(ParserException.class,
compile("("); () -> compile("(")),
fail("Error expected"); () -> assertThrows(ParserException.class,
} catch (ParserException ignored) {} () -> compile("x(")),
try {
compile("x(");
fail("Error expected");
} catch (ParserException ignored) {}
// test overloader errors // test overloader errors
try { () -> {
compile("atan2(1)"); ParserException e = assertThrows(ParserException.class,
fail("Error expected"); () -> compile("atan2(1)"));
} catch (ParserException e) { assertEquals(0, e.getPosition(), "Error position");
assertEquals(0, e.getPosition(), "Error position"); },
} () -> {
try { ParserException e = assertThrows(ParserException.class,
compile("atan2(1, 2, 3)"); () -> compile("atan2(1, 2, 3)"));
fail("Error expected"); assertEquals(0, e.getPosition(), "Error position");
} catch (ParserException e) { },
assertEquals(0, e.getPosition(), "Error position"); () -> {
} ParserException e = assertThrows(ParserException.class,
try { () -> compile("rotate(1, 2, 3)"));
compile("rotate(1, 2, 3)");
fail("Error expected");
} catch (ParserException e) {
assertEquals(0, e.getPosition(), "Error position"); assertEquals(0, e.getPosition(), "Error position");
} }
);
} }
@Test @Test
@ -180,14 +171,12 @@ public class ExpressionTest {
} }
@Test @Test
public void testTimeout() throws Exception { public void testTimeout() {
try { EvaluationException e = assertThrows(EvaluationException.class,
simpleEval("for(i=0;i<256;i++){for(j=0;j<256;j++){for(k=0;k<256;k++){for(l=0;l<256;l++){ln(pi)}}}}"); () -> simpleEval("for(i=0;i<256;i++){for(j=0;j<256;j++){for(k=0;k<256;k++){for(l=0;l<256;l++){ln(pi)}}}}"),
fail("Loop was not stopped."); "Loop was not stopped.");
} catch (EvaluationException e) {
assertTrue(e.getMessage().contains("Calculations exceeded time limit")); assertTrue(e.getMessage().contains("Calculations exceeded time limit"));
} }
}
private double simpleEval(String expressionString) throws ExpressionException { private double simpleEval(String expressionString) throws ExpressionException {
final Expression expression = compile(expressionString); final Expression expression = compile(expressionString);