|
19 | 19 | import static com.google.common.truth.Truth.assertWithMessage; |
20 | 20 | import static org.junit.Assert.assertThrows; |
21 | 21 |
|
| 22 | +import java.util.stream.IntStream; |
22 | 23 | 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; |
24 | 27 |
|
| 28 | +/** Tests for {@link InputValidation}. */ |
25 | 29 | public class InputValidationTest { |
26 | 30 |
|
27 | | - // Tests for the `InputValidation.checkCanId(...)` method. |
| 31 | + /** Tests for the `InputValidation.checkCanId(...)` method. */ |
28 | 32 | @Nested |
29 | 33 | 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"); |
41 | 42 | } |
42 | 43 |
|
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); |
51 | 56 | } |
52 | 57 | } |
53 | 58 | } |
0 commit comments