From dd6ca9d351ea31051c93976041464e695edcfca6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gina=20H=C3=A4u=C3=9Fge?= Date: Fri, 26 Jul 2019 17:22:28 +0200 Subject: [PATCH] =?UTF-8?q?=F0=9F=9A=A7=20WIP:=20Start=20on=20testing=20in?= =?UTF-8?q?frastructure?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tests/comm/__init__.py | 53 +++++++++++++++++++++++++++++++++ tests/comm/protocol/__init__.py | 37 +++++++++++++++++++++++ 2 files changed, 90 insertions(+) diff --git a/tests/comm/__init__.py b/tests/comm/__init__.py index e69de29bb..c6b6ed622 100644 --- a/tests/comm/__init__.py +++ b/tests/comm/__init__.py @@ -0,0 +1,53 @@ +# coding=utf-8 +from __future__ import absolute_import, unicode_literals, print_function, \ + division + +""" +Unit tests for ``octoprint.comm.protocol``. +""" + +__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html' +__copyright__ = "Copyright (C) 2019 The OctoPrint Project - Released under terms of the AGPLv3 License" + + +import time + + +from octoprint.comm.transport import Transport + + +class TestTransport(Transport): + + def __init__(self, *args, **kwargs): + super(TestTransport, self).__init__(*args, **kwargs) + self.incoming = bytearray() + self.outgoing = bytearray() + + @property + def in_waiting(self): + return len(self.outgoing) + + def do_read(self, size=None, timeout=None): + if size is None: + result = bytes(self.outgoing) + + else: + if timeout is None: + while self.in_waiting < size: + time.sleep(0.1) + else: + start = time.time() + while self.in_waiting < size and time.time() - start < timeout: + time.sleep(0.1) + + if size < self.in_waiting: + result = bytes(self.outgoing[0:size - 1]) + del self.outgoing[0:size - 1] + else: + result = bytes(self.outgoing) + del self.outgoing[:] + + return result + + def do_write(self, data): + self.incoming += bytearray(data) diff --git a/tests/comm/protocol/__init__.py b/tests/comm/protocol/__init__.py index e69de29bb..0e5688444 100644 --- a/tests/comm/protocol/__init__.py +++ b/tests/comm/protocol/__init__.py @@ -0,0 +1,37 @@ +# coding=utf-8 +from __future__ import absolute_import, unicode_literals, print_function, \ + division + +""" +Unit tests for ``octoprint.comm.protocol.reprap``. +""" + +__license__ = 'GNU Affero General Public License http://www.gnu.org/licenses/agpl.html' +__copyright__ = "Copyright (C) 2019 The OctoPrint Project - Released under terms of the AGPLv3 License" + + +import unittest +import mock + +from octoprint.comm.protocol.reprap import ReprapGcodeProtocol + +from .. import TestTransport + + +class RepRapProtocolTest(unittest.TestCase): + + def setUp(self): + printer_profile = mock.MagicMock() + plugin_manager = mock.MagicMock() + 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.transport = TestTransport() + + def testHandshake(self): + self.transport.outgoing += b"start\nThis is a test\nNothing to see here\n" + self.protocol.connect(self.transport)