This commit is contained in:
Ivan
2022-04-05 11:42:28 +03:00
commit 6dc0eb0fcf
5565 changed files with 1200500 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
cmake_minimum_required(VERSION 2.8.3)
project(test_roslib_comm)
find_package(catkin REQUIRED COMPONENTS genmsg rosgraph_msgs std_msgs)
if(CATKIN_ENABLE_TESTING)
add_message_files(DIRECTORY msg
FILES
ArrayOfMsgs.msg
FieldNameChange1.msg
FieldNameChange2.msg
FillEmbedTime.msg
FillSimple.msg
HeaderTest.msg
SameSubMsg1.msg
SameSubMsg2.msg
SameSubMsg3.msg
TypeNameChange1.msg
TypeNameChange2.msg
TypeNameChangeArray1.msg
TypeNameChangeArray2.msg
TypeNameChangeComplex1.msg
TypeNameChangeComplex2.msg
NOINSTALL
)
generate_messages(DEPENDENCIES rosgraph_msgs std_msgs)
endif()
catkin_package()
if(CATKIN_ENABLE_TESTING)
catkin_add_nosetests(test)
endif()

View File

@@ -0,0 +1,3 @@
std_msgs/String[] strings
std_msgs/Time[] times
std_msgs/MultiArrayLayout[] muls

View File

@@ -0,0 +1 @@
SameSubMsg1 a

View File

@@ -0,0 +1 @@
SameSubMsg1 b

View File

@@ -0,0 +1,5 @@
time t
duration d
std_msgs/String str_msg
std_msgs/String[] str_msg_array
int32 i32

View File

@@ -0,0 +1,4 @@
int32 i32
string str
int32[] i32_array
bool b

View File

@@ -0,0 +1,2 @@
Header header
int32 i32

View File

@@ -0,0 +1,5 @@
int32 a
float32 b
string c
uint64[10] d
float64[] e

View File

@@ -0,0 +1,5 @@
int32 a
float32 b
string c
uint64[10] d
float64[] e

View File

@@ -0,0 +1,11 @@
# Lots of comments
int32 a #And more comments
float32 b
string c
# And also some white space
uint64[10] d
float64[] e

View File

@@ -0,0 +1 @@
SameSubMsg1 a

View File

@@ -0,0 +1 @@
SameSubMsg2 a

View File

@@ -0,0 +1 @@
SameSubMsg1[] a

View File

@@ -0,0 +1 @@
SameSubMsg2[] a

View File

@@ -0,0 +1,2 @@
SameSubMsg1[] a
SameSubMsg2[10] b

View File

@@ -0,0 +1,2 @@
SameSubMsg2[] a
SameSubMsg3[10] b

View File

@@ -0,0 +1,22 @@
<package>
<name>test_roslib_comm</name>
<version>1.12.14</version>
<description>
Unit tests verifying that roslib is operating as expected.
</description>
<maintainer email="dthomas@osrfoundation.org">Dirk Thomas</maintainer>
<license>BSD</license>
<url>http://ros.org/wiki/roslib</url>
<author>Jeremy Leibs</author>
<author>Ken Conley</author>
<buildtool_depend version_gte="0.5.68">catkin</buildtool_depend>
<build_depend>genmsg</build_depend>
<build_depend>rosgraph_msgs</build_depend>
<build_depend>std_msgs</build_depend>
<test_depend>genpy</test_depend>
<test_depend>roslib</test_depend>
</package>

View File

@@ -0,0 +1 @@
int32 x=1

View File

@@ -0,0 +1 @@
int32 x=2

View File

@@ -0,0 +1 @@
int32 x=10

View File

@@ -0,0 +1,3 @@
# protect against aliasing
string s=u
int32 a

View File

@@ -0,0 +1,2 @@
# protect against aliasing
string s=uint32 a

View File

@@ -0,0 +1,3 @@
int32 a
int32 b
int32 c

View File

@@ -0,0 +1,2 @@
int32 a
int32 b

