Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add WString support #372

Merged
merged 7 commits into from
May 2, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 13 additions & 9 deletions rosidl_adapter/test/test_base_type.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,25 @@ def test_base_type_constructor():
'uint32',
'int64',
'uint64',
'string']
'string',
'wstring',
]
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
for string_type in ('string', 'wstring'):
base_type = BaseType('%s<=23' % string_type)
assert base_type.pkg_name is None
assert base_type.type == string_type
assert base_type.string_upper_bound == 23

with pytest.raises(TypeError):
BaseType('string<=upperbound')
with pytest.raises(TypeError):
BaseType('string<=0')
with pytest.raises(TypeError):
BaseType('%s<=upperbound' % string_type)
with pytest.raises(TypeError):
BaseType('%s<=0' % string_type)

base_type = BaseType('pkg/Msg')
assert base_type.pkg_name == 'pkg'
Expand Down Expand Up @@ -84,3 +87,4 @@ def test_base_type_methods():
assert str(BaseType('pkg/Foo')) == 'pkg/Foo'
assert str(BaseType('bool')) == 'bool'
assert str(BaseType('string<=5')) == 'string<=5'
assert str(BaseType('wstring<=5')) == 'wstring<=5'
1 change: 1 addition & 0 deletions rosidl_adapter/test/test_constant.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,4 @@ def test_constant_methods():
assert str(Constant('bool', 'FOO', '1')) == 'bool FOO=True'

assert str(Constant('string', 'FOO', 'foo')) == "string FOO='foo'"
assert str(Constant('wstring', 'FOO', 'foo')) == "wstring FOO='foo'"
70 changes: 70 additions & 0 deletions rosidl_adapter/test/test_parse_primitive_value_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,76 @@ def test_parse_primitive_value_string_string():
assert value == '"foo"'


def test_parse_primitive_value_wstring_string():
value = parse_primitive_value_string(
Type('wstring'), 'foo')
assert value == 'foo'

value = parse_primitive_value_string(
Type('wstring'), '"foo"')
assert value == 'foo'

value = parse_primitive_value_string(
Type('wstring'), "'foo'")
assert value == 'foo'

value = parse_primitive_value_string(
Type('wstring'), '"\'foo\'"')
assert value == "'foo'"

value = parse_primitive_value_string(
Type('wstring'), '"foo ')
assert value == '"foo '

value = parse_primitive_value_string(
Type('wstring<=3'), 'foo')
assert value == 'foo'

with pytest.raises(InvalidValue):
parse_primitive_value_string(
Type('wstring<=3'), 'foobar')

with pytest.raises(InvalidValue):
parse_primitive_value_string(
Type('wstring'), r"""'foo''""")

with pytest.raises(InvalidValue):
parse_primitive_value_string(
Type('wstring'), r'''"foo"bar\"baz"''') # noqa: Q001

value = parse_primitive_value_string(
Type('wstring'), '"foo')
assert value == '"foo'

value = parse_primitive_value_string(
Type('wstring'), '"foo')
assert value == '"foo'

value = parse_primitive_value_string(
Type('wstring'), "'foo")
assert value == "'foo"

value = parse_primitive_value_string(
Type('wstring'), '"foo')
assert value == '"foo'

value = parse_primitive_value_string(
Type('wstring'), "'fo'o")
assert value == "'fo'o"

value = parse_primitive_value_string(
Type('wstring'), '"fo"o')
assert value == '"fo"o'

value = parse_primitive_value_string(
Type('wstring'), r'''"'foo'"''') # noqa: Q001
assert value == "'foo'"

value = parse_primitive_value_string(
Type('wstring'), r"""'"foo"'""")
assert value == '"foo"'


def test_parse_primitive_value_string_unknown():
class CustomType(Type):

Expand Down
1 change: 1 addition & 0 deletions rosidl_generator_c/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ if(BUILD_TESTING)
"msg/Uint8.msg"
"msg/Various.msg"
"msg/Wire.msg"
"msg/WStrings.msg"
"srv/AddTwoInts.srv"
)

