Permalink
Browse files

Implemented what arguments weren't valid for a WinBy game

Can't play a game to anything less than 1
Can't win by a negative number or a number larger than what is
being played to
  • Loading branch information...
1 parent a4412a8 commit b91468a3db878c5906fbd656ba28e7a6be322b16 @youngnh committed Jan 17, 2010
Showing with 48 additions and 0 deletions.
  1. +4 −0 src/com/twoguys/rps/WinBy.java
  2. +44 −0 test/com/twoguys/rps/WinByTest.java
@@ -8,6 +8,10 @@
private int by;
public WinBy(int to, int by) {
+ boolean valid = (to > 0 && by >= 0 && to >= by);
+ if(!valid) {
+ throw new IllegalArgumentException("Must be to a number larger than 0 by a postive number less than that");
+ }
this.to = to;
this.by = by;
}
@@ -3,6 +3,7 @@
import static org.junit.Assert.*;
import com.twoguys.util.*;
+import java.util.*;
import org.junit.*;
public class WinByTest {
@@ -120,4 +121,47 @@ public void testPlayer1WinsWhenTiedAndNoMargin() {
assertEquals(player1, winner.value());
}
+ @Test(expected=IllegalArgumentException.class)
+ public void testTo0ThrowsIllegalArgumentException() {
+ new WinBy(0, 0);
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testToANegativeNumberThrowsIllegalArgumentException() {
+ Random random = new Random();
+ int num = random.nextInt();
+ while(num == 0) {
+ num = random.nextInt();
+ }
+ if(num > 0) {
+ num *= -1;
+ }
+ new WinBy(num, 2);
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testByANegativeNumberThrowsIllegalArgumentException() {
+ Random random = new Random();
+ int num = random.nextInt();
+ while(num == 0) {
+ num = random.nextInt();
+ }
+ if(num > 0) {
+ num *= -1;
+ }
+ new WinBy(5, num);
+ }
+
+ @Test(expected=IllegalArgumentException.class)
+ public void testByANumberGreaterThanToThrowsIllegalArgumentException() {
+ Random random = new Random();
+ int num = random.nextInt();
+ while(num == 0) {
+ num = random.nextInt();
+ }
+ if(num < 0) {
+ num *= -1;
+ }
+ new WinBy(num, num+2);
+ }
}

0 comments on commit b91468a

Please sign in to comment.