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