39 from rospy_message_converter
import json_message_converter
44 from std_msgs.msg
import String
46 expected_json =
'{"data": "Hello"}' 47 message = String(data=
'Hello')
49 returned_json = json_message_converter.convert_ros_message_to_json(message)
50 self.assertEqual(returned_json, expected_json)
53 from std_msgs.msg
import String
55 expected_json =
'{"data": "Hello \\u00dcnicode"}' 56 message = String(data=
u'Hello \u00dcnicode')
58 returned_json = json_message_converter.convert_ros_message_to_json(message)
59 self.assertEqual(returned_json, expected_json)
62 from std_msgs.msg
import Header
65 now_time = rospy.Time(time())
66 expected_json1 =
'{{"stamp": {{"secs": {0}, "nsecs": {1}}}, "frame_id": "my_frame", "seq": 3}}'.format(
67 now_time.secs, now_time.nsecs
69 expected_json2 =
'{{"seq": 3, "stamp": {{"secs": {0}, "nsecs": {1}}}, "frame_id": "my_frame"}}'.format(
70 now_time.secs, now_time.nsecs
72 expected_json3 =
'{{"frame_id": "my_frame", "seq": 3, "stamp": {{"secs": {0}, "nsecs": {1}}}}}'.format(
73 now_time.secs, now_time.nsecs
75 message =
Header(stamp=now_time, frame_id=
'my_frame', seq=3)
77 returned_json = json_message_converter.convert_ros_message_to_json(message)
79 returned_json == expected_json1
or returned_json == expected_json2
or returned_json == expected_json3
83 from rospy_message_converter.msg
import Uint8ArrayTestMessage
85 input_data = [97, 98, 99, 100]
86 expected_json =
'{"data": "YWJjZA=="}' 87 message = Uint8ArrayTestMessage(data=input_data)
89 returned_json = json_message_converter.convert_ros_message_to_json(message)
90 self.assertEqual(returned_json, expected_json)
93 from rospy_message_converter.msg
import Uint8Array3TestMessage
95 input_data = [97, 98, 99]
96 expected_json =
'{"data": "YWJj"}' 97 message = Uint8Array3TestMessage(data=input_data)
99 returned_json = json_message_converter.convert_ros_message_to_json(message)
100 self.assertEqual(returned_json, expected_json)
103 from std_msgs.msg
import String
105 expected_message = String(data=
'Hello')
106 json_str =
'{"data": "Hello"}' 107 message = json_message_converter.convert_json_to_ros_message(
'std_msgs/String', json_str)
109 self.assertEqual(message, expected_message)
112 from std_msgs.msg
import String
114 expected_message = String(data=
u'Hello \u00dcnicode')
115 json_str =
'{"data": "Hello \\u00dcnicode"}' 116 message = json_message_converter.convert_json_to_ros_message(
'std_msgs/String', json_str)
118 self.assertEqual(message, expected_message)
121 from std_msgs.msg
import Header
122 from time
import time
124 now_time = rospy.Time(time())
125 expected_message =
Header(stamp=now_time, frame_id=
'my_frame', seq=12)
126 json_str =
'{{"stamp": {{"secs": {0}, "nsecs": {1}}}, "frame_id": "my_frame", "seq": 12}}'.format(
127 now_time.secs, now_time.nsecs
129 message = json_message_converter.convert_json_to_ros_message(
'std_msgs/Header', json_str)
131 self.assertEqual(message, expected_message)
134 from std_msgs.msg
import String
136 expected_message = String(data=
'')
137 json_str =
'{"data": null}' 138 message = json_message_converter.convert_json_to_ros_message(
'std_msgs/String', json_str)
140 self.assertEqual(message, expected_message)
144 ValueError, json_message_converter.convert_json_to_ros_message,
'std_msgs/String',
'{"not_data": "Hello"}' 150 Serialize and then deserialize a message. This simulates sending a message 151 between ROS nodes and makes sure that the ROS messages being tested are 152 actually serializable, and are in the same format as they would be received 153 over the network. In rospy, it is possible to assign an illegal data type 154 to a message field (for example, `message = String(data=42)`), but trying 155 to publish this message will throw `SerializationError: field data must be 156 of type str`. This method will expose such bugs. 158 from io
import BytesIO
161 message.serialize(buff)
162 result = message.__class__()
163 result.deserialize(buff.getvalue())
167 PKG =
'rospy_message_converter' 168 NAME =
'test_json_message_converter' 169 if __name__ ==
'__main__':
172 rosunit.unitrun(PKG, NAME, TestJsonMessageConverter)
def test_json_with_invalid_message_fields(self)
def test_json_with_string_unicode(self)
def test_ros_message_with_uint8_array(self)
def test_json_with_string(self)
def test_ros_message_with_header(self)
def test_json_with_header(self)
def test_ros_message_with_string(self)
def serialize_deserialize(message)
def test_ros_message_with_string_unicode(self)
def test_json_with_string_null(self)
def test_ros_message_with_3uint8_array(self)