-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Write a couple of tests, using monkey-patch of socket and selection. …
- Loading branch information
1 parent
6eeb804
commit 8ed2e78
Showing
1 changed file
with
62 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
'''Tests for the legacy minecraft module. | ||
''' | ||
|
||
from mcpi.minecraft import Minecraft | ||
import mcpi.connection | ||
|
||
# We monkey-patch socket and select in connection.py. This allows us | ||
# to 'listen to the wire' without the trouble of setting up a real | ||
# socket server. | ||
|
||
class DummySocket: | ||
'''Dummy socket.socket class. | ||
''' | ||
def __init__(self, address, port): | ||
self._sent = [] | ||
|
||
def connect(self, address_port): | ||
pass | ||
|
||
def sendall(self, s): | ||
self._sent.append(s) | ||
|
||
|
||
class DummySocketModule: | ||
'''Dummy socket module. | ||
''' | ||
AF_INET = SOCK_STREAM = None | ||
socket = DummySocket | ||
|
||
|
||
class DummySelectModule: | ||
'''Dummy select module. | ||
''' | ||
def select(self, *args): | ||
return False, None, None | ||
|
||
|
||
# Do the monkey patch. Affects only connection.py. | ||
mcpi.connection.socket = DummySocketModule() | ||
mcpi.connection.select = DummySelectModule() | ||
|
||
# Create the 'test fixture'. | ||
mc = Minecraft.create() | ||
sent = mc.conn.socket._sent | ||
|
||
|
||
def test_postToChat(): | ||
|
||
# This succeeds. Good. | ||
mc.postToChat('hi') | ||
assert sent[-1] == 'chat.post(hi)\n' | ||
|
||
# This should fail but does not. Bad. | ||
mc.postToChat('lookout)\ngotcha(') | ||
assert sent[-1] == 'chat.post(lookout)\ngotcha()\n' | ||
|
||
|
||
def test_setPos(): | ||
|
||
# Here we get what we expect. | ||
mc.player.setPos(3, 4, 5, 6) | ||
assert sent[-1] == 'player.setPos(3,4,5,6)\n' |