def test_validate_field_types(): msg_spec = MessageSpecification('pkg', 'Foo', [], []) known_msg_type = [] validate_field_types(msg_spec, known_msg_type) msg_spec.fields.append(Field(Type('bool'), 'foo')) validate_field_types(msg_spec, known_msg_type) msg_spec.fields.append(Field(Type('pkg/Bar'), 'bar')) with assert_raises(UnknownMessageType): validate_field_types(msg_spec, known_msg_type) known_msg_type.append(BaseType('pkg/Bar')) validate_field_types(msg_spec, known_msg_type)
def test_base_type_constructor(): primitive_types = [ 'bool', 'byte', 'char', 'float32', 'float64', 'int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'int64', 'uint64', 'string' ] for primitive_type in primitive_types: base_type = BaseType(primitive_type) assert base_type.pkg_name is None assert base_type.type == primitive_type assert base_type.string_upper_bound is None base_type = BaseType('string<=23') assert base_type.pkg_name is None assert base_type.type == 'string' assert base_type.string_upper_bound == 23 with pytest.raises(TypeError): BaseType('string<=upperbound') with pytest.raises(TypeError): BaseType('string<=0') base_type = BaseType('pkg/Msg') assert base_type.pkg_name == 'pkg' assert base_type.type == 'Msg' assert base_type.string_upper_bound is None base_type = BaseType('Msg', 'pkg') assert base_type.pkg_name == 'pkg' assert base_type.type == 'Msg' assert base_type.string_upper_bound is None with pytest.raises(InvalidResourceName): BaseType('Foo') with pytest.raises(InvalidResourceName): BaseType('pkg name/Foo') with pytest.raises(InvalidResourceName): BaseType('pkg/Foo Bar')
def _get_base_type(pkg_name, idl_path): idl_filename = os.path.basename(idl_path) msg_name, extension = os.path.splitext(idl_filename) if extension != '.msg': return None return BaseType(pkg_name + PACKAGE_NAME_MESSAGE_TYPE_SEPARATOR + msg_name)
def test_base_type_methods(): assert BaseType('bool').is_primitive_type() assert not BaseType('pkg/Foo').is_primitive_type() assert BaseType('bool') != 23 assert BaseType('pkg/Foo') == BaseType('pkg/Foo') assert BaseType('bool') != BaseType('pkg/Foo') {BaseType('bool'): None} assert str(BaseType('pkg/Foo')) == 'pkg/Foo' assert str(BaseType('bool')) == 'bool' assert str(BaseType('string<=5')) == 'string<=5'