Expand Down
8 changes: 8 additions & 0 deletions rosidl_generator_c/msg/WStrings.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
wstring empty_wstring
wstring def_wstring "Hello world!"
wstring def_wstring2 "Hello'world!"
wstring def_wstring3 'Hello"world!'
wstring def_wstring4 'Hello\'world!'
wstring def_wstring5 "Hello\"world!"
wstring<=22 ub_wstring
wstring<=22 ub_def_wstring "Upper bounded string."
37 changes: 36 additions & 1 deletion rosidl_generator_c/test/test_interfaces.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

#include "rosidl_generator_c/primitives_sequence_functions.h"
#include "rosidl_generator_c/string_functions.h"
#include "rosidl_generator_c/u16string_functions.h"

#include "rosidl_generator_c/msg/bool.h"
#include "rosidl_generator_c/msg/bounded_array_nested.h"
Expand Down Expand Up @@ -56,13 +57,16 @@
#include "rosidl_generator_c/msg/uint8.h"
#include "rosidl_generator_c/msg/various.h"
#include "rosidl_generator_c/msg/wire.h"
#include "rosidl_generator_c/msg/w_strings.h"

#define TEST_STRING \
"Deep into that darkness peering, long I stood there wondering, fearing"
#define TEST_STRING2 \
"The quick brown fox jumps over the lazy dog."
#define TEST_STRING3 \
"PACK MY BOX WITH FIVE DOZEN LIQUOR JUGS."
#define TEST_WSTRING \
u"Deep into that darkness peering, long I stood there wondering, fearing \u2122"
#define ARRAY_SIZE 7

#define STRINGIFY(x) _STRINGIFY(x)
Expand Down Expand Up @@ -333,8 +337,39 @@ int test_strings(void)
return 0;
}


/**
* Test message with different wstring types.
*/
int test_wstrings(void)
{
bool res = false;
rosidl_generator_c__msg__WStrings * wstrings = NULL;

wstrings = rosidl_generator_c__msg__WStrings__create();
EXPECT_NE(wstrings, NULL);

res = rosidl_generator_c__U16String__assign(&wstrings->empty_wstring, TEST_WSTRING);
EXPECT_EQ(true, res);
// EXPECT_EQ(0, strcmp(wstrings->empty_wstring.data, TEST_WSTRING));
dirk-thomas marked this conversation as resolved.
Show resolved Hide resolved
// EXPECT_EQ(0, strcmp(wstrings->def_wstring.data, "Hello world!"));
// EXPECT_EQ(0, strcmp(wstrings->def_wstring2.data, "Hello'world!"));
// EXPECT_EQ(0, strcmp(wstrings->def_wstring3.data, "Hello\"world!"));
// EXPECT_EQ(0, strcmp(wstrings->def_wstring4.data, "Hello'world!"));
// EXPECT_EQ(0, strcmp(wstrings->def_wstring5.data, "Hello\"world!"));
// since upper-bound checking is not implemented yet, we restrict the string copying
// TODO(mikaelarguedas) Test string length properly instead of cheating copy
dirk-thomas marked this conversation as resolved.
Show resolved Hide resolved
// res = rosidl_generator_c__String__assign(&wstrings->ub_wstring, TEST_WSTRING);
res = rosidl_generator_c__U16String__assignn(&wstrings->ub_wstring, TEST_WSTRING, 24);
EXPECT_EQ(true, res);
// EXPECT_EQ(0, strcmp(wstrings->ub_wstring.data, "Deep into that darknes"));
// EXPECT_EQ(0, strcmp(wstrings->ub_def_wstring.data, "Upper bounded string."));

rosidl_generator_c__msg__WStrings__destroy(wstrings);

return 0;
}

/**
* Test message with different string array types.
*/
int test_string_arrays(void)
Expand Down
4 changes: 3 additions & 1 deletion rosidl_generator_cpp/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ if(BUILD_TESTING)
"msg/UnboundedArrayUnbounded.msg"

"msg/Various.msg"
)

"msg/WString.msg"
)

