configurationengine/source/cone/public/tests/unittest_utils.py
changeset 3 e7e0ae78773e
parent 0 2e8eeb919028
child 5 d2c80f5cab53
equal deleted inserted replaced
2:87cfa131b535 3:e7e0ae78773e
    18 
    18 
    19 """
    19 """
    20 Test the CPF root file parsing routines
    20 Test the CPF root file parsing routines
    21 """
    21 """
    22 
    22 
    23 import zipfile
       
    24 import unittest
    23 import unittest
    25 import string
    24 import StringIO
    26 import token
    25 import os,re
    27 import sys,os,re
    26 
    28 import __init__
    27 from cone.public import utils, api, exceptions
    29 
       
    30 from cone.public import utils, rules, api, exceptions
       
    31 
    28 
    32 ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
    29 ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
    33 testdata  = os.path.abspath(os.path.join(ROOT_PATH,'utils-testdata'))
    30 testdata  = os.path.abspath(os.path.join(ROOT_PATH,'utils-testdata'))
    34 
    31 
    35 class T(object): 
    32 class T(object): 
    76 
    73 
    77     def test_filter_files_with_extension(self):
    74     def test_filter_files_with_extension(self):
    78         filtered = utils.resourceref.filter_resources(['test.txt','foo.dat','teat/data/bar.confml'],".*\.dat")
    75         filtered = utils.resourceref.filter_resources(['test.txt','foo.dat','teat/data/bar.confml'],".*\.dat")
    79         self.assertEquals(filtered[0],'foo.dat')
    76         self.assertEquals(filtered[0],'foo.dat')
    80 
    77 
    81     def test_neg_filter_resources(self):
    78     def test_neg_filter_resources2(self):
    82         filtered = utils.resourceref.neg_filter_resources(['/test/.svn/test.txt','.svn/test/foo.dat','teat/data/bar.confml'],"\.svn")
    79         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')
    80         self.assertEquals(filtered[0],'teat/data/bar.confml')
    84 
    81 
    85     def test_insert_begin_slash_without(self):
    82     def test_insert_begin_slash_without(self):
    86         self.assertEquals(utils.resourceref.insert_begin_slash('data/folder/siitake.txt'),'/data/folder/siitake.txt')
    83         self.assertEquals(utils.resourceref.insert_begin_slash('data/folder/siitake.txt'),'/data/folder/siitake.txt')
   136 
   133 
   137     def test_norm_ref_with_begin_dot(self):
   134     def test_norm_ref_with_begin_dot(self):
   138         self.assertEquals(utils.resourceref.norm('./skeleton.txt'),
   135         self.assertEquals(utils.resourceref.norm('./skeleton.txt'),
   139                                      'skeleton.txt')
   136                                      'skeleton.txt')
   140 
   137 
   141     def test_norm_ref_with_begin_dot(self):
   138     def test_norm_ref_with_begin_dot2(self):
   142         self.assertEquals(utils.resourceref.norm('.svn'),
   139         self.assertEquals(utils.resourceref.norm('.svn'),
   143                                      '.svn')
   140                                      '.svn')
   144 
   141 
   145     def test_norm_ref_with_empty_string(self):
   142     def test_norm_ref_with_empty_string(self):
   146         self.assertEquals(utils.resourceref.norm(''),
   143         self.assertEquals(utils.resourceref.norm(''),
   272     def test_to_dottedref_with_ext(self):
   269     def test_to_dottedref_with_ext(self):
   273         self.assertEquals(utils.resourceref.to_dottedref('.metadata'),'metadata')
   270         self.assertEquals(utils.resourceref.to_dottedref('.metadata'),'metadata')
   274 
   271 
   275     def test_to_objref_dotted_start(self):
   272     def test_to_objref_dotted_start(self):
   276         self.assertEquals(utils.resourceref.to_objref('../foo/bar/test.confml'),'____foo__bar__test_confml')
   273         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 
   274 
   281     def test_to_objref_number_in_name_start(self):
   275     def test_to_objref_number_in_name_start(self):
   282         self.assertEquals(utils.resourceref.to_objref('_1.0.test_one'),'_1_0_test_one')
   276         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')
   277         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')
   278         self.assertEquals(utils.resourceref.to_objref('1.0.test_one'),'_1_0_test_one')
   291                           'sub_417_48964__hw_drv__display_driver_common_features___thermal_event_handling_in_rdisplaykernel_and_thermal_management_for_display_module_behind_feature_flag')
   285                           'sub_417_48964__hw_drv__display_driver_common_features___thermal_event_handling_in_rdisplaykernel_and_thermal_management_for_display_module_behind_feature_flag')
   292 
   286 
   293     def test_to_objref_dot_in_name_start(self):
   287     def test_to_objref_dot_in_name_start(self):
   294         self.assertEquals(utils.resourceref.to_objref('/foo.bar/test.confml'),'__foo_bar__test_confml')
   288         self.assertEquals(utils.resourceref.to_objref('/foo.bar/test.confml'),'__foo_bar__test_confml')
   295 
   289 
       
   290     def test_to_objref_with_python_comment(self):
       
   291         ##STRATEGIC## Customization offering for ##PROD_CODE##
       
   292         self.assertEquals(utils.resourceref.to_objref('##STRATEGIC## Customization offering for ##PROD_CODE##'),
       
   293                                                       '__STRATEGIC__Customizationofferingfor__PROD_CODE__')
       
   294         self.assertEquals(utils.resourceref.to_objref('Feature A ## Testing ##'),
       
   295                                                       'FeatureA__Testing__')
       
   296 
   296     def test_to_hash_dotted_start(self):
   297     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')
   298         self.assertEquals(utils.resourceref.to_hash('../foo/bar/test.confml'),'0x5a063087')
   299         self.assertEquals(utils.resourceref.to_hash('../foo/bar/test.confml'),'0x5a063087')
   299 
   300 
   300     def test_to_hash(self):
   301     def test_to_hash(self):
   310         self.assertEquals(utils.resourceref.to_dref('fii'),'fii')
   311         self.assertEquals(utils.resourceref.to_dref('fii'),'fii')
   311 
   312 
   312     def test_to_dref_from_dotted(self):
   313     def test_to_dref_from_dotted(self):
   313         self.assertEquals(utils.resourceref.to_dref('fii.faa.foo'),'fii.faa')
   314         self.assertEquals(utils.resourceref.to_dref('fii.faa.foo'),'fii.faa')
   314 
   315 
       
   316     def test_is_path(self):
       
   317         self.assertEquals(utils.resourceref.is_path('foo'),False)
       
   318         self.assertEquals(utils.resourceref.is_path('foo.txt'),True)
       
   319         self.assertEquals(utils.resourceref.is_path('bar/foo'),True)
       
   320         self.assertEquals(utils.resourceref.is_path('bar/foo.txt'),True)
       
   321 
   315 class TestUtils(unittest.TestCase):    
   322 class TestUtils(unittest.TestCase):    
   316     def test_distinct_array_with_single_values(self):
   323     def test_distinct_array_with_single_values(self):
   317         self.assertEquals(utils.distinct_array(['1','2','1','1']),['1','2'])
   324         self.assertEquals(utils.distinct_array(['1','2','1','1']),['1','2'])
   318 
   325 
   319     def test_distinct_array_with_single_values(self):
   326     def test_distinct_array_with_single_values2(self):
   320         self.assertEquals(utils.distinct_array(['1','2','3','1','2','4']),['1','2','3','4'])
   327         self.assertEquals(utils.distinct_array(['1','2','3','1','2','4']),['1','2','3','4'])
   321 
   328 
   322     def test_list_files_from_testdata(self):
   329     def test_list_files_from_testdata(self):
   323         files = utils.list_files(testdata)
   330         files = utils.list_files(testdata)
   324         self.assertTrue(files[0].endswith('scrot.txt'))
   331         self.assertTrue(files[0].endswith('scrot.txt'))
   335         self.assertEquals(utils.pathmatch('test.bar','test.foo'),False)
   342         self.assertEquals(utils.pathmatch('test.bar','test.foo'),False)
   336 
   343 
   337     def test_pathmatch_with_star(self):
   344     def test_pathmatch_with_star(self):
   338         self.assertEquals(utils.pathmatch('test.foo.*','test.foo.bar'),True)
   345         self.assertEquals(utils.pathmatch('test.foo.*','test.foo.bar'),True)
   339 
   346 
   340     def test_pathmatch_with_star(self):
   347     def test_pathmatch_with_star2(self):
   341         self.assertEquals(utils.pathmatch('test.foo.*','test.foo.bar.fiba'),False)
   348         self.assertEquals(utils.pathmatch('test.foo.*','test.foo.bar.fiba'),False)
   342 
   349 
   343     def test_pathmatch_with_twostar(self):
   350     def test_pathmatch_with_twostar(self):
   344         self.assertEquals(utils.pathmatch('test.foo.**','test.foo.bar.fiba'),True)
   351         self.assertEquals(utils.pathmatch('test.foo.**','test.foo.bar.fiba'),True)
   345 
   352 
   365         class F(object): pass
   372         class F(object): pass
   366         obj = T()
   373         obj = T()
   367         filters = []
   374         filters = []
   368         filters.append(lambda x: isinstance(x,F))
   375         filters.append(lambda x: isinstance(x,F))
   369         self.assertEquals(utils.filter(obj,filters),False)
   376         self.assertEquals(utils.filter(obj,filters),False)
       
   377 
       
   378     def test_is_float(self):
       
   379         self.assertEquals(utils.is_float(-10), False)
       
   380         self.assertEquals(utils.is_float(0), False)
       
   381         self.assertEquals(utils.is_float(1), False)
       
   382         self.assertEquals(utils.is_float(10), False)
       
   383         self.assertEquals(utils.is_float(212), False)
       
   384         self.assertEquals(utils.is_float('-10'), False)
       
   385         self.assertEquals(utils.is_float('0'), False)
       
   386         self.assertEquals(utils.is_float('1'), False)
       
   387         self.assertEquals(utils.is_float('10'), False)
       
   388         self.assertEquals(utils.is_float('212'), False)
       
   389         self.assertEquals(utils.is_float(-10.1), True)
       
   390         self.assertEquals(utils.is_float(0.1), True)
       
   391         self.assertEquals(utils.is_float(1.1), True)
       
   392         self.assertEquals(utils.is_float(10.1), True)
       
   393         self.assertEquals(utils.is_float(212.1), True)
       
   394         self.assertEquals(utils.is_float('-10.1'), True)
       
   395         self.assertEquals(utils.is_float('0.00001'), True)
       
   396         self.assertEquals(utils.is_float('1.1231'), True)
       
   397         self.assertEquals(utils.is_float('10.000000001'), True)
       
   398         self.assertEquals(utils.is_float('212.1'), True)
   370 
   399 
   371 class TestDottedRefs(unittest.TestCase):
   400 class TestDottedRefs(unittest.TestCase):
   372     def test_join_two_refs(self):
   401     def test_join_two_refs(self):
   373         self.assertEquals(utils.dottedref.join_refs(['foo.bar.test','fancy.ref']),'foo.bar.test.fancy.ref')
   402         self.assertEquals(utils.dottedref.join_refs(['foo.bar.test','fancy.ref']),'foo.bar.test.fancy.ref')
   374 
   403 
   542             "The quick ${Test.Color} ${Test.Foo} jumps over the lazy ${Test.Animal2}.",
   571             "The quick ${Test.Color} ${Test.Foo} jumps over the lazy ${Test.Animal2}.",
   543             dview,
   572             dview,
   544             default_value_for_missing = 'giraffe')
   573             default_value_for_missing = 'giraffe')
   545         self.assertEquals(result, "The quick brown giraffe jumps over the lazy dog.")
   574         self.assertEquals(result, "The quick brown giraffe jumps over the lazy dog.")
   546         
   575         
       
   576     def test_has_wildcard(self):
       
   577         self.assertEquals(utils.dottedref.has_wildcard('fii.*'),True)
       
   578         self.assertEquals(utils.dottedref.has_wildcard('fii.faa'),False)
       
   579         self.assertEquals(utils.dottedref.has_wildcard('fii.faa.*'),True)
       
   580         self.assertEquals(utils.dottedref.has_wildcard('fii.faa.**'),True)
       
   581         self.assertEquals(utils.dottedref.has_wildcard('fii.faa.foo'),False)
       
   582         self.assertEquals(utils.dottedref.has_wildcard('fii.*.faa'),True)
       
   583         self.assertEquals(utils.dottedref.has_wildcard('*'),True)
       
   584         
       
   585     def test_get_wildcard_prefix(self):
       
   586         self.assertEquals(utils.dottedref.get_static_ref('fii.*'),'fii')
       
   587         self.assertEquals(utils.dottedref.get_static_ref('fii.faa'),'fii.faa')
       
   588         self.assertEquals(utils.dottedref.get_static_ref('fii.faa.*'),'fii.faa')
       
   589         self.assertEquals(utils.dottedref.get_static_ref('fii.faa.**'),'fii.faa')
       
   590         self.assertEquals(utils.dottedref.get_static_ref('fii.faa.foo'),'fii.faa.foo')
       
   591         self.assertEquals(utils.dottedref.get_static_ref('fii.*.faa'),'fii')
       
   592         self.assertEquals(utils.dottedref.get_static_ref('*'),'')
   547 
   593 
   548 class TestUtilsSubclasses(unittest.TestCase):    
   594 class TestUtilsSubclasses(unittest.TestCase):    
   549 
   595 
   550     def setUp(self):
   596     def setUp(self):
   551         pass
   597         pass
   562         class class12(class1): pass
   608         class class12(class1): pass
   563         class class123(class2): pass
   609         class class123(class2): pass
   564         classnames = utils.all_subclasses(base)
   610         classnames = utils.all_subclasses(base)
   565         self.assertEquals(len(classnames),4)
   611         self.assertEquals(len(classnames),4)
   566 
   612 
   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 
   613 
   583 class TestMakeList(unittest.TestCase):
   614 class TestMakeList(unittest.TestCase):
   584     def test_get_list_string(self):
   615     def test_get_list_string(self):
   585         self.assertEquals(utils.get_list('test'), ['test'])
   616         self.assertEquals(utils.get_list('test'), ['test'])
   586 
   617 
   636         
   667         
   637         self.assertEquals(
   668         self.assertEquals(
   638             utils.xml.split_tag_namespace("{http://www.test.com/xml/1}test"),
   669             utils.xml.split_tag_namespace("{http://www.test.com/xml/1}test"),
   639             ('http://www.test.com/xml/1', 'test'))
   670             ('http://www.test.com/xml/1', 'test'))
   640 
   671 
       
   672     
       
   673     def test_get_xml_root_without_namespace_1(self):
       
   674         resource = StringIO.StringIO("""<?xml version="1.0" encoding="UTF-8"?>
       
   675             <root><elem1 attr1="test" att""")
       
   676         namespace, name = utils.xml.get_xml_root(resource)
       
   677         self.assertEquals(namespace, None)
       
   678         self.assertEquals(name, 'root')
       
   679     
       
   680     def test_get_xml_root_without_namespace_2(self):
       
   681         resource = StringIO.StringIO("""<?xml version="1.0" encoding="UTF-8"?>
       
   682             <root xmlns:myns="http://www.test.com/xml/1"><elem1 attr1="test" att""")
       
   683         namespace, name = utils.xml.get_xml_root(resource)
       
   684         self.assertEquals(namespace, None)
       
   685         self.assertEquals(name, 'root')
       
   686     
       
   687     def test_get_xml_root_with_namespace_1(self):
       
   688         resource = StringIO.StringIO("""<?xml version="1.0" encoding="UTF-8"?>
       
   689             <root xmlns="http://www.test.com/xml/1">
       
   690                 <elem1 attr1="test" attr2="test 2"/>
       
   691                 <elem2>testing</elem3>
       
   692             </root>""")
       
   693         namespace, name = utils.xml.get_xml_root(resource)
       
   694         self.assertEquals(namespace, 'http://www.test.com/xml/1')
       
   695         self.assertEquals(name, 'root')
       
   696     
       
   697     def test_get_xml_root_with_namespace_2(self):
       
   698         resource = StringIO.StringIO("""<?xml version="1.0" encoding="UTF-16"?>
       
   699             <myns:root xmlns:myns="http://www.test.com/xml/1">
       
   700                 <elem1 attr1="test" attr2="test 2"/>
       
   701                 <elem2>testing</elem3>
       
   702             </myns:root>""".encode('utf-16'))
       
   703         namespace, name = utils.xml.get_xml_root(resource)
       
   704         self.assertEquals(namespace, 'http://www.test.com/xml/1')
       
   705         self.assertEquals(name, 'root')
       
   706     
       
   707     def test_get_xml_root_with_namespace_3(self):
       
   708         resource = StringIO.StringIO("""<configuration name="testdata_confml" xmlns="http://www.s60.com/xml/confml/2" xmlns:xi="http://www.w3.org/2001/XInclude" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:xs="http://www.w3.org/2001/XMLSchema">
       
   709               <data>""")
       
   710         namespace, name = utils.xml.get_xml_root(resource)
       
   711         self.assertEquals(namespace, 'http://www.s60.com/xml/confml/2')
       
   712         self.assertEquals(name, 'configuration')
       
   713     
       
   714     def test_get_xml_root_invalid_xml_1(self):
       
   715         resource = StringIO.StringIO("""<?xml version="1.0" encoding="UTF-16"?>
       
   716             <root test test><elem """.encode('utf-16'))
       
   717         self.assertRaises(exceptions.XmlParseError, utils.xml.get_xml_root, resource)
       
   718     
       
   719     def test_get_xml_root_invalid_xml_2(self):
       
   720         resource = StringIO.StringIO("test test")
       
   721         self.assertRaises(exceptions.XmlParseError, utils.xml.get_xml_root, resource)
       
   722     
       
   723     def test_get_xml_root_empty_string(self):
       
   724         resource = StringIO.StringIO('')
       
   725         self.assertRaises(exceptions.XmlParseError, utils.xml.get_xml_root, resource)
       
   726 
       
   727 class TestDictUtils(unittest.TestCase):
       
   728     def test_update_tag_dict(self):
       
   729         self.assertEquals(utils.update_dict({'foo' :  ['bar']}, {}),
       
   730                                             {'foo': ['bar']})
       
   731         self.assertEquals(utils.update_dict({}, {'foo' :  ['bar']}),
       
   732                                             {'foo': ['bar']})
       
   733         self.assertEquals(utils.update_dict({'foo' :  ['bar']},{'bar' : ['foo']}),
       
   734                                             {'foo': [ 'bar'], 'bar' : ['foo']})
       
   735         self.assertEquals(utils.update_dict({'foo' :  ['bar']},{'foo' : ['foo']}),
       
   736                                             {'foo': ['bar', 'foo']})
       
   737         self.assertEquals(utils.update_dict({'foo' :  ['bar'], 'moro' : ['sano','poro']},{'foo' : ['foo']}),
       
   738                                             {'foo': ['bar', 'foo'], 'moro' : ['sano','poro']})
       
   739 
       
   740         self.assertEquals(utils.update_dict({'foo' :  ['bar'], },{'foo' : ['foo'], 'moro' : ['sano','poro']}),
       
   741                                             {'foo': ['bar', 'foo'], 'moro' : ['sano','poro']})
       
   742 
       
   743 class TestGrep(unittest.TestCase):
       
   744     def test_grep_tuple(self):
       
   745         data =['foo one','foo two','foo three','bar one','bar two','bar three']
       
   746         self.assertEquals(utils.grep_tuple('one', data), [(0,'foo one'), (3,'bar one')])
       
   747         self.assertEquals(utils.grep_tuple('two', data), [(1,'foo two'), (4,'bar two')])
       
   748         self.assertEquals(utils.grep_tuple('foo', data), [(0,'foo one'), (1,'foo two'), (2,'foo three')])
       
   749         self.assertEquals(utils.grep_tuple('foo|bar', data), [(0, 'foo one'), (1, 'foo two'), (2, 'foo three'), 
       
   750                                                               (3, 'bar one'), (4, 'bar two'), (5, 'bar three')])
       
   751 
       
   752     def test_grep_dict(self):
       
   753         data =['foo one','foo two','foo three','bar one','bar two','bar three']
       
   754         self.assertEquals(utils.grep_dict('one', data), {0 : 'foo one', 3 : 'bar one'})
       
   755         self.assertEquals(utils.grep_dict('two', data), {1 : 'foo two', 4 : 'bar two'})
       
   756         self.assertEquals(utils.grep_dict('foo', data), {0 : 'foo one', 1 : 'foo two', 2 : 'foo three'})
       
   757 
       
   758 class TestCmdLineParsing(unittest.TestCase):
       
   759     def test_nt_parsing(self):
       
   760         self.assertEquals(utils.cmdsplit(""), [])
       
   761         self.assertEquals(utils.cmdsplit('--foo="foo bar"', 'nt'), ['--foo=foo bar'])
       
   762         self.assertEquals(utils.cmdsplit('--foo="foo bar" -v5 -o "\epoc32\rom stuff\config"', 'nt'), ['--foo=foo bar',
       
   763                                                                                                       '-v5', 
       
   764                                                                                                       '-o', 
       
   765                                                                                                       '\\epoc32\rom stuff\\config'])
       
   766 
       
   767     def test_posix_parsing(self):
       
   768         self.assertEquals(utils.cmdsplit("", 'posix'), [])
       
   769         self.assertEquals(utils.cmdsplit('--foo="foo bar"', 'posix'), ['--foo=foo bar'])
       
   770         self.assertEquals(utils.cmdsplit('--foo="foo bar" -v5 -o "/epoc32/rom stuff/config"', 'posix'), ['--foo=foo bar',
       
   771                                                                                                       '-v5', 
       
   772                                                                                                       '-o', 
       
   773                                                                                                       '/epoc32/rom stuff/config'])
       
   774 
       
   775 
       
   776 class TestPathMethods(unittest.TestCase):
       
   777     def test_relpath(self):
       
   778         self.assertEquals(utils.relpath('foo/bar.txt','foo'), 'bar.txt')
       
   779         self.assertEquals(utils.relpath('foo/bar.txt'), os.path.normpath('foo/bar.txt'))
       
   780 
   641 if __name__ == '__main__':
   781 if __name__ == '__main__':
   642     unittest.main()
   782     unittest.main()
   643       
   783       
       
   784