本文整理汇总了Python中Rammbock.templates.containers.MessageTemplate.add方法的典型用法代码示例。如果您正苦于以下问题:Python MessageTemplate.add方法的具体用法?Python MessageTemplate.add怎么用?Python MessageTemplate.add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Rammbock.templates.containers.MessageTemplate
的用法示例。
在下文中一共展示了MessageTemplate.add方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: TestStructs
# 需要导入模块: from Rammbock.templates.containers import MessageTemplate [as 别名]
# 或者: from Rammbock.templates.containers.MessageTemplate import add [as 别名]
class TestStructs(TestCase):
def test_access_struct(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('StructuredRequest', self._protocol, {})
struct = get_pair()
self.tmp.add(struct)
msg = self.tmp.encode({}, {})
self.assertEquals(msg.pair.first.int, 1)
def test_create_struct(self):
struct = get_pair()
self.assertEquals(struct.name, 'pair')
def test_add_fields_to_struct(self):
struct = get_pair()
encoded = struct.encode({}, {})
self.assertEquals(encoded.first.int, 1)
def test_add_fields_to_struct_and_override_values(self):
struct = get_pair()
encoded = struct.encode({'pair.first': 42}, {})
self.assertEquals(encoded.first.int, 42)
def test_yo_dawg_i_heard(self):
str_str = get_recursive_struct()
encoded = str_str.encode({}, {})
self.assertEquals(encoded.pair.first.int, 1)
def test_get_recursive_names(self):
pair = get_pair()
names = pair._get_params_sub_tree({'pair.foo': 0, 'pairnotyourname.ploo': 2, 'pair.goo.doo': 3})
self.assertEquals(len(names), 2)
self.assertEquals(names['foo'], 0)
self.assertEquals(names['goo.doo'], 3)
def test_set_recursive(self):
str_str = get_recursive_struct()
encoded = str_str.encode({'str_str.pair.first': 42}, {})
self.assertEquals(encoded.pair.first.int, 42)
def test_decode_several_structs(self):
str_list = get_struct_list()
decoded = str_list.decode(to_bin('0xcafebabe d00df00d'), {})
self.assertEquals(decoded[0].first.hex, '0xcafe')
self.assertEquals(decoded[1].second.hex, '0xf00d')
def test_length_of_struct(self):
pair = get_pair()
encoded = pair.encode({}, {})
self.assertEquals(len(encoded), 4)
def test_decode_struct(self):
pair = get_pair()
decoded = pair.decode(to_bin('0xcafebabe'), {})
self.assertEquals(decoded.first.hex, '0xcafe')
self.assertEquals(decoded.second.hex, '0xbabe')
示例2: test_encode_dynamic_primitive_automatically
# 需要导入模块: from Rammbock.templates.containers import MessageTemplate [as 别名]
# 或者: from Rammbock.templates.containers.MessageTemplate import add [as 别名]
def test_encode_dynamic_primitive_automatically(self):
tmp = MessageTemplate('Dymagic', self._protocol, {})
tmp.add(UInt(4, 'len', None))
tmp.add(Char('len', 'chars', 'abcd'))
encoded = tmp.encode({}, {})
self.assertEquals(encoded.chars.ascii, 'abcd')
self.assertEquals(encoded.len.int, 4)
示例3: TestLittleEndianProtocol
# 需要导入模块: from Rammbock.templates.containers import MessageTemplate [as 别名]
# 或者: from Rammbock.templates.containers.MessageTemplate import add [as 别名]
class TestLittleEndianProtocol(TestCase):
def setUp(self):
self._protocol = Protocol('TestProtocol', little_endian=True)
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'))
def test_encode_little_endian_header(self):
encoded = self.tmp.encode({}, {})
self.assertEquals(encoded._header.msgId.hex, '0x0005')
self.assertEquals(encoded._header.length.hex, '0x0008')
self.assertEquals(encoded._header.msgId._raw, to_bin('0x0500'))
self.assertEquals(encoded._header.length._raw, to_bin('0x0800'))
def test_decode_little_endian_header(self):
byte_stream = MockStream(to_bin('0x0500 0800 cafe babe'))
self._msg_stream = MessageStream(byte_stream, self._protocol, threading.RLock())
decoded = self._msg_stream.get(self.tmp)
self.assertEquals(decoded._header.msgId.hex, '0x0005')
self.assertEquals(decoded._header.msgId._raw, to_bin('0x0500'))
self.assertEquals(decoded.field_1.hex, '0xcafe')
self.assertEquals(decoded.field_1._raw, to_bin('0xcafe'))
self.assertEquals(decoded.field_2.hex, '0xbabe')
self.assertEquals(decoded.field_2._raw, to_bin('0xbabe'))
示例4: TestMessageTemplate
# 需要导入模块: from Rammbock.templates.containers import MessageTemplate [as 别名]
# 或者: from Rammbock.templates.containers.MessageTemplate import add [as 别名]
class TestMessageTemplate(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', 1))
self.tmp.add(UInt(2, 'field_2', 2))
def test_create_template(self):
self.assertEquals(len(self.tmp._fields), 2)
def test_encode_template(self):
msg = self.tmp.encode({}, {})
self.assertEquals(msg.field_1.int, 1)
self.assertEquals(msg.field_2.int, 2)
def test_message_field_type_conversions(self):
msg = self.tmp.encode({'field_1': 1024}, {})
self.assertEquals(msg.field_1.int, 1024)
self.assertEquals(msg.field_1.hex, '0x0400')
self.assertEquals(msg.field_1.bytes, '\x04\x00')
def test_encode_template_with_params(self):
msg = self.tmp.encode({'field_1': 111, 'field_2': 222}, {})
self.assertEquals(msg.field_1.int, 111)
self.assertEquals(msg.field_2.int, 222)
def test_encode_template_header(self):
msg = self.tmp.encode({}, {})
self.assertEquals(msg._header.msgId.int, 5)
self.assertEquals(msg._header.length.int, 8)
def test_encode_to_bytes(self):
msg = self.tmp.encode({}, {})
self.assertEquals(msg._header.msgId.int, 5)
self.assertEquals(msg._raw, to_bin_of_length(8, '0x0005 0008 0001 0002'))
def test_pretty_print(self):
msg = self.tmp.encode({}, {})
self.assertEquals(msg._header.msgId.int, 5)
self.assertEquals(str(msg), 'Message FooRequest')
self.assertEquals(repr(msg),
"""Message FooRequest
Header TestProtocol
msgId = 5 (0x0005)
length = 8 (0x0008)
field_1 = 1 (0x0001)
field_2 = 2 (0x0002)
""")
def test_unknown_params_cause_exception(self):
self.assertRaises(AssertionError, self.tmp.encode, {'unknown': 111}, {})
def test_decode_message(self):
msg = self.tmp.decode(to_bin('0xcafebabe'))
self.assertEquals(msg.field_1.hex, '0xcafe')
示例5: test_encode_dynamic_list
# 需要导入模块: from Rammbock.templates.containers import MessageTemplate [as 别名]
# 或者: from Rammbock.templates.containers.MessageTemplate import add [as 别名]
def test_encode_dynamic_list(self):
tmp = MessageTemplate('Dymagic', self._protocol, {})
tmp.add(UInt(2, 'len', None))
lst = ListTemplate('len', 'foo', parent=None)
lst.add(UInt(1, 'bar', 1))
tmp.add(lst)
encoded = tmp.encode({'len': 6}, {})
self.assertEquals(len(encoded.foo), 6)
示例6: test_decode_dynamic_list
# 需要导入模块: from Rammbock.templates.containers import MessageTemplate [as 别名]
# 或者: from Rammbock.templates.containers.MessageTemplate import add [as 别名]
def test_decode_dynamic_list(self):
tmp = MessageTemplate('Dymagic', self._protocol, {})
tmp.add(UInt(2, 'len', None))
lst = ListTemplate('len', 'foo', parent=None)
lst.add(UInt(1, 'bar', None))
tmp.add(lst)
decoded = tmp.decode(to_bin('0x 00 04 6162 6364'))
self.assertEquals(decoded.len.int, 4)
self.assertEquals(decoded.foo[0].hex, '0x61')
示例7: TestMessageTemplateValidation
# 需要导入模块: from Rammbock.templates.containers import MessageTemplate [as 别名]
# 或者: from Rammbock.templates.containers.MessageTemplate import add [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)
示例8: test_decode_dynamic_primitive
# 需要导入模块: from Rammbock.templates.containers import MessageTemplate [as 别名]
# 或者: from Rammbock.templates.containers.MessageTemplate import add [as 别名]
def test_decode_dynamic_primitive(self):
tmp = MessageTemplate('Dymagic', self._protocol, {})
tmp.add(UInt(1, 'len', None))
tmp.add(Char('len', 'chars', None))
tmp.add(UInt(1, 'len2', None))
tmp.add(Char('len2', 'chars2', None))
decoded = tmp.decode(to_bin('0x 04 6162 6364 02 6566'))
self.assertEquals(decoded.len.int, 4)
self.assertEquals(decoded.chars.ascii, 'abcd')
self.assertEquals(decoded.chars2.ascii, 'ef')
示例9: test_encode_dynamic_primitive_with_defined_value
# 需要导入模块: from Rammbock.templates.containers import MessageTemplate [as 别名]
# 或者: from Rammbock.templates.containers.MessageTemplate import add [as 别名]
def test_encode_dynamic_primitive_with_defined_value(self):
tmp = MessageTemplate('Dymagic', self._protocol, {})
tmp.add(UInt(4, 'len', '4'))
tmp.add(Char('len', 'chars', 'abcd'))
tmp.add(UInt(4, 'len2', None))
tmp.add(Char('len2', 'chars2', 'ef'))
encoded = tmp.encode({'len2': '6'}, {})
self.assertEquals(encoded.chars.ascii, 'abcd')
self.assertEquals(len(encoded.chars), 4)
self.assertEquals(encoded.chars2.ascii, 'ef')
self.assertEquals(len(encoded.chars2), 6)
示例10: TestTBCDContainerTemplate
# 需要导入模块: from Rammbock.templates.containers import MessageTemplate [as 别名]
# 或者: from Rammbock.templates.containers.MessageTemplate import add [as 别名]
class TestTBCDContainerTemplate(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', 1))
self.tmp.add(UInt(2, 'field_2', 2))
def test_verify_only_tbcd_number_passes(self):
container = TBCDContainerTemplate('tbcd', None)
container.add(TBCD('4', 'first', None))
self.assertRaises(AssertionError, container.add, UInt(2, 'not allowed', None))
def test_get_even_tbcd_field(self):
container = TBCDContainerTemplate('tbcd', None)
container.add(TBCD('4', 'first', '1234'))
self.tmp.add(container)
msg = self.tmp.encode({}, {})
self.assertEqual('1234', msg.tbcd.first.tbcd)
def test_get_odd_tbcd_field(self):
container = TBCDContainerTemplate('tbcd', None)
container.add(TBCD('3', 'first', '123'))
self.tmp.add(container)
msg = self.tmp.encode({}, {})
self.assertEqual('123', msg.tbcd.first.tbcd)
def test_get_binlength_for_odd_value_tbcd_container(self):
container = TBCDContainerTemplate('tbcd', None)
container.add(TBCD('1', 'first', '1'))
self.assertEquals(8, container.binlength)
container = TBCDContainerTemplate('tbcd', None)
container.add(TBCD('3', 'first', '123'))
self.assertEquals(16, container.binlength)
def test_get_binlength_for_even_value_tbcd_container(self):
container = TBCDContainerTemplate('tbcd', None)
container.add(TBCD('2', 'first', '12'))
self.assertEquals(8, container.binlength)
container = TBCDContainerTemplate('tbcd', None)
container.add(TBCD('4', 'first', '1234'))
self.assertEquals(16, container.binlength)
def test_decode(self):
container = TBCDContainerTemplate('tbcd', None)
container.add(TBCD('3', 'first', None))
decoded = container.decode(to_bin('0x21f3'))
self.assertEquals('123', decoded.first.tbcd)
def test_little_endian_tbcd_unsupported(self):
container = TBCDContainerTemplate('tbcd', None)
container.add(TBCD('3', 'first', '123'))
self.assertRaises(AssertionError, container.encode, {}, {}, little_endian=True)
self.assertRaises(AssertionError, container.decode, to_bin('0x21f3'), little_endian=True)
def test_get_encoded_raw_bytes(self):
container = TBCDContainerTemplate('tbcd', None)
container.add(TBCD('3', 'first', '123'))
container.add(TBCD('13', 'second', '6100000000001'))
encoded = container.encode({}, {})
self.assertEquals(to_bin("0b0010000101100011000000010000000000000000000000000000000000010000"), encoded._get_raw_bytes())
def test_decode_raw_bytes_with_star_length(self):
container = TBCDContainerTemplate('tbcd', None)
container.add(TBCD('3', 'first', '123'))
container.add(TBCD('*', 'second', '6100000000001'))
decoded = container.decode(to_bin("0b0010000101100011000000010000000000000000000000000000000000010000"))
self.assertEquals('123', decoded.first.tbcd)
self.assertEquals('6100000000001', decoded.second.tbcd)
def test_encoded_even_value_container_returns_correct_length(self):
container = TBCDContainerTemplate('tbcd', None)
container.add(TBCD('3', 'first', '123'))
container.add(TBCD('13', 'second', '6100000000001'))
encoded = container.encode({})
self.assertEquals(8, len(encoded))
def test_encoded_odd_value_container_returns_correct_length(self):
container = TBCDContainerTemplate('tbcd', None)
container.add(TBCD('3', 'first', '456'))
container.add(TBCD('4', 'second', '1234'))
encoded = container.encode({})
self.assertEquals(4, len(encoded))
示例11: test_add_field_with_length_reference_missing
# 需要导入模块: from Rammbock.templates.containers import MessageTemplate [as 别名]
# 或者: from Rammbock.templates.containers.MessageTemplate import add [as 别名]
def test_add_field_with_length_reference_missing(self):
tmp = MessageTemplate('Dymagic', self._protocol, {})
tmp.add(UInt(2, 'len', None))
str = StructTemplate('FooType', 'foo', tmp)
self.assertRaises(AssertionError, str.add, Char('notfound', "bar"))
示例12: test_add_field_with_length_reference_to_parent
# 需要导入模块: from Rammbock.templates.containers import MessageTemplate [as 别名]
# 或者: from Rammbock.templates.containers.MessageTemplate import add [as 别名]
def test_add_field_with_length_reference_to_parent(self):
tmp = MessageTemplate('Dymagic', self._protocol, {})
tmp.add(UInt(2, 'len', None))
str = StructTemplate('FooType', 'foo', tmp)
str.add(Char('len', "bar"))