|
1 | 1 | package com.thealgorithms.strings; |
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.params.provider.Arguments.arguments; |
4 | 5 |
|
| 6 | +import java.util.stream.Stream; |
| 7 | +import org.junit.jupiter.api.DisplayName; |
| 8 | +import org.junit.jupiter.api.Test; |
5 | 9 | import org.junit.jupiter.params.ParameterizedTest; |
6 | | -import org.junit.jupiter.params.provider.CsvSource; |
| 10 | +import org.junit.jupiter.params.provider.Arguments; |
| 11 | +import org.junit.jupiter.params.provider.MethodSource; |
7 | 12 |
|
8 | | -public class AlphabeticalTest { |
| 13 | +@DisplayName("Alphabetical.isAlphabetical()") |
| 14 | +class AlphabeticalTest { |
9 | 15 |
|
10 | | - @ParameterizedTest(name = "\"{0}\" → Expected: {1}") |
11 | | - @CsvSource({"'abcdefghijklmno', true", "'abcdxxxyzzzz', true", "'123a', false", "'abcABC', false", "'abcdefghikjlmno', false", "'aBC', true", "'abc', true", "'xyzabc', false", "'abcxyz', true", "'', false", "'1', false"}) |
12 | | - void testIsAlphabetical(String input, boolean expected) { |
13 | | - assertEquals(expected, Alphabetical.isAlphabetical(input)); |
| 16 | + static Stream<Arguments> testCases() { |
| 17 | + // Workaround for SpotBugs false positive (NAB_NEEDLESS_BOOLEAN_CONSTANT_CONVERSION) |
| 18 | + // due to JUnit Arguments.of(Object...) auto-boxing |
| 19 | + return Stream.of(arguments("", Boolean.FALSE, "Should return false for empty string"), arguments(" ", Boolean.FALSE, "Should return false for blank string"), arguments("a1b2", Boolean.FALSE, "Should return false when string contains numbers"), |
| 20 | + arguments("abc!DEF", Boolean.FALSE, "Should return false when string contains symbols"), arguments("#abc", Boolean.FALSE, "Should return false when first character is not a letter"), arguments("abc", Boolean.TRUE, "Should return true for non-decreasing order"), |
| 21 | + arguments("aBcD", Boolean.TRUE, "Should return true for mixed case increasing sequence"), arguments("a", Boolean.TRUE, "Should return true for single letter"), arguments("'", Boolean.FALSE, "Should return false for single symbol"), |
| 22 | + arguments("aabbcc", Boolean.TRUE, "Should return true for repeated letters"), arguments("cba", Boolean.FALSE, "Should return false when order decreases"), arguments("abzba", Boolean.FALSE, "Should return false when middle breaks order")); |
| 23 | + } |
| 24 | + |
| 25 | + private void assertAlphabetical(String input, boolean expected, String message) { |
| 26 | + // Arrange & Act |
| 27 | + boolean result = Alphabetical.isAlphabetical(input); |
| 28 | + |
| 29 | + // Assert |
| 30 | + assertEquals(expected, result, message); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + @DisplayName("Should return false for null input") |
| 35 | + void nullInputTest() { |
| 36 | + assertAlphabetical(null, false, "Should return false for null input"); |
| 37 | + } |
| 38 | + |
| 39 | + @ParameterizedTest(name = "{2}") |
| 40 | + @MethodSource("testCases") |
| 41 | + @DisplayName("Alphabetical cases") |
| 42 | + void isAlphabeticalTest(String input, boolean expected, String message) { |
| 43 | + assertAlphabetical(input, expected, message); |
14 | 44 | } |
15 | 45 | } |
0 commit comments