View File

@@ -0,0 +1,4 @@
int32 a
int32 b
int32 c
int32 d

View File

@@ -0,0 +1,3 @@
int32 a1
int32 b
int32 c

View File

@@ -0,0 +1,3 @@
uint32 a
int32 b
int32 c

View File

@@ -0,0 +1 @@
int32 x=123456

View File

@@ -0,0 +1 @@
int32 x = 123456

View File

@@ -0,0 +1,3 @@
#x is a value
int32 x=123456 #a constant
#i'm done

View File

@@ -0,0 +1 @@
string foo="#bar

View File

@@ -0,0 +1,3 @@
#blah
string foo ="#bar
#b

View File

@@ -0,0 +1,6 @@
int32 x=123
int32 y=234
int32 z=345
float32 a=1.0
float32 b=2.0
float32 pi=3.14159

View File

@@ -0,0 +1,6 @@
int32 x = 123
int32 y = 234
int32 z = 345
float32 a= 1.0
float32 b= 2.0
float32 pi = 3.14159

View File

@@ -0,0 +1 @@
acffd30cd6b6de30f120938c17c593fb log

View File

@@ -0,0 +1 @@
rosgraph_msgs/Log log

View File

@@ -0,0 +1,3 @@
#commented
rosgraph_msgs/Log log
#another comment

View File

@@ -0,0 +1,4 @@
#comment. note the stripped package name
Log log
#comment

View File

@@ -0,0 +1 @@
### Just a comment

View File

@@ -0,0 +1,6 @@
# A comment
# With multiple lines

View File

@@ -0,0 +1 @@
int32 field

View File

@@ -0,0 +1,2 @@
#comment before
int32 field

View File

@@ -0,0 +1,2 @@
int32 field
#comment after

View File

@@ -0,0 +1 @@
int32 field

View File

@@ -0,0 +1,5 @@
int32 field #field does something
#the end

View File

@@ -0,0 +1,7 @@
int32 a
acffd30cd6b6de30f120938c17c593fb log
string s
a9c97c1d230cfc112e270351a944ee47 time
int32 b
duration d
time t

View File

@@ -0,0 +1,14 @@
# comment about a
int32 a
# comment about log
rosgraph_msgs/Log log
# comment about s
string s
# comment about time
rosgraph_msgs/Clock time
# comment about b
int32 b
# comment about d
duration d
# comment about t
time t

View File

@@ -0,0 +1,7 @@
int32 a
Log log
string s
Clock time
int32 b
duration d
time t

View File

@@ -0,0 +1,16 @@
# short comment about a
int32 a #a
# short comment about log
Log log #log
#
# short comment about s
string s #s
# short comment about time
Clock time #time
# short comment about b
int32 b #b
# short comment about d
duration d #d
# short comment about t
time t # t

View File

@@ -0,0 +1,8 @@
int32 a
rosgraph_msgs/Log log
string s
rosgraph_msgs/Clock time
int32 b
duration d
time t

View File

@@ -0,0 +1 @@
int32 x=123456

View File

@@ -0,0 +1 @@
int32 x = 123456

View File

@@ -0,0 +1,3 @@
#x is a value
int32 x=123456 #a constant
#i'm done

View File

@@ -0,0 +1 @@
string foo="#bar

View File

@@ -0,0 +1,3 @@
#blah
string foo ="#bar
#b

View File

@@ -0,0 +1,6 @@
int32 x=123
int32 y=234
int32 z=345
float32 a=1.0
float32 b=2.0
float32 pi=3.14159

View File

@@ -0,0 +1,6 @@
int32 x = 123
int32 y = 234
int32 z = 345
float32 a= 1.0
float32 b= 2.0
float32 pi = 3.14159

View File

@@ -0,0 +1 @@
rosgraph_msgs/Log log

View File

@@ -0,0 +1 @@
Log log

View File

@@ -0,0 +1,3 @@
#commented
rosgraph_msgs/Log log
#another comment

View File

