# -*- coding: ISO-8859-1 -*-
##############################################################################
#
# Copyright (c) 2001, 2002 Zope Corporation and Contributors.
# All Rights Reserved.
#
# This software is subject to the provisions of the Zope Public License,
# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
# FOR A PARTICULAR PURPOSE.
#
##############################################################################
"""Tests for TALInterpreter.
$Id: test_talinterpreter.py 69783 2006-08-25 17:17:29Z mgedmin $
"""
import os
import sys
import unittest
from StringIO import StringIO
from zope.tal.taldefs import METALError, I18NError, TAL_VERSION
from zope.tal.taldefs import TALExpressionError
from zope.tal.htmltalparser import HTMLTALParser
from zope.tal.talparser import TALParser
from zope.tal.talinterpreter import TALInterpreter
from zope.tal.talgenerator import TALGenerator
from zope.tal.dummyengine import DummyEngine
from zope.tal.dummyengine import MultipleDomainsDummyEngine
from zope.tal.dummyengine import DummyTranslationDomain
from zope.tal.tests import utils
from zope.i18nmessageid import Message
class TestCaseBase(unittest.TestCase):
def _compile(self, source, source_file=None):
generator = TALGenerator(xml=0, source_file=source_file)
parser = HTMLTALParser(generator)
parser.parseString(source)
program, macros = parser.getCode()
return program, macros
class MacroErrorsTestCase(TestCaseBase):
def setUp(self):
dummy, macros = self._compile('
\n')
def test_content_with_messageid_and_i18nname_and_i18ntranslate(self):
# Let's tell the user this is incredibly silly!
self.assertRaises(
I18NError, self._compile,
'')
def test_content_with_explicit_messageid(self):
# Let's tell the user this is incredibly silly!
self.assertRaises(
I18NError, self._compile,
'')
def test_content_with_plaintext_and_i18nname_and_i18ntranslate(self):
# Let's tell the user this is incredibly silly!
self.assertRaises(
I18NError, self._compile,
'green')
def test_translate_static_text_as_dynamic(self):
program, macros = self._compile(
'
\n')
def test_for_correct_msgids(self):
self.engine.translationDomain.clearMsgids()
result = StringIO()
#GChapelle:
#I have the feeling the i18n:translate with the i18n:name is wrong
#
#program, macros = self._compile(
# '
This is text for '
# '.
')
program, macros = self._compile(
'
This is text for '
'.
')
self.interpreter = TALInterpreter(program, {}, self.engine,
stream=result)
self.interpreter()
msgids = self.engine.translationDomain.getMsgids('default')
msgids.sort()
self.assertEqual(1, len(msgids))
self.assertEqual('This is text for ${bar_name}.', msgids[0][0])
self.assertEqual({'bar_name': 'BaRvAlUe'}, msgids[0][1])
self.assertEqual(
'
')
self.interpreter = TALInterpreter(program, {}, self.engine,
stream=result)
self.interpreter()
msgids = self.engine.translationDomain.getMsgids('default')
msgids.sort()
self.assertEqual(2, len(msgids))
self.assertEqual('This is text for ${bar_name}.', msgids[1][0])
self.assertEqual({'bar_name': 'BARVALUE'}, msgids[1][1])
self.assertEqual(
'
THIS IS TEXT FOR BARVALUE.
\n',
result.getvalue())
def test_i18ntranslate_i18nname_and_attributes(self):
# Test for Issue 301: Bug with i18n:name and i18n:translate
# on the same element
self.engine.translationDomain.clearMsgids()
result = StringIO()
program, macros = self._compile(
'
')
self._check(program, 'hello\n\n')
def test_html_script(self):
program, macros = self._compile(
'')
self._check(program, 'Hello world!\n')
def test_html_script_and_javascript(self):
program, macros = self._compile(
'\n'
'')
self._check(program,
'\n'
'Hello world!\n')
class I18NErrorsTestCase(TestCaseBase):
def _check(self, src, msg):
try:
self._compile(src)
except I18NError:
pass
else:
self.fail(msg)
def test_id_with_replace(self):
self._check('',
"expected i18n:id with tal:replace to be denied")
def test_missing_values(self):
self._check('',
"missing i18n:attributes value not caught")
self._check('',
"missing i18n:data value not caught")
self._check('',
"missing i18n:id value not caught")
def test_id_with_attributes(self):
self._check('''''',
"expected attribute being both part of tal:attributes" +
" and having a msgid in i18n:attributes to be denied")
class OutputPresentationTestCase(TestCaseBase):
def test_attribute_wrapping(self):
# To make sure the attribute-wrapping code is invoked, we have to
# include at least one TAL/METAL attribute to avoid having the start
# tag optimized into a rawtext instruction.
INPUT = r"""
"""
EXPECTED = r'''
''' "\n"
self.compare(INPUT, EXPECTED)
def test_unicode_content(self):
INPUT = """
""", source_file='page.pt')
engine = DummyEngine(macros=m_macros)
interp = TALInterpreter(p_program, {}, engine, StringIO())
# Expect TALExpressionError: unknown variable: 'no_such_thing'
self.assertRaises(TALExpressionError, interp)
# Now the engine should know where the error occurred
self.assertEquals(engine.source_file, 'macros.pt')
self.assertEquals(engine.position, (5, 14))
def test_suite():
suite = unittest.makeSuite(I18NErrorsTestCase)
suite.addTest(unittest.makeSuite(MacroErrorsTestCase))
suite.addTest(unittest.makeSuite(MacroExtendTestCase))
suite.addTest(unittest.makeSuite(OutputPresentationTestCase))
suite.addTest(unittest.makeSuite(ScriptTestCase))
suite.addTest(unittest.makeSuite(I18NCornerTestCaseMessage))
suite.addTest(unittest.makeSuite(UnusedExplicitDomainTestCase))
suite.addTest(unittest.makeSuite(TestSourceAnnotations))
suite.addTest(unittest.makeSuite(TestErrorTracebacks))
# TODO: Deactivated test, since we have not found a solution for this and
# it is a deep and undocumented HTML parser issue.
# Fred is looking into this.
#suite.addTest(unittest.makeSuite(MacroFunkyErrorTest))
return suite
if __name__ == "__main__":
errs = utils.run_suite(test_suite())
sys.exit(errs and 1 or 0)