$search
00001 #!/usr/bin/env python 00002 # Software License Agreement (BSD License) 00003 # 00004 # Copyright (c) 2008, Willow Garage, Inc. 00005 # All rights reserved. 00006 # 00007 # Redistribution and use in source and binary forms, with or without 00008 # modification, are permitted provided that the following conditions 00009 # are met: 00010 # 00011 # * Redistributions of source code must retain the above copyright 00012 # notice, this list of conditions and the following disclaimer. 00013 # * Redistributions in binary form must reproduce the above 00014 # copyright notice, this list of conditions and the following 00015 # disclaimer in the documentation and/or other materials provided 00016 # with the distribution. 00017 # * Neither the name of Willow Garage, Inc. nor the names of its 00018 # contributors may be used to endorse or promote products derived 00019 # from this software without specific prior written permission. 00020 # 00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00032 # POSSIBILITY OF SUCH DAMAGE. 00033 # 00034 # Revision $Id$ 00035 00036 from __future__ import with_statement 00037 NAME = 'test_rosmsg' 00038 import roslib; roslib.load_manifest('test_rosmsg') 00039 00040 import os 00041 import sys 00042 import unittest 00043 import cStringIO 00044 import time 00045 00046 import rostest 00047 00048 import roslib.packages 00049 import rosmsg 00050 00051 from subprocess import Popen, PIPE, check_call, call 00052 00053 class TestRosmsg(unittest.TestCase): 00054 00055 def setUp(self): 00056 pass 00057 00058 def test_fullusage(self): 00059 text = rosmsg.fullusage('rosmsg') 00060 self.assert_("Commands" in text) 00061 cmds = ['show', 'users', 'md5', 'package', 'packages'] 00062 for c in cmds: 00063 self.assert_(c in text) 00064 00065 def test_get_msg_text(self): 00066 d = roslib.packages.get_pkg_dir('test_rosmsg') 00067 msg_d = os.path.join(d, 'msg') 00068 for t in ['RosmsgA', 'RosmsgB']: 00069 with open(os.path.join(msg_d, '%s.msg'%t), 'r') as f: 00070 text = f.read() 00071 type_ = 'test_rosmsg/'+t 00072 self.assertEquals(text, rosmsg.get_msg_text(type_, raw=False)) 00073 self.assertEquals(text, rosmsg.get_msg_text(type_, raw=True)) 00074 00075 # test recursive types 00076 t = 'RosmsgC' 00077 with open(os.path.join(msg_d, '%s.msg'%t), 'r') as f: 00078 text = f.read() 00079 type_ = 'test_rosmsg/'+t 00080 self.assertEquals(text, rosmsg.get_msg_text(type_, raw=True)) 00081 self.assertEquals("""std_msgs/String s1 00082 string data 00083 std_msgs/String s2 00084 string data""", rosmsg.get_msg_text(type_, raw=False).strip()) 00085 00086 def test_rosmsg_list_packages(self): 00087 try: 00088 l = rosmsg.rosmsg_list_packages('.foo') 00089 self.fail("should have failed on invalid mode") 00090 except ValueError: pass 00091 00092 # test msgs 00093 l = rosmsg.rosmsg_list_packages('.msg') 00094 for p in ['test_rosmsg', 'test_ros', 'std_msgs']: 00095 self.assert_(p in l, "%s not in %s"%(p, l)) 00096 for p in ['rospy', 'std_srvs']: 00097 self.assert_(p not in l) 00098 00099 # test srvs 00100 l = rosmsg.rosmsg_list_packages('.srv') 00101 for p in ['test_rosmsg', 'test_ros', 'std_srvs']: 00102 self.assert_(p in l, "%s not in %s"%(p, l)) 00103 for p in ['roslib', 'rospy', 'std_msgs']: 00104 self.assert_(p not in l) 00105 00106 def test_rosmsg_list_package(self): 00107 try: 00108 l = rosmsg.rosmsg_list_package('.foo', 'test_rosmsg') 00109 self.fail("should have failed on invalid mode") 00110 except ValueError: pass 00111 00112 # test msgs 00113 l = rosmsg.rosmsg_list_package('.msg', 'rospy') 00114 self.assertEquals([], l) 00115 l = rosmsg.rosmsg_list_package('.msg', 'test_rosmsg') 00116 self.assertEquals(set(['test_rosmsg/RosmsgA', 00117 'test_rosmsg/RosmsgB', 00118 'test_rosmsg/RosmsgC', 00119 ]), set(l)) 00120 00121 l = rosmsg.rosmsg_list_package('.srv', 'rospy') 00122 self.assertEquals([], l) 00123 l = rosmsg.rosmsg_list_package('.srv', 'test_rosmsg') 00124 self.assertEquals(set(['test_rosmsg/RossrvA', 00125 'test_rosmsg/RossrvB', 00126 ]), set(l)) 00127 00128 def test_get_srv_text(self): 00129 d = roslib.packages.get_pkg_dir('test_rosmsg') 00130 srv_d = os.path.join(d, 'srv') 00131 with open(os.path.join(srv_d, 'RossrvA.srv'), 'r') as f: 00132 text = f.read() 00133 self.assertEquals(text, rosmsg.get_srv_text('test_rosmsg/RossrvA', raw=False)) 00134 self.assertEquals(text, rosmsg.get_srv_text('test_rosmsg/RossrvA', raw=True)) 00135 00136 # std_msgs/empty / std_msgs/empty 00137 with open(os.path.join(srv_d, 'RossrvB.srv'), 'r') as f: 00138 text = f.read() 00139 self.assertEquals(text, rosmsg.get_srv_text('test_rosmsg/RossrvB', raw=False)) 00140 self.assertEquals(text, rosmsg.get_srv_text('test_rosmsg/RossrvB', raw=True)) 00141 00142 00143 00144 if __name__ == '__main__': 00145 rostest.unitrun('test_rosmsg', NAME, TestRosmsg, sys.argv, coverage_packages=['rosmsg'])