test_roslib_message.py
Go to the documentation of this file.
1 # Software License Agreement (BSD License)
2 #
3 # Copyright (c) 2009, Willow Garage, Inc.
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 #
10 # * Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # * Redistributions in binary form must reproduce the above
13 # copyright notice, this list of conditions and the following
14 # disclaimer in the documentation and/or other materials provided
15 # with the distribution.
16 # * Neither the name of Willow Garage, Inc. nor the names of its
17 # contributors may be used to endorse or promote products derived
18 # from this software without specific prior written permission.
19 #
20 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 # POSSIBILITY OF SUCH DAMAGE.
32 
33 from __future__ import print_function
34 
35 import os
36 import sys
37 import time
38 import unittest
39 import traceback
40 
41 import yaml
42 
43 import roslib.message
44 import genpy
45 from genpy import Time, Duration
46 
47 # Not much to test, just tripwires
48 
49 class MessageTest(unittest.TestCase):
50 
52  # #2128: test that check_types works with a Header
53  # #message. This is a weird case because Header has an aliased
54  # #type and is the only ROS type for which that is true
55  from test_roslib_comm.msg import HeaderTest
56  x = HeaderTest()
57  x._check_types()
58 
60  from genpy.message import Message, strify_message, fill_message_args
61  def roundtrip(m):
62  yaml_text = strify_message(m)
63  print(yaml_text)
64  loaded = yaml.load(yaml_text)
65  print("loaded", loaded)
66  new_inst = m.__class__()
67  if loaded is not None:
68  fill_message_args(new_inst, [loaded])
69  else:
70  fill_message_args(new_inst, [])
71  return new_inst
72 
73  # The following tests have not been ported to genpy yet
74 
75  # test array of Messages field. We can't use M4 or M5 because fill_message_args has to instantiate the embedded type
76  from test_roslib_comm.msg import ArrayOfMsgs
77  from std_msgs.msg import String, Time, MultiArrayLayout, MultiArrayDimension
78  dims1 = [MultiArrayDimension(*args) for args in [('', 0, 0), ('x', 1, 2), ('y of z', 3, 4)]]
79  dims2 = [MultiArrayDimension('hello world', 91280, 1983274)]
80  times = [Time(genpy.Time(*args)) for args in [(0,), (12345, 6789), (1, 1)]]
81  val = ArrayOfMsgs([String(''), String('foo'), String('bar of soap')],
82  times,
83  [MultiArrayLayout(dims1, 0), MultiArrayLayout(dims2, 12354)],
84  )
85  self.assertEquals(val, roundtrip(val))
86 
87 
89  from roslib.message import get_message_class
90 
91  try:
92  self.assertEquals(None, get_message_class('String'))
93  self.fail("should have thrown ValueError")
94  except ValueError: pass
95  # non-existent package
96  self.assertEquals(None, get_message_class('fake/Fake'))
97  # non-existent message
98  self.assertEquals(None, get_message_class('roslib/Fake'))
99  # package with no messages
100  self.assertEquals(None, get_message_class('genmsg_cpp/Fake'))
101 
102  import rosgraph_msgs.msg
103  import std_msgs.msg
104  self.assertEquals(std_msgs.msg.Header, get_message_class('Header'))
105  self.assertEquals(std_msgs.msg.Header, get_message_class('std_msgs/Header'))
106  self.assertEquals(rosgraph_msgs.msg.Log, get_message_class('rosgraph_msgs/Log'))
107 
109  from roslib.message import get_service_class
110 
111  # non-existent package
112  self.assertEquals(None, get_service_class('fake/Fake'))
113  # non-existent message
114  self.assertEquals(None, get_service_class('roslib/Fake'))
115  # package with no messages
116  self.assertEquals(None, get_service_class('genmsg_cpp/Fake'))
117 
118  import std_srvs.srv
119  self.assertEquals(std_srvs.srv.Empty, get_service_class('std_srvs/Empty'))
120 


test_roslib_comm
Author(s): Jeremy Leibs, Ken Conley, Dirk Thomas
autogenerated on Mon Nov 2 2020 03:52:23