-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
126 lines (81 loc) · 3.46 KB
/
test.py
File metadata and controls
126 lines (81 loc) · 3.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import subprocess
import os
import time
import socket
from unittest import TestCase
from server import Server
class TestClient(object):
def __init__(self, port):
self.client_socket = socket.socket()
self.client_socket.connect(("127.0.0.1", port))
self.response = self.client_socket.recv(4096).decode();
def send(self, message):
self.client_socket.sendall(message.encode())
self.response = self.client_socket.recv(4096).decode()
class ServerUnitTest(TestCase):
def setUp(self):
self.server = Server()
def test_room_description(self):
descriptions = [self.server.room_description(i) for i in range(4)]
self.assertCountEqual(descriptions, set(descriptions))
def test_say(self):
for phrase in ["OK!", "Whazzzzzup?", "African or European?"]:
self.server.say(phrase)
self.assertEqual("You say, \"{}\"".format(phrase), self.server.output_buffer)
class AcceptanceTests(TestCase):
@staticmethod
def make_server(port):
server = subprocess.Popen(
[
"python",
os.path.join(
os.getcwd(),
"serve.py"
),
str(port),
],
stdout=subprocess.PIPE,
creationflags=subprocess.CREATE_NEW_PROCESS_GROUP,
)
time.sleep(3)
return server
@staticmethod
def make_client(port):
client = TestClient(port)
return client
def test_for_acceptance(self):
self.make_server(50005)
client = self.make_client(50005)
room_descriptions = {}
initial_response = client.response
self.assertIn(
"Welcome to Realms of Venture",
initial_response,
"The server provides the greeting message"
)
client.send("move south")
client.send("move west")
client.send("move west") # We should now be in room 1
room_descriptions[1] = client.response.split("OK! ")[1] # Retrieve the room 1 description
client.send("move east")
room_descriptions[0] = client.response.split("OK! ")[1] # Retrieve the room 0 description
client.send("move east")
room_descriptions[2] = client.response.split("OK! ")[1] # Retrieve the room 2 description
client.send("move west")
client.send("move north")
room_descriptions[3] = client.response.split("OK! ")[1] # Retrieve the room 3 description
client.send("move south")
self.assertIn(room_descriptions[0], client.response, "Client can navigate through server's rooms.")
client.send("move west")
self.assertIn(room_descriptions[1], client.response, "Client can navigate through server's rooms.")
client.send("move east")
client.send("move east")
self.assertIn(room_descriptions[2], client.response, "Client can navigate through server's rooms.")
client.send("move west")
client.send("move north")
self.assertIn(room_descriptions[3], client.response, "Client can navigate through server's rooms.")
self.assertEqual(4, len(set(room_descriptions.keys())), "Room descriptions are unique.")
client.send("say Hello?")
self.assertIn("You say, \"Hello?\"", client.response, "Client can see their speakings.")
client.send("quit")
self.assertEqual("OK! Goodbye!", client.response, "The server says goodbye.")