Esempio n. 1
0
    def test_compute_constructor(self):
        from roslib.msgs import register, MsgSpec
        from roslib.genpy import compute_constructor
        register('fake_msgs/String', MsgSpec(['string'], ['data'], [], 'string data\n'))
        register('fake_msgs/ThreeNums', MsgSpec(['int32', 'int32', 'int32'], ['x', 'y', 'z'], [], 'int32 x\nint32 y\nint32 z\n'))
        
        # builtin specials
        self.assertEquals('roslib.rostime.Time()', compute_constructor('roslib', 'time'))
        self.assertEquals('roslib.rostime.Duration()', compute_constructor('roslib', 'duration'))
        self.assertEquals('roslib.msg._Header.Header()', compute_constructor('roslib', 'Header'))

        self.assertEquals('roslib.rostime.Time()', compute_constructor('std_msgs', 'time'))
        self.assertEquals('roslib.rostime.Duration()', compute_constructor('std_msgs', 'duration'))
        self.assertEquals('roslib.msg._Header.Header()', compute_constructor('std_msgs', 'Header'))

        # generic instances
        # - unregistered type
        self.assertEquals(None, compute_constructor("unknown_msgs", "unknown_msgs/Foo"))
        self.assertEquals(None, compute_constructor("unknown_msgs", "Foo"))
        # - wrong context
        self.assertEquals(None, compute_constructor('std_msgs', 'ThreeNums'))

        # - registered types
        self.assertEquals('fake_msgs.msg.String()', compute_constructor('std_msgs', 'fake_msgs/String'))
        self.assertEquals('fake_msgs.msg.String()', compute_constructor('fake_msgs', 'fake_msgs/String'))
        self.assertEquals('fake_msgs.msg.String()', compute_constructor('fake_msgs', 'String'))
        self.assertEquals('fake_msgs.msg.ThreeNums()', compute_constructor('fake_msgs', 'fake_msgs/ThreeNums'))
        self.assertEquals('fake_msgs.msg.ThreeNums()', compute_constructor('fake_msgs', 'fake_msgs/ThreeNums'))
        self.assertEquals('fake_msgs.msg.ThreeNums()', compute_constructor('fake_msgs', 'ThreeNums'))
Esempio n. 2
0
 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(zip(types, names), m.fields())
     self.assertEquals(m, MsgSpec(types, names, constants, text))
     return m
Esempio n. 3
0
    def test_flatten(self):
        from roslib.msgs import register, MsgSpec
        from roslib.genpy import flatten

        simple = MsgSpec(['string'], ['data'], [], 'string data\n')
        simple2 = MsgSpec(['string', 'int32'], ['data', 'data2'], [],
                          'string data\nint32 data2\n')
        self.assertEquals(simple, flatten(simple))
        self.assertEquals(simple2, flatten(simple2))

        b1 = MsgSpec(['int8'], ['data'], [], 'X')
        b2 = MsgSpec(['f_msgs/Base'], ['data'], [], 'X')
        b3 = MsgSpec(['f_msgs/Base2', 'f_msgs/Base2'], ['data3', 'data4'], [],
                     'X')
        b4 = MsgSpec(['f_msgs/Base3', 'f_msgs/Base3'], ['dataA', 'dataB'], [],
                     'X')
        register('f_msgs/Base', b1)
        register('f_msgs/Base2', b2)
        register('f_msgs/Base3', b3)
        register('f_msgs/Base4', b4)

        self.assertEquals(MsgSpec(['int8'], ['data.data'], [], 'X'),
                          flatten(b2))
        self.assertEquals(
            MsgSpec(['int8', 'int8'], ['data3.data.data', 'data4.data.data'],
                    [], 'X'), flatten(b3))
        self.assertEquals(
            MsgSpec(['int8', 'int8', 'int8', 'int8'], [
                'dataA.data3.data.data', 'dataA.data4.data.data',
                'dataB.data3.data.data', 'dataB.data4.data.data'
            ], [], 'X'), flatten(b4))
    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)
Esempio n. 5
0
 def test_get_registered_ex(self):
     from roslib.msgs import MsgSpec, register
     from roslib.genpy import MsgGenerationException, get_registered_ex
     s = MsgSpec(['string'], ['data'], [], 'string data\n')
     register('tgr_msgs/String', s)
     self.assertEquals(s, get_registered_ex('tgr_msgs/String'))
     try:
         get_registered_ex('bad_msgs/String')
     except MsgGenerationException: pass
