|
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
4 | 4 |
|
5 |
| -import java.util.HashMap; |
6 |
| -import org.junit.jupiter.api.Test; |
| 5 | +import java.util.stream.Stream; |
| 6 | +import org.junit.jupiter.params.ParameterizedTest; |
| 7 | +import org.junit.jupiter.params.provider.Arguments; |
| 8 | +import org.junit.jupiter.params.provider.MethodSource; |
7 | 9 |
|
8 | 10 | class CountWordsTest {
|
9 |
| - @Test |
10 |
| - public void testWordCount() { |
11 |
| - HashMap<String, Integer> testCases = new HashMap<>(); |
12 |
| - testCases.put("", 0); |
13 |
| - testCases.put(null, 0); |
14 |
| - testCases.put("aaaa bbb cccc", 3); |
15 |
| - testCases.put("note extra spaces here", 4); |
16 |
| - testCases.put(" a b c d e ", 5); |
17 | 11 |
|
18 |
| - for (final var tc : testCases.entrySet()) { |
19 |
| - assertEquals(CountWords.wordCount(tc.getKey()), tc.getValue()); |
20 |
| - } |
| 12 | + @ParameterizedTest |
| 13 | + @MethodSource("wordCountTestCases") |
| 14 | + void testWordCount(String input, int expectedCount) { |
| 15 | + assertEquals(expectedCount, CountWords.wordCount(input)); |
21 | 16 | }
|
22 | 17 |
|
23 |
| - @Test |
24 |
| - public void testSecondaryWordCount() { |
25 |
| - HashMap<String, Integer> testCases = new HashMap<>(); |
26 |
| - testCases.put("", 0); |
27 |
| - testCases.put(null, 0); |
28 |
| - testCases.put("aaaa bbb cccc", 3); |
29 |
| - testCases.put("this-is-one-word!", 1); |
30 |
| - testCases.put("What, about, this? Hmmm----strange", 4); |
31 |
| - testCases.put("word1 word-2 word-3- w?o,r.d.@!@#$&*()<>4", 4); |
| 18 | + @ParameterizedTest |
| 19 | + @MethodSource("secondaryWordCountTestCases") |
| 20 | + void testSecondaryWordCount(String input, int expectedCount) { |
| 21 | + assertEquals(expectedCount, CountWords.secondaryWordCount(input)); |
| 22 | + } |
| 23 | + |
| 24 | + private static Stream<Arguments> wordCountTestCases() { |
| 25 | + return Stream.of(Arguments.of("", 0), Arguments.of(null, 0), Arguments.of("aaaa bbb cccc", 3), Arguments.of("note extra spaces here", 4), Arguments.of(" a b c d e ", 5)); |
| 26 | + } |
32 | 27 |
|
33 |
| - for (final var tc : testCases.entrySet()) { |
34 |
| - assertEquals(CountWords.secondaryWordCount(tc.getKey()), tc.getValue()); |
35 |
| - } |
| 28 | + private static Stream<Arguments> secondaryWordCountTestCases() { |
| 29 | + return Stream.of(Arguments.of("", 0), Arguments.of(null, 0), Arguments.of("aaaa bbb cccc", 3), Arguments.of("this-is-one-word!", 1), Arguments.of("What, about, this? Hmmm----strange", 4), Arguments.of("word1 word-2 word-3- w?o,r.d.@!@#$&*()<>4", 4)); |
36 | 30 | }
|
37 | 31 | }
|
0 commit comments