|
1 | 1 | #-*- coding: utf-8 -*-
|
| 2 | +import codecs |
| 3 | +import sys |
| 4 | +import unittest |
2 | 5 |
|
3 |
| -def test_something(): |
4 |
| - assert 1 == 1 |
| 6 | +from django_babel.extract import extract_django |
| 7 | +from babel.messages import extract |
| 8 | +from babel._compat import BytesIO, StringIO |
| 9 | + |
| 10 | + |
| 11 | +class ExtractDjangoTestCase(unittest.TestCase): |
| 12 | + # TODO: translator comments are not yet supported! |
| 13 | + |
| 14 | + def test_extract_simple(self): |
| 15 | + buf = BytesIO(b'{% trans "Bunny" %}') |
| 16 | + messages = list(extract_django(buf, extract.DEFAULT_KEYWORDS.keys(), [], {})) |
| 17 | + self.assertEqual([(1, None, u'Bunny', [])], messages) |
| 18 | + |
| 19 | + def test_extract_var(self): |
| 20 | + buf = BytesIO(b'{% blocktrans %}{{ anton }}{% endblocktrans %}') |
| 21 | + messages = list(extract_django(buf, extract.DEFAULT_KEYWORDS.keys(), [], {})) |
| 22 | + self.assertEqual([(1, None, u'%(anton)s', [])], messages) |
| 23 | + |
| 24 | + def test_extract_filter_with_filter(self): |
| 25 | + buf = BytesIO(b'{% blocktrans with berta=anton|lower %}{{ berta }}{% endblocktrans %}') |
| 26 | + messages = list(extract_django(buf, extract.DEFAULT_KEYWORDS.keys(), [], {})) |
| 27 | + self.assertEqual([(1, None, u'%(berta)s', [])], messages) |
| 28 | + |
| 29 | + def test_extract_with_interpolation(self): |
| 30 | + buf = BytesIO(b'{% blocktrans %}xxx{{ anton }}xxx{% endblocktrans %}') |
| 31 | + messages = list(extract_django(buf, extract.DEFAULT_KEYWORDS.keys(), [], {})) |
| 32 | + self.assertEqual([(1, None, u'xxx%(anton)sxxx', [])], messages) |
| 33 | + |
| 34 | + # TODO: Yet expected to not extract the comments. |
| 35 | + def test_extract_ignored_comment(self): |
| 36 | + buf = BytesIO(b'{# ignored comment #1 #}{% trans "Translatable literal #9a" %}') |
| 37 | + messages = list(extract_django(buf, extract.DEFAULT_KEYWORDS.keys(), [], {})) |
| 38 | + self.assertEqual([(1, None, u'Translatable literal #9a', [])], messages) |
| 39 | + |
| 40 | + def test_extract_ignored_comment2(self): |
| 41 | + buf = BytesIO(b'{# Translators: ignored i18n comment #1 #}{% trans "Translatable literal #9a" %}') |
| 42 | + messages = list(extract_django(buf, extract.DEFAULT_KEYWORDS.keys(), [], {})) |
| 43 | + self.assertEqual([(1, None, u'Translatable literal #9a', [])], messages) |
| 44 | + |
| 45 | + def test_extract_valid_comment(self): |
| 46 | + buf = BytesIO(b'{# ignored comment #6 #}{% trans "Translatable literal #9h" %}{# Translators: valid i18n comment #7 #}') |
| 47 | + messages = list(extract_django(buf, extract.DEFAULT_KEYWORDS.keys(), [], {})) |
| 48 | + self.assertEqual([(1, None, u'Translatable literal #9h', [])], messages) |
| 49 | + |
| 50 | + def test_extract_singular_form(self): |
| 51 | + buf = BytesIO(b'{% blocktrans count counter=number %}singular{% plural %}{{ counter }} plural{% endblocktrans %}') |
| 52 | + messages = list(extract_django(buf, extract.DEFAULT_KEYWORDS.keys(), [], {})) |
| 53 | + self.assertEqual([(1, 'ngettext', (u'singular', u'%(counter)s plural'), [])], messages) |
0 commit comments