Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

PromptFor, RockPaperScissorsTest accounts for trailing newline,

remove InteractivePlayer still hanging around
  • Loading branch information...
commit 7b8816c1f2025881be4387b9670000033a72d580 1 parent 726bbd1
Nate Young authored
66  src/com/twoguys/rps/InteractivePlayer.java
... ...
@@ -1,66 +0,0 @@
1  
-package com.twoguys.rps;
2  
-
3  
-import java.io.*;
4  
-
5  
-public class InteractivePlayer implements Player {
6  
-
7  
-    private String prompt;
8  
-    private LineNumberReader fromPlayer;
9  
-    private Writer toPlayer;
10  
-    private String name;
11  
-
12  
-    public InteractivePlayer(Reader fromPlayer, Writer toPlayer, int n) throws IOException {
13  
-	this.prompt = "Player " + n + " Name: ";
14  
-	this.toPlayer = toPlayer;
15  
-	this.fromPlayer = new LineNumberReader(fromPlayer);
16  
-	init();
17  
-    }
18  
-
19  
-    private void init() throws IOException {
20  
-	toPlayer.write(prompt);
21  
-	toPlayer.flush();
22  
-	name = fromPlayer.readLine();
23  
-    }
24  
-
25  
-    @Override
26  
-    public String getName() {
27  
-	return name;
28  
-    }
29  
-
30  
-    @Override
31  
-    public Throw getThrow() {
32  
-	Throw thrown = null;
33  
-	try {
34  
-	    toPlayer.write("[R]ock, [P]aper, or [S]cissors? ");
35  
-	    toPlayer.flush();
36  
-	    String input = fromPlayer.readLine();
37  
-	    if(input != null) {
38  
-		char choice = input.toUpperCase().charAt(0);
39  
-		switch(choice) {
40  
-		case 'R':
41  
-		    thrown = new Rock();
42  
-		    break;
43  
-		case 'P':
44  
-		    thrown = new Paper();
45  
-		    break;
46  
-		case 'S':
47  
-		    thrown = new Scissors();
48  
-		    break;
49  
-		}
50  
-	    }
51  
-	} catch(IOException e) {
52  
-	    // squelch
53  
-	}
54  
-	return thrown;
55  
-    }
56  
-
57  
-    @Override
58  
-    public void notifyWinner() {
59  
-	try {
60  
-	    toPlayer.write(getName() + " Wins!\n");
61  
-	    toPlayer.flush();
62  
-	} catch(IOException e) {
63  
-	    // squelch
64  
-	}
65  
-    }
66  
-}
11  src/com/twoguys/util/PromptFor.java
... ...
@@ -0,0 +1,11 @@
  1
+package com.twoguys.util;
  2
+
  3
+import java.io.*;
  4
+
  5
