def setUp(self): program = [] macros = {} engine = DummyEngine() self.interpreter = TALInterpreter(program, macros, engine) self.sio = self.interpreter.stream = StringIO() self.interpreter._pending_source_annotation = True
def test_for_raw_msgids(self): # Test for Issue 314: i18n:translate removes line breaks from # <pre>...</pre> contents # HTML mode xlatdmn = self._getCollectingTranslationDomain() result = StringIO() program, macros = self._compile( '<div i18n:translate=""> This is text\n' ' \tfor\n div. </div>' '<pre i18n:translate=""> This is text\n' ' <b>\tfor</b>\n pre. </pre>') self.interpreter = TALInterpreter(program, {}, self.engine, stream=result) self.interpreter() msgids = list(xlatdmn.data) msgids.sort() self.assertEqual(2, len(msgids)) self.assertEqual(' This is text\n <b>\tfor</b>\n pre. ', msgids[0][0]) self.assertEqual('This is text for div.', msgids[1][0]) self.assertEqual( '<div>THIS IS TEXT FOR DIV.</div>' '<pre> THIS IS TEXT\n <B>\tFOR</B>\n PRE. </pre>\n', result.getvalue()) # XML mode xlatdmn = self._getCollectingTranslationDomain() result = StringIO() parser = TALParser() parser.parseString( '<?xml version="1.0"?>\n' '<pre xmlns:i18n="http://xml.zpt._zope.org/namespaces/i18n"' ' i18n:translate=""> This is text\n' ' <b>\tfor</b>\n barvalue. </pre>') program, macros = parser.getCode() self.interpreter = TALInterpreter(program, {}, self.engine, stream=result) self.interpreter() msgids = list(xlatdmn.data) msgids.sort() self.assertEqual(1, len(msgids)) self.assertEqual('This is text <b> for</b> barvalue.', msgids[0][0]) self.assertEqual( '<?xml version="1.0"?>\n' '<pre>THIS IS TEXT <B> FOR</B> BARVALUE.</pre>\n', result.getvalue())
def test_div_in_p_using_macro(self): dummy, macros = self._compile('<p metal:define-macro="M">Booh</p>') engine = DummyEngine(macros) program, dummy = self._compile( '<p metal:use-macro="M"><div>foo</div></p>') interpreter = TALInterpreter(program, {}, engine) output = interpreter() self.assertEqual(output, '<p><div>foo</div></p>')
def test_preview_acme_template_source(self): # Render METAL attributes in acme_template result = StringIO() interpreter = TALInterpreter( self.acme_program, {}, self.engine, stream=result, tal=False) interpreter() actual = result.getvalue().strip() expected = self._read(('output', 'acme_template_source.html')).strip() self.assertEqual(actual, expected)
def test_preview_acme_template(self): # An ACME designer is previewing the ACME design. For the # purposes of this use case, extending a macro should act the # same as using a macro. result = StringIO() interpreter = TALInterpreter( self.acme_program, {}, self.engine, stream=result) interpreter() actual = result.getvalue().strip() expected = self._read(('output', 'acme_template.html')).strip() self.assertEqual(actual, expected)
def test(): import sys p = TALParser() file = "tests/input/test01.xml" if sys.argv[1:]: file = sys.argv[1] p.parseFile(file) program, macros = p.getCode() from zpt._zope.tal.talinterpreter import TALInterpreter from zpt._zope.tal.dummyengine import DummyEngine engine = DummyEngine(macros) TALInterpreter(program, macros, engine, sys.stdout, wrap=0)()
def time_tal(fn, count): p = HTMLTALParser() p.parseFile(fn) program, macros = p.getCode() engine = DummyEngine(macros) engine.globals = data tal = TALInterpreter(program, macros, engine, StringIO(), wrap=0, tal=1, strictinsert=0) return time_apply(tal, (), {}, count)
def profile_tal(fn, count, profiler): p = HTMLTALParser() p.parseFile(fn) program, macros = p.getCode() engine = DummyEngine(macros) engine.globals = data tal = TALInterpreter(program, macros, engine, StringIO(), wrap=0, tal=1, strictinsert=0) for i in range(4): tal() r = [None] * count for i in r: profiler.runcall(tal)
def test_for_correct_msgids(self): xlatdmn = self._getCollectingTranslationDomain() result = StringIO() program, macros = self._compile( '<div i18n:translate="">This is text for ' '<span i18n:translate="" tal:content="bar" ' 'i18n:name="bar_name"/>.</div>') self.interpreter = TALInterpreter(program, {}, self.engine, stream=result) self.interpreter() msgids = list(xlatdmn.data) msgids.sort() self.assertEqual(1, len(msgids)) self.assertEqual('This is text for ${bar_name}.', msgids[0][0]) self.assertEqual({'bar_name': '<span>BaRvAlUe</span>'}, msgids[0][1]) self.assertEqual( '<div>THIS IS TEXT FOR <span>BaRvAlUe</span>.</div>\n', result.getvalue())
def test_raw_msgids_and_i18ntranslate_i18nname(self): xlatdmn = self._getCollectingTranslationDomain() result = StringIO() program, macros = self._compile( '<div i18n:translate=""> This is text\n \tfor\n' '<pre i18n:name="bar" i18n:translate=""> \tbar\n </pre>.</div>') self.interpreter = TALInterpreter(program, {}, self.engine, stream=result) self.interpreter() msgids = list(xlatdmn.data) msgids.sort() self.assertEqual(2, len(msgids)) self.assertEqual(' \tbar\n ', msgids[0][0]) self.assertEqual('This is text for ${bar}.', msgids[1][0]) self.assertEqual({'bar': '<pre> \tBAR\n </pre>'}, msgids[1][1]) self.assertEqual( u'<div>THIS IS TEXT FOR <pre> \tBAR\n </pre>.</div>\n', result.getvalue())
def test_source_positions(self): # Ensure source file and position are set correctly by TAL macros = {} eng = DummyEngine(macros) page1_program, page1_macros = self.parse(eng, page1, 'page1') main_template_program, main_template_macros = self.parse( eng, main_template, 'main_template') footer_program, footer_macros = self.parse(eng, footer, 'footer') macros['main'] = main_template_macros['main'] macros['foot'] = footer_macros['foot'] stream = StringIO() interp = TALInterpreter(page1_program, macros, eng, stream) interp() self.assertEqual( stream.getvalue().strip(), expected.strip(), "Got result:\n%s\nExpected:\n%s" % (stream.getvalue(), expected))
def test_i18ntranslate_i18nname_and_attributes(self): # Test for Issue 301: Bug with i18n:name and i18n:translate # on the same element xlatdmn = self._getCollectingTranslationDomain() result = StringIO() program, macros = self._compile( '<p i18n:translate="">' 'Some static text and a <a tal:attributes="href string:url"' ' i18n:name="link" i18n:translate="">link text</a>.</p>') self.interpreter = TALInterpreter(program, {}, self.engine, stream=result) self.interpreter() msgids = list(xlatdmn.data) msgids.sort() self.assertEqual(2, len(msgids)) self.assertEqual('Some static text and a ${link}.', msgids[0][0]) self.assertEqual({'link': '<a href="url">LINK TEXT</a>'}, msgids[0][1]) self.assertEqual('link text', msgids[1][0]) self.assertEqual( '<p>SOME STATIC TEXT AND A <a href="url">LINK TEXT</a>.</p>\n', result.getvalue())
def pt_render(self, namespace, source=False, sourceAnnotations=False, showtal=False): """Render this Page Template""" self._cook_check() __traceback_supplement__ = (PageTemplateTracebackSupplement, self, namespace) if self._v_errors: raise PTRuntimeError(str(self._v_errors)) output = StringIO(u'') context = self.pt_getEngineContext(namespace) TALInterpreter(self._v_program, self._v_macros, context, output, tal=not source, showtal=showtal, strictinsert=0, sourceAnnotations=sourceAnnotations)() return output.getvalue()
def interpretit(it, engine=None, stream=None, tal=1, showtal=-1, strictinsert=1, i18nInterpolate=1, sourceAnnotations=0): from zpt._zope.tal.talinterpreter import TALInterpreter program, macros = it assert zpt._zope.tal.taldefs.isCurrentVersion(program) if engine is None: engine = DummyEngine(macros) TALInterpreter(program, macros, engine, stream, wrap=0, tal=tal, showtal=showtal, strictinsert=strictinsert, i18nInterpolate=i18nInterpolate, sourceAnnotations=sourceAnnotations)()
def _check(self, program, expected): result = StringIO() self.interpreter = TALInterpreter(program, {}, self.engine, stream=result) self.interpreter() self.assertEqual(expected, result.getvalue())
def compare(self, INPUT, EXPECTED): program, macros = self._compile(INPUT) sio = StringIO() interp = TALInterpreter(program, {}, DummyEngine(), sio, wrap=60) interp() self.assertEqual(sio.getvalue(), EXPECTED)
def setUp(self): dummy, macros = self._compile('<p metal:define-macro="M">Booh</p>') self.macro = macros['M'] self.engine = DummyEngine(macros) program, dummy = self._compile('<p metal:use-macro="M">Bah</p>') self.interpreter = TALInterpreter(program, {}, self.engine)