PageRenderTime 135ms CodeModel.GetById 53ms app.highlight 37ms RepoModel.GetById 40ms app.codeStats 1ms

/tests/regressiontests/i18n/commands/extraction.py

https://code.google.com/p/mango-py/
Python | 170 lines | 131 code | 37 blank | 2 comment | 22 complexity | d90c9a19088d16aceef10f4ff13006a7 MD5 | raw file
  1# -*- encoding: utf-8 -*-
  2import os
  3import re
  4import shutil
  5from django.test import TestCase
  6from django.core import management
  7
  8LOCALE='de'
  9
 10class ExtractorTests(TestCase):
 11
 12    PO_FILE='locale/%s/LC_MESSAGES/django.po' % LOCALE
 13
 14    def setUp(self):
 15        self._cwd = os.getcwd()
 16        self.test_dir = os.path.abspath(os.path.dirname(__file__))
 17
 18    def _rmrf(self, dname):
 19        if os.path.commonprefix([self.test_dir, os.path.abspath(dname)]) != self.test_dir:
 20            return
 21        shutil.rmtree(dname)
 22
 23    def tearDown(self):
 24        os.chdir(self.test_dir)
 25        try:
 26            self._rmrf('locale/%s' % LOCALE)
 27        except OSError:
 28            pass
 29        os.chdir(self._cwd)
 30
 31    def assertMsgId(self, msgid, s, use_quotes=True):
 32        if use_quotes:
 33            msgid = '"%s"' % msgid
 34        return self.assertTrue(re.search('^msgid %s' % msgid, s, re.MULTILINE))
 35
 36    def assertNotMsgId(self, msgid, s, use_quotes=True):
 37        if use_quotes:
 38            msgid = '"%s"' % msgid
 39        return self.assertTrue(not re.search('^msgid %s' % msgid, s, re.MULTILINE))
 40
 41
 42class BasicExtractorTests(ExtractorTests):
 43
 44    def test_comments_extractor(self):
 45        os.chdir(self.test_dir)
 46        management.call_command('makemessages', locale=LOCALE, verbosity=0)
 47        self.assertTrue(os.path.exists(self.PO_FILE))
 48        po_contents = open(self.PO_FILE, 'r').read()
 49        self.assertTrue('#. Translators: This comment should be extracted' in po_contents)
 50        self.assertTrue('This comment should not be extracted' not in po_contents)
 51        # Comments in templates
 52        self.assertTrue('#. Translators: Django template comment for translators' in po_contents)
 53        self.assertTrue("#. Translators: Django comment block for translators\n#. string's meaning unveiled" in po_contents)
 54
 55        self.assertTrue('#. Translators: One-line translator comment #1' in po_contents)
 56        self.assertTrue('#. Translators: Two-line translator comment #1\n#. continued here.' in po_contents)
 57
 58        self.assertTrue('#. Translators: One-line translator comment #2' in po_contents)
 59        self.assertTrue('#. Translators: Two-line translator comment #2\n#. continued here.' in po_contents)
 60
 61        self.assertTrue('#. Translators: One-line translator comment #3' in po_contents)
 62        self.assertTrue('#. Translators: Two-line translator comment #3\n#. continued here.' in po_contents)
 63
 64        self.assertTrue('#. Translators: One-line translator comment #4' in po_contents)
 65        self.assertTrue('#. Translators: Two-line translator comment #4\n#. continued here.' in po_contents)
 66
 67        self.assertTrue('#. Translators: One-line translator comment #5 -- with non ASCII characters: αινσϊφ' in po_contents)
 68        self.assertTrue('#. Translators: Two-line translator comment #5 -- with non ASCII characters: αινσϊφ\n#. continued here.' in po_contents)
 69
 70    def test_templatize(self):
 71        os.chdir(self.test_dir)
 72        management.call_command('makemessages', locale=LOCALE, verbosity=0)
 73        self.assertTrue(os.path.exists(self.PO_FILE))
 74        po_contents = open(self.PO_FILE, 'r').read()
 75        self.assertMsgId('I think that 100%% is more that 50%% of anything.', po_contents)
 76        self.assertMsgId('I think that 100%% is more that 50%% of %\(obj\)s.', po_contents)
 77
 78    def test_extraction_error(self):
 79        os.chdir(self.test_dir)
 80        shutil.copyfile('./templates/template_with_error.txt', './templates/template_with_error.html')
 81        self.assertRaises(SyntaxError, management.call_command, 'makemessages', locale=LOCALE, verbosity=0)
 82        try: # TODO: Simplify this try/try block when we drop support for Python 2.4
 83            try:
 84                management.call_command('makemessages', locale=LOCALE, verbosity=0)
 85            except SyntaxError, e:
 86                self.assertEqual(str(e), 'Translation blocks must not include other block tags: blocktrans (file templates/template_with_error.html, line 3)')
 87        finally:
 88            os.remove('./templates/template_with_error.html')
 89            os.remove('./templates/template_with_error.html.py') # Waiting for #8536 to be fixed
 90
 91
 92class JavascriptExtractorTests(ExtractorTests):
 93
 94    PO_FILE='locale/%s/LC_MESSAGES/djangojs.po' % LOCALE
 95
 96    def test_javascript_literals(self):
 97        os.chdir(self.test_dir)
 98        management.call_command('makemessages', domain='djangojs', locale=LOCALE, verbosity=0)
 99        self.assertTrue(os.path.exists(self.PO_FILE))
