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 import roslib; roslib.load_manifest('test_roslib')
00033
00034 import os
00035 import sys
00036 import unittest
00037
00038 import rosunit
00039
00040 class SubArgsTest(unittest.TestCase):
00041
00042 def test__arg(self):
00043 import random
00044 from roslib.substitution_args import _arg, ArgException, SubstitutionException
00045
00046 ctx = { 'arg': {
00047 'foo': '12345',
00048 'bar': 'hello world',
00049 'baz': 'sim',
00050 'empty': '',
00051 }
00052 }
00053
00054
00055 try:
00056 _arg('$(arg)', 'arg', [], ctx)
00057 self.fail("should have thrown")
00058 except SubstitutionException:
00059 pass
00060
00061
00062 tests = [
00063 ('12345', ('$(arg foo)', 'arg foo', ['foo'], ctx)),
00064 ('', ('$(arg empty)', 'arg empty', ['empty'], ctx)),
00065 ('sim', ('$(arg baz)', 'arg baz', ['baz'], ctx)),
00066
00067
00068 ('1234512345', ('$(arg foo)$(arg foo)', 'arg foo', ['foo'], ctx)),
00069 ('12345$(arg baz)', ('$(arg foo)$(arg baz)', 'arg foo', ['foo'], ctx)),
00070 ('$(arg foo)sim', ('$(arg foo)$(arg baz)', 'arg baz', ['baz'], ctx)),
00071
00072
00073 ('12345', ('12345', 'arg foo', ['foo'], ctx)),
00074 ]
00075
00076 for result, test in tests:
00077 resolved, a, args, context = test
00078 self.assertEquals(result, _arg(resolved, a, args, context))
00079
00080
00081 for result, test in tests:
00082 resolved, a, args, context = test
00083 try:
00084 _arg(resolved, a, args, {})
00085 self.fail("should have thrown")
00086 except ArgException, e:
00087 self.assertEquals(args[0], str(e))
00088 try:
00089 _arg(resolved, a, args, {'arg': {}})
00090 self.fail("should have thrown")
00091 except ArgException, e:
00092 self.assertEquals(args[0], str(e))
00093
00094
00095 def test_resolve_args(self):
00096 from roslib.substitution_args import resolve_args, SubstitutionException
00097 from roslib.packages import get_pkg_dir
00098 roslib_dir = get_pkg_dir('roslib', required=True)
00099
00100 anon_context = {'foo': 'bar'}
00101 arg_context = {'fuga': 'hoge', 'car': 'cdr'}
00102 context = {'anon': anon_context, 'arg': arg_context }
00103
00104 tests = [
00105 ('$(find roslib)', roslib_dir),
00106 ('hello$(find roslib)', 'hello'+roslib_dir),
00107 ('$(find roslib )', roslib_dir),
00108 ('$$(find roslib )', '$'+roslib_dir),
00109 ('$( find roslib )', roslib_dir),
00110 ('$(find roslib )', roslib_dir),
00111 ('$(find roslib)$(find roslib)', roslib_dir+roslib_dir),
00112 ('$(find roslib)/foo/bar.xml', roslib_dir+os.sep+'foo'+os.sep+'bar.xml'),
00113 (r'$(find roslib)\foo\bar.xml $(find roslib)\bar.xml', roslib_dir+os.sep+'foo'+os.sep+'bar.xml '+roslib_dir+os.sep+'bar.xml'),
00114 ('$(find roslib)\\foo\\bar.xml more/stuff\\here', roslib_dir+os.sep+'foo'+os.sep+'bar.xml more/stuff\\here'),
00115 ('$(env ROS_ROOT)', os.environ['ROS_ROOT']),
00116 ('$(env ROS_ROOT)', os.environ['ROS_ROOT']),
00117 ('$(env ROS_ROOT )', os.environ['ROS_ROOT']),
00118 ('$(optenv ROS_ROOT)', os.environ['ROS_ROOT']),
00119 ('$(optenv ROS_ROOT)$(optenv ROS_ROOT)', os.environ['ROS_ROOT']+os.environ['ROS_ROOT']),
00120 ('$(optenv ROS_ROOT alternate text)', os.environ['ROS_ROOT']),
00121 ('$(optenv NOT_ROS_ROOT)', ''),
00122 ('$(optenv NOT_ROS_ROOT)more stuff', 'more stuff'),
00123 ('$(optenv NOT_ROS_ROOT alternate)', 'alternate'),
00124 ('$(optenv NOT_ROS_ROOT alternate text)', 'alternate text'),
00125
00126
00127 ('$(anon foo)', 'bar'),
00128 ('$(anon foo)/baz', 'bar/baz'),
00129 ('$(anon foo)/baz/$(anon foo)', 'bar/baz/bar'),
00130
00131
00132 ('$(arg fuga)', 'hoge'),
00133 ('$(arg fuga)$(arg fuga)', 'hogehoge'),
00134 ('$(arg car)$(arg fuga)', 'cdrhoge'),
00135 ('$(arg fuga)hoge', 'hogehoge'),
00136 ]
00137 for arg, val in tests:
00138 self.assertEquals(val, resolve_args(arg, context=context))
00139
00140
00141 r = resolve_args('$(anon foo)/bar')
00142 self.assert_('/bar' in r)
00143 self.failIf('$(anon foo)' in r)
00144
00145
00146
00147 noop_tests = [
00148 '$(find roslib', '$find roslib', '', ' ', 'noop', 'find roslib', 'env ROS_ROOT', '$$', ')', '(', '()',
00149 None,
00150 ]
00151 for t in noop_tests:
00152 self.assertEquals(t, resolve_args(t))
00153 failures = [
00154 '$((find roslib))', '$(find $roslib)',
00155 '$(find)', '$(find roslib roslib)', '$(export roslib)',
00156 '$(env)', '$(env ROS_ROOT alternate)',
00157 '$(env NOT_SET)',
00158 '$(optenv)',
00159 '$(anon)',
00160 '$(anon foo bar)',
00161 ]
00162 for f in failures:
00163 try:
00164 resolve_args(f)
00165 self.fail("resolve_args(%s) should have failed"%f)
00166 except SubstitutionException: pass
00167
00168 if __name__ == '__main__':
00169 rosunit.unitrun('test_roslib', 'test_substitution_args', SubArgsTest, coverage_packages=['roslib.substitution_args'])
00170