4 from rospy_message_converter
import json_message_converter
9 from std_msgs.msg
import String
10 expected_json =
'{"data": "Hello"}' 11 message = String(data =
'Hello')
13 returned_json = json_message_converter.convert_ros_message_to_json(message)
14 self.assertEqual(returned_json, expected_json)
17 from std_msgs.msg
import Header
19 now_time = rospy.Time(time())
20 expected_json =
'{{"stamp": {{"secs": {0}, "nsecs": {1}}}, "frame_id": "my_frame", "seq": 3}}'\
21 .format(now_time.secs, now_time.nsecs)
22 message =
Header(stamp = now_time, frame_id =
'my_frame', seq = 3)
24 returned_json = json_message_converter.convert_ros_message_to_json(message)
25 self.assertEqual(returned_json, expected_json)
28 from rospy_message_converter.msg
import Uint8ArrayTestMessage
29 input_data =
"".join([chr(i)
for i
in [97, 98, 99, 100]])
30 expected_json =
'{"data": "YWJjZA=="}' 31 message = Uint8ArrayTestMessage(data=input_data)
33 returned_json = json_message_converter.convert_ros_message_to_json(message)
34 self.assertEqual(returned_json, expected_json)
37 from rospy_message_converter.msg
import Uint8Array3TestMessage
38 input_data =
"".join([chr(i)
for i
in [97, 98, 99]])
39 expected_json =
'{"data": "YWJj"}' 40 message = Uint8Array3TestMessage(data=input_data)
42 returned_json = json_message_converter.convert_ros_message_to_json(message)
43 self.assertEqual(returned_json, expected_json)
46 from std_msgs.msg
import String
47 expected_message = String(data =
'Hello')
48 json_str =
'{"data": "Hello"}' 49 message = json_message_converter.convert_json_to_ros_message(
'std_msgs/String', json_str)
51 self.assertEqual(message, expected_message)
54 from std_msgs.msg
import Header
56 now_time = rospy.Time(time())
59 frame_id =
'my_frame',
62 json_str =
'{{"stamp": {{"secs": {0}, "nsecs": {1}}}, "frame_id": "my_frame", "seq": 12}}'\
63 .format(now_time.secs, now_time.nsecs)
64 message = json_message_converter.convert_json_to_ros_message(
'std_msgs/Header', json_str)
66 self.assertEqual(message, expected_message)
69 self.assertRaises(ValueError,
70 json_message_converter.convert_json_to_ros_message,
72 '{"not_data": "Hello"}')
77 Serialize and then deserialize a message. This simulates sending a message 78 between ROS nodes and makes sure that the ROS messages being tested are 79 actually serializable, and are in the same format as they would be received 80 over the network. In rospy, it is possible to assign an illegal data type 81 to a message field (for example, `message = String(data=42)`), but trying 82 to publish this message will throw `SerializationError: field data must be 83 of type str`. This method will expose such bugs. 85 from StringIO
import StringIO
87 message.serialize(buff)
88 result = message.__class__()
89 result.deserialize(buff.getvalue())
93 PKG =
'rospy_message_converter' 94 NAME =
'test_json_message_converter' 95 if __name__ ==
'__main__':
97 rosunit.unitrun(PKG, NAME, TestJsonMessageConverter)
def test_json_with_invalid_message_fields(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_3uint8_array(self)