当前位置: 首页>>代码示例>>Python>>正文


Python CommandResponseInstrumentProtocol._add_build_handler方法代码示例

本文整理汇总了Python中mi.core.instrument.instrument_protocol.CommandResponseInstrumentProtocol._add_build_handler方法的典型用法代码示例。如果您正苦于以下问题:Python CommandResponseInstrumentProtocol._add_build_handler方法的具体用法?Python CommandResponseInstrumentProtocol._add_build_handler怎么用?Python CommandResponseInstrumentProtocol._add_build_handler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在mi.core.instrument.instrument_protocol.CommandResponseInstrumentProtocol的用法示例。


在下文中一共展示了CommandResponseInstrumentProtocol._add_build_handler方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestUnitCommandInstrumentProtocol

# 需要导入模块: from mi.core.instrument.instrument_protocol import CommandResponseInstrumentProtocol [as 别名]
# 或者: from mi.core.instrument.instrument_protocol.CommandResponseInstrumentProtocol import _add_build_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)
开发者ID:aplmmilcic,项目名称:mi-instrument,代码行数:102,代码来源:test_instrument_protocol.py


注:本文中的mi.core.instrument.instrument_protocol.CommandResponseInstrumentProtocol._add_build_handler方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。