# HG changeset patch
# User cmlenz
# Date 1271851004 0
# Node ID 6fe4feb2635b33e670c61c51a56bf5437fa4ff67
# Parent bf76a0fe20ae80a4ad9de12da7aacf5024ade4df
Split up the i18n unit tests into per-directive test cases.
diff --git a/genshi/filters/tests/i18n.py b/genshi/filters/tests/i18n.py
--- a/genshi/filters/tests/i18n.py
+++ b/genshi/filters/tests/i18n.py
@@ -181,6 +181,20 @@
messages = list(translator.extract(tmpl.stream))
self.assertEqual(0, len(messages))
+ def test_translate_with_translations_object(self):
+ tmpl = MarkupTemplate("""
+
Foo
+ """)
+ translator = Translator(DummyTranslations({'Foo': 'Voh'}))
+ translator.setup(tmpl)
+ self.assertEqual("""
+ Voh
+ """, tmpl.generate().render())
+
+
+class MsgDirectiveTestCase(unittest.TestCase):
+
def test_extract_i18n_msg(self):
tmpl = MarkupTemplate("""
@@ -666,17 +680,6 @@
Voh
""", tmpl.generate().render())
- def test_translate_with_translations_object(self):
- tmpl = MarkupTemplate("""
- Foo
- """)
- translator = Translator(DummyTranslations({'Foo': 'Voh'}))
- translator.setup(tmpl)
- self.assertEqual("""
- Voh
- """, tmpl.generate().render())
-
def test_translate_i18n_msg_and_py_strip_directives(self):
tmpl = MarkupTemplate("""
@@ -849,146 +852,49 @@
e não pode ser editado nesta página.
""", tmpl.generate(editable=False).render())
- def test_translate_i18n_domain_with_msg_directives(self):
- #"""translate with i18n:domain and nested i18n:msg directives """
-
- tmpl = MarkupTemplate("""
-
- """)
- translations = DummyTranslations({'Bar': 'Voh'})
- translations.add_domain('foo', {'FooBar': 'BarFoo', 'Bar': 'PT_Foo'})
- translator = Translator(translations)
- translator.setup(tmpl)
- self.assertEqual("""
-
- """, tmpl.generate().render())
-
- def test_translate_i18n_domain_with_inline_directives(self):
- #"""translate with inlined i18n:domain and i18n:msg directives"""
- tmpl = MarkupTemplate("""
- FooBar
- """)
- translations = DummyTranslations({'Bar': 'Voh'})
- translations.add_domain('foo', {'FooBar': 'BarFoo'})
- translator = Translator(translations)
- translator.setup(tmpl)
- self.assertEqual("""
- BarFoo
- """, tmpl.generate().render())
-
- def test_translate_i18n_domain_without_msg_directives(self):
- #"""translate domain call without i18n:msg directives still uses current domain"""
-
- tmpl = MarkupTemplate("""
- Bar
-
- Bar
- """)
- translations = DummyTranslations({'Bar': 'Voh'})
- translations.add_domain('foo', {'FooBar': 'BarFoo', 'Bar': 'PT_Foo'})
- translator = Translator(translations)
- translator.setup(tmpl)
- self.assertEqual("""
- Voh
-
-
BarFoo
-
PT_Foo
-
PT_Foo
-
- Voh
- """, tmpl.generate().render())
-
- def test_translate_i18n_domain_as_directive_not_attribute(self):
- #"""translate with domain as directive"""
-
+ def test_extract_i18n_msg_with_py_strip(self):
tmpl = MarkupTemplate("""
-
- FooBar
- Bar
- Bar
-
- Bar
+
+ Please see Help for details.
+
""")
- translations = DummyTranslations({'Bar': 'Voh'})
- translations.add_domain('foo', {'FooBar': 'BarFoo', 'Bar': 'PT_Foo'})
- translator = Translator(translations)
- translator.setup(tmpl)
- self.assertEqual("""
- BarFoo
- PT_Foo
- PT_Foo
- Voh
- """, tmpl.generate().render())
+ translator = Translator()
+ tmpl.add_directives(Translator.NAMESPACE, translator)
+ messages = list(translator.extract(tmpl.stream))
+ self.assertEqual(1, len(messages))
+ self.assertEqual((3, None, 'Please see [1:Help] for details.', []),
+ messages[0])
- def test_translate_i18n_domain_nested_directives(self):
- #"""translate with nested i18n:domain directives"""
-
+ def test_extract_i18n_msg_with_py_strip_and_comment(self):
tmpl = MarkupTemplate("""
- Bar
-
- Bar
+
+ Please see Help for details.
+
""")
- translations = DummyTranslations({'Bar': 'Voh'})
- translations.add_domain('foo', {'FooBar': 'BarFoo', 'Bar': 'foo_Bar'})
- translations.add_domain('bar', {'Bar': 'bar_Bar'})
- translator = Translator(translations)
+ translator = Translator()
+ tmpl.add_directives(Translator.NAMESPACE, translator)
+ messages = list(translator.extract(tmpl.stream))
+ self.assertEqual(1, len(messages))
+ self.assertEqual((3, None, 'Please see [1:Help] for details.',
+ ['Foo']), messages[0])
+
+ def test_translate_i18n_msg_and_comment_with_py_strip_directives(self):
+ tmpl = MarkupTemplate("""
+ Foo
+ Foo
+ """)
+ translator = Translator(DummyTranslations({'Foo': 'Voh'}))
translator.setup(tmpl)
self.assertEqual("""
- Voh
-
-
BarFoo
-
bar_Bar
-
foo_Bar
-
- Voh
+ Voh
+ Voh
""", tmpl.generate().render())
- def test_translate_i18n_domain_with_empty_nested_domain_directive(self):
- #"""translate with empty nested i18n:domain directive does not use dngettext"""
- tmpl = MarkupTemplate("""
- Bar
-
- Bar
- """)
- translations = DummyTranslations({'Bar': 'Voh'})
- translations.add_domain('foo', {'FooBar': 'BarFoo', 'Bar': 'foo_Bar'})
- translations.add_domain('bar', {'Bar': 'bar_Bar'})
- translator = Translator(translations)
- translator.setup(tmpl)
- self.assertEqual("""
- Voh
-
-
BarFoo
-
Voh
-
foo_Bar
-
- Voh
- """, tmpl.generate().render())
+class ChooseDirectiveTestCase(unittest.TestCase):
def test_translate_i18n_choose_as_attribute(self):
tmpl = MarkupTemplate("""
@@ -1425,7 +1331,7 @@
'There are [1:%(num)s things].'), []),
messages[4])
- def test_translate_i18n_msg_as_element_with_attributes(self):
+ def test_translate_i18n_choose_as_element_with_attributes(self):
tmpl = MarkupTemplate("""
@@ -1453,178 +1359,6 @@
Da sind 3 Sachen.
""", tmpl.generate(link="/things", num=3).render(encoding=None))
- def test_translate_i18n_domain_with_nested_inlcudes(self):
- import os, shutil, tempfile
- from genshi.template.loader import TemplateLoader
- dirname = tempfile.mkdtemp(suffix='genshi_test')
- try:
- for idx in range(7):
- file1 = open(os.path.join(dirname, 'tmpl%d.html' % idx), 'w')
- try:
- file1.write("""
- Included tmpl$idx
- Bar $idx
- Bar
- Bar $idx
- Bar $idx
-
-
-
- """)
- finally:
- file1.close()
-
- file2 = open(os.path.join(dirname, 'tmpl10.html'), 'w')
- try:
- file2.write("""
-
- """)
- finally:
- file2.close()
-
- def callback(template):
- translations = DummyTranslations({'Bar %(idx)s': 'Voh %(idx)s'})
- translations.add_domain('foo', {'Bar %(idx)s': 'foo_Bar %(idx)s'})
- translations.add_domain('bar', {'Bar': 'bar_Bar'})
- translator = Translator(translations)
- translator.setup(template)
- loader = TemplateLoader([dirname], callback=callback)
- tmpl = loader.load('tmpl10.html')
-
- self.assertEqual("""
- Included tmpl0
- foo_Bar 0
- bar_Bar
- Voh 0
- Voh 0
- Included tmpl1
- foo_Bar 1
- bar_Bar
- Voh 1
- Voh 1
- Included tmpl2
- foo_Bar 2
- bar_Bar
- Voh 2
- Voh 2
- Included tmpl3
- foo_Bar 3
- bar_Bar
- Voh 3
- Voh 3
- Included tmpl4
- foo_Bar 4
- bar_Bar
- Voh 4
- Voh 4
- Included tmpl5
- foo_Bar 5
- bar_Bar
- Voh 5
- Voh 5
- Included tmpl6
- foo_Bar 6
- bar_Bar
- Voh 6
- Voh 6
- """, tmpl.generate(idx=-1).render())
- finally:
- shutil.rmtree(dirname)
-
- def test_translate_i18n_domain_with_nested_inlcudes_with_translatable_attrs(self):
- import os, shutil, tempfile
- from genshi.template.loader import TemplateLoader
- dirname = tempfile.mkdtemp(suffix='genshi_test')
- try:
- for idx in range(4):
- file1 = open(os.path.join(dirname, 'tmpl%d.html' % idx), 'w')
- try:
- file1.write("""
- Included tmpl$idx
- Bar $idx
- Bar
- Bar $idx
- Bar $idx
- Bar $idx
-
-
-
- """)
- finally:
- file1.close()
-
- file2 = open(os.path.join(dirname, 'tmpl10.html'), 'w')
- try:
- file2.write("""
-
- """)
- finally:
- file2.close()
-
- translations = DummyTranslations({'Bar %(idx)s': 'Voh %(idx)s',
- 'Bar': 'Voh'})
- translations.add_domain('foo', {'Bar %(idx)s': 'foo_Bar %(idx)s'})
- translations.add_domain('bar', {'Bar': 'bar_Bar'})
- translator = Translator(translations)
-
- def callback(template):
- translator.setup(template)
- loader = TemplateLoader([dirname], callback=callback)
- tmpl = loader.load('tmpl10.html')
-
- self.assertEqual("""
- Included tmpl0
- foo_Bar 0
- bar_Bar
- Voh 0
- Voh 0
- Voh 0
- Included tmpl1
- foo_Bar 1
- bar_Bar
- Voh 1
- Voh 1
- Voh 1
- Included tmpl2
- foo_Bar 2
- bar_Bar
- Voh 2
- Voh 2
- Voh 2
- Included tmpl3
- foo_Bar 3
- bar_Bar
- Voh 3
- Voh 3
- Voh 3
- """, tmpl.generate(idx=-1,
- dg=translations.dugettext).render())
- finally:
- shutil.rmtree(dirname)
-
- def test_translate_i18n_msg_and_comment_with_py_strip_directives(self):
- tmpl = MarkupTemplate("""
- Foo
- Foo
- """)
- translator = Translator(DummyTranslations({'Foo': 'Voh'}))
- translator.setup(tmpl)
- self.assertEqual("""
- Voh
- Voh
- """, tmpl.generate().render())
-
def test_translate_i18n_choose_and_py_strip(self):
tmpl = MarkupTemplate("""
@@ -1723,34 +1457,6 @@
""", tmpl.generate(two=1, fname='John', lname='Doe').render())
- def test_extract_i18n_msg_with_py_strip(self):
- tmpl = MarkupTemplate("""
-
- Please see Help for details.
-
- """)
- translator = Translator()
- tmpl.add_directives(Translator.NAMESPACE, translator)
- messages = list(translator.extract(tmpl.stream))
- self.assertEqual(1, len(messages))
- self.assertEqual((3, None, 'Please see [1:Help] for details.', []),
- messages[0])
-
- def test_extract_i18n_msg_with_py_strip_and_comment(self):
- tmpl = MarkupTemplate("""
-
- Please see Help for details.
-
- """)
- translator = Translator()
- tmpl.add_directives(Translator.NAMESPACE, translator)
- messages = list(translator.extract(tmpl.stream))
- self.assertEqual(1, len(messages))
- self.assertEqual((3, None, 'Please see [1:Help] for details.',
- ['Foo']), messages[0])
-
def test_extract_i18n_choose_as_attribute_and_py_strip(self):
tmpl = MarkupTemplate("""
@@ -1765,6 +1471,150 @@
self.assertEqual(1, len(messages))
self.assertEqual((3, 'ngettext', ('FooBar', 'FooBars'), []), messages[0])
+
+class DomainDirectiveTestCase(unittest.TestCase):
+
+ def test_translate_i18n_domain_with_msg_directives(self):
+ #"""translate with i18n:domain and nested i18n:msg directives """
+
+ tmpl = MarkupTemplate("""
+
+ """)
+ translations = DummyTranslations({'Bar': 'Voh'})
+ translations.add_domain('foo', {'FooBar': 'BarFoo', 'Bar': 'PT_Foo'})
+ translator = Translator(translations)
+ translator.setup(tmpl)
+ self.assertEqual("""
+
+ """, tmpl.generate().render())
+
+ def test_translate_i18n_domain_with_inline_directives(self):
+ #"""translate with inlined i18n:domain and i18n:msg directives"""
+ tmpl = MarkupTemplate("""
+ FooBar
+ """)
+ translations = DummyTranslations({'Bar': 'Voh'})
+ translations.add_domain('foo', {'FooBar': 'BarFoo'})
+ translator = Translator(translations)
+ translator.setup(tmpl)
+ self.assertEqual("""
+ BarFoo
+ """, tmpl.generate().render())
+
+ def test_translate_i18n_domain_without_msg_directives(self):
+ #"""translate domain call without i18n:msg directives still uses current domain"""
+
+ tmpl = MarkupTemplate("""
+ Bar
+
+ Bar
+ """)
+ translations = DummyTranslations({'Bar': 'Voh'})
+ translations.add_domain('foo', {'FooBar': 'BarFoo', 'Bar': 'PT_Foo'})
+ translator = Translator(translations)
+ translator.setup(tmpl)
+ self.assertEqual("""
+ Voh
+
+
BarFoo
+
PT_Foo
+
PT_Foo
+
+ Voh
+ """, tmpl.generate().render())
+
+ def test_translate_i18n_domain_as_directive_not_attribute(self):
+ #"""translate with domain as directive"""
+
+ tmpl = MarkupTemplate("""
+
+ FooBar
+ Bar
+ Bar
+
+ Bar
+ """)
+ translations = DummyTranslations({'Bar': 'Voh'})
+ translations.add_domain('foo', {'FooBar': 'BarFoo', 'Bar': 'PT_Foo'})
+ translator = Translator(translations)
+ translator.setup(tmpl)
+ self.assertEqual("""
+ BarFoo
+ PT_Foo
+ PT_Foo
+ Voh
+ """, tmpl.generate().render())
+
+ def test_translate_i18n_domain_nested_directives(self):
+ #"""translate with nested i18n:domain directives"""
+
+ tmpl = MarkupTemplate("""
+ Bar
+
+ Bar
+ """)
+ translations = DummyTranslations({'Bar': 'Voh'})
+ translations.add_domain('foo', {'FooBar': 'BarFoo', 'Bar': 'foo_Bar'})
+ translations.add_domain('bar', {'Bar': 'bar_Bar'})
+ translator = Translator(translations)
+ translator.setup(tmpl)
+ self.assertEqual("""
+ Voh
+
+
BarFoo
+
bar_Bar
+
foo_Bar
+
+ Voh
+ """, tmpl.generate().render())
+
+ def test_translate_i18n_domain_with_empty_nested_domain_directive(self):
+ #"""translate with empty nested i18n:domain directive does not use dngettext"""
+
+ tmpl = MarkupTemplate("""
+ Bar
+
+ Bar
+ """)
+ translations = DummyTranslations({'Bar': 'Voh'})
+ translations.add_domain('foo', {'FooBar': 'BarFoo', 'Bar': 'foo_Bar'})
+ translations.add_domain('bar', {'Bar': 'bar_Bar'})
+ translator = Translator(translations)
+ translator.setup(tmpl)
+ self.assertEqual("""
+ Voh
+
+
BarFoo
+
Voh
+
foo_Bar
+
+ Voh
+ """, tmpl.generate().render())
+
def test_translate_i18n_domain_with_inline_directive_on_START_NS(self):
tmpl = MarkupTemplate("""
@@ -1792,6 +1642,165 @@
BarFoo
""", tmpl.generate().render())
+ def test_translate_i18n_domain_with_nested_includes(self):
+ import os, shutil, tempfile
+ from genshi.template.loader import TemplateLoader
+ dirname = tempfile.mkdtemp(suffix='genshi_test')
+ try:
+ for idx in range(7):
+ file1 = open(os.path.join(dirname, 'tmpl%d.html' % idx), 'w')
+ try:
+ file1.write("""
+ Included tmpl$idx
+ Bar $idx
+ Bar
+ Bar $idx
+ Bar $idx
+
+
+
+ """)
+ finally:
+ file1.close()
+
+ file2 = open(os.path.join(dirname, 'tmpl10.html'), 'w')
+ try:
+ file2.write("""
+
+ """)
+ finally:
+ file2.close()
+
+ def callback(template):
+ translations = DummyTranslations({'Bar %(idx)s': 'Voh %(idx)s'})
+ translations.add_domain('foo', {'Bar %(idx)s': 'foo_Bar %(idx)s'})
+ translations.add_domain('bar', {'Bar': 'bar_Bar'})
+ translator = Translator(translations)
+ translator.setup(template)
+ loader = TemplateLoader([dirname], callback=callback)
+ tmpl = loader.load('tmpl10.html')
+
+ self.assertEqual("""
+ Included tmpl0
+ foo_Bar 0
+ bar_Bar
+ Voh 0
+ Voh 0
+ Included tmpl1
+ foo_Bar 1
+ bar_Bar
+ Voh 1
+ Voh 1
+ Included tmpl2
+ foo_Bar 2
+ bar_Bar
+ Voh 2
+ Voh 2
+ Included tmpl3
+ foo_Bar 3
+ bar_Bar
+ Voh 3
+ Voh 3
+ Included tmpl4
+ foo_Bar 4
+ bar_Bar
+ Voh 4
+ Voh 4
+ Included tmpl5
+ foo_Bar 5
+ bar_Bar
+ Voh 5
+ Voh 5
+ Included tmpl6
+ foo_Bar 6
+ bar_Bar
+ Voh 6
+ Voh 6
+ """, tmpl.generate(idx=-1).render())
+ finally:
+ shutil.rmtree(dirname)
+
+ def test_translate_i18n_domain_with_nested_includes_with_translatable_attrs(self):
+ import os, shutil, tempfile
+ from genshi.template.loader import TemplateLoader
+ dirname = tempfile.mkdtemp(suffix='genshi_test')
+ try:
+ for idx in range(4):
+ file1 = open(os.path.join(dirname, 'tmpl%d.html' % idx), 'w')
+ try:
+ file1.write("""
+ Included tmpl$idx
+ Bar $idx
+ Bar
+ Bar $idx
+ Bar $idx
+ Bar $idx
+
+
+
+ """)
+ finally:
+ file1.close()
+
+ file2 = open(os.path.join(dirname, 'tmpl10.html'), 'w')
+ try:
+ file2.write("""
+
+ """)
+ finally:
+ file2.close()
+
+ translations = DummyTranslations({'Bar %(idx)s': 'Voh %(idx)s',
+ 'Bar': 'Voh'})
+ translations.add_domain('foo', {'Bar %(idx)s': 'foo_Bar %(idx)s'})
+ translations.add_domain('bar', {'Bar': 'bar_Bar'})
+ translator = Translator(translations)
+
+ def callback(template):
+ translator.setup(template)
+ loader = TemplateLoader([dirname], callback=callback)
+ tmpl = loader.load('tmpl10.html')
+
+ self.assertEqual("""
+ Included tmpl0
+ foo_Bar 0
+ bar_Bar
+ Voh 0
+ Voh 0
+ Voh 0
+ Included tmpl1
+ foo_Bar 1
+ bar_Bar
+ Voh 1
+ Voh 1
+ Voh 1
+ Included tmpl2
+ foo_Bar 2
+ bar_Bar
+ Voh 2
+ Voh 2
+ Voh 2
+ Included tmpl3
+ foo_Bar 3
+ bar_Bar
+ Voh 3
+ Voh 3
+ Voh 3
+ """, tmpl.generate(idx=-1,
+ dg=translations.dugettext).render())
+ finally:
+ shutil.rmtree(dirname)
+
class ExtractTestCase(unittest.TestCase):
@@ -1947,6 +1956,9 @@
suite = unittest.TestSuite()
suite.addTest(doctest.DocTestSuite(Translator.__module__))
suite.addTest(unittest.makeSuite(TranslatorTestCase, 'test'))
+ suite.addTest(unittest.makeSuite(MsgDirectiveTestCase, 'test'))
+ suite.addTest(unittest.makeSuite(ChooseDirectiveTestCase, 'test'))
+ suite.addTest(unittest.makeSuite(DomainDirectiveTestCase, 'test'))
suite.addTest(unittest.makeSuite(ExtractTestCase, 'test'))
return suite