@@ -0,0 +1,4 @@
#comment. note the stripped package name
Log log
#comment

View File

@@ -0,0 +1 @@

View File

@@ -0,0 +1 @@
### Just a comment

View File

@@ -0,0 +1,6 @@
# A comment
# With multiple lines

View File

@@ -0,0 +1 @@
int32 field

View File

@@ -0,0 +1,2 @@
#comment before
int32 field

View File

@@ -0,0 +1,2 @@
int32 field
#comment after

View File

@@ -0,0 +1 @@
int32 field

View File

@@ -0,0 +1,5 @@
int32 field #field does something
#the end

View File

@@ -0,0 +1,7 @@
int32 a
rosgraph_msgs/Log log
string s
rosgraph_msgs/Clock time
int32 b
duration d
time t

View File

@@ -0,0 +1,14 @@
# comment about a
int32 a
# comment about log
rosgraph_msgs/Log log
# comment about s
string s
# comment about time
rosgraph_msgs/Clock time
# comment about b
int32 b
# comment about d
duration d
# comment about t
time t

View File

@@ -0,0 +1,7 @@
int32 a
Log log
string s
Clock time
int32 b
duration d
time t

View File

@@ -0,0 +1,16 @@
# short comment about a
int32 a #a
# short comment about log
Log log #log
#
# short comment about s
string s #s
# short comment about time
Clock time #time
# short comment about b
int32 b #b
# short comment about d
duration d #d
# short comment about t
time t # t

View File

@@ -0,0 +1,8 @@
int32 a
rosgraph_msgs/Log log
string s
rosgraph_msgs/Clock time
int32 b
duration d
time t

View File

@@ -0,0 +1,51 @@
#!/usr/bin/env python
# Software License Agreement (BSD License)
#
# Copyright (c) 2008, Willow Garage, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of Willow Garage, Inc. nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import unittest
from test_roslib_comm.msg import *
class TestMd5sums(unittest.TestCase):
def test_field_name_change(self):
self.assertNotEquals(FieldNameChange1._md5sum, FieldNameChange2._md5sum)
def test_type_name_change(self):
self.assertEquals(TypeNameChange1._md5sum, TypeNameChange2._md5sum)
def test_type_name_change_array(self):
self.assertEquals(TypeNameChangeArray1._md5sum, TypeNameChangeArray2._md5sum)
def test_type_name_change_complex(self):
self.assertEquals(TypeNameChangeComplex1._md5sum, TypeNameChangeComplex2._md5sum)

View File

