🚧 First unit tests
This commit is contained in:
1 parent
f5e56b4d4a
commit
3eb572ede5
2 files changed
+89
-9
No files matched your search
+36
-3
@@ -12,16 +12,16 @@ __copyright__ = "Copyright (C) 2019 The OctoPrint Project - Released under terms
|
||||
|
||||
import time
|
||||
|
||||
|
||||
from octoprint.comm.transport import Transport
|
||||
|
||||
from octoprint.util import monotonic_time, to_bytes
|
||||
|
||||
class TestTransport(Transport):
|
||||
|
||||
def __init__(self, *args, **kwargs):
|
||||
super(TestTransport, self).__init__(*args, **kwargs)
|
||||
self.incoming = bytearray()
|
||||
self.outgoing = bytearray()
|
||||
self.incoming = []
|
||||
|
||||
@property
|
||||
def in_waiting(self):
|
||||
@@ -50,4 +50,37 @@ class TestTransport(Transport):
|
||||
return result
|
||||
|
||||
def do_write(self, data):
|
||||
self.incoming += bytearray(data)
|
||||
self.incoming.append(data)
|
||||
|
||||
def send(self, data):
|
||||
self.outgoing += bytearray(to_bytes(data))
|
||||
|
||||
def expect(self, expected, timeout=10.0):
|
||||
if timeout is not None:
|
||||
stop = monotonic_time() + timeout
|
||||
timed_out = lambda: monotonic_time() >= stop
|
||||
else:
|
||||
timed_out = lambda: False
|
||||
|
||||
while len(self.incoming) == 0 and not timed_out():
|
||||
time.sleep(0.1)
|
||||
|
||||
try:
|
||||
actual = self.incoming.pop(0)
|
||||
except IndexError:
|
||||
raise ExpectTimeout()
|
||||
|
||||
if callable(expected):
|
||||
expected = expected()
|
||||
|
||||
if expected != actual:
|
||||
raise AssertionError("Expected {!r}, got {!r}".format(expected, actual))
|
||||
|
||||
return actual
|
||||
|
||||
def expect_and_send(self, expected, data, timeout=10.0):
|
||||
self.expect(expected, timeout=timeout)
|
||||
self.send(data)
|
||||
|
||||
class ExpectTimeout(Exception):
|
||||
pass
|
||||
@@ -13,7 +13,9 @@ __copyright__ = "Copyright (C) 2019 The OctoPrint Project - Released under terms
|
||||
import unittest
|
||||
import mock
|
||||
|
||||
from octoprint.comm.protocol.reprap import ReprapGcodeProtocol
|
||||
import octoprint.comm.protocol.reprap
|
||||
|
||||
from octoprint.util import to_bytes
|
||||
|
||||
from .. import TestTransport
|
||||
|
||||
@@ -26,12 +28,57 @@ class RepRapProtocolTest(unittest.TestCase):
|
||||
event_bus = mock.MagicMock()
|
||||
settings = mock.MagicMock()
|
||||
|
||||
self.protocol = ReprapGcodeProtocol(printer_profile=printer_profile,
|
||||
plugin_manager=plugin_manager,
|
||||
event_bus=event_bus,
|
||||
settings=settings)
|
||||
self.protocol = octoprint.comm.protocol.reprap.ReprapGcodeProtocol(printer_profile=printer_profile,
|
||||
plugin_manager=plugin_manager,
|
||||
event_bus=event_bus,
|
||||
settings=settings)
|
||||
self.transport = TestTransport()
|
||||
|
||||
def testHandshake(self):
|
||||
self.transport.outgoing += b"start\nThis is a test\nNothing to see here\n"
|
||||
self.protocol.connect(self.transport)
|
||||
self._perform_and_test_handshake()
|
||||
|
||||
def testHandshakeWithTemperatureAutoreport(self):
|
||||
self.protocol.interval["temperature_autoreport"] = 2.0
|
||||
self.protocol.connect(self.transport)
|
||||
self._perform_and_test_handshake(capabilities=dict(AUTOREPORT_TEMP=True))
|
||||
self.transport.expect_and_send("M155 S2\n", "ok\n")
|
||||
|
||||
def testHandshakeWithTemperatureAutoreportButDisabled(self):
|
||||
self.protocol.interval["temperature_autoreport"] = 2.0
|
||||
self.protocol._capability_support[octoprint.comm.protocol.reprap.CAPABILITY_AUTOREPORT_TEMP] = False
|
||||
|
||||
self.protocol.connect(self.transport)
|
||||
self._perform_and_test_handshake(capabilities=dict(AUTOREPORT_TEMP=True))
|
||||
self.transport.expect_and_send("M105\n", "ok T:21.3/0\n")
|
||||
|
||||
def testHandshakeWithSdAutoreport(self):
|
||||
self.protocol.interval["sd_status_autoreport"] = 2.0
|
||||
self.protocol.connect(self.transport)
|
||||
self._perform_and_test_handshake(capabilities=dict(AUTOREPORT_SD_STATUS=True))
|
||||
self.transport.expect_and_send("M27 S2\n", "ok\n")
|
||||
|
||||
def _perform_and_test_handshake(self, firmware_name=None, capabilities=None):
|
||||
if firmware_name is None:
|
||||
firmware_name = "ProtocolTest"
|
||||
if capabilities is None:
|
||||
capabilities = dict()
|
||||
|
||||
firmware_info = "FIRMWARE_NAME:{}\n".format(firmware_name)
|
||||
for cap, enabled in capabilities.items():
|
||||
firmware_info += "Cap:{}:{}\n".format(cap.upper(), "1" if enabled else "0")
|
||||
firmware_info += "ok\n"
|
||||
|
||||
self.transport.expect_and_send("N0 M110 N0*125\n", "start\n") # tickle 1
|
||||
self.transport.expect_and_send("N0 M110 N0*125\n", "ok\n") # tickle 2
|
||||
self.transport.expect_and_send("N0 M110 N0*125\n", "ok\n") # reset line numbers
|
||||
self.transport.expect_and_send("N1 M115*39\n", firmware_info) # get firmware info
|
||||
self.transport.expect_and_send("N2 M105*37\n", "ok T:21.3/0\n") # temperature query
|
||||
|
||||
@staticmethod
|
||||
def _checksummed(line, linenumber):
|
||||
result = b"N" + str(linenumber).encode("ascii") + b" " + to_bytes(line)
|
||||
checksum = 0
|
||||
for c in bytearray(result):
|
||||
checksum ^= c
|
||||
return result + b"*" + str(checksum).encode("ascii")
|
||||
Reference in new issue
Block a user