00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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
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
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
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
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
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'])