Add several tests for the Connection class.
This commit is contained in:
1 parent
ab9ca6dfee
commit
92f2eff681
3 files changed
+94
-17
No files matched your search
@@ -1,9 +1,14 @@
|
||||
from minecraft import SUPPORTED_MINECRAFT_VERSIONS
|
||||
from minecraft.networking.packets import clientbound, serverbound
|
||||
from minecraft.networking.connection import IgnorePacket
|
||||
from minecraft.compat import unicode
|
||||
|
||||
from . import fake_server
|
||||
|
||||
import sys
|
||||
import re
|
||||
import io
|
||||
|
||||
|
||||
class ConnectTest(fake_server._FakeServerTest):
|
||||
def test_connect(self):
|
||||
@@ -31,6 +36,37 @@ class PingTest(ConnectTest):
|
||||
client.status(handle_status=False, handle_ping=handle_ping)
|
||||
|
||||
|
||||
class StatusTest(ConnectTest):
|
||||
def _start_client(self, client):
|
||||
def handle_status(status_dict):
|
||||
assert status_dict['description'] == {'text': 'FakeServer'}
|
||||
raise fake_server.FakeServerTestSuccess
|
||||
client.status(handle_status=handle_status, handle_ping=False)
|
||||
|
||||
|
||||
class DefaultStatusTest(ConnectTest):
|
||||
def setUp(self):
|
||||
class FakeStdOut(io.BytesIO):
|
||||
def write(self, data):
|
||||
if isinstance(data, unicode):
|
||||
data = data.encode('utf8')
|
||||
super(FakeStdOut, self).write(data)
|
||||
sys.stdout, self.old_stdout = FakeStdOut(), sys.stdout
|
||||
|
||||
def tearDown(self):
|
||||
sys.stdout, self.old_stdout = self.old_stdout, None
|
||||
|
||||
def _start_client(self, client):
|
||||
def handle_exit():
|
||||
output = sys.stdout.getvalue()
|
||||
assert re.match(b'{.*}\\nPing: \\d+ ms\\n$', output), \
|
||||
'Invalid stdout contents: %r.' % output
|
||||
raise fake_server.FakeServerTestSuccess
|
||||
client.handle_exit = handle_exit
|
||||
|
||||
client.status(handle_status=None, handle_ping=None)
|
||||
|
||||
|
||||
class ConnectCompressionLowTest(ConnectTest):
|
||||
compression_threshold = 0
|
||||
|
||||
@@ -189,3 +225,22 @@ class IgnorePacketTest(ConnectTest):
|
||||
'Returned keep-alive IDs %r != %r' % \
|
||||
(self._keep_alive_ids_returned, [3])
|
||||
raise fake_server.FakeServerTestSuccess
|
||||
|
||||
|
||||
class HandleExceptionTest(ConnectTest):
|
||||
ignore_extra_exceptions = True
|
||||
|
||||
def _start_client(self, client):
|
||||
message = 'Min skoldpadda ar inte snabb, men den ar en skoldpadda.'
|
||||
|
||||
def handle_login_success(_packet):
|
||||
raise Exception(message)
|
||||
client.register_packet_listener(
|
||||
handle_login_success, clientbound.login.LoginSuccessPacket)
|
||||
|
||||
def handle_exception(exc, exc_info):
|
||||
assert isinstance(exc, Exception) and exc.args == (message,)
|
||||
raise fake_server.FakeServerTestSuccess
|
||||
client.handle_exception = handle_exception
|
||||
|
||||
client.connect()
|
||||
Reference in new issue
Block a user