@@ -0,0 +1,131 @@
#!/usr/bin/env python
# Software License Agreement (BSD License)
#
# Copyright (c) 2008, Willow Garage, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of Willow Garage, Inc. nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
from __future__ import print_function
import os
import string
import sys
import unittest
import roslib.names
import roslib.packages
TEST_CTX = 'rosgraph_msgs'
import roslib
PKG='test_roslib_comm'
class TestGentools(unittest.TestCase):
def setUp(self):
pass
def _load_md5_tests(self, dir):
test_dir = os.path.join(roslib.packages.get_pkg_dir(PKG), 'test', 'md5tests', dir)
tests = {}
for f in os.listdir(test_dir):
path = os.path.join(test_dir, f)
if not f.endswith('.txt'):
continue
name = f[:-4]
while name and name[-1].isdigit():
name = name[:-1]
self.assert_(name)
if name in tests:
tests[name].append(path)
else:
tests[name] = [path]
return tests
def _compute_md5(self, f):
from roslib.gentools import compute_md5, get_dependencies
from roslib.msgs import load_from_string
text = open(f, 'r').read()
spec = load_from_string(text, package_context=TEST_CTX)
get_deps_dict = get_dependencies(spec, TEST_CTX, compute_files=False)
return compute_md5(get_deps_dict)
def _compute_md5_text(self, f):
from roslib.gentools import compute_md5_text, get_dependencies
from roslib.msgs import load_from_string
text = open(f, 'r').read()
spec = load_from_string(text, package_context=TEST_CTX)
get_deps_dict = get_dependencies(spec, TEST_CTX, compute_files=False)
return compute_md5_text(get_deps_dict, spec)
def test_compute_md5_text(self):
from std_msgs.msg import Header
Header_md5 = Header._md5sum
rg_msg_dir = os.path.join(roslib.packages.get_pkg_dir(TEST_CTX), 'msg')
clock_msg = os.path.join(rg_msg_dir, 'Clock.msg')
# a bit gory, but go ahead and regression test these important messages
self.assertEquals("time clock", self._compute_md5_text(clock_msg))
log_msg = os.path.join(rg_msg_dir, 'Log.msg')
self.assertEquals("byte DEBUG=1\nbyte INFO=2\nbyte WARN=4\nbyte ERROR=8\nbyte FATAL=16\n%s header\nbyte level\nstring name\nstring msg\nstring file\nstring function\nuint32 line\nstring[] topics"%Header_md5, self._compute_md5_text(log_msg))
tests = self._load_md5_tests('md5text')
# text file #1 is the reference
for k, files in tests.items():
print("running tests", k)
ref_file = [f for f in files if f.endswith('%s1.txt'%k)]
if not ref_file:
self.fail("failed to load %s"%k)
ref_file = ref_file[0]
ref_text = open(ref_file, 'r').read().strip()
print("KEY", k)
files = [f for f in files if not f.endswith('%s1.txt'%k)]
for f in files[1:]:
f_text = self._compute_md5_text(f)
self.assertEquals(ref_text, f_text, "failed on %s\n%s\n%s: \n[%s]\nvs.\n[%s]\n"%(k, ref_file, f, ref_text, f_text))
def test_md5_equals(self):
tests = self._load_md5_tests('same')
for k, files in tests.items():
print("running tests", k)
md5sum = self._compute_md5(files[0])
for f in files[1:]:
self.assertEquals(md5sum, self._compute_md5(f), "failed on %s: \n[%s]\nvs.\n[%s]\n"%(k, self._compute_md5_text(files[0]), self._compute_md5_text(f)))
def test_md5_not_equals(self):
tests = self._load_md5_tests('different')
for k, files in tests.items():
print("running tests", k)
md5s = set()
md6md5sum = self._compute_md5(files[0])
for f in files:
md5s.add(self._compute_md5(f))
# each md5 should be unique
self.assertEquals(len(md5s), len(files))

View File

