def test_field_constructor(): type_ = Type('bool') field = Field(type_, 'foo') assert field.type == type_ assert field.name == 'foo' assert field.default_value is None field = Field(type_, 'foo', '1') assert field.default_value with assert_raises(TypeError): Field('type', 'foo') with assert_raises(NameError): Field(type_, 'foo bar') type_ = Type('bool[2]') field = Field(type_, 'foo', '[false, true]') assert field.default_value == [False, True] type_ = Type('bool[]') field = Field(type_, 'foo', '[false, true, false]') assert field.default_value == [False, True, False] type_ = Type('bool[3]') with assert_raises(InvalidValue): Field(type_, 'foo', '[false, true]')
def test_parse_primitive_value_string_float(): for float_type in ['float32', 'float64']: value = parse_primitive_value_string(Type(float_type), '0') assert value == 0 value = parse_primitive_value_string(Type(float_type), '0.0') assert value == 0 value = parse_primitive_value_string(Type(float_type), '3.14') assert value == 3.14 with pytest.raises(InvalidValue): parse_primitive_value_string(Type(float_type), 'value')
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_parse_value_string(): with assert_raises(InvalidValue): parse_value_string(Type('bool[]'), '1') with assert_raises(InvalidValue): parse_value_string(Type('bool[2]'), '[1]') with assert_raises(InvalidValue): parse_value_string(Type('bool[<=1]'), '[1, 0]') with assert_raises(InvalidValue): parse_value_string(Type('bool[]'), '[2]') value = parse_value_string(Type('bool[]'), '[1]') assert value
def test_message_specification_constructor(): msg_spec = MessageSpecification('pkg', 'Foo', [], []) assert msg_spec.base_type.pkg_name == 'pkg' assert msg_spec.base_type.type == 'Foo' assert len(msg_spec.fields) == 0 assert len(msg_spec.constants) == 0 with assert_raises(TypeError): MessageSpecification('pkg', 'Foo', None, []) with assert_raises(TypeError): MessageSpecification('pkg', 'Foo', [], None) with assert_raises(TypeError): MessageSpecification('pkg', 'Foo', ['field'], []) with assert_raises(TypeError): MessageSpecification('pkg', 'Foo', [], ['constant']) field = Field(Type('bool'), 'foo', '1') constant = Constant('bool', 'BAR', '1') msg_spec = MessageSpecification('pkg', 'Foo', [field], [constant]) assert len(msg_spec.fields) == 1 assert msg_spec.fields[0] == field assert len(msg_spec.constants) == 1 assert msg_spec.constants[0] == constant with assert_raises(ValueError): MessageSpecification('pkg', 'Foo', [field, field], []) with assert_raises(ValueError): MessageSpecification('pkg', 'Foo', [], [constant, constant])
def test_message_specification_methods(): field = Field(Type('bool'), 'foo', '1') constant = Constant('bool', 'BAR', '1') msg_spec = MessageSpecification('pkg', 'Foo', [field], [constant]) assert msg_spec != 'spec' assert msg_spec == MessageSpecification('pkg', 'Foo', [field], [constant]) assert msg_spec != MessageSpecification('pkg', 'Bar', [], []) assert msg_spec != MessageSpecification('pkg', 'Foo', [field], []) assert msg_spec != MessageSpecification('pkg', 'Foo', [], [constant])
def test_parse_primitive_value_string_bool(): valid_bool_string_values = { 'true': True, 'TrUe': True, 'TRUE': True, '1': True, 'false': False, 'FaLsE': False, 'FALSE': False, '0': False, } for string_value, expected_value in valid_bool_string_values.items(): value = parse_primitive_value_string(Type('bool'), string_value) assert value == expected_value with pytest.raises(InvalidValue): parse_primitive_value_string(Type('bool'), '5') with pytest.raises(InvalidValue): parse_primitive_value_string(Type('bool'), 'true ')
def test_parse_primitive_value_string_integer(): integer_types = { 'byte': [8, True], 'char': [8, False], 'int8': [8, False], 'uint8': [8, True], 'int16': [16, False], 'uint16': [16, True], 'int32': [32, False], 'uint32': [32, True], 'int64': [64, False], 'uint64': [64, True], } for integer_type, (bits, is_unsigned) in integer_types.items(): lower_bound = 0 if is_unsigned else -(2**(bits - 1)) upper_bound = (2**(bits if is_unsigned else (bits - 1))) - 1 value = parse_primitive_value_string(Type(integer_type), str(lower_bound)) assert value == lower_bound value = parse_primitive_value_string(Type(integer_type), str(0)) assert value == 0 value = parse_primitive_value_string(Type(integer_type), str(upper_bound)) assert value == upper_bound with pytest.raises(InvalidValue): parse_primitive_value_string(Type(integer_type), 'value') with pytest.raises(InvalidValue): parse_primitive_value_string(Type(integer_type), str(lower_bound - 1)) with pytest.raises(InvalidValue): parse_primitive_value_string(Type(integer_type), str(upper_bound + 1))
def test_type_constructor(): type_ = Type('bool') assert type_.pkg_name is None assert type_.type == 'bool' assert type_.string_upper_bound is None assert not type_.is_array assert type_.array_size is None assert not type_.is_upper_bound type_ = Type('bool[]') assert type_.pkg_name is None assert type_.type == 'bool' assert type_.string_upper_bound is None assert type_.is_array assert type_.array_size is None assert not type_.is_upper_bound with assert_raises(TypeError): Type('bool]') type_ = Type('bool[5]') assert type_.pkg_name is None assert type_.type == 'bool' assert type_.string_upper_bound is None assert type_.is_array assert type_.array_size == 5 assert not type_.is_upper_bound type_ = Type('bool[<=5]') assert type_.pkg_name is None assert type_.type == 'bool' assert type_.string_upper_bound is None assert type_.is_array assert type_.array_size == 5 assert type_.is_upper_bound with assert_raises(TypeError): Type('bool[size]') with assert_raises(TypeError): Type('bool[0]') with assert_raises(TypeError): Type('bool[<=size]') with assert_raises(TypeError): Type('bool[<=0]')
def test_parse_primitive_value_string_string(): value = parse_primitive_value_string(Type('string'), 'foo') assert value == 'foo' value = parse_primitive_value_string(Type('string'), '"foo"') assert value == 'foo' value = parse_primitive_value_string(Type('string'), "'foo'") assert value == 'foo' value = parse_primitive_value_string(Type('string'), '"\'foo\'"') assert value == "'foo'" value = parse_primitive_value_string(Type('string'), '"foo ') assert value == '"foo ' value = parse_primitive_value_string(Type('string<=3'), 'foo') assert value == 'foo' with assert_raises(InvalidValue): parse_primitive_value_string(Type('string<=3'), 'foobar')
def test_field_methods(): assert Field(Type('bool'), 'foo') != 23 assert (Field(Type('bool'), 'foo', '1') == Field(Type('bool'), 'foo', 'true')) assert (Field(Type('bool'), 'foo', '1') != Field(Type('bool'), 'foo', 'false')) assert (Field(Type('bool'), 'foo', '1') != Field(Type('bool'), 'bar', '1')) assert (Field(Type('bool'), 'foo', '1') != Field(Type('byte'), 'foo', '1')) assert str(Field(Type('bool'), 'foo', '1')) == 'bool foo True' assert str(Field(Type('string<=5'), 'foo', 'value')) == \ "string<=5 foo 'value'"
def test_parse_value_string_primitive(): parse_value_string(Type('bool'), '1')
def test_parse_primitive_value_string_string(): value = parse_primitive_value_string(Type('string'), 'foo') assert value == 'foo' value = parse_primitive_value_string(Type('string'), '"foo"') assert value == 'foo' value = parse_primitive_value_string(Type('string'), "'foo'") assert value == 'foo' value = parse_primitive_value_string(Type('string'), '"\'foo\'"') assert value == "'foo'" value = parse_primitive_value_string(Type('string'), '"foo ') assert value == '"foo ' value = parse_primitive_value_string(Type('string<=3'), 'foo') assert value == 'foo' with pytest.raises(InvalidValue): parse_primitive_value_string(Type('string<=3'), 'foobar') with pytest.raises(InvalidValue): parse_primitive_value_string(Type('string'), r"""'foo''""") with pytest.raises(InvalidValue): parse_primitive_value_string(Type('string'), r'''"foo"bar\"baz"''') # noqa: Q001 value = parse_primitive_value_string(Type('string'), '"foo') assert value == '"foo' value = parse_primitive_value_string(Type('string'), '"foo') assert value == '"foo' value = parse_primitive_value_string(Type('string'), '\'foo') assert value == "'foo" value = parse_primitive_value_string(Type('string'), '"foo') assert value == '"foo' value = parse_primitive_value_string(Type('string'), '\'fo\'o') assert value == "'fo'o" value = parse_primitive_value_string(Type('string'), '"fo"o') assert value == '"fo"o' value = parse_primitive_value_string(Type('string'), r'''"'foo'"''') # noqa: Q001 assert value == "'foo'" value = parse_primitive_value_string(Type('string'), r"""'"foo"'""") assert value == '"foo"'
def test_parse_value_string(): with pytest.raises(InvalidValue): parse_value_string(Type('bool[]'), '1') with pytest.raises(InvalidValue): parse_value_string(Type('bool[2]'), '[1]') with pytest.raises(InvalidValue): parse_value_string(Type('bool[<=1]'), '[1, 0]') with pytest.raises(InvalidValue): parse_value_string(Type('bool[]'), '[2]') value = parse_value_string(Type('bool[]'), '[1]') assert value value = parse_value_string(Type('string[]'), "['foo', 'bar']") assert value == ['foo', 'bar'] with pytest.raises(InvalidValue): parse_value_string(Type('string[<=2]'), '[foo, bar, baz]') with pytest.raises(InvalidValue): parse_value_string(Type('string[<=2]'), """["foo", 'ba"r', "ba,z"]""") with pytest.raises(ValueError): parse_value_string(Type('string[<=3]'), """[,"foo", 'ba"r', "ba,z"]""") with pytest.raises(ValueError): parse_value_string(Type('string[<=3]'), """["foo", ,'ba"r', "ba,z"]""") parse_value_string(Type('string[<=2]'), """["fo'o\\\", bar", baz]""") assert value
def test_parse_value_string_not_implemented(): with pytest.raises(NotImplementedError): parse_value_string(Type('pkg/Foo[]'), '')
def test_parse_primitive_value_string_invalid(): with pytest.raises(ValueError): parse_primitive_value_string(Type('pkg/Foo'), '') with pytest.raises(ValueError): parse_primitive_value_string(Type('bool[]'), '')
def generate_dds_idl(generator_arguments_file, subfolders, extension_module_name): args = read_generator_arguments(generator_arguments_file) template_file = os.path.join(args['template_dir'], 'msg.idl.em') assert os.path.exists(template_file), \ "The template '%s' does not exist" % template_file # look for extensions for the default functions functions = { 'get_include_directives': get_include_directives, 'get_post_struct_lines': get_post_struct_lines, 'msg_type_to_idl': msg_type_to_idl, } if extension_module_name is not None: pkg = __import__(extension_module_name) module_name = extension_module_name.rsplit('.', 1)[1] if hasattr(pkg, module_name): module = getattr(pkg, module_name) for function_name in functions.keys(): if hasattr(module, function_name): functions[function_name] = \ getattr(module, function_name) pkg_name = args['package_name'] latest_target_timestamp = get_newest_modification_time(args['target_dependencies']) for ros_interface_file in args['ros_interface_files']: subfolder = os.path.basename(os.path.dirname(ros_interface_file)) extension = os.path.splitext(ros_interface_file)[1] output_path = os.path.join(args['output_dir'], subfolder) for sub in subfolders: output_path = os.path.join(output_path, sub) if extension == '.msg': spec = parse_message_file(pkg_name, ros_interface_file) generated_file = os.path.join(output_path, '%s_.idl' % spec.base_type.type) data = {'spec': spec, 'subfolder': subfolder, 'subfolders': subfolders} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) elif extension == '.srv': srv_spec = parse_service_file(pkg_name, ros_interface_file) request_fields = [ Field( Type('uint64', context_package_name=pkg_name), 'client_guid_0', default_value_string=None), Field( Type('uint64', context_package_name=pkg_name), 'client_guid_1', default_value_string=None), Field( Type('int64', context_package_name=pkg_name), 'sequence_number', default_value_string=None), Field( Type('%s_Request' % srv_spec.srv_name, context_package_name=pkg_name), 'request', default_value_string=None) ] response_fields = [ Field( Type('uint64', context_package_name=pkg_name), 'client_guid_0', default_value_string=None), Field( Type('uint64', context_package_name=pkg_name), 'client_guid_1', default_value_string=None), Field( Type('int64', context_package_name=pkg_name), 'sequence_number', default_value_string=None), Field( Type('%s_Response' % srv_spec.srv_name, context_package_name=pkg_name), 'response', default_value_string=None) ] constants = [] sample_spec_request = MessageSpecification( srv_spec.pkg_name, 'Sample_%s_Request' % srv_spec.srv_name, request_fields, constants) sample_spec_response = MessageSpecification( srv_spec.pkg_name, 'Sample_%s_Response' % srv_spec.srv_name, response_fields, constants) generated_files = [ (sample_spec_request, os.path.join( output_path, '%s_.idl' % sample_spec_request.base_type.type)), (sample_spec_response, os.path.join( output_path, '%s_.idl' % sample_spec_response.base_type.type)), ] for spec, generated_file in generated_files: data = {'spec': spec, 'subfolder': 'srv', 'subfolders': subfolders} data.update(functions) expand_template( template_file, data, generated_file, minimum_timestamp=latest_target_timestamp) return 0
def test_type_methods(): assert Type('bool[5]') != 23 assert Type('pkg/Foo') == Type('pkg/Foo') assert Type('pkg/Foo[]') == Type('pkg/Foo[]') assert Type('pkg/Foo[5]') == Type('pkg/Foo[5]') assert Type('pkg/Foo[<=5]') == Type('pkg/Foo[<=5]') assert Type('bool') != Type('pkg/Foo') assert Type('pkg/Foo[]') != Type('pkg/Foo[5]') assert Type('pkg/Foo[5]') != Type('pkg/Foo[<=5]') assert Type('pkg/Foo[<=5]') != Type('pkg/Foo[<=23]') {Type('pkg/Foo[5]'): None} assert str(Type('pkg/Foo[]')) == 'pkg/Foo[]' assert str(Type('pkg/Foo[5]')) == 'pkg/Foo[5]' assert str(Type('pkg/Foo[<=5]')) == 'pkg/Foo[<=5]'
def test_parse_value_string_not_implemented(): with assert_raises(NotImplementedError): parse_value_string(Type('string[]'), '[foo, bar]') with assert_raises(NotImplementedError): parse_value_string(Type('pkg/Foo[]'), '')