configurationengine/source/plugins/common/ConeTemplatePlugin/templatemlplugin/tests/unittest_templatemlplugin.py
changeset 0 2e8eeb919028
child 3 e7e0ae78773e
equal deleted inserted replaced
-1:000000000000 0:2e8eeb919028
       
     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 import unittest, os, shutil, sys
       
    19 
       
    20 try:
       
    21     from cElementTree import ElementTree, ElementInclude
       
    22 except ImportError:
       
    23     try:    
       
    24         from elementtree import ElementTree, ElementInclude
       
    25     except ImportError:
       
    26         try:
       
    27             from xml.etree import cElementTree as ElementTree
       
    28         except ImportError:
       
    29             from xml.etree import ElementTree
       
    30 
       
    31 import __init__	
       
    32 from templatemlplugin import templatemlplugin
       
    33 from types import NoneType
       
    34 from testautomation.base_testcase import BaseTestCase
       
    35 from testautomation.utils import hex_to_bindata
       
    36 
       
    37 from cone.public import exceptions,plugin,api,utils
       
    38 from cone.storage import filestorage
       
    39 from cone.confml import implml
       
    40 
       
    41 # Hardcoded value of testdata folder that must be under the current working dir
       
    42 ROOT_PATH = os.path.dirname(os.path.abspath(__file__))
       
    43 testdata  = os.path.join(ROOT_PATH,'project')
       
    44 
       
    45 TEMPML_DOC1 = "<?xml version=\"1.0\" encoding=\"ascii\"?> " \
       
    46           "<templateml xmlns=\"http://www.s60.com/xml/templateml/1\" xmlns:xi=\"http://www.w3.org/2001/XInclude\">" \
       
    47             "<desc>Desc</desc>" \
       
    48             "<output file=\"test.txt\" encoding=\"UTF-16\">" \
       
    49                 "<template extension=\"foo/foobar:MyClass\">ABCDF</template>" \
       
    50              "</output>" \
       
    51              "<filter name=\"test_filter\">lambda a,b: a+b</filter>" \
       
    52           "</templateml>"
       
    53           
       
    54 TEMPML_DOC2 = "<?xml version=\"1.0\" encoding=\"ascii\"?> " \
       
    55           "<templateml xmlns=\"http://www.s60.com/xml/templateml/1\" xmlns:xi=\"http://www.w3.org/2001/XInclude\">" \
       
    56             "<desc>Desc</desc>" \
       
    57             "<output file=\"test.txt\" encoding=\"UTF-16\">" \
       
    58                 "<template extension=\"foo/foobar:MyClass\"><xi:include href=\"project/templates/template.xml\" parse=\"xml\"/></template>" \
       
    59              "</output>" \
       
    60              "<filter name=\"test_filter\">lambda a,b: a+b</filter>" \
       
    61           "</templateml>"
       
    62 
       
    63 TEMPML_DOC3 = "<?xml version=\"1.0\" encoding=\"ascii\"?> " \
       
    64           "<templateml xmlns=\"http://www.s60.com/xml/templateml/1\" xmlns:xi=\"http://www.w3.org/2001/XInclude\">" \
       
    65             "<desc>Desc</desc>" \
       
    66             "<output file=\"test.txt\" encoding=\"UTF-16\">" \
       
    67                 "<template extension=\"foo/foobar:MyClass\"><xi:include href=\"project/templates/template.xml\" parse=\"xml\"/></template>" \
       
    68              "</output>" \
       
    69              "<filter name=\"test_filter\">lambda a,b: a+b</filter>" \
       
    70           "</templateml>"
       
    71 
       
    72 TEMPML_DOC4 = "<?xml version=\"1.0\" encoding=\"ascii\"?> " \
       
    73           "<templateml xmlns=\"http://www.s60.com/xml/templateml/1\">" \
       
    74             "<output file=\"test.txt\" encoding=\"ascii\">" \
       
    75                 "<template file=\"../../templates/template.xml\"/>" \
       
    76              "</output>" \
       
    77              "<filter name=\"test_filter\" file=\"../../filter/filter.py\"/>" \
       
    78           "</templateml>"
       
    79 
       
    80 TEMPML1 = "<ns0:output encoding=\"ASCII\" file=\"test.txt\" xmlns:ns0=\"http://www.s60.com/xml/templateml/1\">" \
       
    81           "<ns0:template extension=\"foo/foobar:MyClass\">ABCDF</ns0:template>" \
       
    82           "</ns0:output>"
       
    83 
       
    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\">" \
       
    85           "<ns0:template extension=\"foo/foobar:MyClass\">" \
       
    86           "include AABBCC" \
       
    87           "</ns0:template>" \
       
    88           "</ns0:output>"
       
    89 
       
    90 TEMPML3 = "<ns0:output encoding=\"ASCII\" file=\"test.txt\" xmlns:ns0=\"http://www.s60.com/xml/templateml/1\" xmlns:xi=\"http://www.w3.org/2001/XInclude\">" \
       
    91           "<ns0:template extension=\"foo/foobar:MyClass\">" \
       
    92           "include AABBCC" \
       
    93           "</ns0:template>" \
       
    94           "<filter name=\"test_filter\">lambda a,b: a+b</filter>" \
       
    95           "</ns0:output>"
       
    96 
       
    97 TEMPML4 = "<ns0:output encoding=\"ASCII\" file=\"test.txt\" xmlns:ns0=\"http://www.s60.com/xml/templateml/1\" xmlns:xi=\"http://www.w3.org/2001/XInclude\">" \
       
    98           "<ns0:template file=\"../../templates/template.xml\"/>" \
       
    99           "<filter name=\"test_filter\" file=\"../../filter/filter.py\"/>" \
       
   100           "</ns0:output>"
       
   101           
       
   102           
       
   103 def impl_from_resource(resource_ref, configuration):
       
   104     doc_root = plugin.ReaderBase._read_xml_doc_from_resource(resource_ref, configuration)
       
   105     readers = { templateml.TemplatemlImplReader.NAMESPACE: templateml.TemplatemlImplReader }
       
   106     ns = utils.xml.split_tag_namespace(doc_root.tag)[0]
       
   107     if ns in readers:
       
   108         return readers[ns].read_impl(resource_ref, configuration, doc_root)
       
   109     else:
       
   110         return None
       
   111           
       
   112 class TestTemplatemlPlugin(BaseTestCase):    
       
   113     def setUp(self):
       
   114         self.curdir = os.getcwd()
       
   115         self.output = os.path.join(ROOT_PATH, 'output')
       
   116         os.chdir(os.path.join(ROOT_PATH))
       
   117 
       
   118     def tearDown(self):
       
   119         os.chdir(self.curdir)
       
   120     
       
   121     def load_config(self, config):
       
   122         fs = filestorage.FileStorage(testdata)
       
   123         p = api.Project(fs)
       
   124         return p.get_configuration(config)
       
   125     
       
   126     def load_impl(self, resource_ref, config='root1.confml'):
       
   127         configuration = self.load_config(config)
       
   128         pattern = '^' + resource_ref.replace('.', r'\.') + '$'
       
   129         impls = plugin.get_impl_set(configuration, pattern)
       
   130         impls.output = self.output
       
   131         impl_list = impls.get_implementations_by_file(resource_ref)
       
   132         self.assertEquals(1, len(impl_list))
       
   133         return impl_list[0]
       
   134     
       
   135     def test_parse_desc(self):
       
   136         impl = self.load_impl('Layer1/implml/file1.templateml')
       
   137         self.assertEqual("Description field text", impl.reader.desc)
       
   138 
       
   139     def test_parse_output_with_file_ref(self):
       
   140         impl = self.load_impl('Layer1/implml/output_with_ref.templateml')
       
   141         self.assertEquals(impl.list_output_files(), [os.path.normpath('output/confmlref_filename.txt')])
       
   142         
       
   143     def test_parse_outputs(self):
       
   144         impl = self.load_impl('Layer1/implml/file1.templateml')
       
   145         outputs = []
       
   146         output1 = templatemlplugin.OutputFile()
       
   147         output1.set_encoding("UTF-16")
       
   148         output1.set_filename("test.txt")
       
   149         output1.set_path('')
       
   150         temp1 = templatemlplugin.TempFile()
       
   151         temp1.set_template(u'ABC kissa k\xe4velee')
       
   152         output1.set_template(temp1)
       
   153         outputs.append(output1)
       
   154 
       
   155         output2 = templatemlplugin.OutputFile()
       
   156         output2.set_encoding("UTF-16")
       
   157         output2.set_filename("test2.txt")
       
   158         output2.set_path("output")
       
   159         temp2 = templatemlplugin.TempFile()
       
   160         temp2.set_template('AABBCC')
       
   161         output2.set_template(temp2)
       
   162         outputs.append(output2)
       
   163 
       
   164         output3 = templatemlplugin.OutputFile()
       
   165         output3.set_encoding("UTF-8")
       
   166         output3.set_filename("test3.txt")
       
   167         output3.set_path('')
       
   168         temp3 = templatemlplugin.TempFile()
       
   169         temp3.set_template(u'ABC kissa k\xe4velee')
       
   170         output3.set_template(temp3)
       
   171 
       
   172         outputs.append(output3)
       
   173         
       
   174         self.assertNotEqual(impl.reader.outputs, None)
       
   175         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].path, impl.reader.outputs[0].path)
       
   178         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)
       
   180         self.assertEqual(outputs[0].template.filters, impl.reader.outputs[0].template.filters)
       
   181         self.assertEqual(outputs[0].template, impl.reader.outputs[0].template)
       
   182         self.assertEqual(outputs[0], impl.reader.outputs[0])
       
   183 
       
   184         self.assertEqual(outputs[1].encoding, impl.reader.outputs[1].encoding)
       
   185         self.assertEqual(outputs[1].filename, impl.reader.outputs[1].filename)
       
   186         self.assertEqual(outputs[1].path, impl.reader.outputs[1].path)
       
   187         #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)
       
   189         self.assertEqual(outputs[1].template.filters, impl.reader.outputs[1].template.filters)
       
   190         #self.assertEqual(outputs[1].template, impl.reader.outputs[1].template)
       
   191         #self.assertEqual(outputs[1], impl.reader.outputs[1])
       
   192 
       
   193         self.assertEqual(outputs[2].encoding, impl.reader.outputs[2].encoding)
       
   194         self.assertEqual(outputs[2].filename, impl.reader.outputs[2].filename)
       
   195         self.assertEqual(outputs[2].path, impl.reader.outputs[2].path)
       
   196         #self.assertEqual(outputs[2].template.template, impl.reader.outputs[2].template.template)
       
   197         self.assertEqual(outputs[2].template.extensions, impl.reader.outputs[2].template.extensions)
       
   198         self.assertEqual(outputs[2].template.filters, impl.reader.outputs[2].template.filters)
       
   199         #self.assertEqual(outputs[2].template, impl.reader.outputs[2].template)
       
   200 
       
   201         #self.assertEqual(outputs[2], impl.reader.outputs[2])
       
   202 
       
   203     def test_parse_outputfile(self):
       
   204         output1 = templatemlplugin.OutputFile()
       
   205         output2 = templatemlplugin.OutputFile()
       
   206         
       
   207         self.assertEqual(output1, output2)
       
   208         self.assertEqual(output1.encoding, output2.encoding)
       
   209         self.assertEqual(output1.path, output2.path)
       
   210         self.assertEqual(output1.filename, output2.filename)
       
   211         
       
   212         output1.set_encoding("utf-8")
       
   213         output2.set_encoding("utf-8")
       
   214         
       
   215         self.assertEqual(output1, output2)# utf-8, utf-8
       
   216         
       
   217         output1.set_encoding("utf-16")
       
   218         
       
   219         self.assertNotEqual(output1, output2)#utf-16, utf-8
       
   220         
       
   221         output2.set_encoding("utf-16")
       
   222         
       
   223         self.assertEqual(output1, output2)#utf-16, utf-16
       
   224 
       
   225     def test_parse_template(self):
       
   226         reader = templatemlplugin.TemplatemlImplReader()
       
   227         reader.fromstring(TEMPML_DOC1)
       
   228         
       
   229         temp1 = reader.parse_template(ElementTree.fromstring(TEMPML1))
       
   230         temp2 = templatemlplugin.TempFile()
       
   231         temp2.set_template("ABCDF")
       
   232         
       
   233         self.assertEqual(temp1.template, temp2.template)
       
   234         self.assertEqual(temp1.extensions, temp2.extensions)
       
   235         self.assertEqual(temp1.filters, temp2.filters)
       
   236         self.assertEqual(temp1, temp2)
       
   237 
       
   238     def test_parse_template_from_xinclude(self):
       
   239         try:
       
   240             reader = templatemlplugin.TemplatemlImplReader()
       
   241             reader.fromstring(TEMPML_DOC2)
       
   242             
       
   243             temp1 = reader.parse_template(ElementTree.fromstring(TEMPML2))
       
   244             temp2 = templatemlplugin.TempFile()
       
   245             temp2.set_template("include AABBCC")
       
   246         except exceptions.ParseError, e:
       
   247             self.fail("Known bug: ticket #2007")
       
   248         
       
   249         self.assertEqual(temp1.template, temp2.template)
       
   250         self.assertEqual(temp1.extensions, temp2.extensions)
       
   251         self.assertEqual(temp1.filters, temp2.filters)
       
   252         self.assertEqual(temp1, temp2)
       
   253 
       
   254     def test_parse_template_filter(self):
       
   255         try:
       
   256             reader = templatemlplugin.TemplatemlImplReader()
       
   257             reader.fromstring(TEMPML_DOC3)
       
   258             filters1 = reader.filters
       
   259             filter2 = templatemlplugin.Filter()
       
   260             filter2.set_code("lambda a,b: a+b")
       
   261             filter2.set_name("test_filter")
       
   262         except exceptions.ParseError, e:
       
   263             self.fail( "Known bug: ticket #2007")
       
   264         self.assertEqual(filters1[0].name, filter2.name)
       
   265         self.assertEqual(filters1[0].code, filter2.code)
       
   266         self.assertEqual(filters1[0], filter2)
       
   267         
       
   268     def test_parse_template_filter(self):
       
   269         class DummyConfiguration(object):
       
   270             def get_resource(self, ref):
       
   271                 class DummyResource(object):
       
   272                     def read(self): return ''
       
   273                     def close(self): return None
       
   274                 return DummyResource()
       
   275             def get_default_view(self): return None
       
   276         reader = templatemlplugin.TemplatemlImplReader('foo/implml/bar.implml', DummyConfiguration())
       
   277         reader.fromstring(TEMPML_DOC4)
       
   278         
       
   279         filters1 = reader.filters
       
   280         filter2 = templatemlplugin.Filter()
       
   281         filter2.set_code(None)
       
   282         filter2.set_name("test_filter")
       
   283         filter2.set_path("../../filter/filter.py")
       
   284         temp1 = reader.parse_template(ElementTree.fromstring(TEMPML4))
       
   285         temp2 = templatemlplugin.TempFile()
       
   286         temp2.set_template('')
       
   287         temp2.set_path('')
       
   288 
       
   289         self.assertEqual(filters1[0].name, filter2.name)
       
   290         self.assertEqual(filters1[0].code, filter2.code)
       
   291         self.assertEqual(filters1[0].path, filter2.path)
       
   292         self.assertEqual(filters1[0], filter2)
       
   293         self.assertEqual(temp1.template, temp2.template)
       
   294         self.assertEqual(temp1.extensions, temp2.extensions)
       
   295         self.assertEqual(temp1.filters, temp2.filters)
       
   296         self.assertEqual(temp1, temp2)
       
   297         
       
   298     def test_simple_generate_prj(self):
       
   299         
       
   300         self.remove_if_exists(os.path.normpath("output/output/test.txt"))
       
   301         
       
   302         impl = self.load_impl('Layer1/implml/file2.templateml')
       
   303         #impl.context = {'name' : 'some value'}
       
   304         impl.generate()
       
   305         
       
   306         self.assertTrue(os.path.exists(os.path.normpath("output/output/test.txt")))
       
   307         result_file = None
       
   308         try:
       
   309             result_file = open(os.path.normpath("output/output/test.txt"))
       
   310             for line in result_file:
       
   311                 self.assertTrue(line == "include AABBCC")
       
   312         finally:
       
   313             if result_file != None: result_file.close()
       
   314     
       
   315     def test_simple_generate_prj3(self):
       
   316         
       
   317         self.remove_if_exists(os.path.normpath("output/output/test3.txt"))
       
   318         
       
   319         impl = self.load_impl('Layer1/implml/file3.templateml')
       
   320         impl.generate()
       
   321         
       
   322         self.assertTrue(os.path.exists(os.path.normpath("output/output/test3.txt")))
       
   323         result_file = None
       
   324         try:
       
   325             result_file = open(os.path.normpath("output/output/test3.txt"))
       
   326             for line in result_file:
       
   327                 self.assertTrue(line == "'Hello John Doe!'")
       
   328         finally:
       
   329             if result_file != None: result_file.close()
       
   330 
       
   331     def test_simple_generate_prj4_with_filters(self):
       
   332         
       
   333         self.remove_if_exists(os.path.normpath("output/output/test4a.txt"))
       
   334         self.remove_if_exists(os.path.normpath("output/output/test4b.txt"))
       
   335         self.remove_if_exists(os.path.normpath("output/output/test4c.txt"))
       
   336         self.remove_if_exists(os.path.normpath("output/output/test4d.txt"))
       
   337         
       
   338         impl = self.load_impl('Layer1/implml/file4.templateml')
       
   339         #impl.context = {'name' : 'John Doe'}
       
   340         impl.generate()
       
   341         
       
   342         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")))
       
   344         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")))
       
   346         
       
   347         result_file1 = None
       
   348         result_file2 = None
       
   349         result_file3 = None
       
   350         result_file4 = None
       
   351         
       
   352         try:
       
   353             result_file1 = open(os.path.normpath("output/output/test4a.txt"))
       
   354             result_file2 = open(os.path.normpath("output/output/test4b.txt"))
       
   355             result_file3 = open(os.path.normpath("output/output/test4c.txt"))
       
   356             result_file4 = open(os.path.normpath("output/output/test4d.txt"))
       
   357             
       
   358             if result_file1 != None: 
       
   359                 for line in result_file1:
       
   360                     self.assertTrue(line == "'Hello John Doe!'")
       
   361             else:
       
   362                 self.fail("No result file found: output/output/test4a.txt")
       
   363             
       
   364             if result_file2 != None: 
       
   365                 for line in result_file2:
       
   366                     self.assertTrue(line == "'Hello John Doe again!'")
       
   367             else:
       
   368                 self.fail("No result file found: output/output/test4b.txt")
       
   369             
       
   370             if result_file3 != None:
       
   371                 for line in result_file3:
       
   372                     self.assertTrue(line == "2+3=5")
       
   373             else:
       
   374                 self.fail("No result file found: output/output/test4c.txt")
       
   375 
       
   376             if result_file4 != None:
       
   377                 for line in result_file4:
       
   378                     self.assertTrue(line == "--6")
       
   379             else:
       
   380                 self.fail("No result file found: output/output/test4d.txt")
       
   381                 
       
   382         finally:
       
   383             if result_file1 != None: result_file1.close()
       
   384             if result_file2 != None: result_file2.close()
       
   385             if result_file3 != None: result_file3.close()
       
   386             if result_file4 != None: result_file4.close()
       
   387 
       
   388     def test_simple_generate_prj_extfiles_with_filters(self):
       
   389         
       
   390         self.remove_if_exists(os.path.normpath("output/output/test_ext_temp_file.txt"))
       
   391         
       
   392         impl = self.load_impl('Layer1/implml/external_tempfile.templateml')
       
   393         
       
   394         impl.generate()
       
   395         
       
   396         self.assertTrue(os.path.exists(os.path.normpath("output/output/test_ext_temp_file.txt")))
       
   397         
       
   398         result_file1 = None
       
   399         
       
   400         try:
       
   401             result_file1 = open(os.path.normpath("output/output/test_ext_temp_file.txt"))
       
   402             lines = 0
       
   403 
       
   404             if result_file1 != None: 
       
   405                 for line in result_file1:
       
   406                     self.assertTrue(line == "2+3=-1")
       
   407                     lines += 1
       
   408             else:
       
   409                 self.fail("No result file found: output/output/test_ext_temp_file.txt")
       
   410             
       
   411             self.assertTrue(lines == 1, "Wrong number of lines generated.")
       
   412             
       
   413         finally:
       
   414             if result_file1 != None: result_file1.close()
       
   415 
       
   416     def test_generate_prj1(self):
       
   417         
       
   418         self.remove_if_exists(os.path.normpath("output/output/test5a.txt"))
       
   419         
       
   420         impl = self.load_impl('Layer1/implml/file5.templateml')
       
   421         impl.generate()
       
   422         self.assertTrue(os.path.exists(os.path.normpath("output/output/test5a.txt")))
       
   423         
       
   424         result_file1 = None
       
   425         try:
       
   426             result_file1 = open(os.path.normpath("output/output/test5a.txt"))
       
   427             
       
   428             if result_file1 != None: 
       
   429                 for line in result_file1:
       
   430                     self.assertTrue(line == "'Hello John Doe'")
       
   431             else:
       
   432                 self.fail("No result file found: output/output/test5a.txt")
       
   433         finally:
       
   434             if result_file1 != None: result_file1.close()
       
   435 
       
   436     def test_generate_access_configuration(self):
       
   437         
       
   438         self.remove_if_exists(os.path.normpath("output/access_configuration.txt"))
       
   439         
       
   440         impl = self.load_impl('Layer1/implml/access_configuration.templateml')
       
   441         impl.generate()
       
   442         self.assertTrue(os.path.exists(os.path.normpath("output/access_configuration.txt")))
       
   443         
       
   444         result_file1 = None
       
   445         try:
       
   446             result_file1 = open(os.path.normpath("output/access_configuration.txt"))
       
   447             data = result_file1.read() 
       
   448             if result_file1 != None: 
       
   449                 self.assertTrue(data.startswith("Configuration name: root1.confml"))
       
   450             else:
       
   451                 self.fail("No result file found: output/access_configuration.txt")
       
   452         finally:
       
   453             if result_file1 != None: result_file1.close()
       
   454     
       
   455     def test_create_context_dict1(self):
       
   456         impl = self.load_impl('Layer1/implml/file6.templateml')
       
   457         impl.context = impl.create_dict()
       
   458         impl.generate()
       
   459     
       
   460     def test_list_output_files(self):
       
   461         impl = self.load_impl('Layer1/implml/file1.templateml')
       
   462         impl.set_output_root('outdir')
       
   463         output_files = impl.list_output_files()
       
   464         expected = map(lambda n: os.path.normpath(n), [
       
   465             'outdir/test.txt',
       
   466             'outdir/output/test2.txt',
       
   467             'outdir/test3.txt',
       
   468             'outdir/output/test4.txt',
       
   469             'outdir/some/test/path/test5.txt',
       
   470         ])
       
   471         self.assertEquals(sorted(output_files), sorted(expected))
       
   472     
       
   473     def test_has_ref(self):
       
   474         impl = self.load_impl('Layer1/implml/has_ref_template_test2.templateml')
       
   475         self.assertEquals(impl.has_ref('Feature1.StringSetting_not_found'), False)
       
   476         self.assertEquals(impl.has_ref('Feature1.StringSetting1'), True)
       
   477         self.assertEquals(impl.has_ref('Feature2'), True)
       
   478         self.assertEquals(impl.has_ref('Feature2.set1.sub1'), True)
       
   479         self.assertEquals(impl.has_ref('Feature2.set1.sub2'), True)
       
   480         self.assertEquals(impl.has_ref('Feature1.UnicodeValueSetting'), True)
       
   481     
       
   482     def test_has_ref_external_template(self):
       
   483         impl = self.load_impl('Layer1/implml/has_ref_template_test3.templateml')
       
   484         self.assertEquals(impl.has_ref('Feature1.StringSetting_not_found'), False)
       
   485         self.assertEquals(impl.has_ref('Feature1.StringSetting1'), True)
       
   486         self.assertEquals(impl.has_ref('Feature2'), True)
       
   487         self.assertEquals(impl.has_ref('Feature2.set1.sub1'), True)
       
   488         self.assertEquals(impl.has_ref('Feature2.set1.sub2'), True)
       
   489         self.assertEquals(impl.has_ref('Feature1.UnicodeValueSetting'), True)
       
   490 
       
   491     def test_has_ref_with_featree(self):
       
   492         impl = self.load_impl('Layer1/implml/has_ref_template_test.templateml')
       
   493         self.assertEquals(impl.has_ref('Feature1.StringSetting'), True)
       
   494         self.assertEquals(impl.has_ref('Feature2.StringSetting'), True)
       
   495     
       
   496     def test_unicode_in_template_and_value(self):
       
   497         OUTPUT_DIR = os.path.join(ROOT_PATH, 'output', 'unicode_test')
       
   498         self.remove_if_exists(OUTPUT_DIR)
       
   499         
       
   500         fs = filestorage.FileStorage(testdata)
       
   501         p = api.Project(fs)
       
   502         config = p.get_configuration('root1.confml')
       
   503         impls = plugin.get_impl_set(config,'unicode_template_test\.templateml$')
       
   504         impls.output = OUTPUT_DIR
       
   505         impls.generate()
       
   506         self.assert_exists_and_contains_something(os.path.join(OUTPUT_DIR, "unicode_template_test.txt"))
       
   507         
       
   508         # Check that the output exists and contains expected lines
       
   509         f = open(os.path.join(OUTPUT_DIR, "unicode_template_test.txt"), "rb")
       
   510         try:        data = f.read().decode('utf-8')
       
   511         finally:    f.close()
       
   512         self.assertTrue(data.find(u'Value of Feature1.UnicodeValueSetting: カタカナ') != -1)
       
   513         self.assertTrue(data.find(u'Unicode from template: ελληνικά') != -1)
       
   514     
       
   515     def test_create_context_dict_and_list(self):
       
   516         config = self.load_config('create_dict_test.confml')
       
   517         impls = plugin.get_impl_set(config, r'^create_dict_test/implml/test\.templateml$')
       
   518         self.assertEquals(1, len(impls))
       
   519         impl = iter(impls).next()
       
   520         context = impl.create_dict()
       
   521         feat_tree = context['feat_tree']
       
   522         feat_list = context['feat_list']
       
   523         self.assertEqual(context['configuration'], config)
       
   524         
       
   525         # Check the created dictionary
       
   526         expected_tree_file = os.path.join(ROOT_PATH, 'create_dict_test', 'expected_tree.txt')
       
   527         expected_tree = eval(self.read_data_from_file(expected_tree_file).replace('\r', ''))
       
   528         if feat_tree != expected_tree:
       
   529             dir = os.path.join(ROOT_PATH, "temp", "create_dict_test", "tree")
       
   530             self.create_dir(dir)
       
   531             filename = os.path.join(dir, "expected.txt")
       
   532             self.write_data_to_file(filename, self.feature_tree_to_str(expected_tree))
       
   533             filename = os.path.join(dir, "actual.txt")
       
   534             self.write_data_to_file(filename, self.feature_tree_to_str(feat_tree))
       
   535             self.fail("Feature tree is not what was expected, see the files in '%s'" % dir)
       
   536         
       
   537         # Check the created list
       
   538         expected_list_file = os.path.join(ROOT_PATH, 'create_dict_test', 'expected_list.txt')
       
   539         expected_list = eval(self.read_data_from_file(expected_list_file).replace('\r', ''))
       
   540         if feat_list != expected_list:
       
   541             dir = os.path.join(ROOT_PATH, "temp", "create_dict_test", "list")
       
   542             self.create_dir(dir)
       
   543             filename = os.path.join(dir, "expected.txt")
       
   544             self.write_data_to_file(filename, self.feature_list_to_str(expected_list))
       
   545             filename = os.path.join(dir, "actual.txt")
       
   546             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)
       
   548     
       
   549     def feature_tree_to_str(self, d, indent_amount=0):
       
   550         """
       
   551         Pretty-print a feature tree dictionary into a string.
       
   552         """
       
   553         INDENT_AMOUNT = 4
       
   554         indent = indent_amount * ' '
       
   555         temp = ['{']
       
   556         if len(d) > 0: temp.append('\n')
       
   557         
       
   558         indent += (INDENT_AMOUNT + indent_amount) * ' '
       
   559         
       
   560         # Function for sorting the dict contents so that keys starting with
       
   561         # '_' are first
       
   562         def key_func(key_and_value):
       
   563             key = key_and_value[0]
       
   564             if key.startswith('_'): return '\x00' + key
       
   565             else:                   return key
       
   566         
       
   567         for key, value in sorted(d.items(), key=key_func):
       
   568             temp.append(indent)
       
   569             if isinstance(value, dict):
       
   570                 temp.append("%r: %s," % (key, self.feature_tree_to_str(value, indent_amount + INDENT_AMOUNT)))
       
   571             else:
       
   572                 temp.append("%r: %r," % (key, value))
       
   573             temp.append('\n')
       
   574             
       
   575         if len(d) > 0: temp.append(indent)
       
   576         temp.append('}')
       
   577         return ''.join(temp)
       
   578     
       
   579     def feature_list_to_str(self, lst):
       
   580         """
       
   581         Pretty-print a feature list into a string.
       
   582         """
       
   583         temp = ['[\n']
       
   584         for item in lst:
       
   585             if isinstance(item, dict):  temp.append(self.feature_tree_to_str(item))
       
   586             else:                       temp.append(repr(item))
       
   587             temp.append(',\n')
       
   588         temp.append(']')
       
   589         return ''.join(temp)
       
   590     
       
   591     
       
   592     def test_utf_bom_support(self):
       
   593         OUTPUT_DIR = os.path.join(ROOT_PATH, 'temp/utf_bom_test')
       
   594         self.recreate_dir(OUTPUT_DIR)
       
   595         impl = self.load_impl('Layer1/implml/utf_bom_test.templateml')
       
   596         impl.set_output_root(OUTPUT_DIR)
       
   597         impl.generate()
       
   598         
       
   599         def check(file, contents):
       
   600             FILE = os.path.join(OUTPUT_DIR, file)
       
   601             self.assert_file_content_equals(FILE, contents)
       
   602         
       
   603         h = hex_to_bindata
       
   604         
       
   605         # The all-around default should be UTF-8 without BOM
       
   606         check('default.txt',           h('31 30 30 E282AC'))
       
   607         
       
   608         check('iso_8859_15_default.txt',    h('31 30 30 A4'))
       
   609         check('utf8_default.txt',           h('31 30 30 E282AC'))
       
   610         check('utf16be_default.txt',        h('0031 0030 0030 20AC'))
       
   611         check('utf16le_default.txt',        h('3100 3000 3000 AC20'))
       
   612 
       
   613         check('iso_8859_15_no_bom.txt',     h('31 30 30 A4'))
       
   614         check('utf8_no_bom.txt',            h('31 30 30 E282AC'))
       
   615         check('utf16be_no_bom.txt',         h('0031 0030 0030 20AC'))
       
   616         check('utf16le_no_bom.txt',         h('3100 3000 3000 AC20'))
       
   617         
       
   618         check('iso_8859_15_with_bom.txt',   h('31 30 30 A4'))
       
   619         check('utf8_with_bom.txt',          h('EFBBBF 31 30 30 E282AC'))
       
   620         check('utf16be_with_bom.txt',       h('FEFF 0031 0030 0030 20AC'))
       
   621         check('utf16le_with_bom.txt',       h('FFFE 3100 3000 3000 AC20'))
       
   622         
       
   623         if sys.byteorder == 'little':
       
   624             check('utf16_default.txt',  h('FFFE 3100 3000 3000 AC20'))
       
   625             check('utf16_no_bom.txt',   h('3100 3000 3000 AC20'))
       
   626             check('utf16_with_bom.txt', h('FFFE 3100 3000 3000 AC20'))
       
   627         else:
       
   628             check('utf16_default.txt',  h('FEFF 0031 0030 0030 20AC'))
       
   629             check('utf16_no_bom.txt',   h('0031 0030 0030 20AC'))
       
   630             check('utf16_with_bom.txt', h('FEFF 0031 0030 0030 20AC'))
       
   631     
       
   632     def test_invalid_encoding(self):
       
   633         DATA = """<?xml version="1.0" encoding="UTF-8"?>
       
   634           <templateml xmlns=\"http://www.s60.com/xml/templateml/1\">
       
   635             <output file="test.txt" encoding="foocode">foo</output>
       
   636           </templateml>"""
       
   637         reader = templatemlplugin.TemplatemlImplReader()
       
   638         self.assertRaises(exceptions.ParseError, reader.fromstring, DATA)
       
   639     
       
   640 
       
   641 class TestExtractRefsFromTemplate(unittest.TestCase):
       
   642     def test_extract_refs_from_template(self):
       
   643         def t(data, expected_refs):
       
   644             actual_refs = templatemlplugin.TemplatemlImpl._extract_refs_from_template(data)
       
   645             self.assertEquals(expected_refs, actual_refs)
       
   646         
       
   647         t("some text {{ feat_tree.Foo }} more text",
       
   648           ['Foo'])
       
   649         t("some text {{ feat_tree.Foo.Bar }} more text",
       
   650           ['Foo.Bar'])
       
   651         t("some text {{feat_tree.Foo.Bar}} more text",
       
   652           ['Foo.Bar'])
       
   653         t("some text {{ feat_tree.Foo.Bar.Baz }} more text",
       
   654           ['Foo.Bar.Baz'])
       
   655         t("some text {{ feat_tree.Foo.Bar.Baz._value }} more text",
       
   656           ['Foo.Bar.Baz'])
       
   657         t(u"some text {{ feat_tree.ударения.ελληνικά }} more text",
       
   658           [u'ударения.ελληνικά'])
       
   659         t(u"some text {{ feat_tree.ударения.ελληνικά._value }} more text",
       
   660           [u'ударения.ελληνικά'])
       
   661         t("some text {{ feat_tree.MyFeature.MySetting|some_filter }} more text",
       
   662           ['MyFeature.MySetting'])
       
   663         t(u"some text {{ feat_tree.MyFeature.MySetting|some_filter }} more text",
       
   664           ['MyFeature.MySetting'])
       
   665         t("some text {{ feat_tree.MyFeature.MySetting | some_filter }} more text",
       
   666           ['MyFeature.MySetting'])
       
   667         t("some text {{ feat_tree.MyFeature.MySetting._value|some_filter }} more text",
       
   668           ['MyFeature.MySetting'])
       
   669         t("some text {{ feat_tree.Xyz.Zyx._type }} more {{feat_tree.Xyz.Zyx._type}} text",
       
   670           ['Xyz.Zyx', 'Xyz.Zyx'])
       
   671         t("some text {% for x in feat_tree.MyFeature.MySetting._value|sort %} more text",
       
   672           ['MyFeature.MySetting'])
       
   673         
       
   674 if __name__ == '__main__':
       
   675   unittest.main()