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('std_msgs.msg._Header.Header()', compute_constructor('std_msgs', 'Header'))

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

        # 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'))
Exemple #2
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'))
 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
Exemple #4
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
Exemple #5
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_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_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'))
    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('std_msgs.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('std_msgs.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']:
            self.assertEquals('[]', default_value(t+'[]', 'std_msgs'))
            self.assertEquals('[]', default_value(t+'[]', 'roslib'))

        self.assertEquals("''", default_value('uint8[]', 'roslib'))
        
        # fixed-length arrays should be zero-filled... except for uint8 which is a string
        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']:
            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('[]', 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'))        
Exemple #9
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'))
Exemple #10
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'))