configurationengine/source/plugins/common/ConeTemplatePlugin/templatemlplugin/tests/unittest_templatemlplugin.py
changeset 3 e7e0ae78773e
parent 0 2e8eeb919028
child 4 0951727b8815
equal deleted inserted replaced
2:87cfa131b535 3:e7e0ae78773e
    13 # Contributors:
    13 # Contributors:
    14 #
    14 #
    15 # Description: 
    15 # Description: 
    16 #
    16 #
    17 
    17 
    18 import unittest, os, shutil, sys
    18 import unittest, os, sys
       
    19 import logging
    19 
    20 
    20 try:
    21 try:
    21     from cElementTree import ElementTree, ElementInclude
    22     from cElementTree import ElementTree, ElementInclude
    22 except ImportError:
    23 except ImportError:
    23     try:    
    24     try:    
    26         try:
    27         try:
    27             from xml.etree import cElementTree as ElementTree
    28             from xml.etree import cElementTree as ElementTree
    28         except ImportError:
    29         except ImportError:
    29             from xml.etree import ElementTree
    30             from xml.etree import ElementTree
    30 
    31 
    31 import __init__	
       
    32 from templatemlplugin import templatemlplugin
    32 from templatemlplugin import templatemlplugin
    33 from types import NoneType
       
    34 from testautomation.base_testcase import BaseTestCase
    33 from testautomation.base_testcase import BaseTestCase
    35 from testautomation.utils import hex_to_bindata
    34 from testautomation.utils import hex_to_bindata
    36 
    35 
    37 from cone.public import exceptions,plugin,api,utils
    36 from cone.public import exceptions,plugin,api,utils
    38 from cone.storage import filestorage
    37 from cone.storage import filestorage
    75                 "<template file=\"../../templates/template.xml\"/>" \
    74                 "<template file=\"../../templates/template.xml\"/>" \
    76              "</output>" \
    75              "</output>" \
    77              "<filter name=\"test_filter\" file=\"../../filter/filter.py\"/>" \
    76              "<filter name=\"test_filter\" file=\"../../filter/filter.py\"/>" \
    78           "</templateml>"
    77           "</templateml>"
    79 
    78 
    80 TEMPML1 = "<ns0:output encoding=\"ASCII\" file=\"test.txt\" xmlns:ns0=\"http://www.s60.com/xml/templateml/1\">" \
    79 TEMPML1 = "<ns0:output encoding=\"ASCII\" file=\"test.txt\" xmlns:ns0=\"http://www.s60.com/xml/templateml/1\" newline=\"win\">" \
    81           "<ns0:template extension=\"foo/foobar:MyClass\">ABCDF</ns0:template>" \
    80           "<ns0:template extension=\"foo/foobar:MyClass\">ABCDF</ns0:template>" \
       
    81           "</ns0:output>"
       
    82 
       
    83 TEMPML1_LINUX = "<ns0:output encoding=\"ASCII\" file=\"test.txt\" xmlns:ns0=\"http://www.s60.com/xml/templateml/1\">" \
       
    84           "<ns0:template extension=\"foo/foobar:MyClass\" newline=\"unix\">ABCDF</ns0:template>" \
    82           "</ns0:output>"
    85           "</ns0:output>"
    83 
    86 
    84 TEMPML2 = "<ns0:output encoding=\"ASCII\" file=\"test.txt\" xmlns:ns0=\"http://www.s60.com/xml/templateml/1\" xmlns:xi=\"http://www.w3.org/2001/XInclude\">" \
    87 TEMPML2 = "<ns0:output encoding=\"ASCII\" file=\"test.txt\" xmlns:ns0=\"http://www.s60.com/xml/templateml/1\" xmlns:xi=\"http://www.w3.org/2001/XInclude\">" \
    85           "<ns0:template extension=\"foo/foobar:MyClass\">" \
    88           "<ns0:template extension=\"foo/foobar:MyClass\">" \
    86           "include AABBCC" \
    89           "include AABBCC" \
    99           "<filter name=\"test_filter\" file=\"../../filter/filter.py\"/>" \
   102           "<filter name=\"test_filter\" file=\"../../filter/filter.py\"/>" \
   100           "</ns0:output>"
   103           "</ns0:output>"
   101           
   104           
   102           
   105           
   103 def impl_from_resource(resource_ref, configuration):
   106 def impl_from_resource(resource_ref, configuration):
   104     doc_root = plugin.ReaderBase._read_xml_doc_from_resource(resource_ref, configuration)
   107     impls = plugin.ImplFactory.get_impls_from_file(resource_ref, configuration)
   105     readers = { templateml.TemplatemlImplReader.NAMESPACE: templateml.TemplatemlImplReader }
   108     assert len(impls) == 1
   106     ns = utils.xml.split_tag_namespace(doc_root.tag)[0]
   109     return impls[0]
   107     if ns in readers:
       
   108         return readers[ns].read_impl(resource_ref, configuration, doc_root)
       
   109     else:
       
   110         return None
       
   111           
   110           
   112 class TestTemplatemlPlugin(BaseTestCase):    
   111 class TestTemplatemlPlugin(BaseTestCase):    
   113     def setUp(self):
   112     def setUp(self):
   114         self.curdir = os.getcwd()
   113         self.curdir = os.getcwd()
   115         self.output = os.path.join(ROOT_PATH, 'output')
   114         self.output = os.path.join(ROOT_PATH, 'output')
   128         pattern = '^' + resource_ref.replace('.', r'\.') + '$'
   127         pattern = '^' + resource_ref.replace('.', r'\.') + '$'
   129         impls = plugin.get_impl_set(configuration, pattern)
   128         impls = plugin.get_impl_set(configuration, pattern)
   130         impls.output = self.output
   129         impls.output = self.output
   131         impl_list = impls.get_implementations_by_file(resource_ref)
   130         impl_list = impls.get_implementations_by_file(resource_ref)
   132         self.assertEquals(1, len(impl_list))
   131         self.assertEquals(1, len(impl_list))
   133         return impl_list[0]
   132         return (configuration,impl_list[0])
   134     
   133     
   135     def test_parse_desc(self):
   134     def test_parse_desc(self):
   136         impl = self.load_impl('Layer1/implml/file1.templateml')
   135         (config,impl) = self.load_impl('Layer1/implml/file1.templateml')
   137         self.assertEqual("Description field text", impl.reader.desc)
   136         self.assertEqual("Description field text", impl.reader.desc)
   138 
   137 
   139     def test_parse_output_with_file_ref(self):
   138     def test_parse_output_with_file_ref(self):
   140         impl = self.load_impl('Layer1/implml/output_with_ref.templateml')
   139         (config,impl) = self.load_impl('Layer1/implml/output_with_ref.templateml')
   141         self.assertEquals(impl.list_output_files(), [os.path.normpath('output/confmlref_filename.txt')])
   140         self.assertEquals(impl.list_output_files(), [os.path.normpath('confmlref_filename.txt')])
   142         
   141         
   143     def test_parse_outputs(self):
   142     def test_parse_outputs(self):
   144         impl = self.load_impl('Layer1/implml/file1.templateml')
   143         (config,impl) = self.load_impl('Layer1/implml/file1.templateml')
   145         outputs = []
   144         outputs = []
   146         output1 = templatemlplugin.OutputFile()
   145         output1 = templatemlplugin.OutputFile()
   147         output1.set_encoding("UTF-16")
   146         output1.set_encoding("UTF-16")
   148         output1.set_filename("test.txt")
   147         output1.set_filename("test.txt")
   149         output1.set_path('')
   148         output1.set_path('')
       
   149         output1.set_newline('unix')
   150         temp1 = templatemlplugin.TempFile()
   150         temp1 = templatemlplugin.TempFile()
   151         temp1.set_template(u'ABC kissa k\xe4velee')
   151         temp1.set_template(u'ABC kissa k\xe4velee')
   152         output1.set_template(temp1)
   152         output1.set_template(temp1)
   153         outputs.append(output1)
   153         outputs.append(output1)
   154 
   154 
   155         output2 = templatemlplugin.OutputFile()
   155         output2 = templatemlplugin.OutputFile()
   156         output2.set_encoding("UTF-16")
   156         output2.set_encoding("UTF-16")
   157         output2.set_filename("test2.txt")
   157         output2.set_filename("test2.txt")
   158         output2.set_path("output")
   158         output2.set_path("output")
       
   159         output2.set_newline('win')
   159         temp2 = templatemlplugin.TempFile()
   160         temp2 = templatemlplugin.TempFile()
   160         temp2.set_template('AABBCC')
   161         temp2.set_template('AABBCC')
   161         output2.set_template(temp2)
   162         output2.set_template(temp2)
   162         outputs.append(output2)
   163         outputs.append(output2)
   163 
   164 
   173         
   174         
   174         self.assertNotEqual(impl.reader.outputs, None)
   175         self.assertNotEqual(impl.reader.outputs, None)
   175         self.assertEqual(outputs[0].encoding, impl.reader.outputs[0].encoding)
   176         self.assertEqual(outputs[0].encoding, impl.reader.outputs[0].encoding)
   176         self.assertEqual(outputs[0].filename, impl.reader.outputs[0].filename)
   177         self.assertEqual(outputs[0].filename, impl.reader.outputs[0].filename)
   177         self.assertEqual(outputs[0].path, impl.reader.outputs[0].path)
   178         self.assertEqual(outputs[0].path, impl.reader.outputs[0].path)
       
   179         self.assertEqual(outputs[0].newline, impl.reader.outputs[0].newline)
   178         self.assertEqual(outputs[0].template.template, impl.reader.outputs[0].template.template)
   180         self.assertEqual(outputs[0].template.template, impl.reader.outputs[0].template.template)
   179         self.assertEqual(outputs[0].template.extensions, impl.reader.outputs[0].template.extensions)
   181         self.assertEqual(outputs[0].template.extensions, impl.reader.outputs[0].template.extensions)
   180         self.assertEqual(outputs[0].template.filters, impl.reader.outputs[0].template.filters)
   182         self.assertEqual(outputs[0].template.filters, impl.reader.outputs[0].template.filters)
   181         self.assertEqual(outputs[0].template, impl.reader.outputs[0].template)
   183         self.assertEqual(outputs[0].template, impl.reader.outputs[0].template)
   182         self.assertEqual(outputs[0], impl.reader.outputs[0])
   184         self.assertEqual(outputs[0], impl.reader.outputs[0])
   183 
   185 
   184         self.assertEqual(outputs[1].encoding, impl.reader.outputs[1].encoding)
   186         self.assertEqual(outputs[1].encoding, impl.reader.outputs[1].encoding)
   185         self.assertEqual(outputs[1].filename, impl.reader.outputs[1].filename)
   187         self.assertEqual(outputs[1].filename, impl.reader.outputs[1].filename)
   186         self.assertEqual(outputs[1].path, impl.reader.outputs[1].path)
   188         self.assertEqual(outputs[1].path, impl.reader.outputs[1].path)
       
   189         self.assertEqual(outputs[1].newline, impl.reader.outputs[1].newline)
   187         #self.assertEqual(outputs[1].template.template, impl.reader.outputs[1].template.template)
   190         #self.assertEqual(outputs[1].template.template, impl.reader.outputs[1].template.template)
   188         self.assertEqual(outputs[1].template.extensions, impl.reader.outputs[1].template.extensions)
   191         self.assertEqual(outputs[1].template.extensions, impl.reader.outputs[1].template.extensions)
   189         self.assertEqual(outputs[1].template.filters, impl.reader.outputs[1].template.filters)
   192         self.assertEqual(outputs[1].template.filters, impl.reader.outputs[1].template.filters)
   190         #self.assertEqual(outputs[1].template, impl.reader.outputs[1].template)
   193         #self.assertEqual(outputs[1].template, impl.reader.outputs[1].template)
   191         #self.assertEqual(outputs[1], impl.reader.outputs[1])
   194         #self.assertEqual(outputs[1], impl.reader.outputs[1])
   263             self.fail( "Known bug: ticket #2007")
   266             self.fail( "Known bug: ticket #2007")
   264         self.assertEqual(filters1[0].name, filter2.name)
   267         self.assertEqual(filters1[0].name, filter2.name)
   265         self.assertEqual(filters1[0].code, filter2.code)
   268         self.assertEqual(filters1[0].code, filter2.code)
   266         self.assertEqual(filters1[0], filter2)
   269         self.assertEqual(filters1[0], filter2)
   267         
   270         
   268     def test_parse_template_filter(self):
   271     def test_parse_template_filter_2(self):
   269         class DummyConfiguration(object):
   272         class DummyConfiguration(object):
   270             def get_resource(self, ref):
   273             def get_resource(self, ref):
   271                 class DummyResource(object):
   274                 class DummyResource(object):
   272                     def read(self): return ''
   275                     def read(self): return ''
   273                     def close(self): return None
   276                     def close(self): return None
   297         
   300         
   298     def test_simple_generate_prj(self):
   301     def test_simple_generate_prj(self):
   299         
   302         
   300         self.remove_if_exists(os.path.normpath("output/output/test.txt"))
   303         self.remove_if_exists(os.path.normpath("output/output/test.txt"))
   301         
   304         
   302         impl = self.load_impl('Layer1/implml/file2.templateml')
   305         (config,impl) = self.load_impl('Layer1/implml/file2.templateml')
   303         #impl.context = {'name' : 'some value'}
   306         #impl.context = {'name' : 'some value'}
   304         impl.generate()
   307         gc = plugin.GenerationContext(configuration=config)
       
   308         impl.generate(gc)
   305         
   309         
   306         self.assertTrue(os.path.exists(os.path.normpath("output/output/test.txt")))
   310         self.assertTrue(os.path.exists(os.path.normpath("output/output/test.txt")))
   307         result_file = None
   311         result_file = None
   308         try:
   312         try:
   309             result_file = open(os.path.normpath("output/output/test.txt"))
   313             result_file = open(os.path.normpath("output/output/test.txt"))
   310             for line in result_file:
   314             for line in result_file:
   311                 self.assertTrue(line == "include AABBCC")
   315                 self.assertTrue(line == "include AABBCC")
   312         finally:
   316         finally:
   313             if result_file != None: result_file.close()
   317             if result_file != None: result_file.close()
   314     
   318     
       
   319     def test_simple_generate_newline(self):
       
   320         
       
   321         self.remove_if_exists(os.path.normpath("output/output/test_newline_win.txt"))
       
   322         self.remove_if_exists(os.path.normpath("output/output/test_newline_unix.txt"))
       
   323         
       
   324         (config,impl) = self.load_impl('Layer1/implml/newline.templateml')
       
   325         gc = plugin.GenerationContext(configuration=config)
       
   326         impl.generate(gc)
       
   327         
       
   328         self.assertTrue(os.path.exists(os.path.normpath("output/output/test_newline_win.txt")))
       
   329         self.assertTrue(os.path.exists(os.path.normpath("output/output/test_newline_unix.txt")))
       
   330 
       
   331         result_file_win = None
       
   332         try:
       
   333             result_file_win = open(os.path.normpath("output/output/test_newline_win.txt"),'rb')
       
   334             line = result_file_win.read()
       
   335             self.assertEquals(line, "line1\r\nline2")
       
   336         finally:
       
   337             if result_file_win != None: result_file_win.close()
       
   338 
       
   339         result_file_unix = None
       
   340         try:
       
   341             result_file_unix = open(os.path.normpath("output/output/test_newline_unix.txt"), 'rb')
       
   342             line = result_file_unix.read()
       
   343             self.assertEquals(line, "line1\nline2")
       
   344         finally:
       
   345             if result_file_unix != None: result_file_unix.close()
       
   346 
   315     def test_simple_generate_prj3(self):
   347     def test_simple_generate_prj3(self):
   316         
   348         
   317         self.remove_if_exists(os.path.normpath("output/output/test3.txt"))
   349         self.remove_if_exists(os.path.normpath("output/output/test3.txt"))
   318         
   350         
   319         impl = self.load_impl('Layer1/implml/file3.templateml')
   351         (config,impl) = self.load_impl('Layer1/implml/file3.templateml')
   320         impl.generate()
   352         gc = plugin.GenerationContext(configuration=config)
       
   353         impl.generate(gc)
   321         
   354         
   322         self.assertTrue(os.path.exists(os.path.normpath("output/output/test3.txt")))
   355         self.assertTrue(os.path.exists(os.path.normpath("output/output/test3.txt")))
   323         result_file = None
   356         result_file = None
   324         try:
   357         try:
   325             result_file = open(os.path.normpath("output/output/test3.txt"))
   358             result_file = open(os.path.normpath("output/output/test3.txt"))
   326             for line in result_file:
   359             for line in result_file:
   327                 self.assertTrue(line == "'Hello John Doe!'")
   360                 self.assertTrue(line == "'Hello John Doe!'")
   328         finally:
   361         finally:
   329             if result_file != None: result_file.close()
   362             if result_file != None: result_file.close()
   330 
   363 
       
   364 
   331     def test_simple_generate_prj4_with_filters(self):
   365     def test_simple_generate_prj4_with_filters(self):
   332         
   366         
   333         self.remove_if_exists(os.path.normpath("output/output/test4a.txt"))
   367         self.remove_if_exists(os.path.normpath("output/output/test4a.txt"))
   334         self.remove_if_exists(os.path.normpath("output/output/test4b.txt"))
   368         self.remove_if_exists(os.path.normpath("output/output/test4b.txt"))
   335         self.remove_if_exists(os.path.normpath("output/output/test4c.txt"))
   369         self.remove_if_exists(os.path.normpath("output/output/test4c.txt"))
   336         self.remove_if_exists(os.path.normpath("output/output/test4d.txt"))
   370         self.remove_if_exists(os.path.normpath("output/output/test4d.txt"))
   337         
   371         
   338         impl = self.load_impl('Layer1/implml/file4.templateml')
   372         (config,impl) = self.load_impl('Layer1/implml/file4.templateml')
   339         #impl.context = {'name' : 'John Doe'}
   373         #impl.context = {'name' : 'John Doe'}
   340         impl.generate()
   374         gc = plugin.GenerationContext(configuration=config)
       
   375         impl.generate(gc)
   341         
   376         
   342         self.assertTrue(os.path.exists(os.path.normpath("output/output/test4a.txt")))
   377         self.assertTrue(os.path.exists(os.path.normpath("output/output/test4a.txt")))
   343         self.assertTrue(os.path.exists(os.path.normpath("output/output/test4b.txt")))
   378         self.assertTrue(os.path.exists(os.path.normpath("output/output/test4b.txt")))
   344         self.assertTrue(os.path.exists(os.path.normpath("output/output/test4c.txt")))
   379         self.assertTrue(os.path.exists(os.path.normpath("output/output/test4c.txt")))
   345         self.assertTrue(os.path.exists(os.path.normpath("output/output/test4d.txt")))
   380         self.assertTrue(os.path.exists(os.path.normpath("output/output/test4d.txt")))
   387 
   422 
   388     def test_simple_generate_prj_extfiles_with_filters(self):
   423     def test_simple_generate_prj_extfiles_with_filters(self):
   389         
   424         
   390         self.remove_if_exists(os.path.normpath("output/output/test_ext_temp_file.txt"))
   425         self.remove_if_exists(os.path.normpath("output/output/test_ext_temp_file.txt"))
   391         
   426         
   392         impl = self.load_impl('Layer1/implml/external_tempfile.templateml')
   427         (config,impl) = self.load_impl('Layer1/implml/external_tempfile.templateml')
   393         
   428         gc = plugin.GenerationContext(configuration=config)
   394         impl.generate()
   429         impl.generate(gc)
   395         
   430         
   396         self.assertTrue(os.path.exists(os.path.normpath("output/output/test_ext_temp_file.txt")))
   431         self.assertTrue(os.path.exists(os.path.normpath("output/output/test_ext_temp_file.txt")))
   397         
   432         
   398         result_file1 = None
   433         result_file1 = None
   399         
   434         
   415 
   450 
   416     def test_generate_prj1(self):
   451     def test_generate_prj1(self):
   417         
   452         
   418         self.remove_if_exists(os.path.normpath("output/output/test5a.txt"))
   453         self.remove_if_exists(os.path.normpath("output/output/test5a.txt"))
   419         
   454         
   420         impl = self.load_impl('Layer1/implml/file5.templateml')
   455         (config,impl) = self.load_impl('Layer1/implml/file5.templateml')
   421         impl.generate()
   456         gc = plugin.GenerationContext(configuration=config)
       
   457         impl.generate(gc)
   422         self.assertTrue(os.path.exists(os.path.normpath("output/output/test5a.txt")))
   458         self.assertTrue(os.path.exists(os.path.normpath("output/output/test5a.txt")))
   423         
   459         
   424         result_file1 = None
   460         result_file1 = None
   425         try:
   461         try:
   426             result_file1 = open(os.path.normpath("output/output/test5a.txt"))
   462             result_file1 = open(os.path.normpath("output/output/test5a.txt"))
   435 
   471 
   436     def test_generate_access_configuration(self):
   472     def test_generate_access_configuration(self):
   437         
   473         
   438         self.remove_if_exists(os.path.normpath("output/access_configuration.txt"))
   474         self.remove_if_exists(os.path.normpath("output/access_configuration.txt"))
   439         
   475         
   440         impl = self.load_impl('Layer1/implml/access_configuration.templateml')
   476         (config,impl) = self.load_impl('Layer1/implml/access_configuration.templateml')
   441         impl.generate()
   477         gc = plugin.GenerationContext(configuration=config)
       
   478         impl.generate(gc)
   442         self.assertTrue(os.path.exists(os.path.normpath("output/access_configuration.txt")))
   479         self.assertTrue(os.path.exists(os.path.normpath("output/access_configuration.txt")))
   443         
   480         
   444         result_file1 = None
   481         result_file1 = None
   445         try:
   482         try:
   446             result_file1 = open(os.path.normpath("output/access_configuration.txt"))
   483             result_file1 = open(os.path.normpath("output/access_configuration.txt"))
   451                 self.fail("No result file found: output/access_configuration.txt")
   488                 self.fail("No result file found: output/access_configuration.txt")
   452         finally:
   489         finally:
   453             if result_file1 != None: result_file1.close()
   490             if result_file1 != None: result_file1.close()
   454     
   491     
   455     def test_create_context_dict1(self):
   492     def test_create_context_dict1(self):
   456         impl = self.load_impl('Layer1/implml/file6.templateml')
   493         (config,impl) = self.load_impl('Layer1/implml/file6.templateml')
   457         impl.context = impl.create_dict()
   494         impl.context = impl.create_dict()
   458         impl.generate()
   495         gc = plugin.GenerationContext(configuration=config)
       
   496         impl.generate(gc)
   459     
   497     
   460     def test_list_output_files(self):
   498     def test_list_output_files(self):
   461         impl = self.load_impl('Layer1/implml/file1.templateml')
   499         (config,impl) = self.load_impl('Layer1/implml/file1.templateml')
   462         impl.set_output_root('outdir')
   500         impl.set_output_root('outdir')
   463         output_files = impl.list_output_files()
   501         output_files = impl.list_output_files()
   464         expected = map(lambda n: os.path.normpath(n), [
   502         expected = map(lambda n: os.path.normpath(n), [
   465             'outdir/test.txt',
   503             'outdir/test.txt',
   466             'outdir/output/test2.txt',
   504             'outdir/output/test2.txt',
   469             'outdir/some/test/path/test5.txt',
   507             'outdir/some/test/path/test5.txt',
   470         ])
   508         ])
   471         self.assertEquals(sorted(output_files), sorted(expected))
   509         self.assertEquals(sorted(output_files), sorted(expected))
   472     
   510     
   473     def test_has_ref(self):
   511     def test_has_ref(self):
   474         impl = self.load_impl('Layer1/implml/has_ref_template_test2.templateml')
   512         (config,impl) = self.load_impl('Layer1/implml/has_ref_template_test2.templateml')
   475         self.assertEquals(impl.has_ref('Feature1.StringSetting_not_found'), False)
   513         self.assertEquals(impl.has_ref('Feature1.StringSetting_not_found'), False)
   476         self.assertEquals(impl.has_ref('Feature1.StringSetting1'), True)
   514         self.assertEquals(impl.has_ref('Feature1.StringSetting1'), True)
   477         self.assertEquals(impl.has_ref('Feature2'), True)
   515         self.assertEquals(impl.has_ref('Feature2'), True)
   478         self.assertEquals(impl.has_ref('Feature2.set1.sub1'), True)
   516         self.assertEquals(impl.has_ref('Feature2.set1.sub1'), True)
   479         self.assertEquals(impl.has_ref('Feature2.set1.sub2'), True)
   517         self.assertEquals(impl.has_ref('Feature2.set1.sub2'), True)
   480         self.assertEquals(impl.has_ref('Feature1.UnicodeValueSetting'), True)
   518         self.assertEquals(impl.has_ref('Feature1.UnicodeValueSetting'), True)
   481     
   519     
   482     def test_has_ref_external_template(self):
   520     def test_has_ref_external_template(self):
   483         impl = self.load_impl('Layer1/implml/has_ref_template_test3.templateml')
   521         (config,impl) = self.load_impl('Layer1/implml/has_ref_template_test3.templateml')
   484         self.assertEquals(impl.has_ref('Feature1.StringSetting_not_found'), False)
   522         self.assertEquals(impl.has_ref('Feature1.StringSetting_not_found'), False)
   485         self.assertEquals(impl.has_ref('Feature1.StringSetting1'), True)
   523         self.assertEquals(impl.has_ref('Feature1.StringSetting1'), True)
   486         self.assertEquals(impl.has_ref('Feature2'), True)
   524         self.assertEquals(impl.has_ref('Feature2'), True)
   487         self.assertEquals(impl.has_ref('Feature2.set1.sub1'), True)
   525         self.assertEquals(impl.has_ref('Feature2.set1.sub1'), True)
   488         self.assertEquals(impl.has_ref('Feature2.set1.sub2'), True)
   526         self.assertEquals(impl.has_ref('Feature2.set1.sub2'), True)
   489         self.assertEquals(impl.has_ref('Feature1.UnicodeValueSetting'), True)
   527         self.assertEquals(impl.has_ref('Feature1.UnicodeValueSetting'), True)
   490 
   528 
   491     def test_has_ref_with_featree(self):
   529     def test_has_ref_with_featree(self):
   492         impl = self.load_impl('Layer1/implml/has_ref_template_test.templateml')
   530         (config,impl) = self.load_impl('Layer1/implml/has_ref_template_test.templateml')
   493         self.assertEquals(impl.has_ref('Feature1.StringSetting'), True)
   531         self.assertEquals(impl.has_ref('Feature1.StringSetting'), True)
   494         self.assertEquals(impl.has_ref('Feature2.StringSetting'), True)
   532         self.assertEquals(impl.has_ref('Feature2.StringSetting'), True)
   495     
   533     
   496     def test_unicode_in_template_and_value(self):
   534     def test_unicode_in_template_and_value(self):
   497         OUTPUT_DIR = os.path.join(ROOT_PATH, 'output', 'unicode_test')
   535         OUTPUT_DIR = os.path.join(ROOT_PATH, 'output', 'unicode_test')
   499         
   537         
   500         fs = filestorage.FileStorage(testdata)
   538         fs = filestorage.FileStorage(testdata)
   501         p = api.Project(fs)
   539         p = api.Project(fs)
   502         config = p.get_configuration('root1.confml')
   540         config = p.get_configuration('root1.confml')
   503         impls = plugin.get_impl_set(config,'unicode_template_test\.templateml$')
   541         impls = plugin.get_impl_set(config,'unicode_template_test\.templateml$')
   504         impls.output = OUTPUT_DIR
   542         gc = plugin.GenerationContext(output=OUTPUT_DIR,
   505         impls.generate()
   543                                       configuration=config)
       
   544         impls.generate(gc)
   506         self.assert_exists_and_contains_something(os.path.join(OUTPUT_DIR, "unicode_template_test.txt"))
   545         self.assert_exists_and_contains_something(os.path.join(OUTPUT_DIR, "unicode_template_test.txt"))
   507         
   546         
   508         # Check that the output exists and contains expected lines
   547         # Check that the output exists and contains expected lines
   509         f = open(os.path.join(OUTPUT_DIR, "unicode_template_test.txt"), "rb")
   548         f = open(os.path.join(OUTPUT_DIR, "unicode_template_test.txt"), "rb")
   510         try:        data = f.read().decode('utf-8')
   549         try:        data = f.read().decode('utf-8')
   542             self.create_dir(dir)
   581             self.create_dir(dir)
   543             filename = os.path.join(dir, "expected.txt")
   582             filename = os.path.join(dir, "expected.txt")
   544             self.write_data_to_file(filename, self.feature_list_to_str(expected_list))
   583             self.write_data_to_file(filename, self.feature_list_to_str(expected_list))
   545             filename = os.path.join(dir, "actual.txt")
   584             filename = os.path.join(dir, "actual.txt")
   546             self.write_data_to_file(filename, self.feature_list_to_str(feat_list))
   585             self.write_data_to_file(filename, self.feature_list_to_str(feat_list))
   547             self.fail("Feature tree is not what was expected, see the files in '%s'" % dir)
   586             self.fail("Feature list is not what was expected, see the files in '%s'" % dir)
   548     
   587     
   549     def feature_tree_to_str(self, d, indent_amount=0):
   588     def feature_tree_to_str(self, d, indent_amount=0):
   550         """
   589         """
   551         Pretty-print a feature tree dictionary into a string.
   590         Pretty-print a feature tree dictionary into a string.
   552         """
   591         """
   564             if key.startswith('_'): return '\x00' + key
   603             if key.startswith('_'): return '\x00' + key
   565             else:                   return key
   604             else:                   return key
   566         
   605         
   567         for key, value in sorted(d.items(), key=key_func):
   606         for key, value in sorted(d.items(), key=key_func):
   568             temp.append(indent)
   607             temp.append(indent)
   569             if isinstance(value, dict):
   608             if isinstance(value, (dict, templatemlplugin.FeatureDictProxy)):
   570                 temp.append("%r: %s," % (key, self.feature_tree_to_str(value, indent_amount + INDENT_AMOUNT)))
   609                 temp.append("%r: %s," % (key, self.feature_tree_to_str(value, indent_amount + INDENT_AMOUNT)))
   571             else:
   610             else:
   572                 temp.append("%r: %r," % (key, value))
   611                 temp.append("%r: %r," % (key, value))
   573             temp.append('\n')
   612             temp.append('\n')
   574             
   613             
   580         """
   619         """
   581         Pretty-print a feature list into a string.
   620         Pretty-print a feature list into a string.
   582         """
   621         """
   583         temp = ['[\n']
   622         temp = ['[\n']
   584         for item in lst:
   623         for item in lst:
   585             if isinstance(item, dict):  temp.append(self.feature_tree_to_str(item))
   624             if isinstance(item, (dict, templatemlplugin.FeatureDictProxy)):  temp.append(self.feature_tree_to_str(item))
   586             else:                       temp.append(repr(item))
   625             else:                       temp.append(repr(item))
   587             temp.append(',\n')
   626             temp.append(',\n')
   588         temp.append(']')
   627         temp.append(']')
   589         return ''.join(temp)
   628         return ''.join(temp)
   590     
   629     
   591     
   630     
   592     def test_utf_bom_support(self):
   631     def test_utf_bom_support(self):
   593         OUTPUT_DIR = os.path.join(ROOT_PATH, 'temp/utf_bom_test')
   632         OUTPUT_DIR = os.path.join(ROOT_PATH, 'temp/utf_bom_test')
   594         self.recreate_dir(OUTPUT_DIR)
   633         self.recreate_dir(OUTPUT_DIR)
   595         impl = self.load_impl('Layer1/implml/utf_bom_test.templateml')
   634         (config,impl) = self.load_impl('Layer1/implml/utf_bom_test.templateml')
   596         impl.set_output_root(OUTPUT_DIR)
   635         gc = plugin.GenerationContext(output=OUTPUT_DIR,
   597         impl.generate()
   636                                       configuration=config)
       
   637         impl.generate(gc)
   598         
   638         
   599         def check(file, contents):
   639         def check(file, contents):
   600             FILE = os.path.join(OUTPUT_DIR, file)
   640             FILE = os.path.join(OUTPUT_DIR, file)
   601             self.assert_file_content_equals(FILE, contents)
   641             self.assert_file_content_equals(FILE, contents)
   602         
   642         
   633         DATA = """<?xml version="1.0" encoding="UTF-8"?>
   673         DATA = """<?xml version="1.0" encoding="UTF-8"?>
   634           <templateml xmlns=\"http://www.s60.com/xml/templateml/1\">
   674           <templateml xmlns=\"http://www.s60.com/xml/templateml/1\">
   635             <output file="test.txt" encoding="foocode">foo</output>
   675             <output file="test.txt" encoding="foocode">foo</output>
   636           </templateml>"""
   676           </templateml>"""
   637         reader = templatemlplugin.TemplatemlImplReader()
   677         reader = templatemlplugin.TemplatemlImplReader()
   638         self.assertRaises(exceptions.ParseError, reader.fromstring, DATA)
   678         reader.fromstring(DATA)
   639     
   679         self.assertRaises(exceptions.ParseError, reader.expand_output_refs_by_default_view)
       
   680     
       
   681     def test_generate_from_template_with_feat_tree_iteration(self):
       
   682         OUTPUT_DIR = os.path.join(ROOT_PATH, 'temp/feat_tree_iteration')
       
   683         self.recreate_dir(OUTPUT_DIR)
       
   684         (config,impl) = self.load_impl('Layer1/implml/feat_tree_iteration_test.templateml')
       
   685         gc = plugin.GenerationContext(output=OUTPUT_DIR,
       
   686                                       configuration=config)
       
   687         impl.generate(gc)
       
   688         
       
   689         OUTPUT_FILE = os.path.join(OUTPUT_DIR, 'feat_tree_iteration_test.txt')
       
   690         self.assert_exists_and_contains_something(OUTPUT_FILE)
       
   691         self.assert_file_content_equals(OUTPUT_FILE,
       
   692             u"\n"\
       
   693             u"Boolean setting = True\n"\
       
   694             u"File setting = default_file.txt\n"\
       
   695             u"Folder setting = default_folder\n"\
       
   696             u"Int setting = 10\n"\
       
   697             u"Real setting = 3.14\n"\
       
   698             u"Selection setting = 1\n"\
       
   699             u"Sequence setting = [[[None, None], 1.25, [None, None], 128, 'def1', False, '1'], [[None, None], 1.5, [None, None], 256, 'def2', False, '1']]\n"\
       
   700             u"String setting = John Doe\n"\
       
   701             u"String for unicode value test = カタカナ\n".encode('utf-8'))
       
   702     
       
   703     def test_generate_from_template_generation_context_accessed(self):
       
   704         OUTPUT_DIR = os.path.join(ROOT_PATH, 'temp/access_context')
       
   705         self.recreate_dir(OUTPUT_DIR)
       
   706         (config,impl) = self.load_impl('Layer1/implml/access_context.templateml')
       
   707         context = plugin.GenerationContext(output=OUTPUT_DIR,
       
   708                                            configuration=config)
       
   709         context.tags = {'sometag': ['foo', 'bar']}
       
   710         impl.generate(context)
       
   711         
       
   712         OUTPUT_FILE = os.path.join(OUTPUT_DIR, 'access_context.txt')
       
   713         self.assert_exists_and_contains_something(OUTPUT_FILE)
       
   714         self.assert_file_content_equals(OUTPUT_FILE,
       
   715             "Tags: {'sometag': ['foo', 'bar']}")
       
   716     
       
   717     def test_invalid_ref_in_template(self):
       
   718         OUTPUT_DIR = os.path.join(ROOT_PATH, 'temp/invalid_ref')
       
   719         self.recreate_dir(OUTPUT_DIR)
       
   720         (config,impl) = self.load_impl('Layer1/implml/invalid_ref.templateml')
       
   721         context = plugin.GenerationContext(output=OUTPUT_DIR,
       
   722                                            configuration=config)
       
   723         
       
   724         log_file, handler, logger = self._prepare_log('invalid_refs.log')
       
   725         impl.generate(context)
       
   726         logger.removeHandler(handler)
       
   727         
       
   728         self.assert_file_content_equals(os.path.join(OUTPUT_DIR, 'invalid_ref_1.txt'), "")
       
   729         self.assert_file_content_equals(os.path.join(OUTPUT_DIR, 'invalid_ref_2.txt'), "")
       
   730         self.assert_file_content_equals(os.path.join(OUTPUT_DIR, 'invalid_ref_3.txt'), "")
       
   731         
       
   732         self.assert_file_contains(log_file,
       
   733             ["TemplatemlImpl(ref='Layer1/implml/invalid_ref.templateml', type='templateml', lineno=2): Failed to generate output: NotFound: Feature 'Foo' not found",
       
   734              "TemplatemlImpl(ref='Layer1/implml/invalid_ref.templateml', type='templateml', lineno=2): Failed to generate output: NotFound: Feature 'Feature1.Nonexistent' not found",
       
   735              "TemplatemlImpl(ref='Layer1/implml/invalid_ref.templateml', type='templateml', lineno=2): Failed to generate output: NotFound: Feature 'Feature1.SequenceSetting.Nonexistent' not found"])
       
   736     
       
   737     def _prepare_log(self, log_file, level=logging.DEBUG, formatter="%(levelname)s - %(name)s - %(message)s", logger='cone'):
       
   738         FULL_PATH = os.path.join(ROOT_PATH, "temp/logs", log_file)
       
   739         self.remove_if_exists(FULL_PATH)
       
   740         self.create_dir_for_file_path(FULL_PATH)
       
   741         
       
   742         handler = logging.FileHandler(FULL_PATH)
       
   743         handler.setLevel(level)
       
   744         frm = logging.Formatter(formatter)
       
   745         handler.setFormatter(frm)
       
   746         logger = logging.getLogger(logger)
       
   747         logger.addHandler(handler)
       
   748         
       
   749         return [FULL_PATH, handler, logger]
   640 
   750 
   641 class TestExtractRefsFromTemplate(unittest.TestCase):
   751 class TestExtractRefsFromTemplate(unittest.TestCase):
   642     def test_extract_refs_from_template(self):
   752     def test_extract_refs_from_template(self):
   643         def t(data, expected_refs):
   753         def t(data, expected_refs):
   644             actual_refs = templatemlplugin.TemplatemlImpl._extract_refs_from_template(data)
   754             actual_refs = templatemlplugin.TemplatemlImpl._extract_refs_from_template(data)