Esempio n. 6
0
 def test_make_python_safe(self):
     from roslib.msgs import register, MsgSpec, Constant
     from roslib.genpy import make_python_safe
     s = MsgSpec(['int32', 'int32', 'int32', 'int32'], ['ok', 'if', 'self', 'fine'],
                 [Constant('int32', 'if', '1', '1'), Constant('int32', 'okgo', '1', '1')],
                 'x')
     s2 = make_python_safe(s)
     self.assertNotEquals(s, s2)
     self.assertEquals(['ok', 'if_', 'self_', 'fine'], s2.names)
     self.assertEquals(s2.types, s.types)
     self.assertEquals([Constant('int32', 'if_', '1', '1'), Constant('int32', 'okgo', '1', '1')], s2.constants)
     self.assertEquals(s2.text, s.text)
Esempio n. 7
0
    def test_compute_import(self):
        from roslib.msgs import register, MsgSpec
        from roslib.genpy import compute_import

        self.assertEquals([], compute_import('foo', 'bar'))
        self.assertEquals([], compute_import('foo', 'int32'))        
        
        register('ci_msgs/Base', MsgSpec(['int8'], ['data'], [], 'int8 data\n'))
        register('ci2_msgs/Base2', MsgSpec(['ci_msgs/Base'], ['data2'], [], 'ci_msgs/Base data2\n'))
        register('ci3_msgs/Base3', MsgSpec(['ci2_msgs/Base2'], ['data3'], [], 'ci2_msgs/Base2 data3\n'))
        register('ci4_msgs/Base', MsgSpec(['int8'], ['data'], [], 'int8 data\n'))
        register('ci4_msgs/Base4', MsgSpec(['ci2_msgs/Base2', 'ci3_msgs/Base3', 'ci4_msgs/Base'],
                                           ['data4a', 'data4b', 'data4c'],
                                           [], 'ci2_msgs/Base2 data4a\nci3_msgs/Base3 data4b\nci4_msgs/Base data4c\n'))

        register('ci5_msgs/Base', MsgSpec(['time'], ['data'], [], 'time data\n'))
        
        self.assertEquals(['import ci_msgs.msg'], compute_import('foo', 'ci_msgs/Base'))
        self.assertEquals(['import ci_msgs.msg'], compute_import('ci_msgs', 'ci_msgs/Base'))
        self.assertEquals(['import ci2_msgs.msg', 'import ci_msgs.msg'], compute_import('ci2_msgs', 'ci2_msgs/Base2'))
        self.assertEquals(['import ci2_msgs.msg', 'import ci_msgs.msg'], compute_import('foo', 'ci2_msgs/Base2'))
        self.assertEquals(['import ci3_msgs.msg', 'import ci2_msgs.msg', 'import ci_msgs.msg'], compute_import('ci3_msgs', 'ci3_msgs/Base3'))
        
        self.assertEquals(set(['import ci4_msgs.msg', 'import ci3_msgs.msg', 'import ci2_msgs.msg', 'import ci_msgs.msg']),
                          set(compute_import('foo', 'ci4_msgs/Base4')))        
        self.assertEquals(set(['import ci4_msgs.msg', 'import ci3_msgs.msg', 'import ci2_msgs.msg', 'import ci_msgs.msg']),
                          set(compute_import('ci4_msgs', 'ci4_msgs/Base4')))
        
        self.assertEquals(['import ci4_msgs.msg'], compute_import('foo', 'ci4_msgs/Base'))        
        self.assertEquals(['import ci4_msgs.msg'], compute_import('ci4_msgs', 'ci4_msgs/Base'))
        self.assertEquals(['import ci4_msgs.msg'], compute_import('ci4_msgs', 'Base'))    
        
        self.assertEquals(['import ci5_msgs.msg', 'import roslib.rostime'], compute_import('foo', 'ci5_msgs/Base'))
Esempio n. 8
0
  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(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 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)
    
    # 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]]
