# -*- 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$
"""
import os
import sys
import unittest
from StringIO import StringIO
from zope.tal.taldefs import METALError, I18NError, TAL_VERSION
from zope.tal.htmltalparser import HTMLTALParser
from zope.tal.talparser import TALParser
from zope.tal.talinterpreter import TALInterpreter
from zope.tal.dummyengine import DummyEngine
from zope.tal.dummyengine import MultipleDomainsDummyEngine
from zope.tal.dummyengine import DummyTranslationDomain
from zope.tal.tests import utils
# BBB 2005/10/10 -- MessageIDs are to be removed for Zope 3.3
import zope.deprecation
zope.deprecation.__show__.off()
from zope.i18nmessageid import MessageID, Message
zope.deprecation.__show__.on()
class TestCaseBase(unittest.TestCase):
def _compile(self, source):
parser = HTMLTALParser()
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 = """