本文整理汇总了Python中mi.core.instrument.instrument_protocol.CommandResponseInstrumentProtocol._add_response_handler方法的典型用法代码示例。如果您正苦于以下问题:Python CommandResponseInstrumentProtocol._add_response_handler方法的具体用法?Python CommandResponseInstrumentProtocol._add_response_handler怎么用?Python CommandResponseInstrumentProtocol._add_response_handler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类mi.core.instrument.instrument_protocol.CommandResponseInstrumentProtocol
的用法示例。
在下文中一共展示了CommandResponseInstrumentProtocol._add_response_handler方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestUnitCommandInstrumentProtocol
# 需要导入模块: from mi.core.instrument.instrument_protocol import CommandResponseInstrumentProtocol [as 别名]
# 或者: from mi.core.instrument.instrument_protocol.CommandResponseInstrumentProtocol import _add_response_handler [as 别名]
class TestUnitCommandInstrumentProtocol(MiUnitTestCase):
"""
Test cases for instrument protocol class. Functions in this class provide
instrument protocol unit tests and provide a tutorial on use of
the protocol interface.
"""
class TestState(BaseEnum):
"""
Protocol states for SBE37. Cherry picked from DriverProtocolState
enum.
"""
TEST = "TEST"
class TestEvent(BaseEnum):
"""
Protocol events for SBE37. Cherry picked from DriverEvent enum.
"""
ENTER = "ENTER"
EXIT = "EXIT"
TEST = "TEST"
def setUp(self):
"""
"""
self.prompts = [">"]
self.newline = "\n"
self.callback_result = None
self._trigger_count = 0
self._events = []
self.protocol = CommandResponseInstrumentProtocol(self.prompts,
self.newline,
self.event_callback)
self.protocol_fsm = ThreadSafeFSM(self.TestState, self.TestEvent,
self.TestEvent.ENTER, self.TestEvent.EXIT)
self.protocol_fsm.add_handler(self.TestState.TEST, self.TestEvent.TEST, lambda x : x)
self.protocol._add_build_handler(self.TestEvent.TEST, self._build_simple_command)
self.protocol._add_response_handler(self.TestEvent.TEST, self._parse_test_response)
self.protocol._connection = Mock()
self.protocol._connection.send = lambda x : self.protocol.add_to_buffer("%s >->" % x)
self.protocol.get_current_state = Mock(return_value=self.TestState.TEST)
self.protocol._send_wakeup = lambda: self.protocol.add_to_buffer("wakeup response >->")
def _build_simple_command(self, cmd):
return "cmd...do it!"
def _parse_test_response(self, resp, prompt):
return "c=%s p=%s" % (resp, prompt)
def event_callback(self, event, value=None):
log.debug("Test event callback: %s" % event)
self._events.append(event)
self._trigger_count += 1
def test_cmd_response(self):
"""
Test getting a response from a command supplied with prompts and regexes.
"""
regex1 = re.compile(r'.*(do it).*')
regex2 = re.compile(r'foobar')
regex3 = re.compile(r'.*(do) (it).*')
regex4 = re.compile(r'.*do it.*')
# Normal case
result = self.protocol._do_cmd_resp(self.TestEvent.TEST)
self.assertEqual(result, self._parse_test_response(self._build_simple_command(None)+" >", ">"))
# expected prompt cases
result = self.protocol._do_cmd_resp(self.TestEvent.TEST, expected_prompt=">")
self.assertEqual(result, self._parse_test_response(self._build_simple_command(None)+" >", ">"))
result = self.protocol._do_cmd_resp(self.TestEvent.TEST, expected_prompt=">-")
self.assertEqual(result, self._parse_test_response(self._build_simple_command(None)+" >-", ">-"))
# Should time out looking for a bad prompt
self.assertRaises(InstrumentTimeoutException,
self.protocol._do_cmd_resp,
self.TestEvent.TEST, expected_prompt="-->", timeout=5)
# regex cases
result = self.protocol._do_cmd_resp(self.TestEvent.TEST, response_regex=regex1)
self.assertEqual(result, self._parse_test_response("do it", ""))
result = self.protocol._do_cmd_resp(self.TestEvent.TEST, response_regex=regex3)
self.assertEqual(result, self._parse_test_response("doit", ""))
result = self.protocol._do_cmd_resp(self.TestEvent.TEST, response_regex=regex4)
self.assertEqual(result, self._parse_test_response("", ""))
# Should time out looking for a bad regex
self.assertRaises(InstrumentTimeoutException,
self.protocol._do_cmd_resp,
self.TestEvent.TEST, response_regex=regex2)
# combo case
self.assertRaises(InstrumentProtocolException,
self.protocol._do_cmd_resp,
self.TestEvent.TEST, expected_prompt=">", response_regex=regex1)