set(srv_files
"srv/ComplexTypeZeroFill.srv"
Expand Down
1 change: 1 addition & 0 deletions rosidl_generator_cpp/msg/WString.msg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
wstring wstring_value
19 changes: 19 additions & 0 deletions rosidl_generator_cpp/test/test_interfaces.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@
#include "rosidl_generator_cpp/msg/unbounded_array_static.hpp"
#include "rosidl_generator_cpp/msg/unbounded_array_unbounded.hpp"

#include "rosidl_generator_cpp/msg/w_string.hpp"

#define PRIMITIVES_ARRAY_SIZE 10
#define BOUNDED_STRING_LENGTH 10
#define SUBMESSAGE_ARRAY_SIZE 3
Expand Down Expand Up @@ -136,6 +138,10 @@ TEST(Test_rosidl_generator_traits, has_fixed_size) {
!rosidl_generator_traits::has_fixed_size<
rosidl_generator_cpp::msg::UnboundedArrayUnbounded>::value,
"UnboundedArrayUnbounded::has_fixed_size is true");

static_assert(
!rosidl_generator_traits::has_fixed_size<rosidl_generator_cpp::msg::WString>::value,
"WString::has_fixed_size is true");
}

TEST(Test_rosidl_generator_traits, has_bounded_size) {
Expand Down Expand Up @@ -224,6 +230,10 @@ TEST(Test_rosidl_generator_traits, has_bounded_size) {
!rosidl_generator_traits::has_bounded_size<
rosidl_generator_cpp::msg::UnboundedArrayUnbounded>::value,
"UnboundedArrayUnbounded::has_bounded_size is true");

static_assert(
!rosidl_generator_traits::has_bounded_size<rosidl_generator_cpp::msg::WString>::value,
"WString::has_bounded_size is true");
}

#define TEST_PRIMITIVE_FIELD_ASSIGNMENT(Message, FieldName, InitialValue, FinalValue) \
Expand All @@ -238,6 +248,10 @@ TEST(Test_rosidl_generator_traits, has_bounded_size) {
Message.FieldName = FinalValue; \
ASSERT_STREQ(FinalValue, Message.FieldName.c_str());

#define TEST_WSTRING_FIELD_ASSIGNMENT(Message, FieldName, InitialValue, FinalValue) \
Message.FieldName = InitialValue; \
Message.FieldName = FinalValue;

void test_message_primitives_static(rosidl_generator_cpp::msg::PrimitivesStatic message)
{
// workaround for https://github.com/google/googletest/issues/322
Expand Down Expand Up @@ -601,6 +615,11 @@ TEST(Test_messages, Test_string) {
TEST_STRING_FIELD_ASSIGNMENT(message, string_value, "", "Deep into")
}

TEST(Test_messages, Test_wstring) {
rosidl_generator_cpp::msg::WString message;
TEST_WSTRING_FIELD_ASSIGNMENT(message, wstring_value, u"", u"wstring_value_\u2122")
}

#define TEST_STATIC_ARRAY_STRING( \
Message, FieldName, PrimitiveType, ArraySize, MinVal, MaxVal, MinLength, MaxLength) \
std::array<PrimitiveType, ArraySize> pattern_ ## FieldName; \
Expand Down
1 change: 1 addition & 0 deletions rosidl_parser/test/msg/MyMessage.idl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module rosidl_parser {
const float FLOAT_CONSTANT = 1.25;
const boolean BOOLEAN_CONSTANT = TRUE;
const string STRING_CONSTANT = "string_value";
const wstring WSTRING_CONSTANT = "wstring_value_\u2122";
};

@verbatim ( language="comment", text="Documentation of MyMessage." )
Expand Down
6 changes: 5 additions & 1 deletion rosidl_parser/test/test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def test_message_parser_structure(message_idl_file):
assert len(messages) == 1

constants = messages[0].constants
assert len(constants) == 5
assert len(constants) == 6

assert constants[0].name == 'SHORT_CONSTANT'
assert isinstance(constants[0].type, BasicType)
Expand All @@ -88,6 +88,10 @@ def test_message_parser_structure(message_idl_file):
assert isinstance(constants[4].type, BoundedString)
assert constants[4].value == 'string_value'

assert constants[5].name == 'WSTRING_CONSTANT'
assert isinstance(constants[5].type, BoundedWString)
assert constants[5].value == 'wstring_value_\\u2122'

structure = messages[0].structure
assert structure.namespaced_type.namespaces == ['rosidl_parser', 'msg']
assert structure.namespaced_type.name == 'MyMessage'
Expand Down