コード例 #1
0
    def test_copy_template(self):
        """
        template.copy() should create missing lang specific templates.
        """
        template = L10nTemplate(path.join(TEMPLATE_DIRS[0],
                                          'l10n_blocks_without_langs.html'))
        # cause the template to be read and parsed before mocking open
        template.blocks
        codecs_open = 'lib.l10n_utils.management.commands.l10n_check.codecs.open'
        open_mock = MagicMock(spec=file)
        open_buffer = StringIO()
        open_mock.return_value.__enter__.return_value = open_buffer
        with patch(codecs_open, open_mock):
            template.copy('de')

        # braces doubled for .format()
        good_value = dedent("""\
            {{# Version: {0} #}}

            {{% extends "l10n_blocks_without_langs.html" %}}

            {{% l10n donnie %}}
            Phone's ringing Dude.
            {{% endl10n %}}\n
        """.format(get_todays_version()))
        self.assertEqual(open_buffer.getvalue(), good_value)
コード例 #2
0
    def test_update_template(self):
        """
        template.update() should update lang specific templates.
        """
        template = L10nTemplate(path.join(TEMPLATE_DIRS[0],
                                          'l10n_blocks_with_langs.html'))
        # cause the template to be read and parsed before mocking open
        template.blocks
        codecs_open = 'lib.l10n_utils.management.commands.l10n_check.codecs.open'
        open_mock = MagicMock(spec=file)
        open_buffer = StringIO()
        # for writing the new file
        open_mock.return_value.__enter__.return_value = open_buffer
        # for reading the old file
        open_mock().read.return_value = codecs.open(
            template.l10n_path('de')).read()

        with patch(codecs_open, open_mock):
            template.update('de')

        # braces doubled for .format()
        good_value = dedent("""\
            {{# Version: {0} #}}

            {{% extends "l10n_blocks_with_langs.html" %}}

            {{% l10n donnie %}}
            Phone's ringing Dude.
            {{% was %}}
            I am the walrus.
            {{% endl10n %}}\n\n
        """.format(get_todays_version()))
        self.assertEqual(open_buffer.getvalue(), good_value)
コード例 #3
0
    def test_filter_blocks(self):
        """Should return a list of blocks appropriate for a given lang"""
        template = L10nTemplate(source="""
            {% l10n dude locales=fr,es-ES,ru 20121212 %}
                This aggression will not stand, man.
            {% endl10n %}
            {% l10n walter, locales=es-ES,ru 20121212 %}
                I'm stayin'. Finishin' my coffee.
            {% endl10n %}
            {% l10n donnie 20121212 %}
                Phone's ringing Dude.
            {% endl10n %}
        """)

        lang_blocks = template.blocks_for_lang('fr')
        self.assertEqual(len(lang_blocks), 2)
        self.assertEqual(lang_blocks[0]['name'], 'dude')
        self.assertEqual(lang_blocks[1]['name'], 'donnie')

        lang_blocks = template.blocks_for_lang('es-ES')
        self.assertEqual(len(lang_blocks), 3)
        self.assertEqual(lang_blocks[0]['name'], 'dude')
        self.assertEqual(lang_blocks[1]['name'], 'walter')
        self.assertEqual(lang_blocks[2]['name'], 'donnie')

        lang_blocks = template.blocks_for_lang('pt-BR')
        self.assertEqual(len(lang_blocks), 1)
        self.assertEqual(lang_blocks[0]['name'], 'donnie')
コード例 #4
0
 def test_list_templates(self):
     """Make sure we capture both html and txt templates."""
     TEMPLATES = ['mozorg/home.html',
                  'mozorg/emails/other.txt']
     tmpls = [t for t in list_templates()
              if L10nTemplate(t).rel_path in TEMPLATES]
     assert len(tmpls) == len(TEMPLATES)
コード例 #5
0
 def test_blocks_called_once(self):
     """
     Test that the cached_property decorator really works in our situation.
     """
     template = L10nTemplate(source="""
         {% l10n donnie 20121212 %}
             Phone's ringing Dude.
         {% endl10n %}
     """)
     with patch.object(template, 'parser') as mock_parser:
         template.blocks
         template.blocks_for_lang('de')
         template.blocks
         self.assertEqual(mock_parser.parse.call_count, 1)
コード例 #6
0
 def test_update_template_no_lang(self):
     """
     template.update() should skip files without blocks for the given locale.
     """
     template = L10nTemplate(path.join(TEMPLATE_DIRS[0],
                                       'l10n_blocks_with_langs.html'))
     # cause the template to be read and parsed before mocking open
     template.blocks
     codecs_open = 'lib.l10n_utils.management.commands.l10n_check.codecs.open'
     open_mock = MagicMock(spec=file)
     with patch(codecs_open, open_mock):
         template.update('zh-TW')
         file_handle = open_mock.return_value.__enter__.return_value
         assert not file_handle.write.called
         template.update('de')
         assert file_handle.write.called