test_StringUtil.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 # -*- Python -*-
3 
4 #
5 # \file test_StringUtil.py
6 # \brief test for String operation utility
7 # \date $Date: $
8 # \author Shinji Kurihara
9 #
10 # Copyright (C) 2003-2005
11 # Task-intelligence Research Group,
12 # Intelligent Systems Research Institute,
13 # National Institute of
14 # Advanced Industrial Science and Technology (AIST), Japan
15 # All rights reserved.
16 #
17 
18 import sys
19 sys.path.insert(1,"../")
20 
21 import unittest
22 
23 from StringUtil import *
24 import OpenRTM_aist
25 
26 class TestStringUtil(unittest.TestCase):
27 
28  def setUp(self):
29  pass
30 
31  def tearDown(self):
32  OpenRTM_aist.Manager.instance().shutdownManager()
33  pass
34 
35  def test_isEscaped(self):
36  self.assertEqual(isEscaped("test\\\\test",5), True, "Result failed.")
37 
38 
39  def test_escape(self):
40  self.assertEqual(escape("test\ttest"), "test\\ttest", "Result failed.")
41  self.assertEqual(escape("test\ntest"), "test\\ntest", "Result failed.")
42  self.assertEqual(escape("test\ftest"), "test\\ftest", "Result failed.")
43  self.assertEqual(escape("test\rtest"), "test\\rtest", "Result failed.")
44 
45 
46  def test_unescape(self):
47  self.assertEqual(unescape("test\\ttest"), "test\ttest", "Result failed.")
48  self.assertEqual(unescape("test\\ntest"), "test\ntest", "Result failed.")
49  self.assertEqual(unescape("test\\ftest"), "test\ftest", "Result failed.")
50  self.assertEqual(unescape("test\\rtest"), "test\rtest", "Result failed.")
51 
52 
53  def test_eraseBlank(self):
54  _str=[" test"]
55  eraseBlank(_str)
56  self.assertEqual(_str[0], "test", "Result failed.")
57 
58  _str=[" test"]
59  eraseBlank(_str)
60  self.assertEqual(_str[0], "test", "Result failed.")
61 
62  _str=["\t\ttest"]
63  eraseBlank(_str)
64  self.assertEqual(_str[0], "test", "Result failed.")
65 
66  _str=[""]
67  eraseBlank(_str)
68  self.assertEqual(_str[0], "", "Result failed.")
69 
70  _str=["\t\n test"]
71  eraseBlank(_str)
72  self.assertEqual(_str[0], "\ntest")
73 
74  _str=["\t\\t test"]
75  eraseBlank(_str)
76  self.assertEqual(_str[0], "\\ttest", "Result failed.")
77 
79  _str=[" test"]
80  eraseHeadBlank(_str)
81  self.assertEqual(_str[0], "test", "Result failed.")
82 
83  _str=[" test"]
84  eraseHeadBlank(_str)
85  self.assertEqual(_str[0], "test", "Result failed.")
86 
87  _str=["\t\ttest"]
88  eraseHeadBlank(_str)
89  self.assertEqual(_str[0], "test", "Result failed.")
90 
91  _str=[""]
92  eraseHeadBlank(_str)
93  self.assertEqual(_str[0], "", "Result failed.")
94 
95  _str=["\t\n test"]
96  eraseHeadBlank(_str)
97  self.assertEqual(_str[0], "\n test", "Result failed.")
98 
99  _str=["\t\\t test"]
100  eraseHeadBlank(_str)
101  self.assertEqual(_str[0], "\\t test", "Result failed.")
102 
103 
104  # failed case
105  # _str=["\\t\\ttest"]
106  # eraseHeadBlank(_str)
107  # self.assertEqual(_str[0], "test", "Result failed.")
108 
109 
111  _str=["test "]
112  eraseTailBlank(_str)
113  self.assertEqual(_str[0], "test", "Result failed.")
114 
115  _str=["test "]
116  eraseTailBlank(_str)
117  self.assertEqual(_str[0], "test", "Result failed.")
118 
119  _str=["test\t\t"]
120  eraseTailBlank(_str)
121  self.assertEqual(_str[0], "test", "Result failed.")
122 
123  _str=["test\t\n\t"]
124  eraseTailBlank(_str)
125  self.assertEqual(_str[0], "test\t\n", "Result failed.")
126 
127  _str=["test\\t"]
128  eraseTailBlank(_str)
129  self.assertEqual(_str[0], "test\\t", "Result failed.")
130 
131  # failed case
132  # _str=["test\\t\\t"]
133  # eraseTailBlank(_str)
134  # self.assertEqual(_str[0], "test", "Result failed.")
135 
136  def test_normalize(self):
137  _str = [" NORMALIZE "]
138  normalize(_str)
139  self.assertEqual(_str[0],"normalize")
140 
141  _str = [" \t \tNORmALIZE "]
142  normalize(_str)
143  self.assertEqual(_str[0],"normalize")
144 
145  _str = [" nORMALIZE\t \t\t\t"]
146  normalize(_str)
147  self.assertEqual(_str[0],"normalize")
148 
149 
151  _str= ["replace"]
152  replaceString(_str,"l", "r")
153  self.assertEqual(_str[0], "reprace", "Result failed.")
154 
155  _str= ["replace"]
156  replaceString(_str,"re", "")
157  self.assertEqual(_str[0], "place", "Result failed.")
158 
159  _str= ["replace"]
160  replaceString(_str,"e", "a")
161  self.assertEqual(_str[0], "raplaca", "Result failed.")
162 
163 
164  def test_split(self):
165  _str = split("test0,test1,test2", ",")
166  self.assertEqual(_str[0], "test0", "Result failed.")
167  self.assertEqual(_str[1], "test1", "Result failed.")
168  self.assertEqual(_str[2], "test2", "Result failed.")
169 
170  _str = split("test0.test1.test2", ".")
171  self.assertEqual(_str[0], "test0", "Result failed.")
172  self.assertEqual(_str[1], "test1", "Result failed.")
173  self.assertEqual(_str[2], "test2", "Result failed.")
174 
175  _str = split("test0/test1/test2", "/")
176  self.assertEqual(_str[0], "test0", "Result failed.")
177  self.assertEqual(_str[1], "test1", "Result failed.")
178  self.assertEqual(_str[2], "test2", "Result failed.")
179 
180  _str = split("test0 test1 test2", " ")
181  self.assertEqual(_str[0], "test0", "Result failed.")
182  self.assertEqual(_str[1], "test1", "Result failed.")
183  self.assertEqual(_str[2], "test2", "Result failed.")
184 
185 
186  def test_toBool(self):
187  ret = toBool("yes", "yes", "no", True)
188  self.assertEqual(ret, True, "Result failed.")
189 
190  ret = toBool("no", "yes", "no", True)
191  self.assertEqual(ret, False, "Result failed.")
192 
193  ret = toBool("Yes", "YES", "NO", True)
194  self.assertEqual(ret, True, "Result failed.")
195 
196  ret = toBool("No", "YES", "NO", True)
197  self.assertEqual(ret, False, "Result failed.")
198 
199  def test_includes(self):
200  self.assertEqual(includes(["abc","abcde","ABC"],"abc"),True)
201  self.assertEqual(includes(["abc","abcde","ABC"],"a"),False)
202  self.assertEqual(includes(["abc","abcde","ABC"],"ABC"),True)
203  self.assertEqual(includes("abc,abcde,ABC","ABC"),True)
204  self.assertEqual(includes("abc,abcde,ABC","AbC",False),False)
205  self.assertEqual(includes(["abc","abcde","AbC"],"ABC"),True)
206  self.assertEqual(includes(["abc","abcde","AbC"],"ABC",False),False)
207 
208 
210  self.assertEqual(isAbsolutePath("/usr/loca/bin"), True, "Result failed.")
211  self.assertEqual(isAbsolutePath("c:\\"), True, "Result failed.")
212  self.assertEqual(isAbsolutePath("\\\\localhost"), True, "Result failed.")
213  # failed case
214  # self.assertEqual(isAbsolutePath("\\localhost"), True, "Result failed.")
215 
216 
217  def test_isURL(self):
218  self.assertEqual(isURL("http://www.google.co.jp"), True, "Result failed.")
219 
220  # failed case
221  # self.assertEqual(isURL("www.google.co.jp"), True, "Result failed.")
222  # self.assertEqual(isURL("http:://www.google.co.jp"), True, "Result failed.")
223 
224 
225  def test_otos(self):
226  self.assertEqual(otos(123), "123", "Result failed.")
227  self.assertEqual(otos("123"), "123", "Result failed.")
228  self.assertEqual(otos(123456789123456789), "123456789123456789", "Result failed.")
229  self.assertEqual(otos(0.123), "0.123", "Result failed.")
230 
231 
232  def test_stringTo(self):
233  int_ = [0]
234  long_ = [0]
235  float_ = [0.0]
236  list_ = [[0.0,0.0,0.0,0.0]]
237  str_ = [""]
238  stringTo(int_,"123")
239  stringTo(long_,"123")
240  stringTo(float_,"0.123")
241  stringTo(list_,"0,1.1,2.2,3.3")
242  stringTo(str_,"hoge")
243  print list_[0]
244  self.assertEqual(int_[0], 123, "Result failed.")
245  self.assertEqual(long_[0], 123, "Result failed.")
246  self.assertEqual(float_[0], 0.123, "Result failed.")
247  self.assertEqual(list_[0], [0,1.1,2.2,3.3], "Result failed.")
248  self.assertEqual(str_[0], "hoge", "Result failed.")
249 
250  # failed case
251  # self.assertEqual(stringTo("int",0.123), 0.123, "Result failed.")
252 
253 
254  def test_unique_sv(self):
255  sv = ["test0","test1","test0","test2","test0"]
256  sv_ret = unique_sv(sv)
257  self.assertEqual(sv_ret[0], "test0", "Result failed.")
258  self.assertEqual(sv_ret[1], "test1", "Result failed.")
259  self.assertEqual(sv_ret[2], "test2", "Result failed.")
260  self.assertEqual(len(sv_ret), 3, "Result failed.")
261 
262  # failed case: len(sv_ret) is 4
263  #
264  # sv = ["test0","test1","test0","test2","TEST0"]
265  # sv_ret = unique_sv(sv)
266  # self.assertEqual(len(sv_ret), 3, "Result failed.")
267 
268 
269  def test_flatten(self):
270  sv = ["test0","test1","test2"]
271  sv_ret = flatten(sv)
272  self.assertEqual(sv_ret, "test0, test1, test2","Result failed.")
273 
274  # failed case: Space is included.
275  # self.assertEqual(sv_ret, "test0,test1,test2","Result failed.")
276 
277 
278  def test_toArgv(self):
279  pass
280 
281 
282 
283 
284 if __name__ == '__main__':
285  unittest.main()
def replaceString(str, _from, _to)
Replace string.
Definition: StringUtil.py:305
def eraseHeadBlank(_str)
Erase the head blank characters of string.
Definition: StringUtil.py:255
def split(input, delimiter)
Split string by delimiter.
Definition: StringUtil.py:323
def unescape(_str)
Unescape string.
Definition: StringUtil.py:200
def otos(n)
Convert the given object to st::string.
Definition: StringUtil.py:487
def stringTo(_type, _str)
Convert the given object to st::string.
Definition: StringUtil.py:555
def escape(_str)
Escape string.
Definition: StringUtil.py:172
def isEscaped(_str, pos)
Whether the character is escaped or not.
Definition: StringUtil.py:44
def isURL(str)
Investigate whether the given string is URL or not.
Definition: StringUtil.py:462
def eraseTailBlank(_str)
Erase the tail blank characters of string.
Definition: StringUtil.py:271
def isAbsolutePath(str)
Investigate whether the given string is absolute path or not.
Definition: StringUtil.py:436
def includes(_list, value, ignore_case=True)
Include if a string is included in string list.
Definition: StringUtil.py:400
def eraseBlank(str)
Erase blank characters of string.
Definition: StringUtil.py:223
def toBool(_str, yes, no, default_value=None)
Convert given string to bool value.
Definition: StringUtil.py:360


openrtm_aist_python
Author(s): Shinji Kurihara
autogenerated on Mon Feb 28 2022 23:01:07