cmlenz@1: # -*- coding: utf-8 -*- cmlenz@1: # jruigrok@530: # Copyright (C) 2007-2011 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 fschwarz@531: from babel.util import FixedOffsetTimezone 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: fschwarz@545: def test_applies_specified_encoding_during_read(self): fschwarz@545: buf = StringIO(u''' fschwarz@545: msgid "" fschwarz@545: msgstr "" fschwarz@545: "Project-Id-Version: 3.15\\n" fschwarz@545: "Report-Msgid-Bugs-To: Fliegender Zirkus \\n" fschwarz@545: "POT-Creation-Date: 2007-09-27 11:19+0700\\n" fschwarz@545: "PO-Revision-Date: 2007-09-27 21:42-0700\\n" fschwarz@545: "Last-Translator: John \\n" fschwarz@545: "Language-Team: German Lang \\n" fschwarz@545: "Plural-Forms: nplurals=2; plural=(n != 1)\\n" fschwarz@545: "MIME-Version: 1.0\\n" fschwarz@545: "Content-Type: text/plain; charset=iso-8859-1\\n" fschwarz@545: "Content-Transfer-Encoding: 8bit\\n" fschwarz@545: "Generated-By: Babel 1.0dev-r313\\n" fschwarz@545: fschwarz@545: msgid "foo" fschwarz@545: msgstr "bär"'''.encode('iso-8859-1')) fschwarz@545: catalog = pofile.read_po(buf, locale='de_DE') fschwarz@545: self.assertEqual(u'bär', catalog.get('foo').string) fschwarz@545: 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: pjenvey@291: def test_header_entry(self): pjenvey@291: buf = StringIO(r'''\ pjenvey@291: # SOME DESCRIPTIVE TITLE. pjenvey@291: # Copyright (C) 2007 THE PACKAGE'S COPYRIGHT HOLDER pjenvey@291: # This file is distributed under the same license as the PACKAGE package. pjenvey@291: # FIRST AUTHOR , 2007. pjenvey@291: # pjenvey@291: #, fuzzy pjenvey@291: msgid "" pjenvey@291: msgstr "" pjenvey@291: "Project-Id-Version: 3.15\n" pjenvey@291: "Report-Msgid-Bugs-To: Fliegender Zirkus \n" pjenvey@291: "POT-Creation-Date: 2007-09-27 11:19+0700\n" pjenvey@291: "PO-Revision-Date: 2007-09-27 21:42-0700\n" pjenvey@291: "Last-Translator: John \n" pjenvey@291: "Language-Team: German Lang \n" pjenvey@291: "Plural-Forms: nplurals=2; plural=(n != 1)\n" pjenvey@291: "MIME-Version: 1.0\n" pjenvey@291: "Content-Type: text/plain; charset=iso-8859-2\n" pjenvey@291: "Content-Transfer-Encoding: 8bit\n" pjenvey@291: "Generated-By: Babel 1.0dev-r313\n" pjenvey@291: ''') pjenvey@291: catalog = pofile.read_po(buf) pjenvey@291: self.assertEqual(1, len(list(catalog))) pjenvey@291: self.assertEqual(u'3.15', catalog.version) pjenvey@291: self.assertEqual(u'Fliegender Zirkus ', pjenvey@291: catalog.msgid_bugs_address) pjenvey@291: self.assertEqual(datetime(2007, 9, 27, 11, 19, pjenvey@291: tzinfo=FixedOffsetTimezone(7 * 60)), pjenvey@291: catalog.creation_date) pjenvey@291: self.assertEqual(u'John ', catalog.last_translator) pjenvey@291: self.assertEqual(u'German Lang ', catalog.language_team) pjenvey@291: self.assertEqual(u'iso-8859-2', catalog.charset) pjenvey@291: self.assertEqual(True, list(catalog)[0].fuzzy) pjenvey@291: 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@335: def test_with_context(self): cmlenz@335: buf = StringIO(r'''# Some string in the menu cmlenz@335: #: main.py:1 cmlenz@335: msgctxt "Menu" cmlenz@335: msgid "foo" cmlenz@335: msgstr "Voh" cmlenz@335: cmlenz@335: # Another string in the menu cmlenz@335: #: main.py:2 cmlenz@335: msgctxt "Menu" cmlenz@335: msgid "bar" cmlenz@335: msgstr "Bahr" cmlenz@335: ''') cmlenz@335: catalog = pofile.read_po(buf, ignore_obsolete=True) cmlenz@335: self.assertEqual(2, len(catalog)) cmlenz@350: message = catalog.get('foo', context='Menu') cmlenz@335: self.assertEqual('Menu', message.context) cmlenz@350: message = catalog.get('bar', context='Menu') cmlenz@335: self.assertEqual('Menu', message.context) cmlenz@335: palgarvio@421: # And verify it pass through write_po palgarvio@421: out_buf = StringIO() palgarvio@421: pofile.write_po(out_buf, catalog, omit_header=True) palgarvio@421: assert out_buf.getvalue().strip() == buf.getvalue().strip(), \ palgarvio@421: out_buf.getvalue() palgarvio@421: cmlenz@428: def test_with_context_two(self): cmlenz@428: buf = StringIO(r'''msgctxt "Menu" cmlenz@428: msgid "foo" cmlenz@428: msgstr "Voh" cmlenz@428: cmlenz@428: msgctxt "Mannu" cmlenz@428: msgid "bar" cmlenz@428: msgstr "Bahr" cmlenz@428: ''') cmlenz@428: catalog = pofile.read_po(buf, ignore_obsolete=True) cmlenz@428: self.assertEqual(2, len(catalog)) cmlenz@428: message = catalog.get('foo', context='Menu') cmlenz@428: self.assertEqual('Menu', message.context) cmlenz@428: message = catalog.get('bar', context='Mannu') cmlenz@428: self.assertEqual('Mannu', message.context) cmlenz@428: cmlenz@428: # And verify it pass through write_po cmlenz@428: out_buf = StringIO() cmlenz@428: pofile.write_po(out_buf, catalog, omit_header=True) cmlenz@428: assert out_buf.getvalue().strip() == buf.getvalue().strip(), out_buf.getvalue() cmlenz@428: jruigrok@443: def test_single_plural_form(self): jruigrok@443: buf = StringIO(r'''msgid "foo" jruigrok@446: msgid_plural "foos" jruigrok@443: msgstr[0] "Voh"''') jruigrok@443: catalog = pofile.read_po(buf, locale='ja_JP') jruigrok@443: self.assertEqual(1, len(catalog)) jruigrok@443: self.assertEqual(1, catalog.num_plurals) jruigrok@443: message = catalog['foo'] jruigrok@443: self.assertEqual(1, len(message.string)) jruigrok@443: jruigrok@442: def test_singular_plural_form(self): palgarvio@370: buf = StringIO(r'''msgid "foo" jruigrok@446: msgid_plural "foos" palgarvio@370: msgstr[0] "Voh" jruigrok@442: msgstr[1] "Vohs"''') jruigrok@442: catalog = pofile.read_po(buf, locale='nl_NL') palgarvio@370: self.assertEqual(1, len(catalog)) jruigrok@442: self.assertEqual(2, catalog.num_plurals) cmlenz@377: message = catalog['foo'] jruigrok@442: self.assertEqual(2, len(message.string)) jruigrok@441: palgarvio@370: def test_more_than_two_plural_forms(self): palgarvio@370: buf = StringIO(r'''msgid "foo" jruigrok@446: msgid_plural "foos" palgarvio@370: msgstr[0] "Voh" jruigrok@445: msgstr[1] "Vohs" jruigrok@445: msgstr[2] "Vohss"''') palgarvio@370: catalog = pofile.read_po(buf, locale='lv_LV') palgarvio@370: self.assertEqual(1, len(catalog)) palgarvio@370: self.assertEqual(3, catalog.num_plurals) cmlenz@377: message = catalog['foo'] palgarvio@370: self.assertEqual(3, len(message.string)) jruigrok@445: self.assertEqual(u'Vohss', message.string[2]) palgarvio@370: jruigrok@444: def test_plural_with_square_brackets(self): jruigrok@444: buf = StringIO(r'''msgid "foo" jruigrok@446: msgid_plural "foos" jruigrok@444: msgstr[0] "Voh [text]" jruigrok@444: msgstr[1] "Vohs [text]"''') jruigrok@444: catalog = pofile.read_po(buf, locale='nb_NO') jruigrok@444: self.assertEqual(1, len(catalog)) jruigrok@444: self.assertEqual(2, catalog.num_plurals) jruigrok@444: message = catalog['foo'] jruigrok@444: self.assertEqual(2, len(message.string)) jruigrok@444: 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: fschwarz@545: def test_write_po_file_with_specified_charset(self): fschwarz@545: catalog = Catalog(charset='iso-8859-1') fschwarz@545: catalog.add('foo', u'äöü', locations=[('main.py', 1)]) fschwarz@545: buf = StringIO() fschwarz@545: pofile.write_po(buf, catalog, omit_header=False) fschwarz@545: po_file = buf.getvalue().strip() fschwarz@545: assert r'"Content-Type: text/plain; charset=iso-8859-1\n"' in po_file fschwarz@545: assert u'msgstr "äöü"'.encode('iso-8859-1') in po_file fschwarz@545: 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: cmlenz@315: def test_wrap_locations_with_hyphens(self): cmlenz@315: catalog = Catalog() cmlenz@315: catalog.add(u'foo', locations=[ cmlenz@315: ('doupy/templates/base/navmenu.inc.html.py', 60) cmlenz@315: ]) cmlenz@315: catalog.add(u'foo', locations=[ cmlenz@315: ('doupy/templates/job-offers/helpers.html', 22) cmlenz@315: ]) cmlenz@315: buf = StringIO() cmlenz@315: pofile.write_po(buf, catalog, omit_header=True) cmlenz@315: self.assertEqual('''#: doupy/templates/base/navmenu.inc.html.py:60 cmlenz@315: #: doupy/templates/job-offers/helpers.html:22 cmlenz@315: msgid "foo" cmlenz@315: msgstr ""''', buf.getvalue().strip()) palgarvio@423: palgarvio@423: def test_no_wrap_and_width_behaviour_on_comments(self): palgarvio@423: catalog = Catalog() palgarvio@423: catalog.add("Pretty dam long message id, which must really be big " palgarvio@423: "to test this wrap behaviour, if not it won't work.", palgarvio@423: locations=[("fake.py", n) for n in range(1, 30)]) palgarvio@423: buf = StringIO() palgarvio@423: pofile.write_po(buf, catalog, width=None, omit_header=True) palgarvio@423: self.assertEqual("""\ palgarvio@423: #: fake.py:1 fake.py:2 fake.py:3 fake.py:4 fake.py:5 fake.py:6 fake.py:7 palgarvio@423: #: fake.py:8 fake.py:9 fake.py:10 fake.py:11 fake.py:12 fake.py:13 fake.py:14 palgarvio@423: #: fake.py:15 fake.py:16 fake.py:17 fake.py:18 fake.py:19 fake.py:20 fake.py:21 palgarvio@423: #: fake.py:22 fake.py:23 fake.py:24 fake.py:25 fake.py:26 fake.py:27 fake.py:28 palgarvio@423: #: fake.py:29 palgarvio@423: msgid "pretty dam long message id, which must really be big to test this wrap behaviour, if not it won't work." palgarvio@423: msgstr "" palgarvio@423: palgarvio@423: """, buf.getvalue().lower()) palgarvio@423: buf = StringIO() palgarvio@423: pofile.write_po(buf, catalog, width=100, omit_header=True) palgarvio@423: self.assertEqual("""\ palgarvio@423: #: fake.py:1 fake.py:2 fake.py:3 fake.py:4 fake.py:5 fake.py:6 fake.py:7 fake.py:8 fake.py:9 fake.py:10 palgarvio@423: #: fake.py:11 fake.py:12 fake.py:13 fake.py:14 fake.py:15 fake.py:16 fake.py:17 fake.py:18 fake.py:19 palgarvio@423: #: fake.py:20 fake.py:21 fake.py:22 fake.py:23 fake.py:24 fake.py:25 fake.py:26 fake.py:27 fake.py:28 palgarvio@423: #: fake.py:29 palgarvio@423: msgid "" palgarvio@423: "pretty dam long message id, which must really be big to test this wrap behaviour, if not it won't" palgarvio@423: " work." palgarvio@423: msgstr "" palgarvio@423: palgarvio@423: """, buf.getvalue().lower()) cmlenz@315: 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: aronacher@356: def test_silent_location_fallback(self): aronacher@356: buf = StringIO('''\ aronacher@356: #: broken_file.py aronacher@356: msgid "missing line number" aronacher@356: msgstr "" aronacher@356: aronacher@356: #: broken_file.py:broken_line_number aronacher@356: msgid "broken line number" aronacher@356: msgstr ""''') aronacher@356: catalog = pofile.read_po(buf) cmlenz@377: self.assertEqual(catalog['missing line number'].locations, []) cmlenz@377: self.assertEqual(catalog['broken line number'].locations, []) aronacher@356: fschwarz@582: fschwarz@582: class PofileFunctionsTestCase(unittest.TestCase): fschwarz@582: fschwarz@582: def test_unescape(self): fschwarz@582: escaped = u'"Say:\\n \\"hello, world!\\"\\n"' fschwarz@582: unescaped = u'Say:\n "hello, world!"\n' fschwarz@582: self.assertNotEqual(unescaped, escaped) fschwarz@582: self.assertEqual(unescaped, pofile.unescape(escaped)) fschwarz@582: fschwarz@582: def test_unescape_of_quoted_newline(self): fschwarz@582: # regression test for #198 fschwarz@582: self.assertEqual(r'\n', pofile.unescape(r'"\\n"')) fschwarz@582: fschwarz@582: cmlenz@1: def suite(): cmlenz@1: suite = unittest.TestSuite() fschwarz@544: suite.addTest(doctest.DocTestSuite(pofile, optionflags=doctest.ELLIPSIS)) cmlenz@106: suite.addTest(unittest.makeSuite(ReadPoTestCase)) cmlenz@104: suite.addTest(unittest.makeSuite(WritePoTestCase)) fschwarz@582: suite.addTest(unittest.makeSuite(PofileFunctionsTestCase)) cmlenz@1: return suite cmlenz@1: cmlenz@1: if __name__ == '__main__': cmlenz@1: unittest.main(defaultTest='suite')