Skip to content

Commit

Permalink
Improve fromString tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
jmp committed Mar 18, 2018
1 parent 40734f5 commit 93ffeaa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/Card.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public Card(int rank, int suit) {
* @return a new {@link Card} instance corresponding to the given string.
*/
public static Card fromString(String string) {
if (string.length() != 2) {
throw new IllegalArgumentException("Card string length must be exactly 2.");
if (string == null || string.length() != 2) {
throw new IllegalArgumentException("Card string must be non-null with length of exactly 2.");
}

final int rank = RANKS.indexOf(string.charAt(0));
Expand Down
26 changes: 18 additions & 8 deletions tests/CardTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,30 @@ void testFromString() {
}

@Test
void testTooShortFromString() {
assertThrows(IllegalArgumentException.class, () -> Card.fromString("K"));
void testFromStringInvalid() {
assertThrows(IllegalArgumentException.class, () -> Card.fromString("Kx"));
assertThrows(IllegalArgumentException.class, () -> Card.fromString("Xd"));
assertThrows(IllegalArgumentException.class, () -> Card.fromString("Xx"));
}

@Test
void testTooLongFromString() {
assertThrows(IllegalArgumentException.class, () -> Card.fromString("Kd Qs"));
void testFromStringNull() {
assertThrows(IllegalArgumentException.class, () -> Card.fromString(null));
}

@Test
void testInvalidFromString() {
assertThrows(IllegalArgumentException.class, () -> Card.fromString("Kx"));
assertThrows(IllegalArgumentException.class, () -> Card.fromString("Xd"));
assertThrows(IllegalArgumentException.class, () -> Card.fromString("Xx"));
void testFromStringEmpty() {
assertThrows(IllegalArgumentException.class, () -> Card.fromString(""));
}

@Test
void testFromStringTooShort() {
assertThrows(IllegalArgumentException.class, () -> Card.fromString("K"));
}

@Test
void testFromStringTooLong() {
assertThrows(IllegalArgumentException.class, () -> Card.fromString("Kd Qs"));
}

@Test
Expand Down

0 comments on commit 93ffeaa

Please sign in to comment.