本文整理汇总了Python中Rammbock.templates.containers.MessageTemplate.validate方法的典型用法代码示例。如果您正苦于以下问题:Python MessageTemplate.validate方法的具体用法?Python MessageTemplate.validate怎么用?Python MessageTemplate.validate使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Rammbock.templates.containers.MessageTemplate
的用法示例。
在下文中一共展示了MessageTemplate.validate方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestMessageTemplateValidation
# 需要导入模块: from Rammbock.templates.containers import MessageTemplate [as 别名]
# 或者: from Rammbock.templates.containers.MessageTemplate import validate [as 别名]
class TestMessageTemplateValidation(TestCase):
def setUp(self):
self._protocol = Protocol('TestProtocol')
self._protocol.add(UInt(2, 'msgId', 5))
self._protocol.add(UInt(2, 'length', None))
self._protocol.add(PDU('length-4'))
self.tmp = MessageTemplate('FooRequest', self._protocol, {})
self.tmp.add(UInt(2, 'field_1', '0xcafe'))
self.tmp.add(UInt(2, 'field_2', '0xbabe'))
self.example = self.tmp.encode({}, {})
def test_validate_passing_hex(self):
msg = self._decode_and_set_fake_header('0xcafebabe')
errors = self.tmp.validate(msg, {}, {})
self.assertEquals(errors, [])
def test_validate_error_default_value(self):
msg = self._decode_and_set_fake_header('0xcafedead')
errors = self.tmp.validate(msg, {}, {})
self.assertEquals(errors, ['Value of field field_2 does not match 0xdead!=0xbabe'])
def test_validate_error_override(self):
msg = self._decode_and_set_fake_header('0xcafebabe')
errors = self.tmp.validate(msg, {'field_2': '0xdead'}, {})
self.assertEquals(errors, ['Value of field field_2 does not match 0xbabe!=0xdead'])
def test_validate_two_errors(self):
msg = self._decode_and_set_fake_header('0xbeefbabe')
errors = self.tmp.validate(msg, {'field_2': '0xdead'}, {})
self.assertEquals(len(errors), 2)
def test_validate_pattern_pass(self):
msg = self._decode_and_set_fake_header('0xcafe0002')
errors = self.tmp.validate(msg, {'field_2': '(0|2)'}, {})
self.assertEquals(len(errors), 0)
def test_validate_pattern_failure(self):
msg = self._decode_and_set_fake_header('0xcafe0002')
errors = self.tmp.validate(msg, {'field_2': '(0|3)'}, {})
self.assertEquals(len(errors), 1)
def test_validate_passing_int(self):
msg = self._decode_and_set_fake_header('0xcafe0200')
errors = self.tmp.validate(msg, {'field_2': '512'}, {})
self.assertEquals(errors, [])
def _decode_and_set_fake_header(self, bin_value):
msg = self.tmp.decode(to_bin(bin_value))
msg['_header'] = self.example._header
return msg
def test_failing_passing_int(self):
msg = self._decode_and_set_fake_header('0xcafe0200')
errors = self.tmp.validate(msg, {'field_2': '513'}, {})
self.assertEquals(len(errors), 1)