Skip to content

Commit 15e6a4e

Browse files
committed
Remove loops in InputValidationTest by using @ParameterizedTest
1 parent f698c77 commit 15e6a4e

1 file changed

Lines changed: 26 additions & 21 deletions

File tree

core/src/test/java/com/team2813/lib2813/util/InputValidationTest.java

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -19,35 +19,40 @@
1919
import static com.google.common.truth.Truth.assertWithMessage;
2020
import static org.junit.Assert.assertThrows;
2121

22+
import java.util.stream.IntStream;
2223
import org.junit.jupiter.api.Nested;
23-
import org.junit.jupiter.api.Test;
24+
import org.junit.jupiter.params.ParameterizedTest;
25+
import org.junit.jupiter.params.provider.MethodSource;
26+
import org.junit.jupiter.params.provider.ValueSource;
2427

28+
/** Tests for {@link InputValidation}. */
2529
public class InputValidationTest {
2630

27-
// Tests for the `InputValidation.checkCanId(...)` method.
31+
/** Tests for the `InputValidation.checkCanId(...)` method. */
2832
@Nested
2933
public class CheckCanIdTest {
30-
@Test
31-
public void invalidCanId() {
32-
// Can IDs can only valid in the range [0, 62].
33-
int[] invalidCanIds = {-50, -1, 63, 100};
34-
for (int invalidCanId : invalidCanIds) {
35-
InvalidCanIdException exception =
36-
assertThrows(
37-
InvalidCanIdException.class, () -> InputValidation.checkCanId(invalidCanId));
38-
assertThat(exception.getCanId()).isEqualTo(invalidCanId);
39-
assertThat(exception).hasMessageThat().contains("is not a valid can id");
40-
}
34+
@ParameterizedTest
35+
@ValueSource(ints = {-50, -1, 63, 100}) // Can IDs can only valid in the range [0, 62]
36+
public void invalidCanId(int invalidCanId) {
37+
InvalidCanIdException exception =
38+
assertThrows(InvalidCanIdException.class, () -> InputValidation.checkCanId(invalidCanId));
39+
40+
assertThat(exception.getCanId()).isEqualTo(invalidCanId);
41+
assertThat(exception).hasMessageThat().contains("is not a valid can id");
4142
}
4243

43-
@Test
44-
public void validCanID() {
45-
// Can IDs can only valid in the range [0, 62].
46-
int[] validCanIds = {0, 1, 10, 62};
47-
for (int validCanId : validCanIds) {
48-
int returnValue = InputValidation.checkCanId(validCanId);
49-
assertWithMessage("Expected a valid CAN ID").that(returnValue).isEqualTo(validCanId);
50-
}
44+
@ParameterizedTest
45+
@MethodSource("validCanIds")
46+
public void checkCanId_returnsInputForValidCanId(int validCanId) {
47+
int returnValue = InputValidation.checkCanId(validCanId);
48+
49+
assertWithMessage("Expected to return the passed-in value")
50+
.that(returnValue)
51+
.isEqualTo(validCanId);
52+
}
53+
54+
private static IntStream validCanIds() {
55+
return IntStream.range(0, 63);
5156
}
5257
}
5358
}

0 commit comments

Comments
 (0)