cmlenz@1: # -*- coding: utf-8 -*- cmlenz@1: # cmlenz@12: # Copyright (C) 2007 Edgewall Software cmlenz@1: # All rights reserved. cmlenz@1: # cmlenz@1: # This software is licensed as described in the file COPYING, which cmlenz@1: # you should have received as part of this distribution. The terms cmlenz@1: # are also available at http://babel.edgewall.org/wiki/License. cmlenz@1: # cmlenz@1: # This software consists of voluntary contributions made by many cmlenz@1: # individuals. For the exact contribution history, see the revision cmlenz@1: # history and logs, available at http://babel.edgewall.org/log/. cmlenz@1: cmlenz@104: from datetime import datetime cmlenz@1: import doctest cmlenz@24: from StringIO import StringIO cmlenz@1: import unittest cmlenz@1: cmlenz@181: from babel.messages.catalog import Catalog, Message cmlenz@54: from babel.messages import pofile cmlenz@1: cmlenz@17: cmlenz@106: class ReadPoTestCase(unittest.TestCase): cmlenz@106: cmlenz@196: def test_preserve_locale(self): cmlenz@196: buf = StringIO(r'''msgid "foo" cmlenz@196: msgstr "Voh"''') cmlenz@196: catalog = pofile.read_po(buf, locale='en_US') cmlenz@196: self.assertEqual('en_US', catalog.locale) cmlenz@196: cmlenz@196: def test_preserve_domain(self): cmlenz@196: buf = StringIO(r'''msgid "foo" cmlenz@196: msgstr "Voh"''') cmlenz@196: catalog = pofile.read_po(buf, domain='mydomain') cmlenz@196: self.assertEqual('mydomain', catalog.domain) cmlenz@196: cmlenz@106: def test_read_multiline(self): cmlenz@106: buf = StringIO(r'''msgid "" cmlenz@106: "Here's some text that\n" cmlenz@106: "includesareallylongwordthatmightbutshouldnt" cmlenz@106: " throw us into an infinite " cmlenz@106: "loop\n" cmlenz@106: msgstr ""''') cmlenz@106: catalog = pofile.read_po(buf) cmlenz@106: self.assertEqual(1, len(catalog)) cmlenz@106: message = list(catalog)[1] cmlenz@106: self.assertEqual("Here's some text that\nincludesareallylongwordthat" cmlenz@106: "mightbutshouldnt throw us into an infinite loop\n", cmlenz@106: message.id) cmlenz@196: palgarvio@175: def test_fuzzy_header(self): palgarvio@175: buf = StringIO(r'''\ palgarvio@175: # Translations template for AReallyReallyLongNameForAProject. palgarvio@175: # Copyright (C) 2007 ORGANIZATION palgarvio@175: # This file is distributed under the same license as the palgarvio@175: # AReallyReallyLongNameForAProject project. palgarvio@175: # FIRST AUTHOR , 2007. palgarvio@175: # palgarvio@175: #, fuzzy palgarvio@175: ''') palgarvio@175: catalog = pofile.read_po(buf) palgarvio@175: self.assertEqual(1, len(list(catalog))) palgarvio@175: self.assertEqual(True, list(catalog)[0].fuzzy) cmlenz@196: palgarvio@175: def test_not_fuzzy_header(self): palgarvio@175: buf = StringIO(r'''\ palgarvio@175: # Translations template for AReallyReallyLongNameForAProject. palgarvio@175: # Copyright (C) 2007 ORGANIZATION palgarvio@175: # This file is distributed under the same license as the palgarvio@175: # AReallyReallyLongNameForAProject project. palgarvio@175: # FIRST AUTHOR , 2007. palgarvio@175: # palgarvio@175: ''') palgarvio@175: catalog = pofile.read_po(buf) palgarvio@175: self.assertEqual(1, len(list(catalog))) palgarvio@175: self.assertEqual(False, list(catalog)[0].fuzzy) cmlenz@106: cmlenz@199: def test_obsolete_message(self): cmlenz@199: buf = StringIO(r'''# This is an obsolete message cmlenz@199: #~ msgid "foo" cmlenz@199: #~ msgstr "Voh" cmlenz@199: cmlenz@199: # This message is not obsolete cmlenz@199: #: main.py:1 cmlenz@199: msgid "bar" cmlenz@199: msgstr "Bahr" cmlenz@199: ''') cmlenz@199: catalog = pofile.read_po(buf) cmlenz@199: self.assertEqual(1, len(catalog)) cmlenz@199: self.assertEqual(1, len(catalog.obsolete)) cmlenz@199: message = catalog.obsolete[u'foo'] cmlenz@199: self.assertEqual(u'foo', message.id) cmlenz@199: self.assertEqual(u'Voh', message.string) cmlenz@199: self.assertEqual(['This is an obsolete message'], message.user_comments) cmlenz@199: cmlenz@199: def test_obsolete_message_ignored(self): cmlenz@199: buf = StringIO(r'''# This is an obsolete message cmlenz@199: #~ msgid "foo" cmlenz@199: #~ msgstr "Voh" cmlenz@199: cmlenz@199: # This message is not obsolete cmlenz@199: #: main.py:1 cmlenz@199: msgid "bar" cmlenz@199: msgstr "Bahr" cmlenz@199: ''') cmlenz@199: catalog = pofile.read_po(buf, ignore_obsolete=True) cmlenz@199: self.assertEqual(1, len(catalog)) cmlenz@199: self.assertEqual(0, len(catalog.obsolete)) cmlenz@199: cmlenz@106: cmlenz@104: class WritePoTestCase(unittest.TestCase): cmlenz@24: cmlenz@24: def test_join_locations(self): cmlenz@56: catalog = Catalog() cmlenz@56: catalog.add(u'foo', locations=[('main.py', 1)]) cmlenz@56: catalog.add(u'foo', locations=[('utils.py', 3)]) cmlenz@24: buf = StringIO() cmlenz@104: pofile.write_po(buf, catalog, omit_header=True) cmlenz@24: self.assertEqual('''#: main.py:1 utils.py:3 cmlenz@24: msgid "foo" cmlenz@24: msgstr ""''', buf.getvalue().strip()) cmlenz@24: cmlenz@228: def test_duplicate_comments(self): cmlenz@228: catalog = Catalog() cmlenz@228: catalog.add(u'foo', auto_comments=['A comment']) cmlenz@228: catalog.add(u'foo', auto_comments=['A comment']) cmlenz@228: buf = StringIO() cmlenz@228: pofile.write_po(buf, catalog, omit_header=True) cmlenz@228: self.assertEqual('''#. A comment cmlenz@228: msgid "foo" cmlenz@228: msgstr ""''', buf.getvalue().strip()) cmlenz@228: cmlenz@24: def test_wrap_long_lines(self): cmlenz@24: text = """Here's some text where cmlenz@24: white space and line breaks matter, and should cmlenz@24: cmlenz@24: not be removed cmlenz@24: cmlenz@24: """ cmlenz@56: catalog = Catalog() cmlenz@56: catalog.add(text, locations=[('main.py', 1)]) cmlenz@24: buf = StringIO() cmlenz@104: pofile.write_po(buf, catalog, no_location=True, omit_header=True, cmlenz@56: width=42) cmlenz@24: self.assertEqual(r'''msgid "" cmlenz@24: "Here's some text where \n" cmlenz@24: "white space and line breaks matter, and" cmlenz@24: " should\n" cmlenz@24: "\n" cmlenz@24: "not be removed\n" cmlenz@24: "\n" cmlenz@24: msgstr ""''', buf.getvalue().strip()) cmlenz@24: cmlenz@24: def test_wrap_long_lines_with_long_word(self): cmlenz@24: text = """Here's some text that cmlenz@24: includesareallylongwordthatmightbutshouldnt throw us into an infinite loop cmlenz@24: """ cmlenz@56: catalog = Catalog() cmlenz@56: catalog.add(text, locations=[('main.py', 1)]) cmlenz@24: buf = StringIO() cmlenz@104: pofile.write_po(buf, catalog, no_location=True, omit_header=True, cmlenz@56: width=32) cmlenz@24: self.assertEqual(r'''msgid "" cmlenz@24: "Here's some text that\n" cmlenz@24: "includesareallylongwordthatmightbutshouldnt" cmlenz@24: " throw us into an infinite " cmlenz@24: "loop\n" cmlenz@24: msgstr ""''', buf.getvalue().strip()) palgarvio@80: cmlenz@103: def test_wrap_long_lines_in_header(self): cmlenz@103: """ cmlenz@103: Verify that long lines in the header comment are wrapped correctly. cmlenz@103: """ cmlenz@104: catalog = Catalog(project='AReallyReallyLongNameForAProject', cmlenz@104: revision_date=datetime(2007, 4, 1)) cmlenz@103: buf = StringIO() cmlenz@104: pofile.write_po(buf, catalog) cmlenz@103: self.assertEqual('''\ cmlenz@103: # Translations template for AReallyReallyLongNameForAProject. cmlenz@103: # Copyright (C) 2007 ORGANIZATION cmlenz@103: # This file is distributed under the same license as the cmlenz@103: # AReallyReallyLongNameForAProject project. cmlenz@104: # FIRST AUTHOR , 2007. cmlenz@103: # cmlenz@103: #, fuzzy''', '\n'.join(buf.getvalue().splitlines()[:7])) cmlenz@103: palgarvio@80: def test_pot_with_translator_comments(self): palgarvio@80: catalog = Catalog() palgarvio@80: catalog.add(u'foo', locations=[('main.py', 1)], palgarvio@105: auto_comments=['Comment About `foo`']) palgarvio@80: catalog.add(u'bar', locations=[('utils.py', 3)], palgarvio@105: user_comments=['Comment About `bar` with', palgarvio@105: 'multiple lines.']) palgarvio@80: buf = StringIO() cmlenz@104: pofile.write_po(buf, catalog, omit_header=True) palgarvio@80: self.assertEqual('''#. Comment About `foo` palgarvio@80: #: main.py:1 palgarvio@80: msgid "foo" palgarvio@80: msgstr "" palgarvio@80: palgarvio@105: # Comment About `bar` with palgarvio@105: # multiple lines. palgarvio@80: #: utils.py:3 palgarvio@80: msgid "bar" palgarvio@80: msgstr ""''', buf.getvalue().strip()) cmlenz@24: cmlenz@190: def test_po_with_obsolete_message(self): cmlenz@181: catalog = Catalog() cmlenz@181: catalog.add(u'foo', u'Voh', locations=[('main.py', 1)]) cmlenz@181: catalog.obsolete['bar'] = Message(u'bar', u'Bahr', cmlenz@181: locations=[('utils.py', 3)], cmlenz@181: user_comments=['User comment']) cmlenz@181: buf = StringIO() cmlenz@181: pofile.write_po(buf, catalog, omit_header=True) cmlenz@181: self.assertEqual('''#: main.py:1 cmlenz@181: msgid "foo" cmlenz@181: msgstr "Voh" cmlenz@181: cmlenz@181: # User comment cmlenz@181: #~ msgid "bar" cmlenz@181: #~ msgstr "Bahr"''', buf.getvalue().strip()) cmlenz@181: cmlenz@190: def test_po_with_multiline_obsolete_message(self): cmlenz@190: catalog = Catalog() cmlenz@190: catalog.add(u'foo', u'Voh', locations=[('main.py', 1)]) cmlenz@190: msgid = r"""Here's a message that covers cmlenz@190: multiple lines, and should still be handled cmlenz@190: correctly. cmlenz@190: """ cmlenz@190: msgstr = r"""Here's a message that covers cmlenz@190: multiple lines, and should still be handled cmlenz@190: correctly. cmlenz@190: """ cmlenz@190: catalog.obsolete[msgid] = Message(msgid, msgstr, cmlenz@190: locations=[('utils.py', 3)]) cmlenz@190: buf = StringIO() cmlenz@190: pofile.write_po(buf, catalog, omit_header=True) cmlenz@190: self.assertEqual(r'''#: main.py:1 cmlenz@190: msgid "foo" cmlenz@190: msgstr "Voh" cmlenz@190: cmlenz@190: #~ msgid "" cmlenz@190: #~ "Here's a message that covers\n" cmlenz@190: #~ "multiple lines, and should still be handled\n" cmlenz@190: #~ "correctly.\n" cmlenz@190: #~ msgstr "" cmlenz@190: #~ "Here's a message that covers\n" cmlenz@190: #~ "multiple lines, and should still be handled\n" cmlenz@190: #~ "correctly.\n"''', buf.getvalue().strip()) cmlenz@190: cmlenz@191: def test_po_with_obsolete_message_ignored(self): cmlenz@191: catalog = Catalog() cmlenz@191: catalog.add(u'foo', u'Voh', locations=[('main.py', 1)]) cmlenz@191: catalog.obsolete['bar'] = Message(u'bar', u'Bahr', cmlenz@191: locations=[('utils.py', 3)], cmlenz@191: user_comments=['User comment']) cmlenz@191: buf = StringIO() cmlenz@191: pofile.write_po(buf, catalog, omit_header=True, ignore_obsolete=True) cmlenz@191: self.assertEqual('''#: main.py:1 cmlenz@191: msgid "foo" cmlenz@191: msgstr "Voh"''', buf.getvalue().strip()) cmlenz@191: cmlenz@203: def test_po_with_previous_msgid(self): cmlenz@203: catalog = Catalog() cmlenz@203: catalog.add(u'foo', u'Voh', locations=[('main.py', 1)], cmlenz@203: previous_id=u'fo') cmlenz@203: buf = StringIO() cmlenz@203: pofile.write_po(buf, catalog, omit_header=True, include_previous=True) cmlenz@203: self.assertEqual('''#: main.py:1 cmlenz@203: #| msgid "fo" cmlenz@203: msgid "foo" cmlenz@203: msgstr "Voh"''', buf.getvalue().strip()) cmlenz@203: cmlenz@203: def test_po_with_previous_msgid_plural(self): cmlenz@203: catalog = Catalog() cmlenz@203: catalog.add((u'foo', u'foos'), (u'Voh', u'Voeh'), cmlenz@203: locations=[('main.py', 1)], previous_id=(u'fo', u'fos')) cmlenz@203: buf = StringIO() cmlenz@203: pofile.write_po(buf, catalog, omit_header=True, include_previous=True) cmlenz@203: self.assertEqual('''#: main.py:1 cmlenz@203: #| msgid "fo" cmlenz@203: #| msgid_plural "fos" cmlenz@203: msgid "foo" cmlenz@203: msgid_plural "foos" cmlenz@203: msgstr[0] "Voh" cmlenz@203: msgstr[1] "Voeh"''', buf.getvalue().strip()) cmlenz@203: pjenvey@249: def test_sorted_po(self): pjenvey@249: catalog = Catalog() pjenvey@249: catalog.add(u'bar', locations=[('utils.py', 3)], pjenvey@249: user_comments=['Comment About `bar` with', pjenvey@249: 'multiple lines.']) pjenvey@249: catalog.add((u'foo', u'foos'), (u'Voh', u'Voeh'), pjenvey@249: locations=[('main.py', 1)]) pjenvey@249: buf = StringIO() pjenvey@249: pofile.write_po(buf, catalog, sort_output=True) pjenvey@249: value = buf.getvalue().strip() pjenvey@249: assert '''\ pjenvey@249: # Comment About `bar` with pjenvey@249: # multiple lines. pjenvey@249: #: utils.py:3 pjenvey@249: msgid "bar" pjenvey@249: msgstr "" pjenvey@249: pjenvey@249: #: main.py:1 pjenvey@249: msgid "foo" pjenvey@249: msgid_plural "foos" pjenvey@249: msgstr[0] "Voh" pjenvey@249: msgstr[1] "Voeh"''' in value pjenvey@249: assert value.find('msgid ""') < value.find('msgid "bar"') < value.find('msgid "foo"') cmlenz@24: cmlenz@1: def suite(): cmlenz@1: suite = unittest.TestSuite() cmlenz@1: suite.addTest(doctest.DocTestSuite(pofile)) cmlenz@106: suite.addTest(unittest.makeSuite(ReadPoTestCase)) cmlenz@104: suite.addTest(unittest.makeSuite(WritePoTestCase)) cmlenz@1: return suite cmlenz@1: cmlenz@1: if __name__ == '__main__': cmlenz@1: unittest.main(defaultTest='suite')