def create_member(m_ast, ctx): if m_ast[0] == 'pad': return ir.OFPadMember(length=m_ast[1]) elif m_ast[0] == 'type': return ir.OFTypeMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx), value=m_ast[3]) elif m_ast[0] == 'data': if m_ast[2] == 'length' or m_ast[ 2] == 'len': # Should be moved to parser return ir.OFLengthMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx)) elif m_ast[2] == 'actions_len': return ir.OFFieldLengthMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx), field_name='actions') if m_ast[2] == 'version': # Should be moved to parser return ir.OFVersionMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx)) elif m_ast[2] == 'key_length': return ir.OFFieldLengthMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx), field_name='key') else: return ir.OFDataMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx)) elif m_ast[0] == 'discriminator': return ir.OFDiscriminatorMember(name=m_ast[2], oftype=get_type(m_ast[1], ctx)) else: raise InputError("Dont know how to create member: %s" % m_ast[0])
def test_simple(self): version = ir.OFVersion("1.0", 1) input = fe.OFInput(filename="test.dat", wire_versions=(1,), classes=( fe.OFClass(name="OFMessage", superclass=None, members=( fe.OFDataMember(name='version', oftype='uint32_t'), fe.OFLengthMember(name='length', oftype='uint16_t') ), virtual=False, params={} ), ), enums=() ) p = ir.build_protocol(version, [ input ]) eq_(1, len(p.classes)) c = p.classes[0] eq_("OFMessage", c.name) eq_(None, c.superclass) eq_(False, c.virtual) eq_({}, c.params) eq_(2, len(c.members)) eq_(p, c.protocol) m1 = c.members[0] ok_(isinstance(m1, ir.OFDataMember)) eq_("version", m1.name) eq_("uint32_t", m1.oftype) eq_(4, m1.length) eq_(True, m1.is_fixed_length) eq_(0, m1.offset) eq_(c, m1.of_class) m2 = c.members[1] ok_(isinstance(m2, ir.OFLengthMember)) eq_("length", m2.name) eq_("uint16_t", m2.oftype) eq_(2, m2.length) eq_(True, m2.is_fixed_length) eq_(4, m2.offset) eq_(c, m2.of_class) eq_(True, c.is_fixed_length) eq_(6, c.length)