@@ -0,0 +1,120 @@
# Software License Agreement (BSD License)
#
# Copyright (c) 2009, Willow Garage, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of Willow Garage, Inc. nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
from __future__ import print_function
import os
import sys
import time
import unittest
import traceback
import yaml
import roslib.message
import genpy
from genpy import Time, Duration
# Not much to test, just tripwires
class MessageTest(unittest.TestCase):
def test_check_types_Header(self):
# #2128: test that check_types works with a Header
# #message. This is a weird case because Header has an aliased
# #type and is the only ROS type for which that is true
from test_roslib_comm.msg import HeaderTest
x = HeaderTest()
x._check_types()
def test_strify_message(self):
from genpy.message import Message, strify_message, fill_message_args
def roundtrip(m):
yaml_text = strify_message(m)
print(yaml_text)
loaded = yaml.load(yaml_text)
print("loaded", loaded)
new_inst = m.__class__()
if loaded is not None:
fill_message_args(new_inst, [loaded])
else:
fill_message_args(new_inst, [])
return new_inst
# The following tests have not been ported to genpy yet
# test array of Messages field. We can't use M4 or M5 because fill_message_args has to instantiate the embedded type
from test_roslib_comm.msg import ArrayOfMsgs
from std_msgs.msg import String, Time, MultiArrayLayout, MultiArrayDimension
dims1 = [MultiArrayDimension(*args) for args in [('', 0, 0), ('x', 1, 2), ('y of z', 3, 4)]]
dims2 = [MultiArrayDimension('hello world', 91280, 1983274)]
times = [Time(genpy.Time(*args)) for args in [(0,), (12345, 6789), (1, 1)]]
val = ArrayOfMsgs([String(''), String('foo'), String('bar of soap')],
times,
[MultiArrayLayout(dims1, 0), MultiArrayLayout(dims2, 12354)],
)
self.assertEquals(val, roundtrip(val))
def test_get_message_class(self):
from roslib.message import get_message_class
try:
self.assertEquals(None, get_message_class('String'))
self.fail("should have thrown ValueError")
except ValueError: pass
# non-existent package
self.assertEquals(None, get_message_class('fake/Fake'))
# non-existent message
self.assertEquals(None, get_message_class('roslib/Fake'))
# package with no messages
self.assertEquals(None, get_message_class('genmsg_cpp/Fake'))
import rosgraph_msgs.msg
import std_msgs.msg
self.assertEquals(std_msgs.msg.Header, get_message_class('Header'))
self.assertEquals(std_msgs.msg.Header, get_message_class('std_msgs/Header'))
self.assertEquals(rosgraph_msgs.msg.Log, get_message_class('rosgraph_msgs/Log'))
def test_get_service_class(self):
from roslib.message import get_service_class
# non-existent package
self.assertEquals(None, get_service_class('fake/Fake'))
# non-existent message
self.assertEquals(None, get_service_class('roslib/Fake'))
# package with no messages
self.assertEquals(None, get_service_class('genmsg_cpp/Fake'))
import std_srvs.srv
self.assertEquals(std_srvs.srv.Empty, get_service_class('std_srvs/Empty'))

View File

