Exemple #1
0
 def testCallTemplateWithEqualsSyntax(self):
     source = 'abc {% call_template _call_test foo=bar qux=api.xxx %} def'
     template = django_helpers.DjangoTemplate(source)
     rendered = template.render(
         self._GetContext({
             'template_dir': self._TEST_DATA_DIR,
             'api': {
                 'xxx': 'yyy'
             },
             'bar': 'baz'
         }))
     self.assertEquals('abc 1baz1 2yyy2 3yyy3 def', rendered)
Exemple #2
0
 def testParamList(self):
     source = """method({% parameter_list %}
       {% parameter %}int a{% end_parameter%}
       {% parameter %}
         {% if false %}
            The condition fails, so the entire parameter is empty.
         {% endif %}
       {% end_parameter %}
       {% parameter %}string b{% end_parameter %}
     {% end_parameter_list %})"""
     template = django_helpers.DjangoTemplate(source)
     rendered = template.render(self._GetContext())
     self.assertEquals('method(int a, string b)', rendered)
Exemple #3
0
 def testNestedNoBlank(self):
     source = textwrap.dedent("""\
 {% noblank %}
 Foo
 {% noeol %}
 Bar
 {% eol %}
 {% endnoeol %}
 {% eol %}
 {% endnoblank %}X
 """)
     expected = 'Foo\nBar\n\nX\n'
     template = django_helpers.DjangoTemplate(source)
     self.assertEquals(expected, template.render(self._GetContext({})))
Exemple #4
0
 def testCallTemplateRestoreVar(self):
     """Make sure variable stacking happens correctly on call_template."""
     source = 'abc {% call_template _call_test foo bar qux api.xxx %} {{foo}}'
     template = django_helpers.DjangoTemplate(source)
     rendered = template.render(
         self._GetContext({
             'template_dir': self._TEST_DATA_DIR,
             'api': {
                 'xxx': 'yyy'
             },
             'bar': 'baz',
             'foo': 'OrigFoo'
         }))
     self.assertEquals('abc 1baz1 2yyy2 3yyy3 OrigFoo', rendered)
Exemple #5
0
 def Test(language, x):
     ctxt = self._GetContext({
         '_LANGUAGE': language,
         'x': x,
         'y': not x
     })
     template = django_helpers.DjangoTemplate(source)
     key = template_helpers._BOOLEAN_LITERALS
     vals = template_helpers._language_defaults[language].get(
         key, template_helpers._defaults[key])
     if x:
         # If x, true precedes false in the output.
         vals = vals[::-1]
     expected = '|'.join(vals)
     self.assertEquals(expected, template.render(ctxt))
Exemple #6
0
    def testGetArgFromToken(self):
        # This tests indirectly by going through a few tags known to call
        # _GetArgFromToken. That expedient avoids having to create a token stream
        # at a low level.

        # try a good one
        template = django_helpers.DjangoTemplate('{% camel_case foo %}')
        context = self._GetContext({'foo': 'hello_world'})
        self.assertEquals('HelloWorld', template.render(context))

        # Missing the arg
        for tag in ['language', 'comment_if', 'doc_comment_if']:
            self.assertRaises(django_template.TemplateSyntaxError,
                              django_helpers.DjangoTemplate,
                              '{%% %s %%}' % tag)
Exemple #7
0
    def testWrite(self):
        self.name_to_content = {}

        def MyWriter(name, content):
            """Capture the write event."""
            self.name_to_content[name] = content

        template = django_helpers.DjangoTemplate(
            'a{% write file1 %}foo{% endwrite %}'
            'b{% write file2 %}bar{% endwrite %}')
        context = self._GetContext({
            template_helpers.FILE_WRITER: MyWriter,
            'file1': 'x',
            'file2': 'y',
        })
        self.assertEquals('ab', template.render(context))
        self.assertEquals('foo', self.name_to_content['x'])
        self.assertEquals('bar', self.name_to_content['y'])
Exemple #8
0
 def testCopyright(self):
     copyright_text = 'MY COPYRIGHT TEXT'
     expected_license_preamble = 'Licensed under the Apache License'
     template = django_helpers.DjangoTemplate(
         '{% language java %}{% copyright_block %}')
     context = self._GetContext({
         'template_dir': self._TEST_DATA_DIR,
         'api': {},
     })
     text_without_copyright = template.render(context)
     license_pos = text_without_copyright.find(expected_license_preamble)
     self.assertLess(3, license_pos)
     self.assertEquals(-1, text_without_copyright.find(copyright_text))
     context['api']['copyright'] = copyright_text
     text_with_copyright = template.render(context)
     license_pos_with_copyright = text_with_copyright.find(
         expected_license_preamble)
     self.assertLess(license_pos, license_pos_with_copyright)
     copyright_pos = text_with_copyright.find(copyright_text)
     self.assertEquals(license_pos, copyright_pos)
Exemple #9
0
 def TryIt(source, expected):
     ctxt = self._GetContext({'template_dir': self._TEST_DATA_DIR})
     template = django_helpers.DjangoTemplate(source)
     gotten = template.render(ctxt)
     self.assertEquals(expected, gotten)
Exemple #10
0
 def TryIt(source, expected, ctxt=None):
     template = django_helpers.DjangoTemplate(source)
     rendered = template.render(self._GetContext(ctxt))
     self.assertEquals(expected, rendered)