5
5
6
6
import java .util .Arrays ;
7
7
import java .util .List ;
8
+ import java .util .stream .Stream ;
8
9
import org .junit .jupiter .api .BeforeEach ;
9
- import org .junit .jupiter .api . Test ;
10
-
11
- public class WordBoggleTest {
10
+ import org .junit .jupiter .params . ParameterizedTest ;
11
+ import org . junit . jupiter . params . provider . Arguments ;
12
+ import org . junit . jupiter . params . provider . MethodSource ;
12
13
14
+ class WordBoggleTest {
13
15
private char [][] board ;
14
- private String [] words ;
15
16
16
17
@ BeforeEach
17
- public void setup () {
18
+ void setup () {
18
19
board = new char [][] {
19
20
{'t' , 'h' , 'i' , 's' , 'i' , 's' , 'a' },
20
21
{'s' , 'i' , 'm' , 'p' , 'l' , 'e' , 'x' },
@@ -26,58 +27,30 @@ public void setup() {
26
27
{'N' , 'O' , 'T' , 'R' , 'E' , '_' , 'P' },
27
28
{'x' , 'x' , 'D' , 'E' , 'T' , 'A' , 'E' },
28
29
};
29
-
30
- words = new String [] {"this" , "is" , "not" , "a" , "simple" , "test" , "boggle" , "board" , "REPEATED" , "NOTRE_PEATED" };
31
30
}
32
31
33
- @ Test
34
- public void testBoggleBoardFindsAllWords () {
35
- List <String > expected = Arrays . asList ( "this" , "is" , "a" , "simple" , "board" , "boggle" , "NOTRE_PEATED" );
32
+ @ ParameterizedTest
33
+ @ MethodSource ( "provideTestCases" )
34
+ void testBoggleBoard ( String [] words , List <String > expectedWords , String testDescription ) {
36
35
List <String > result = WordBoggle .boggleBoard (board , words );
37
- assertEquals (expected .size (), result .size ());
38
- assertTrue (expected .containsAll (result ));
39
- }
40
-
41
- @ Test
42
- public void testBoggleBoardNoMatchingWords () {
43
- // Test with words that don't exist on the board
44
- String [] nonMatchingWords = {"xyz" , "hello" , "world" };
45
- List <String > result = WordBoggle .boggleBoard (board , nonMatchingWords );
46
- assertEquals (0 , result .size ());
47
- }
48
-
49
- @ Test
50
- public void testBoggleBoardEmptyBoard () {
51
- // Test with an empty board
52
- char [][] emptyBoard = new char [0 ][0 ];
53
- List <String > result = WordBoggle .boggleBoard (emptyBoard , words );
54
- assertEquals (0 , result .size ());
36
+ assertEquals (expectedWords .size (), result .size (), "Test failed for: " + testDescription );
37
+ assertTrue (expectedWords .containsAll (result ), "Test failed for: " + testDescription );
55
38
}
56
39
57
- @ Test
58
- public void testBoggleBoardEmptyWordsArray () {
59
- // Test with an empty words array
60
- String [] emptyWords = {};
61
- List <String > result = WordBoggle .boggleBoard (board , emptyWords );
62
- assertEquals (0 , result .size ());
40
+ private static Stream <Arguments > provideTestCases () {
41
+ return Stream .of (Arguments .of (new String [] {"this" , "is" , "not" , "a" , "simple" , "test" , "boggle" , "board" , "REPEATED" , "NOTRE_PEATED" }, Arrays .asList ("this" , "is" , "a" , "simple" , "board" , "boggle" , "NOTRE_PEATED" ), "All words" ),
42
+ Arguments .of (new String [] {"xyz" , "hello" , "world" }, List .of (), "No matching words" ), Arguments .of (new String [] {}, List .of (), "Empty words array" ), Arguments .of (new String [] {"this" , "this" , "board" , "board" }, Arrays .asList ("this" , "board" ), "Duplicate words in input" ));
63
43
}
64
44
65
- @ Test
66
- public void testBoggleBoardSingleCharacterWords () {
67
- // Test with single-character words
68
- String [] singleCharWords = {"a" , "x" , "o" };
69
- List <String > expected = Arrays .asList ("a" , "o" );
70
- List <String > result = WordBoggle .boggleBoard (board , singleCharWords );
71
- assertEquals (expected .size () + 1 , result .size ());
45
+ @ ParameterizedTest
46
+ @ MethodSource ("provideSpecialCases" )
47
+ void testBoggleBoardSpecialCases (char [][] specialBoard , String [] words , List <String > expectedWords , String testDescription ) {
48
+ List <String > result = WordBoggle .boggleBoard (specialBoard , words );
49
+ assertEquals (expectedWords .size (), result .size (), "Test failed for: " + testDescription );
50
+ assertTrue (expectedWords .containsAll (result ), "Test failed for: " + testDescription );
72
51
}
73
52
74
- @ Test
75
- public void testBoggleBoardDuplicateWordsInInput () {
76
- // Test with duplicate words in the input array
77
- String [] duplicateWords = {"this" , "this" , "board" , "board" };
78
- List <String > expected = Arrays .asList ("this" , "board" );
79
- List <String > result = WordBoggle .boggleBoard (board , duplicateWords );
80
- assertEquals (expected .size (), result .size ());
81
- assertTrue (expected .containsAll (result ));
53
+ private static Stream <Arguments > provideSpecialCases () {
54
+ return Stream .of (Arguments .of (new char [0 ][0 ], new String [] {"this" , "is" , "a" , "test" }, List .of (), "Empty board" ));
82
55
}
83
56
}
0 commit comments