11import subprocess
22import os
3+ import time
4+ import socket
35from unittest import TestCase
46
5-
67from server import Server
78
89
10+ class TestClient (object ):
11+
12+ def __init__ (self , port ):
13+
14+ self .client_socket = socket .socket ()
15+ self .client_socket .connect (("127.0.0.1" , port ))
16+
17+ self .response = self .client_socket .recv (4096 ).decode ();
18+
19+ def send (self , message ):
20+
21+ self .client_socket .sendall (message .encode ())
22+ self .response = self .client_socket .recv (4096 ).decode ()
23+
24+
925class ServerUnitTest (TestCase ):
1026
1127 def setUp (self ):
@@ -21,5 +37,90 @@ def test_say(self):
2137
2238 for phrase in ["OK!" , "Whazzzzzup?" , "African or European?" ]:
2339 self .server .say (phrase )
24- self .assertEqual ("OK! " + phrase , self .server .output_buffer )
40+ self .assertEqual ("You say, \" {}\" " .format (phrase ), self .server .output_buffer )
41+
42+
43+ class AcceptanceTests (TestCase ):
44+
45+ @staticmethod
46+ def make_server (port ):
47+
48+ server = subprocess .Popen (
49+ [
50+ "python" ,
51+ os .path .join (
52+ os .getcwd (),
53+ "serve.py"
54+ ),
55+ str (port ),
56+ ],
57+ stdout = subprocess .PIPE ,
58+ creationflags = subprocess .CREATE_NEW_PROCESS_GROUP ,
59+ )
60+
61+ time .sleep (3 )
62+ return server
63+
64+ @staticmethod
65+ def make_client (port ):
66+ client = TestClient (port )
67+ return client
68+
69+ def test_for_acceptance (self ):
70+
71+ self .make_server (50005 )
72+
73+ client = self .make_client (50005 )
74+
75+ room_descriptions = {}
76+
77+ initial_response = client .response
78+
79+ self .assertIn (
80+ "Welcome to Realms of Venture" ,
81+ initial_response ,
82+ "The server provides the greeting message"
83+ )
84+
85+ client .send ("move south" )
86+ client .send ("move west" )
87+ client .send ("move west" ) # We should now be in room 1
88+
89+ room_descriptions [1 ] = client .response .split ("OK! " )[1 ] # Retrieve the room 1 description
90+
91+ client .send ("move east" )
92+ room_descriptions [0 ] = client .response .split ("OK! " )[1 ] # Retrieve the room 0 description
93+
94+ client .send ("move east" )
95+ room_descriptions [2 ] = client .response .split ("OK! " )[1 ] # Retrieve the room 2 description
96+
97+ client .send ("move west" )
98+ client .send ("move north" )
99+ room_descriptions [3 ] = client .response .split ("OK! " )[1 ] # Retrieve the room 3 description
100+
101+ client .send ("move south" )
102+ self .assertIn (room_descriptions [0 ], client .response , "Client can navigate through server's rooms." )
103+
104+ client .send ("move west" )
105+ self .assertIn (room_descriptions [1 ], client .response , "Client can navigate through server's rooms." )
106+
107+ client .send ("move east" )
108+ client .send ("move east" )
109+ self .assertIn (room_descriptions [2 ], client .response , "Client can navigate through server's rooms." )
110+
111+ client .send ("move west" )
112+ client .send ("move north" )
113+ self .assertIn (room_descriptions [3 ], client .response , "Client can navigate through server's rooms." )
114+
115+ self .assertEqual (4 , len (set (room_descriptions .keys ())), "Room descriptions are unique." )
116+
117+ client .send ("say Hello?" )
118+ self .assertIn ("You say, \" Hello?\" " , client .response , "Client can see their speakings." )
119+
120+ client .send ("quit" )
121+ self .assertEqual ("OK! Goodbye!" , client .response , "The server says goodbye." )
122+
123+
124+
125+
25126
0 commit comments