|
1 # *-* coding: utf-8 *-* |
|
2 # |
|
3 # Copyright (c) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 # All rights reserved. |
|
5 # This component and the accompanying materials are made available |
|
6 # under the terms of "Eclipse Public License v1.0" |
|
7 # which accompanies this distribution, and is available |
|
8 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
9 # |
|
10 # Initial Contributors: |
|
11 # Nokia Corporation - initial contribution. |
|
12 # |
|
13 # Contributors: |
|
14 # |
|
15 # Description: |
|
16 # |
|
17 |
|
18 |
|
19 """ |
|
20 Test the CPF root file parsing routines |
|
21 """ |
|
22 |
|
23 import zipfile |
|
24 import unittest |
|
25 import string |
|
26 import token |
|
27 import sys,os,re |
|
28 import __init__ |
|
29 |
|
30 from cone.public import utils, rules, api, exceptions |
|
31 |
|
32 ROOT_PATH = os.path.dirname(os.path.abspath(__file__)) |
|
33 testdata = os.path.abspath(os.path.join(ROOT_PATH,'utils-testdata')) |
|
34 |
|
35 class T(object): |
|
36 def __init__(self,str=""): self.str = str |
|
37 |
|
38 class TestResourceRefs(unittest.TestCase): |
|
39 def test_join_two_refs(self): |
|
40 self.assertEquals(utils.resourceref.join_refs(['foo/bar/test','fancy.ref']),'foo/bar/test/fancy.ref') |
|
41 self.assertEquals(utils.resourceref.join_refs(['foo/bar/test','/absref/fancy.ref']),'/absref/fancy.ref') |
|
42 |
|
43 def test_join_three_refs(self): |
|
44 self.assertEquals(utils.resourceref.join_refs(['foo/bar/test','fancy/ref','test1.12']),'foo/bar/test/fancy/ref/test1.12') |
|
45 |
|
46 def test_join_two_refs_with_empty_first_ref(self): |
|
47 self.assertEquals(utils.resourceref.join_refs(['','fancy/ref']),'fancy/ref') |
|
48 |
|
49 def test_join_two_refs_with_empty_last_ref(self): |
|
50 self.assertEquals(utils.resourceref.join_refs(['fancy/ref','']),'fancy/ref/') |
|
51 |
|
52 def test_join_two_refs_with_two_emtpy_refs(self): |
|
53 self.assertEquals(utils.resourceref.join_refs(['','']),'') |
|
54 |
|
55 def test_join_three_refs_with_empty(self): |
|
56 self.assertEquals(utils.resourceref.join_refs(['foo/bar/test','','test1/12']),'foo/bar/test/test1/12') |
|
57 |
|
58 def test_join_three_refs_with_extra_slashes(self): |
|
59 self.assertEquals(utils.resourceref.join_refs(['foo/bar/test/','/','one/']),'/one/') |
|
60 |
|
61 def test_join_three_refs_with_extra_slashes_and_begin_slash(self): |
|
62 self.assertEquals(utils.resourceref.join_refs(['/foo/bar/test/','/','one/']),'/one/') |
|
63 |
|
64 def test_filter_files(self): |
|
65 filtered = utils.resourceref.filter_resources(['test.txt','foo.dat','teat/data/bar.confml'],"\.txt") |
|
66 self.assertEquals(filtered[0],'test.txt') |
|
67 |
|
68 def test_filter_files_confmls(self): |
|
69 filtered = utils.resourceref.filter_resources(['test.txt','foo.confml','test/data/bar.confml'],"\.confml") |
|
70 self.assertEquals(filtered[0],'foo.confml') |
|
71 |
|
72 def test_neg_filter_resources(self): |
|
73 filtered = utils.resourceref.neg_filter_resources(['test.txt','foo.dat','teat/data/bar.confml'],"\.txt") |
|
74 self.assertEquals(filtered[0],'foo.dat') |
|
75 self.assertEquals(filtered[1],'teat/data/bar.confml') |
|
76 |
|
77 def test_filter_files_with_extension(self): |
|
78 filtered = utils.resourceref.filter_resources(['test.txt','foo.dat','teat/data/bar.confml'],".*\.dat") |
|
79 self.assertEquals(filtered[0],'foo.dat') |
|
80 |
|
81 def test_neg_filter_resources(self): |
|
82 filtered = utils.resourceref.neg_filter_resources(['/test/.svn/test.txt','.svn/test/foo.dat','teat/data/bar.confml'],"\.svn") |
|
83 self.assertEquals(filtered[0],'teat/data/bar.confml') |
|
84 |
|
85 def test_insert_begin_slash_without(self): |
|
86 self.assertEquals(utils.resourceref.insert_begin_slash('data/folder/siitake.txt'),'/data/folder/siitake.txt') |
|
87 |
|
88 def test_insert_begin_slash_with(self): |
|
89 self.assertEquals(utils.resourceref.insert_begin_slash('/data/folder/siitake.txt'),'/data/folder/siitake.txt') |
|
90 |
|
91 def test_remove_begin_slash_without(self): |
|
92 self.assertEquals(utils.resourceref.remove_begin_slash('data/folder/siitake.txt'),'data/folder/siitake.txt') |
|
93 |
|
94 def test_remove_beging_slash_with(self): |
|
95 self.assertEquals(utils.resourceref.remove_begin_slash('/data/folder/siitake.txt'),'data/folder/siitake.txt') |
|
96 |
|
97 def test_add_end_slash_without(self): |
|
98 self.assertEquals(utils.resourceref.add_end_slash('data/folder'),'data/folder/') |
|
99 |
|
100 def test_add_end_slash_with(self): |
|
101 self.assertEquals(utils.resourceref.add_end_slash('data/folder/'),'data/folder/') |
|
102 |
|
103 def test_remove_end_slash_without(self): |
|
104 self.assertEquals(utils.resourceref.remove_end_slash('data/folder'),'data/folder') |
|
105 |
|
106 def test_remove_end_slash_with(self): |
|
107 self.assertEquals(utils.resourceref.remove_end_slash('/data/folder/'),'/data/folder') |
|
108 |
|
109 def test_split_ref_with_empty_elem(self): |
|
110 self.assertEquals(utils.resourceref.split_ref(''),[]) |
|
111 |
|
112 def test_split_ref_two_elems(self): |
|
113 self.assertEquals(utils.resourceref.split_ref('aaa/bbb'),['aaa','bbb']) |
|
114 |
|
115 def test_split_ref_two_elems_and_begin_slash(self): |
|
116 self.assertEquals(utils.resourceref.split_ref('/aaa/bbb'),['aaa','bbb']) |
|
117 |
|
118 def test_split_ref_two_elems_and_end_slash(self): |
|
119 self.assertEquals(utils.resourceref.split_ref('aaa/bbb/'),['aaa','bbb']) |
|
120 |
|
121 def test_psplit_ref_with_tree_elems(self): |
|
122 self.assertEquals(utils.resourceref.psplit_ref('aaa/bbb/ccc.txt'),('aaa/bbb','ccc.txt')) |
|
123 |
|
124 def test_psplit_ref_with_two_elems(self): |
|
125 self.assertEquals(utils.resourceref.psplit_ref('aaa/bbb'),('aaa','bbb')) |
|
126 |
|
127 def test_psplit_ref_with_one_elems(self): |
|
128 self.assertEquals(utils.resourceref.psplit_ref('aaa.conf'),('','aaa.conf')) |
|
129 |
|
130 def test_psplit_ref_with_empty_elem(self): |
|
131 self.assertEquals(utils.resourceref.psplit_ref(''),('','')) |
|
132 |
|
133 def test_norm_ref(self): |
|
134 self.assertEquals(utils.resourceref.norm('dir/with/something/skeleton.txt'), |
|
135 'dir/with/something/skeleton.txt') |
|
136 |
|
137 def test_norm_ref_with_begin_dot(self): |
|
138 self.assertEquals(utils.resourceref.norm('./skeleton.txt'), |
|
139 'skeleton.txt') |
|
140 |
|
141 def test_norm_ref_with_begin_dot(self): |
|
142 self.assertEquals(utils.resourceref.norm('.svn'), |
|
143 '.svn') |
|
144 |
|
145 def test_norm_ref_with_empty_string(self): |
|
146 self.assertEquals(utils.resourceref.norm(''), |
|
147 '') |
|
148 |
|
149 def test_norm_ref_with_backslash(self): |
|
150 self.assertEquals(utils.resourceref.norm('dir\\with\\something\\skeleton.txt'), |
|
151 'dir/with/something/skeleton.txt') |
|
152 |
|
153 def test_norm_ref_with_backslash_and_forward_slash(self): |
|
154 self.assertEquals(utils.resourceref.norm('dir\\with/something\\skeleton.txt'), |
|
155 'dir/with/something/skeleton.txt') |
|
156 |
|
157 def test_norm_ref_with_begin_slash(self): |
|
158 self.assertEquals(utils.resourceref.norm('/dir/with/something\\skeleton.txt'), |
|
159 '/dir/with/something/skeleton.txt') |
|
160 |
|
161 def test_norm_ref_with_begin_backslash(self): |
|
162 self.assertEquals(utils.resourceref.norm('\\dir\\with\\something\\skeleton.txt'), |
|
163 '/dir/with/something/skeleton.txt') |
|
164 |
|
165 def test_norm_ref_with_end_slash(self): |
|
166 self.assertEquals(utils.resourceref.norm('/dir/with/something/'), |
|
167 '/dir/with/something') |
|
168 |
|
169 def test_norm_ref_with_end_backslash(self): |
|
170 self.assertEquals(utils.resourceref.norm('dir/with/something\\'), |
|
171 'dir/with/something') |
|
172 |
|
173 def test_norm_ref_with_spaces(self): |
|
174 self.assertEquals(utils.resourceref.norm('dir/with/some thing/some.txt'), |
|
175 'dir/with/some thing/some.txt') |
|
176 |
|
177 def test_norm_ref_with_spaces_and_hyphens(self): |
|
178 self.assertEquals(utils.resourceref.norm('"dir/with/some thing/some.txt"'), |
|
179 'dir/with/some thing/some.txt') |
|
180 |
|
181 def test_norm_ref_with_spaces_and_hyphens_and_begin_slash(self): |
|
182 self.assertEquals(utils.resourceref.norm('"/dir/with/some thing/some.txt"'), |
|
183 '/dir/with/some thing/some.txt') |
|
184 |
|
185 def test_replace_dir_part(self): |
|
186 filename = utils.resourceref.replace_dir('dir/to/replace/with/something/skeleton.txt', |
|
187 'dir/to\\replace', |
|
188 'somethingelse/123/') |
|
189 self.assertEquals(filename,'somethingelse/123/with/something/skeleton.txt') |
|
190 |
|
191 def test_replace_dir_part_with_one_part(self): |
|
192 filename = utils.resourceref.replace_dir('dir/to/replace/with/dir/dir.txt', |
|
193 'dir', |
|
194 'somethingelse/123/') |
|
195 self.assertEquals(filename,'somethingelse/123/to/replace/with/dir/dir.txt') |
|
196 |
|
197 def test_replace_dir_part_with_empty_target(self): |
|
198 filename = utils.resourceref.replace_dir('dir/to/replace/with/dir/dir.txt', |
|
199 'dir', |
|
200 '') |
|
201 self.assertEquals(filename,'to/replace/with/dir/dir.txt') |
|
202 |
|
203 def test_replace_dir_part_with_no_match(self): |
|
204 filename = utils.resourceref.replace_dir('foo\\skeleton.txt', |
|
205 'dir/to\\replace', |
|
206 'somethingelse/123/') |
|
207 self.assertEquals(filename,'foo/skeleton.txt') |
|
208 |
|
209 def test_replace_dir_part_with_begin_slash(self): |
|
210 filename = utils.resourceref.replace_dir('dir/to/replace/with/dir/dir.txt', |
|
211 'dir/to/replace', |
|
212 '/epoc32/testing') |
|
213 self.assertEquals(filename,'/epoc32/testing/with/dir/dir.txt') |
|
214 |
|
215 def test_replace_dir_with_match(self): |
|
216 filename = utils.resourceref.replace_dir('foo/test', |
|
217 'foo/test', |
|
218 '') |
|
219 self.assertEquals(filename,'') |
|
220 |
|
221 def test_replace_dir_from_empty(self): |
|
222 filename = utils.resourceref.replace_dir('foo/test', |
|
223 '', |
|
224 'output/bar') |
|
225 self.assertEquals(filename,'output/bar/foo/test') |
|
226 |
|
227 def test_remove_ext(self): |
|
228 self.assertEquals(utils.resourceref.remove_ext('fii/faa/foo.confml'),'fii/faa/foo') |
|
229 |
|
230 def test_remove_ext_from_dot_beginning_file(self): |
|
231 self.assertEquals(utils.resourceref.remove_ext('.metadata'),'.metadata') |
|
232 |
|
233 def test_remove_ext_from_file_without_ext(self): |
|
234 self.assertEquals(utils.resourceref.remove_ext('fii/faa/foo'),'fii/faa/foo') |
|
235 |
|
236 def test_remove_ext_from_file_without_path(self): |
|
237 self.assertEquals(utils.resourceref.remove_ext('fii.faa.foo'),'fii.faa') |
|
238 |
|
239 def test_get_ext(self): |
|
240 self.assertEquals(utils.resourceref.get_ext('fii/faa/foo.confml'),'confml') |
|
241 |
|
242 def test_get_ext_without_ext(self): |
|
243 self.assertEquals(utils.resourceref.get_ext('fii/faa/foo'),'') |
|
244 |
|
245 def test_get_filename(self): |
|
246 self.assertEquals(utils.resourceref.get_filename('fii/faa/foo.confml'),'foo.confml') |
|
247 |
|
248 def test_get_filename_without_filename(self): |
|
249 self.assertEquals(utils.resourceref.get_filename('fii/faa/'),'') |
|
250 |
|
251 def test_get_filename_with_onepart(self): |
|
252 self.assertEquals(utils.resourceref.get_filename('fii'),'fii') |
|
253 |
|
254 def test_get_path(self): |
|
255 self.assertEquals(utils.resourceref.get_path('fii/faa/foo.confml'),'fii/faa') |
|
256 |
|
257 def test_get_path_without_filename(self): |
|
258 self.assertEquals(utils.resourceref.get_path('fii/faa/'),'fii/faa') |
|
259 |
|
260 def test_get_path_with_onepart(self): |
|
261 self.assertEquals(utils.resourceref.get_path('fii'),'') |
|
262 |
|
263 def test_to_dottedref(self): |
|
264 self.assertEquals(utils.resourceref.to_dottedref('fii/faa/foo.confml'),'fii_faa_foo') |
|
265 |
|
266 def test_to_dottedref_from_single_elem(self): |
|
267 self.assertEquals(utils.resourceref.to_dottedref('fii'),'fii') |
|
268 |
|
269 def test_to_dottedref_from_dotted(self): |
|
270 self.assertEquals(utils.resourceref.to_dottedref('fii.faa.foo'),'fii.faa') |
|
271 |
|
272 def test_to_dottedref_with_ext(self): |
|
273 self.assertEquals(utils.resourceref.to_dottedref('.metadata'),'metadata') |
|
274 |
|
275 def test_to_objref_dotted_start(self): |
|
276 self.assertEquals(utils.resourceref.to_objref('../foo/bar/test.confml'),'____foo__bar__test_confml') |
|
277 |
|
278 def test_to_objref_dot_in_name_start(self): |
|
279 self.assertEquals(utils.resourceref.to_objref('/foo.bar/test.confml'),'_foo_bar_test_confml') |
|
280 |
|
281 def test_to_objref_number_in_name_start(self): |
|
282 self.assertEquals(utils.resourceref.to_objref('_1.0.test_one'),'_1_0_test_one') |
|
283 self.assertEquals(utils.resourceref.to_objref('0.test_one'),'_0_test_one') |
|
284 self.assertEquals(utils.resourceref.to_objref('1.0.test_one'),'_1_0_test_one') |
|
285 |
|
286 def test_to_objref_unicode(self): |
|
287 self.assertEquals(utils.resourceref.to_objref(u'fooäbar'),'foo_bar') |
|
288 self.assertEquals(utils.resourceref.to_objref(u'foo:bar-testöne'),'foo_bar_test_ne') |
|
289 self.assertEquals(utils.resourceref.to_objref('foo1.1test.confml'),'foo1_1test_confml') |
|
290 self.assertEquals(utils.resourceref.to_objref('sub:417-48964:_hw_drv:_display_driver_common_features_-_thermal_event_handling_in_rdisplaykernel_and_thermal_management_for_display_module_behind_feature_flag'), |
|
291 'sub_417_48964__hw_drv__display_driver_common_features___thermal_event_handling_in_rdisplaykernel_and_thermal_management_for_display_module_behind_feature_flag') |
|
292 |
|
293 def test_to_objref_dot_in_name_start(self): |
|
294 self.assertEquals(utils.resourceref.to_objref('/foo.bar/test.confml'),'__foo_bar__test_confml') |
|
295 |
|
296 def test_to_hash_dotted_start(self): |
|
297 self.assertEquals(utils.resourceref.to_hash('../foo/bar/test.confml'),'0x5a063087') |
|
298 self.assertEquals(utils.resourceref.to_hash('../foo/bar/test.confml'),'0x5a063087') |
|
299 |
|
300 def test_to_hash(self): |
|
301 self.assertEquals(utils.resourceref.to_hash('../fo.o/number.txt'),'-0x1b381b13') |
|
302 |
|
303 def test_to_dref(self): |
|
304 self.assertEquals(utils.resourceref.to_dref('fii/faa/foo.confml'),'fii.faa.foo') |
|
305 |
|
306 def test_to_dref_with_ext(self): |
|
307 self.assertEquals(utils.resourceref.to_dref('.metadata'),'metadata') |
|
308 |
|
309 def test_to_dref_from_single_elem(self): |
|
310 self.assertEquals(utils.resourceref.to_dref('fii'),'fii') |
|
311 |
|
312 def test_to_dref_from_dotted(self): |
|
313 self.assertEquals(utils.resourceref.to_dref('fii.faa.foo'),'fii.faa') |
|
314 |
|
315 class TestUtils(unittest.TestCase): |
|
316 def test_distinct_array_with_single_values(self): |
|
317 self.assertEquals(utils.distinct_array(['1','2','1','1']),['1','2']) |
|
318 |
|
319 def test_distinct_array_with_single_values(self): |
|
320 self.assertEquals(utils.distinct_array(['1','2','3','1','2','4']),['1','2','3','4']) |
|
321 |
|
322 def test_list_files_from_testdata(self): |
|
323 files = utils.list_files(testdata) |
|
324 self.assertTrue(files[0].endswith('scrot.txt')) |
|
325 self.assertTrue(files[1].endswith('test.txt')) |
|
326 |
|
327 def test_list_files_from_current_dir(self): |
|
328 files = utils.list_files('.') |
|
329 self.assertTrue(len(files)>0) |
|
330 |
|
331 def test_pathmatch_with_same_path(self): |
|
332 self.assertEquals(utils.pathmatch('test.foo','test.foo'),True) |
|
333 |
|
334 def test_pathmatch_with_diff_path(self): |
|
335 self.assertEquals(utils.pathmatch('test.bar','test.foo'),False) |
|
336 |
|
337 def test_pathmatch_with_star(self): |
|
338 self.assertEquals(utils.pathmatch('test.foo.*','test.foo.bar'),True) |
|
339 |
|
340 def test_pathmatch_with_star(self): |
|
341 self.assertEquals(utils.pathmatch('test.foo.*','test.foo.bar.fiba'),False) |
|
342 |
|
343 def test_pathmatch_with_twostar(self): |
|
344 self.assertEquals(utils.pathmatch('test.foo.**','test.foo.bar.fiba'),True) |
|
345 |
|
346 def test_filter_objs_ref(self): |
|
347 obj = T("com.nokia.foo.bar") |
|
348 filters = [] |
|
349 filters.append(lambda x: re.match(".*nokia.*", x.str)) |
|
350 self.assertEquals(utils.filter(obj,filters),True) |
|
351 |
|
352 def test_filter_objs_ref_not(self): |
|
353 obj = T("foo.bar") |
|
354 filters = [] |
|
355 filters.append(lambda x: re.match("foo$", x.str)) |
|
356 self.assertEquals(utils.filter(obj,filters),False) |
|
357 |
|
358 def test_filter_objs_type(self): |
|
359 obj = T() |
|
360 filters = [] |
|
361 filters.append(lambda x: isinstance(x,T)) |
|
362 self.assertEquals(utils.filter(obj,filters),True) |
|
363 |
|
364 def test_filter_objs_type_not(self): |
|
365 class F(object): pass |
|
366 obj = T() |
|
367 filters = [] |
|
368 filters.append(lambda x: isinstance(x,F)) |
|
369 self.assertEquals(utils.filter(obj,filters),False) |
|
370 |
|
371 class TestDottedRefs(unittest.TestCase): |
|
372 def test_join_two_refs(self): |
|
373 self.assertEquals(utils.dottedref.join_refs(['foo.bar.test','fancy.ref']),'foo.bar.test.fancy.ref') |
|
374 |
|
375 def test_join_three_refs(self): |
|
376 self.assertEquals(utils.dottedref.join_refs(['foo.bar.test','fancy.ref','test1.12']),'foo.bar.test.fancy.ref.test1.12') |
|
377 |
|
378 def test_join_two_refs_with_empty_first_ref(self): |
|
379 self.assertEquals(utils.dottedref.join_refs(['','fancy.ref']),'fancy.ref') |
|
380 |
|
381 def test_join_two_refs_with_empty_last_ref(self): |
|
382 self.assertEquals(utils.dottedref.join_refs(['fancy.ref','']),'fancy.ref') |
|
383 |
|
384 def test_join_two_refs_with_two_emtpy_refs(self): |
|
385 self.assertEquals(utils.dottedref.join_refs(['','']),'') |
|
386 |
|
387 def test_join_three_refs_with_empty(self): |
|
388 self.assertEquals(utils.dottedref.join_refs(['foo.bar.test','','test1.12']),'foo.bar.test.test1.12') |
|
389 |
|
390 def test_split_ref_with_tree_elems(self): |
|
391 self.assertEquals(utils.dottedref.split_ref('aaa.bbb.ccc'),['aaa','bbb','ccc']) |
|
392 |
|
393 def test_split_ref_with_empty_elem(self): |
|
394 self.assertEquals(utils.dottedref.split_ref(''),[]) |
|
395 |
|
396 def test_split_ref_with_empty_elem_between(self): |
|
397 self.assertEquals(utils.dottedref.split_ref('aaa..bbb'),['aaa','bbb']) |
|
398 |
|
399 def test_psplit_ref_with_tree_elems(self): |
|
400 self.assertEquals(utils.dottedref.psplit_ref('aaa.bbb.ccc'),('aaa.bbb','ccc')) |
|
401 |
|
402 def test_psplit_ref_with_two_elems(self): |
|
403 self.assertEquals(utils.dottedref.psplit_ref('aaa.bbb'),('aaa','bbb')) |
|
404 |
|
405 def test_psplit_ref_with_one_elems(self): |
|
406 self.assertEquals(utils.dottedref.psplit_ref('aaa'),('','aaa')) |
|
407 |
|
408 def test_psplit_ref_with_empty_elem(self): |
|
409 self.assertEquals(utils.dottedref.psplit_ref(''),('','')) |
|
410 |
|
411 def test_ref2filter_with_one_elem(self): |
|
412 self.assertEquals(utils.dottedref.ref2filter('test'),'test$') |
|
413 |
|
414 def test_ref2filter_with_two_elems(self): |
|
415 self.assertEquals(utils.dottedref.ref2filter('test.foo'),'test\.foo$') |
|
416 |
|
417 def test_ref2filter_with_three_elems(self): |
|
418 self.assertEquals(utils.dottedref.ref2filter('test.foo.bar'),'test\.foo\.bar$') |
|
419 |
|
420 def test_ref2filter_with_one_star(self): |
|
421 self.assertEquals(utils.dottedref.ref2filter('*'),'[^\.]*$') |
|
422 |
|
423 def test_ref2filter_with_two_elems_one_star(self): |
|
424 self.assertEquals(utils.dottedref.ref2filter('test.foo.*'),'test\.foo\.[^\.]*$') |
|
425 |
|
426 def test_ref2filter_with_two_elems_two_stars(self): |
|
427 self.assertEquals(utils.dottedref.ref2filter('test.foo.*'),'test\.foo\.[^\.]*$') |
|
428 |
|
429 def test_remove_extension(self): |
|
430 self.assertEquals(utils.dottedref.remove_last('fii/foo.dat'),'fii/foo') |
|
431 |
|
432 def test_remove_last_elem(self): |
|
433 self.assertEquals(utils.dottedref.remove_last('fii.foo.faa.dat'),'fii.foo.faa') |
|
434 |
|
435 def test_remove_last_elem_from_single(self): |
|
436 self.assertEquals(utils.dottedref.remove_last('fii'),'fii') |
|
437 |
|
438 def test_get_last_elem_from_single(self): |
|
439 self.assertEquals(utils.dottedref.get_last('fii'),'fii') |
|
440 |
|
441 def test_get_last_elem_from_empty(self): |
|
442 self.assertEquals(utils.dottedref.get_last(''),'') |
|
443 |
|
444 def test_get_last_elem_from_multi(self): |
|
445 self.assertEquals(utils.dottedref.get_last('fii.faa.foo'),'foo') |
|
446 |
|
447 def test_remove_begin_dot_without(self): |
|
448 self.assertEquals(utils.dottedref.remove_begin_dot('data.folder.siitake'), |
|
449 'data.folder.siitake') |
|
450 |
|
451 def test_remove_beging_dot_with(self): |
|
452 self.assertEquals(utils.dottedref.remove_begin_dot('.data.folder.siitake'), |
|
453 'data.folder.siitake') |
|
454 |
|
455 def test_get_list_index(self): |
|
456 self.assertEquals(utils.dottedref.get_index('data[0]'),0) |
|
457 |
|
458 def test_get_list_with_long_index(self): |
|
459 self.assertEquals(utils.dottedref.get_index('data[123]'),123) |
|
460 |
|
461 def test_get_list_index_with_no_index(self): |
|
462 self.assertEquals(utils.dottedref.get_index('data'),None) |
|
463 |
|
464 def test_get_name_with_index(self): |
|
465 self.assertEquals(utils.dottedref.get_name('data[0]'),'data') |
|
466 |
|
467 def test_get_name_normal(self): |
|
468 self.assertEquals(utils.dottedref.get_name('data'),'data') |
|
469 |
|
470 def test_extract_delimited_tokens(self): |
|
471 def check(expected, string, delimiters=None): |
|
472 if delimiters != None: actual = utils.extract_delimited_tokens(string, delimiters) |
|
473 else: actual = utils.extract_delimited_tokens(string) |
|
474 self.assertEquals(expected, actual) |
|
475 |
|
476 check([], '') |
|
477 check(['x'], '${x}') |
|
478 check(['x'], '@{x}', delimiters=('@{', '}')) |
|
479 check(['my.ref1', 'my.ref2'],"test1 ${my.ref1} test2 ${ my.ref1 } ${my.ref2}") |
|
480 check(['my.ref1', 'my\nmultiline\nref'],"test1 ${my.ref1} test2 ${ my.ref1 } ${my\nmultiline\nref}") |
|
481 check(['my.ref1', 'my.ref2', u'????????.????????'], u"test1 ${my.ref1} test2 ${ my.ref2 } ${????????.????????}") |
|
482 |
|
483 def test_extract_delimited_token_tuples(self): |
|
484 def check(expected, string, delimiters=None): |
|
485 if delimiters != None: actual = utils.extract_delimited_token_tuples(string, delimiters) |
|
486 else: actual = utils.extract_delimited_token_tuples(string) |
|
487 self.assertEquals(expected, actual) |
|
488 |
|
489 check([], '') |
|
490 check([('x', '${x}')], '${x}') |
|
491 check([('x', '@{x}')], '@{x}', delimiters=('@{', '}')) |
|
492 check([('my.ref1', '${my.ref1}'), ('my.ref1', '${ my.ref1 }'), ('my.ref2', '${my.ref2}')], |
|
493 "test1 ${my.ref1} test2 ${ my.ref1 } ${my.ref2} yeah ${my.ref2}") |
|
494 check([('my.ref1', '${my.ref1}'), ('my.ref1', '${ my.ref1 }'), ('my\nmultiline\nref', '${my\nmultiline\nref}')], |
|
495 "test1 ${my.ref1} test2 ${ my.ref1 } ${my\nmultiline\nref}") |
|
496 check([('my.ref1', '${my.ref1}'), ('my.ref2', '${ my.ref2 }'), (u'????????.????????', u'${????????.????????}')], |
|
497 u"test1 ${my.ref1} test2 ${ my.ref2 } ${????????.????????}") |
|
498 |
|
499 def test_expand_delimited_tokens(self): |
|
500 def check(expected, string, delimiters=None): |
|
501 def expand(ref, index): |
|
502 return "<%d: %s>" % (index, ref) |
|
503 if delimiters != None: actual = utils.expand_delimited_tokens(string, expand, delimiters) |
|
504 else: actual = utils.expand_delimited_tokens(string, expand) |
|
505 self.assertEquals(expected, actual) |
|
506 |
|
507 check('test', 'test') |
|
508 check('<0: x>', '${x}') |
|
509 check('<0: x>', '@{x}', delimiters=('@{', '}')) |
|
510 check('<0: x> <1: y> <0: x>', '${x} ${y} ${ x }') |
|
511 check(u'<0: my.ref1> test <1: ????????.????????>', u'${ my.ref1 } test ${????????.????????}') |
|
512 check('<0: my.ref1> test <1: my\nmultiline\nref>', '${ my.ref1} test ${my\nmultiline\nref}') |
|
513 |
|
514 def test_expand_refs_by_default_view(self): |
|
515 VALUES = {'Test.Color' : 'brown', |
|
516 'Test.Animal1': 'fox', |
|
517 'Test.Animal2': 'dog'} |
|
518 class DummyFeature(object): |
|
519 def __init__(self, ref): |
|
520 self.ref = ref |
|
521 def get_original_value(self): |
|
522 return VALUES[self.ref] |
|
523 class DummyDefaultView(object): |
|
524 def get_feature(self, ref): |
|
525 if ref in VALUES: return DummyFeature(ref) |
|
526 else: raise exceptions.NotFound() |
|
527 dview = DummyDefaultView() |
|
528 |
|
529 result = utils.expand_refs_by_default_view( |
|
530 "The quick ${Test.Color} ${Test.Animal1} jumps over the lazy ${Test.Animal2}.", |
|
531 dview) |
|
532 self.assertEquals(result, "The quick brown fox jumps over the lazy dog.") |
|
533 |
|
534 # Test expanding with a non-existent ref |
|
535 result = utils.expand_refs_by_default_view( |
|
536 "The quick ${Test.Color} ${Test.Foo} jumps over the lazy ${Test.Animal2}.", |
|
537 dview) |
|
538 self.assertEquals(result, "The quick brown jumps over the lazy dog.") |
|
539 |
|
540 # Test expanding with a non-existent ref and a custom default value |
|
541 result = utils.expand_refs_by_default_view( |
|
542 "The quick ${Test.Color} ${Test.Foo} jumps over the lazy ${Test.Animal2}.", |
|
543 dview, |
|
544 default_value_for_missing = 'giraffe') |
|
545 self.assertEquals(result, "The quick brown giraffe jumps over the lazy dog.") |
|
546 |
|
547 |
|
548 class TestUtilsSubclasses(unittest.TestCase): |
|
549 |
|
550 def setUp(self): |
|
551 pass |
|
552 |
|
553 def test_all_subclasses_with_none(self): |
|
554 class base(object): pass |
|
555 classnames = utils.all_subclasses(base) |
|
556 self.assertEquals(len(classnames),0) |
|
557 |
|
558 def test_all_subclasses_class_tree(self): |
|
559 class base(object): pass |
|
560 class class1(base): pass |
|
561 class class2(base): pass |
|
562 class class12(class1): pass |
|
563 class class123(class2): pass |
|
564 classnames = utils.all_subclasses(base) |
|
565 self.assertEquals(len(classnames),4) |
|
566 |
|
567 class TestUtilsDataMapRef(unittest.TestCase): |
|
568 |
|
569 def setUp(self): |
|
570 pass |
|
571 |
|
572 def test_get_feature_ref(self): |
|
573 map = "foo/bar[@key='key 1']" |
|
574 ref = utils.DataMapRef.get_feature_ref(map) |
|
575 self.assertEquals(ref,'foo/bar') |
|
576 |
|
577 def test_get_key_value(self): |
|
578 map = "foo/bar[@key='key 1']" |
|
579 value = utils.DataMapRef.get_key_value(map) |
|
580 self.assertEquals(value,'key 1') |
|
581 |
|
582 |
|
583 class TestMakeList(unittest.TestCase): |
|
584 def test_get_list_string(self): |
|
585 self.assertEquals(utils.get_list('test'), ['test']) |
|
586 |
|
587 def test_get_list_from_list(self): |
|
588 self.assertEquals(utils.get_list(['test']), ['test']) |
|
589 |
|
590 def test_get_list_from_list2(self): |
|
591 self.assertEquals(utils.get_list(['test','test2']), ['test','test2']) |
|
592 |
|
593 def test_is_list(self): |
|
594 self.assertEquals(utils.is_list(['test','test2']), True) |
|
595 |
|
596 def test_is_list_false(self): |
|
597 self.assertEquals(utils.is_list('test'), False) |
|
598 |
|
599 def test_add_list_with_list(self): |
|
600 self.assertEquals(utils.add_list(['test','test2'], 'foo'), ['test','test2','foo']) |
|
601 |
|
602 def test_add_list_with_none(self): |
|
603 self.assertEquals(utils.add_list([], 'foo'), ['foo']) |
|
604 |
|
605 def test_add_list_with_string(self): |
|
606 self.assertEquals(utils.add_list('bar', 'foo'), ['bar','foo']) |
|
607 |
|
608 def test_add_list_with_string2(self): |
|
609 self.assertEquals(utils.add_list(['bar','test'], 'foo'), ['bar','test','foo']) |
|
610 |
|
611 def test_prepend_list_with_none(self): |
|
612 self.assertEquals(utils.prepend_list([], 'foo'), ['foo']) |
|
613 |
|
614 def test_prepend_string_with_string(self): |
|
615 self.assertEquals(utils.prepend_list('bar', 'foo'), ['foo','bar']) |
|
616 |
|
617 def test_prepend_list_with_string(self): |
|
618 self.assertEquals(utils.prepend_list(['bar','test'], 'foo'), ['foo','bar','test']) |
|
619 |
|
620 from cone.confml import model as confmlmodel |
|
621 |
|
622 class TestModelGetters(unittest.TestCase): |
|
623 def test_get_module_specific_class(self): |
|
624 cls = utils.get_class(confmlmodel,api.Configuration) |
|
625 self.assertEquals(cls, confmlmodel.ConfmlConfiguration) |
|
626 |
|
627 def test_get_module_specific_class_with_None_model(self): |
|
628 cls = utils.get_class(None,api.Configuration) |
|
629 self.assertEquals(cls, api.Configuration) |
|
630 |
|
631 class TestXmlUtilFunctions(unittest.TestCase): |
|
632 def test_split_tag_namespace(self): |
|
633 self.assertEquals( |
|
634 utils.xml.split_tag_namespace("test"), |
|
635 (None, 'test')) |
|
636 |
|
637 self.assertEquals( |
|
638 utils.xml.split_tag_namespace("{http://www.test.com/xml/1}test"), |
|
639 ('http://www.test.com/xml/1', 'test')) |
|
640 |
|
641 if __name__ == '__main__': |
|
642 unittest.main() |
|
643 |