@@ -0,0 +1,275 @@
# Software License Agreement (BSD License)
#
# Copyright (c) 2009, Willow Garage, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of Willow Garage, Inc. nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import os
import sys
import unittest
import roslib
PKG='test_roslib_comm'
class MsgSpecTest(unittest.TestCase):
def test_verbose(self):
import roslib.msgs
self.failIf(roslib.msgs.is_verbose())
roslib.msgs.set_verbose(True)
self.assert_(roslib.msgs.is_verbose())
roslib.msgs.set_verbose(False)
self.failIf(roslib.msgs.is_verbose())
def test_base_msg_type(self):
import roslib.msgs
tests = [(None, None), ('String', 'String'), ('std_msgs/String', 'std_msgs/String'),
('String[10]', 'String'), ('string[10]', 'string'), ('std_msgs/String[10]', 'std_msgs/String'),
]
for val, res in tests:
self.assertEquals(res, roslib.msgs.base_msg_type(val))
def test_resolve_type(self):
from roslib.msgs import resolve_type
for t in ['string', 'string[]', 'string[14]', 'int32', 'int32[]']:
bt = roslib.msgs.base_msg_type(t)
self.assertEquals(t, resolve_type(t, PKG))
self.assertEquals('foo/string', resolve_type('foo/string', PKG))
self.assertEquals('std_msgs/Header', resolve_type('Header', 'roslib'))
self.assertEquals('std_msgs/Header', resolve_type('std_msgs/Header', 'roslib'))
self.assertEquals('std_msgs/Header', resolve_type('Header', 'stereo_msgs'))
self.assertEquals('std_msgs/String', resolve_type('String', 'std_msgs'))
self.assertEquals('std_msgs/String', resolve_type('std_msgs/String', 'std_msgs'))
self.assertEquals('std_msgs/String', resolve_type('std_msgs/String', PKG))
self.assertEquals('std_msgs/String[]', resolve_type('std_msgs/String[]', PKG))
def test_parse_type(self):
tests = [
('a', ('a', False, None)),
('int8', ('int8', False, None)),
('std_msgs/String', ('std_msgs/String', False, None)),
('a[]', ('a', True, None)),
('int8[]', ('int8', True, None)),
('std_msgs/String[]', ('std_msgs/String', True, None)),
('a[1]', ('a', True, 1)),
('int8[1]', ('int8', True, 1)),
('std_msgs/String[1]', ('std_msgs/String', True, 1)),
('a[11]', ('a', True, 11)),
('int8[11]', ('int8', True, 11)),
('std_msgs/String[11]', ('std_msgs/String', True, 11)),
]
for val, res in tests:
self.assertEquals(res, roslib.msgs.parse_type(val))
fail = ['a[1][2]', 'a[][]', '', None, 'a[', 'a[[1]', 'a[1]]']
for f in fail:
try:
roslib.msgs.parse_type(f)
self.fail("should have failed on " +f)
except roslib.msgs.MsgSpecException: pass
def test_Constant(self):
import random
import roslib.msgs
vals = [random.randint(0, 1000) for i in range(0, 3)]
type_, name, val = [str(x) for x in vals]
x = roslib.msgs.Constant(type_, name, val, str(val))
self.assertEquals(type_, x.type)
self.assertEquals(name, x.name)
self.assertEquals(val, x.val)
self.assertEquals(roslib.msgs.Constant(type_, name, val, str(val)), x)
self.assertNotEquals(1, x)
self.assertNotEquals(roslib.msgs.Constant('baz', name, val, str(val)), x)
self.assertNotEquals(roslib.msgs.Constant(type_, 'foo', val, str(val)), x)
self.assertNotEquals(roslib.msgs.Constant(type_, name, 'foo', 'foo'), x)
# tripwire
self.assert_(repr(x))
self.assert_(str(x))
try:
roslib.msgs.Constant(None, name, val, str(val))
except: pass
try:
roslib.msgs.Constant(type_, None, val, str(val))
except: pass
try:
roslib.msgs.Constant(type_, name, None, 'None')
except: pass
try:
roslib.msgs.Constant(type_, name, val, None)
except: pass
try:
x.foo = 'bar'
self.fail('Constant should not allow arbitrary attr assignment')
except: pass
def test_MsgSpec(self):
def sub_test_MsgSpec(types, names, constants, text, has_header):
m = MsgSpec(types, names, constants, text)
self.assertEquals(m.types, types)
self.assertEquals(m.names, names)
self.assertEquals(m.text, text)
self.assertEquals(has_header, m.has_header())
self.assertEquals(m.constants, constants)
self.assertEquals(list(zip(types, names)), m.fields())
self.assertEquals(m, MsgSpec(types, names, constants, text))
return m
from roslib.msgs import MsgSpec
# allow empty msg
empty = sub_test_MsgSpec([], [], [], '', False)
self.assertEquals([], empty.fields())
# one-field
one_field = sub_test_MsgSpec(['int32'], ['x'], [], 'int32 x', False)
# make sure that equals tests every declared field
self.assertEquals(one_field, MsgSpec(['int32'], ['x'], [], 'int32 x'))
self.assertNotEquals(one_field, MsgSpec(['uint32'], ['x'], [], 'int32 x'))
self.assertNotEquals(one_field, MsgSpec(['int32'], ['y'], [], 'int32 x'))
self.assertNotEquals(one_field, MsgSpec(['int32'], ['x'], [], 'uint32 x'))
# test against __ne__ as well
self.assert_(one_field != MsgSpec(['int32'], ['x'], [], 'uint32 x'))
#test strify
self.assertEquals("int32 x", str(one_field).strip())
# test variations of multiple fields and headers
two_fields = sub_test_MsgSpec(['int32', 'string'], ['x', 'str'], [], 'int32 x\nstring str', False)
one_header = sub_test_MsgSpec(['Header'], ['header'], [], 'Header header', True)
header_and_fields = sub_test_MsgSpec(['Header', 'int32', 'string'], ['header', 'x', 'str'], [], 'Header header\nint32 x\nstring str', True)
embed_types = sub_test_MsgSpec(['Header', 'std_msgs/Int32', 'string'], ['header', 'x', 'str'], [], 'Header header\nstd_msgs/Int32 x\nstring str', True)
#test strify
self.assertEquals("int32 x\nstring str", str(two_fields).strip())
# types and names mismatch
try:
MsgSpec(['int32', 'int32'], ['intval'], [], 'int32 intval\int32 y')
self.fail("types and names must align")
except: pass
# test (not) equals against non msgspec
self.failIf(one_field == 1)
self.assert_(one_field != 1)
#TODO: test flatten
# test that repr doesn't throw an error
[repr(x) for x in [empty, one_field, one_header, two_fields, embed_types]]
def test_init(self):
import roslib.msgs
roslib.msgs._initialized = False
roslib.msgs._init()
self.assert_(roslib.msgs._initialized)
# test repeated initialization
roslib.msgs._init()
def test___convert_val(self):
from roslib.msgs import _convert_val, MsgSpecException
self.assertEquals(0., _convert_val('float32', '0.0'))
self.assertEquals(0., _convert_val('float64', '0.0'))
self.assertEquals('fo o', _convert_val('string', ' fo o '))
self.assertEquals(1, _convert_val('byte', '1'))
self.assertEquals(1, _convert_val('char', '1'))
self.assertEquals(1, _convert_val('int8', '1'))
self.assertEquals(12, _convert_val('int16', '12'))
self.assertEquals(-13, _convert_val('int32', '-13'))
self.assertEquals(14, _convert_val('int64', '14'))
self.assertEquals(0, _convert_val('uint8', '0'))
self.assertEquals(18, _convert_val('uint16', '18'))
self.assertEquals(19, _convert_val('uint32', '19'))
self.assertEquals(20, _convert_val('uint64', '20'))
width_fail = [('int8', '129'), ('uint8', '256'),
('int16', '35536'), ('uint16', '-1'),('uint16', '65536'),
('int32', '3000000000'),('int32', '-2700000000'),
('uint32', '-1'),('uint32', '41000000000'),
('uint64', '-1')]
for t, v in width_fail:
try:
_convert_val(t, v)
self.fail("should have failed width check: %s, %s"%(t, v))
except MsgSpecException: pass
type_fail = [('int32', 'f'), ('float32', 'baz')]
for t, v in type_fail:
try:
_convert_val(t, v)
self.fail("should have failed type check: %s, %s"%(t, v))
except ValueError: pass
try:
_convert_val('foo', '1')
self.fail("should have failed invalid type")
except MsgSpecException: pass
def test_msg_file(self):
import roslib.msgs
f = roslib.msgs.msg_file('rosgraph_msgs', 'Log')
self.assert_(os.path.isfile(f))
self.assert_(f.endswith('rosgraph_msgs/msg/Log.msg'))
# msg_file should return paths even for non-existent resources
f = roslib.msgs.msg_file('roslib', 'Fake')
self.failIf(os.path.isfile(f))
self.assert_(f.endswith('roslib/msg/Fake.msg'))
def test_is_valid_msg_type(self):
import roslib.msgs
vals = [
#basic
'F', 'f', 'Foo', 'Foo1',
'std_msgs/String',
# arrays
'Foo[]', 'Foo[1]', 'Foo[10]',
]
for v in vals:
self.assert_(roslib.msgs.is_valid_msg_type(v), "roslib.msgs.is_valid_msg_type should have returned True for '%s'"%v)
# bad cases
vals = [None, '', '#', '%', 'Foo%', 'Woo Woo',
'/', '/String',
'Foo[f]', 'Foo[1d]', 'Foo[-1]', 'Foo[1:10]', 'Foo[', 'Foo]', 'Foo[]Bar']
for v in vals:
self.failIf(roslib.msgs.is_valid_msg_type(v), "roslib.msgs.is_valid_msg_type should have returned False for '%s'"%v)
def test_is_valid_constant_type(self):
import roslib.msgs
valid = ['int8', 'uint8', 'int16', 'uint16', 'int32', 'uint32', 'int64', \
'uint64', 'float32', 'float64', 'char', 'byte', 'string']
invalid = [
'std_msgs/String', '/', 'String',
'time', 'duration','header',
]
for v in valid:
self.assert_(roslib.msgs.is_valid_constant_type(v), "roslib.msgs.is_valid_constant_type should have returned True for '%s'"%v)
for v in invalid:
self.failIf(roslib.msgs.is_valid_constant_type(v), "roslib.msgs.is_valid_constant_type should have returned False for '%s'"%v)

