Skip to content

Commit

Permalink
Replace assertThatCode() with assertThatThrownBy()
Browse files Browse the repository at this point in the history
assertThatCode() does not fail if no exception was thrown
  • Loading branch information
FourteenBrush committed Jun 10, 2024
1 parent 6f59590 commit 1eb57c5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.*;

// TODO
public class ExecutionEnvTest {
Expand All @@ -18,14 +18,24 @@ void setUp() {

@Test
void ensureNonExistentFunctionsDontExist() {
String[] nonExistentFunctions = new String[] {
String[] nonExistentFunctions = new String[]{
"some_weird_function",
"some_other_function",
"some_other_other_function",
};

for (String function : nonExistentFunctions) {
assertThatCode(() -> ExpressionParser.parse(function + "()", env)).isInstanceOf(SyntaxException.class);
assertThatThrownBy(() -> ExpressionParser.parse(function + "()", env)).isInstanceOf(SyntaxException.class);
}
}

@Test
void ensureThrowingOnAlreadyExistentSymbolKeepsStateConsistent() {
env.insertFunction("a", () -> 1);

assertThatThrownBy(() -> env.insertFunction("a", () -> 3))
.isInstanceOf(SyntaxException.class);

assertThat(ExpressionParser.parse("a()", env)).isEqualTo(1);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

class FunctionContextTest {
private FunctionContext ctx;
Expand All @@ -20,23 +20,23 @@ void testEmptyContext() {
assertThat(ctx.isEmpty()).isTrue();
assertThat(ctx.size()).isZero();

assertThatCode(() -> ctx.getDouble(0)).isInstanceOf(IndexOutOfBoundsException.class);
assertThatCode(() -> ctx.getDouble(-1)).isInstanceOf(IndexOutOfBoundsException.class);
assertThatCode(() -> ctx.getInt(0)).isInstanceOf(IndexOutOfBoundsException.class);
assertThatCode(() -> ctx.getInt(-3)).isInstanceOf(IndexOutOfBoundsException.class);
assertThatThrownBy(() -> ctx.getDouble(0)).isInstanceOf(IndexOutOfBoundsException.class);
assertThatThrownBy(() -> ctx.getDouble(-1)).isInstanceOf(IndexOutOfBoundsException.class);
assertThatThrownBy(() -> ctx.getInt(0)).isInstanceOf(IndexOutOfBoundsException.class);
assertThatThrownBy(() -> ctx.getInt(-3)).isInstanceOf(IndexOutOfBoundsException.class);
}

@Test
void testBoundsAndConversions() {
ctx.add(2.3);
assertThat(ctx.getDouble(0)).isEqualTo(2.3);
assertThatCode(() -> ctx.getDouble(-1)).isInstanceOf(IndexOutOfBoundsException.class);
assertThatThrownBy(() -> ctx.getDouble(-1)).isInstanceOf(IndexOutOfBoundsException.class);

// conversions
assertThatCode(() -> ctx.getInt(0)).isInstanceOf(SyntaxException.class);
assertThatCode(() -> ctx.getBoundedDouble(0, -10, 2)).isInstanceOf(SyntaxException.class);
assertThatThrownBy(() -> ctx.getInt(0)).isInstanceOf(SyntaxException.class);
assertThatThrownBy(() -> ctx.getBoundedDouble(0, -10, 2)).isInstanceOf(SyntaxException.class);

assertThatCode(() -> ctx.getBoundedDouble(0, 10, 2))
assertThatThrownBy(() -> ctx.getBoundedDouble(0, 10, 2))
.isInstanceOf(IllegalArgumentException.class)
.hasMessageContaining("set up incorrectly");
}
Expand Down

0 comments on commit 1eb57c5

Please sign in to comment.