100        po_contents = open(self.PO_FILE, 'r').read()
101        self.assertMsgId('This literal should be included.', po_contents)
102        self.assertMsgId('This one as well.', po_contents)
103
104
105class IgnoredExtractorTests(ExtractorTests):
106
107    def test_ignore_option(self):
108        os.chdir(self.test_dir)
109        management.call_command('makemessages', locale=LOCALE, verbosity=0, ignore_patterns=['ignore_dir/*'])
110        self.assertTrue(os.path.exists(self.PO_FILE))
111        po_contents = open(self.PO_FILE, 'r').read()
112        self.assertMsgId('This literal should be included.', po_contents)
113        self.assertNotMsgId('This should be ignored.', po_contents)
114
115
116class SymlinkExtractorTests(ExtractorTests):
117
118    def setUp(self):
119        self._cwd = os.getcwd()
120        self.test_dir = os.path.abspath(os.path.dirname(__file__))
121        self.symlinked_dir = os.path.join(self.test_dir, 'templates_symlinked')
122
123    def tearDown(self):
124        super(SymlinkExtractorTests, self).tearDown()
125        os.chdir(self.test_dir)
126        try:
127            os.remove(self.symlinked_dir)
128        except OSError:
129            pass
130        os.chdir(self._cwd)
131
132    def test_symlink(self):
133        if hasattr(os, 'symlink'):
134            if os.path.exists(self.symlinked_dir):
135                self.assertTrue(os.path.islink(self.symlinked_dir))
136            else:
137                os.symlink(os.path.join(self.test_dir, 'templates'), self.symlinked_dir)
138            os.chdir(self.test_dir)
139            management.call_command('makemessages', locale=LOCALE, verbosity=0, symlinks=True)
140            self.assertTrue(os.path.exists(self.PO_FILE))
141            po_contents = open(self.PO_FILE, 'r').read()
142            self.assertMsgId('This literal should be included.', po_contents)
143            self.assertTrue('templates_symlinked/test.html' in po_contents)
144
145
146class CopyPluralFormsExtractorTests(ExtractorTests):
147
148    def test_copy_plural_forms(self):
149        os.chdir(self.test_dir)
150        management.call_command('makemessages', locale=LOCALE, verbosity=0)
151        self.assertTrue(os.path.exists(self.PO_FILE))
152        po_contents = open(self.PO_FILE, 'r').read()
153        self.assertTrue('Plural-Forms: nplurals=2; plural=(n != 1)' in po_contents)
154
155
156class NoWrapExtractorTests(ExtractorTests):
157
158    def test_no_wrap_enabled(self):
159        os.chdir(self.test_dir)
160        management.call_command('makemessages', locale=LOCALE, verbosity=0, no_wrap=True)
161        self.assertTrue(os.path.exists(self.PO_FILE))
162        po_contents = open(self.PO_FILE, 'r').read()
163        self.assertMsgId('This literal should also be included wrapped or not wrapped depending on the use of the --no-wrap option.', po_contents)
164
165    def test_no_wrap_disabled(self):
166        os.chdir(self.test_dir)
167        management.call_command('makemessages', locale=LOCALE, verbosity=0, no_wrap=False)
168        self.assertTrue(os.path.exists(self.PO_FILE))
169        po_contents = open(self.PO_FILE, 'r').read()
170        self.assertMsgId('""\n"This literal should also be included wrapped or not wrapped depending on the "\n"use of the --no-wrap option."', po_contents, use_quotes=False)