View File

@@ -0,0 +1,130 @@
# Software License Agreement (BSD License)
#
# Copyright (c) 2011, Willow Garage, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
# * Redistributions of source code must retain the above copyright
# notice, this list of conditions and the following disclaimer.
# * Redistributions in binary form must reproduce the above
# copyright notice, this list of conditions and the following
# disclaimer in the documentation and/or other materials provided
# with the distribution.
# * Neither the name of Willow Garage, Inc. nor the names of its
# contributors may be used to endorse or promote products derived
# from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
import os
import sys
import unittest
import roslib.packages
class RoslibSrvTest(unittest.TestCase):
def test_SrvSpec(self):
from roslib.msgs import MsgSpec
from roslib.srvs import SrvSpec
types = ['int32']
names = ['a']
constants = []
text = 'int32 a'
msg_a = MsgSpec(types, names, constants, text)
types = ['int64']
names = ['b']
constants = []
text = 'int64 b'
msg_b = MsgSpec(types, names, constants, text)
text = msg_a.text + '\n---\n' + msg_b.text
spec = SrvSpec(msg_a, msg_b, text)
self.assertEquals(msg_a, spec.request)
self.assertEquals(msg_b, spec.response)
self.assertEquals(text, spec.text)
self.assertEquals('', spec.full_name)
self.assertEquals('', spec.short_name)
self.assertEquals('',spec.package)
# tripwire
self.assert_(repr(spec))
self.assert_(str(spec))
# exercise eq
self.assertNotEquals(spec, 'spec')
self.assert_(spec != 'spec')
spec2 = SrvSpec(msg_a, msg_b, text)
self.assertEquals(spec, spec2)
self.failIf(spec != spec2)
# - full_name
spec2.full_name = 'something'
self.assertNotEquals(spec, spec2)
spec2.full_name = ''
self.assertEquals(spec, spec2)
# - short_name
spec2.short_name = 'something'
self.assertNotEquals(spec, spec2)
spec2.short_name = ''
self.assertEquals(spec, spec2)
# - package
spec2.package = 'something'
self.assertNotEquals(spec, spec2)
spec2.package = ''
self.assertEquals(spec, spec2)
def test_srv_file(self):
from roslib.srvs import srv_file
d = roslib.packages.get_pkg_dir('test_rosmaster')
filename = os.path.join(d, 'srv', 'AddTwoInts.srv')
with open(filename, 'r') as f:
text = f.read()
self.assertEquals(filename, srv_file('test_rosmaster', 'AddTwoInts'))
def test_load_from_file(self):
from roslib.srvs import load_from_file, set_verbose
d = roslib.packages.get_pkg_dir('test_rosmaster')
filename = os.path.join(d, 'srv', 'AddTwoInts.srv')
with open(filename, 'r') as f:
text = f.read()
name, spec = load_from_file(filename)
self.assertEquals('AddTwoInts', name)
self.assertEquals(['int64', 'int64'], spec.request.types)
self.assertEquals(['a', 'b'], spec.request.names)
self.assertEquals(text, spec.text)
name2, spec2 = load_from_file(filename, package_context='foo')
self.assertEquals('foo/AddTwoInts', name2)
name2, spec2 = load_from_file(filename, package_context='foo/')
self.assertEquals('foo/AddTwoInts', name2)
name2, spec2 = load_from_file(filename, package_context='foo//')
self.assertEquals('foo/AddTwoInts', name2)
# test with verbose on
set_verbose(True)
name3, spec3 = load_from_file(filename)
self.assertEquals(name, name3)
self.assertEquals(spec, spec3)