Esempio n. 9
0
    def test_default_value(self):
        from roslib.msgs import register, MsgSpec
        from roslib.genpy import default_value

        register('fake_msgs/String', MsgSpec(['string'], ['data'], [], 'string data\n'))
        register('fake_msgs/ThreeNums', MsgSpec(['int32', 'int32', 'int32'], ['x', 'y', 'z'], [], 'int32 x\nint32 y\nint32 z\n'))
        
        # trip-wire: make sure all builtins have a default value
        for t in roslib.msgs.BUILTIN_TYPES:
            self.assert_(type(default_value(t, 'roslib')) == str)
            
        # simple types first
        for t in ['uint8', 'int8', 'uint16', 'int16', 'uint32', 'int32', 'uint64', 'int64', 'byte', 'char']:
            self.assertEquals('0', default_value(t, 'std_msgs'))
            self.assertEquals('0', default_value(t, 'roslib'))
        for t in ['float32', 'float64']:
            self.assertEquals('0.', default_value(t, 'std_msgs'))
            self.assertEquals('0.', default_value(t, 'roslib'))
        self.assertEquals("''", default_value('string', 'roslib'))

        # builtin specials
        self.assertEquals('roslib.rostime.Time()', default_value('time', 'roslib'))
        self.assertEquals('roslib.rostime.Duration()', default_value('duration', 'roslib'))
        self.assertEquals('roslib.msg._Header.Header()', default_value('Header', 'roslib'))

        self.assertEquals('roslib.rostime.Time()', default_value('time', 'std_msgs'))
        self.assertEquals('roslib.rostime.Duration()', default_value('duration', 'std_msgs'))
        self.assertEquals('roslib.msg._Header.Header()', default_value('Header', 'std_msgs'))

        # generic instances
        # - unregistered type
        self.assertEquals(None, default_value("unknown_msgs/Foo", "unknown_msgs"))
        # - wrong context
        self.assertEquals(None, default_value('ThreeNums', 'std_msgs'))

        # - registered types
        self.assertEquals('fake_msgs.msg.String()', default_value('fake_msgs/String', 'std_msgs'))
        self.assertEquals('fake_msgs.msg.String()', default_value('fake_msgs/String', 'fake_msgs'))
        self.assertEquals('fake_msgs.msg.String()', default_value('String', 'fake_msgs'))
        self.assertEquals('fake_msgs.msg.ThreeNums()', default_value('fake_msgs/ThreeNums', 'roslib'))
        self.assertEquals('fake_msgs.msg.ThreeNums()', default_value('fake_msgs/ThreeNums', 'fake_msgs'))
        self.assertEquals('fake_msgs.msg.ThreeNums()', default_value('ThreeNums', 'fake_msgs'))

        # var-length arrays always default to empty arrays... except for byte and uint8 which are strings
        for t in ['int8', 'uint16', 'int16', 'uint32', 'int32', 'uint64', 'int64', 'float32', 'float64', 'char']:
            self.assertEquals('[]', default_value(t+'[]', 'std_msgs'))
            self.assertEquals('[]', default_value(t+'[]', 'roslib'))

        self.assertEquals("''", default_value('uint8[]', 'roslib'))
        self.assertEquals("''", default_value('byte[]', 'roslib'))
        
        # fixed-length arrays should be zero-filled... except for byte and uint8 which are strings
        for t in ['float32', 'float64']:
            self.assertEquals('[0.,0.,0.]', default_value(t+'[3]', 'std_msgs'))
            self.assertEquals('[0.]', default_value(t+'[1]', 'std_msgs'))
        for t in ['int8', 'uint16', 'int16', 'uint32', 'int32', 'uint64', 'int64', 'char']:
            self.assertEquals('[0,0,0,0]', default_value(t+'[4]', 'std_msgs'))
            self.assertEquals('[0]', default_value(t+'[1]', 'roslib'))

        self.assertEquals("chr(0)*1", default_value('uint8[1]', 'roslib'))
        self.assertEquals("chr(0)*4", default_value('uint8[4]', 'roslib'))
        self.assertEquals("chr(0)*1", default_value('byte[1]', 'roslib'))
        self.assertEquals("chr(0)*4", default_value('byte[4]', 'roslib'))
        
        self.assertEquals('[]', default_value('fake_msgs/String[]', 'std_msgs'))        
        self.assertEquals('[fake_msgs.msg.String(),fake_msgs.msg.String()]', default_value('fake_msgs/String[2]', 'std_msgs'))