+public class PromptFor<T> extends PromptForFrom<T> {
  6
+
  7
+    public PromptFor(Print notify, Reader<T> reader) {
  8
+	super(new LineNumberReader(new InputStreamReader(System.in)), notify, reader);
  9
+    }
  10
+
  11
+}
5  test/RockPaperScissorsTest.java
@@ -65,6 +65,7 @@ public void testNoArgumentsResultsInSingleThrowGame() throws Exception {
65 65
 	rps.run(args);
66 66
 
67 67
 	String expected = IOUtils.toString(new FileInputStream("data/RockPaperScissorsTest/noargs_game.expected")).replace("\n", "");
  68
+	expected += "\n";
68 69
 
69 70
 	String actual = out.toString();
70 71
 	assertEquals(expected, actual);
@@ -81,6 +82,7 @@ public void testFirstToGameResultsInBothPlayersPlaying() throws Exception {
81 82
 	rps.run(args);
82 83
 
83 84
 	String expected = IOUtils.toString(new FileInputStream("data/RockPaperScissorsTest/firstto_game.expected")).replace("\n", "");
  85
+	expected += "\n";
84 86
 
85 87
 	String actual = out.toString();
86 88
 	assertEquals(expected, actual);
@@ -97,6 +99,7 @@ public void testBestOfStopsAfterPlayerWinsMoreThanHalf() throws Exception {
97 99
 	rps.run(args);
98 100
 
99 101
 	String expected = IOUtils.toString(new FileInputStream("data/RockPaperScissorsTest/bestof_game.expected")).replace("\n", "");
  102
+	expected += "\n";
100 103
 
101 104
 	String actual = out.toString();
102 105
 	assertEquals(expected, actual);
@@ -113,6 +116,7 @@ public void testWinByGameContinuesUntilOnePlayerBeatsOtherByMargin() throws Exce
113 116
 	rps.run(args);
114 117
 
115 118
 	String expected = IOUtils.toString(new FileInputStream("data/RockPaperScissorsTest/winby_game.expected")).replace("\n", "");
  119
+	expected += "\n";
116 120
 
117 121
 	String actual = out.toString();
118 122
 	assertEquals(expected, actual);
@@ -129,6 +133,7 @@ public void testNateWins() throws Exception {
129 133
 	rps.run(args);
130 134
 
131 135
 	String expected = IOUtils.toString(new FileInputStream("data/RockPaperScissorsTest/nate_wins.expected")).replace("\n", "");
  136
+	expected += "\n";
132 137
 
133 138
 	String actual = out.toString();
134 139
 	assertEquals(expected, actual);
103  test/com/twoguys/rps/InteractivePlayerTest.java
... ...
@@ -1,103 +0,0 @@
1  
-package com.twoguys.rps;
2  
-
3  
-import static org.junit.Assert.*;
4  
-import static org.mockito.Mockito.*;
5  
-
6  
-import java.io.*;
7  
-import org.junit.*;
8  
-
9  
-public class InteractivePlayerTest {
10  
-
11  
-    public final static String P1_PROMPT = "Player 1 Name: ";
12  
-    public final static String P2_PROMPT = "Player 2 Name: ";
13  
-    public final static String MOVE_PROMPT = "[R]ock, [P]aper, or [S]cissors? ";
14  
-    private String nate = "Nate";
15  
-    private String ben = "Ben";
16  
-    private Reader in;
17  
-    private CharArrayWriter out;
18  
-
19  
-    @Before
20  
-    public void setup() {
21  
-	in = new StringReader(nate + "\n");
22  
-	out = new CharArrayWriter();
23  
-    }
24  
-
25  
-    @Test
26  
-    public void testPromptsForName() throws Exception {
27  
-	Player player = new InteractivePlayer(in, out, 1);
28  
-
29  
-	String written = out.toString();
30  
-	assertEquals(P1_PROMPT, written);
31  
-    }
32  
-
33  
-    @Test
34  
-    public void testPromptsPlayerNumber() throws Exception {
35  
-	Player player1 = new InteractivePlayer(in, out, 1);
36  
-	String written = out.toString();
37  
-	assertEquals(P1_PROMPT, written);
38  
-
39  
-	out.reset();
40  
-	Player player2 = new InteractivePlayer(in, out, 2);
41  
-	written = out.toString();
42  
-	assertEquals(P2_PROMPT, written);
43  
-    }
44  
-
45  
-    @Test
46  
-    public void testReadsNameFromInput() throws Exception {
47  
-	Player player = new InteractivePlayer(in, out, 1);
48  
-
49  
-	assertEquals(-1, in.read());
50  
-    }
51  
-
52  
-    @Test
53  
-    public void testSetsPlayerName() throws Exception {
54  
-	Player player = new InteractivePlayer(in, out, 1);
55  
-	assertEquals(nate, player.getName());
56  
-
57  
-	in = new StringReader(ben + "\n");
58  
-	player = new InteractivePlayer(in, out, 2);
59  
-	assertEquals(ben, player.getName());
60  
-    }
61  
-
62  
-    @Test
63  
-    public void testPromptsForMove() throws Exception {
64  
-	Player player = new InteractivePlayer(in, out, 1);
65  
-	out.reset();
66  
-
67  
-	player.getThrow();
68  
-	String written = out.toString();
69  
-	assertEquals(MOVE_PROMPT, written);
70  
-    }
71  
-
72  
-    @Test
73  
-    public void testGetThrowReturnsRock() throws Exception {
74  
-	in = new StringReader(nate + "\n" + "R" + "\n");
75  
-
76  
-	Player player = new InteractivePlayer(in, out, 1);
77  
-	out.reset();
78  
-
79  
-	Throw thrown = player.getThrow();
80  
-	assertEquals(Rock.class, thrown.getClass());
81  
-    }
82  
-
83  
-    @Test
84  
-    public void testGetThrowReturnsPaper() throws Exception {
85  
-	in = new StringReader(nate + "\n" + "P" + "\n");
86  
-
87  
-	Player player = new InteractivePlayer(in, out, 1);
88  
-	out.reset();
89  
-
90  
-	Throw thrown = player.getThrow();
91  
-	assertEquals(Paper.class, thrown.getClass());
92  
-    }
93  
-
94  
-    @Test
95  
-    public void testNotifyWinner() throws Exception {
96  
-	Player player = new InteractivePlayer(in, out, 1);
97  
-	out.reset();
98  
-
99  
-	player.notifyWinner();
100  
-	String written = out.toString();
101  
-	assertEquals(nate + " Wins!\n", written);
102  
-    }
103  
-}

0 notes on commit 7b8816c

Please sign in to comment.
Something went wrong with that request. Please try again.