Exemplo n.º 1
0
 def test_010_041_DottedNameCallable(self):
     """Context variable is a object with a callable name in template"""
     template = 'My name is {{person.__len__}}'
     renderer = templatelite.Renderer(template_str=template,
                                      default='!!Error!!')
     result = renderer.from_context({'person': 'Tony'})
     self.assertEqual(result, 'My name is 4')
Exemplo n.º 2
0
 def test_030_035_expression_substitution_key(self):
     """Valid template - with compiled names as dictionary keys """
     template = """{% if data[key1] == data[key2] %}
     The same
     {% else %}
     Different
     {% endif %}"""
     renderer = templatelite.Renderer(template_str=template)
     self.assertEqual(
         renderer.from_context({
             'data': {
                 1: 1,
                 2: 2,
                 3: 3,
                 4: 3,
                 5: 4
             },
             'key1': 3,
             'key2': 4
         }), 'The same\n')
     self.assertEqual(
         renderer.from_context({
             'data': {
                 1: 1,
                 2: 2,
                 3: 3,
                 4: 3,
                 5: 4
             },
             'key1': 1,
             'key2': 5
         }), 'Different\n')
Exemplo n.º 3
0
 def test_030_000_invalid_if_missing_expression(self):
     """Invalid if statement - missing an expression"""
     template = """{% if %}"""
     with six.assertRaisesRegex(
             self, templatelite.TemplateSyntaxError,
             r"Syntax Error : Invalid if statement \'\{% if %\}\'"):
         renderer = templatelite.Renderer(template_str=template)
Exemplo n.º 4
0
 def test_040_000_invalid_for_loop_missing_itervar(self):
     """Invalid for loop - missing targets and iterable"""
     template = '{% for %}'
     with six.assertRaisesRegex(
             self, templatelite.TemplateSyntaxError,
             r"Syntax Error : Invalid for statement \'\{% for %\}\'"):
         renderer = templatelite.Renderer(template_str=template)
Exemplo n.º 5
0
 def test_030_001_invalid_if_missing_endif(self):
     """Invalid template missing endif"""
     template = """{% if True %}"""
     with six.assertRaisesRegex(
             self, templatelite.TemplateSyntaxError,
             r"Syntax Error : Missing directive \'{% endif %}\'"):
         renderer = templatelite.Renderer(template_str=template)
Exemplo n.º 6
0
 def test_040_004_invalid_for_loop_missing_end_for(self):
     """Invalid for loop - missing endfor"""
     template = '{% for z, plip in dummy %}'
     with six.assertRaisesRegex(
             self, templatelite.TemplateSyntaxError,
             r"Syntax Error : Missing directive \'{% endfor %}\'"):
         renderer = templatelite.Renderer(template_str=template)
Exemplo n.º 7
0
 def test_040_003_invalid_for_loop_invalid_multple_target(self):
     """Invalid for loop - one invalid target"""
     template = '{% for z, plip.x in dummy%}'
     with six.assertRaisesRegex(
             self, templatelite.TemplateSyntaxError,
             r"Syntax Error : Invalid target in for loop \'plip.x\'"):
         renderer = templatelite.Renderer(template_str=template)
Exemplo n.º 8
0
 def test_020_001_unknown_filter(self):
     """Test that an unknown filter raises an error"""
     template = 'My name is {{person.name|blah}}'
     with six.assertRaisesRegex(self, templatelite.UnrecognisedFilter,
                                r'Unknown filter \'blah\''):
         renderer = templatelite.Renderer(template_str=template,
                                          errors=True)
Exemplo n.º 9
0
 def test_010_033_MissingContextValueError(self):
     """Missing context data - error raised"""
     template = 'My name is {{name}}'
     renderer = templatelite.Renderer(template_str=template, errors=True)
     with six.assertRaisesRegex(self, templatelite.UnknownContextValue,
                                r'Unknown context variable \'name\''):
         renderer.from_context({'me': 'Tony Flury'})
Exemplo n.º 10
0
 def test_030_015_if_on_one_line(self):
     """Valid template - if elif else endif"""
     template = "{% if dummy==1 %} Hello {% elif dummy==2 %} Goodbye {% else %} Au Revoir {% endif %}"
     renderer = templatelite.Renderer(template_str=template)
     self.assertEqual(renderer.from_context({'dummy': 1}), 'Hello')
     self.assertEqual(renderer.from_context({'dummy': 2}), 'Goodbye')
     self.assertEqual(renderer.from_context({'dummy': 3}), 'Au Revoir')
Exemplo n.º 11
0
 def test_010_034_MissingContextValueDefault(self):
     """Missing context data - error raised"""
     template = 'My name is {{name}}'
     renderer = templatelite.Renderer(template_str=template,
                                      default='!!Error!!')
     result = renderer.from_context({'me': 'Tony Flury'})
     self.assertEqual(result, 'My name is !!Error!!')
Exemplo n.º 12
0
 def test_030_005_invalid_endif(self):
     """Invalid template - endif without if statement"""
     template = """{% endif %}"""
     with six.assertRaisesRegex(
             self, templatelite.TemplateSyntaxError,
             r"Syntax Error : Unexpected directive - found \'{% endif %}\' outside an \'{% if %}\' block"
     ):
         renderer = templatelite.Renderer(template_str=template)
Exemplo n.º 13
0
 def test_030_002_invalid_lone_else(self):
     """Invalid template - else without and if or for"""
     template = """{% else %}"""
     with six.assertRaisesRegex(
             self, templatelite.TemplateSyntaxError,
             r"Syntax Error : Unexpected directive - found \'{% else %}\' outside {% if %} or {% for %} block"
     ):
         renderer = templatelite.Renderer(template_str=template)
Exemplo n.º 14
0
 def test_020_012_split_filter_error(self):
     """Test the capitalise filter with default"""
     template = 'My name is {{v|split e b}}'
     renderer = templatelite.Renderer(template_str=template, errors=True)
     with six.assertRaisesRegex(
             self, templatelite.UnexpectedFilterArguments,
             r"Unexpected filter arguments in \'{{v|split e b}}\'"):
         renderer.from_context({'v': 'Hello'})
Exemplo n.º 15
0
 def test_020_011_split_filter_argument(self):
     """Test the split filter with argument"""
     template = 'My name is {{v|split e}}'
     renderer = templatelite.Renderer(template_str=template, errors=True)
     result = renderer.from_context({'v': '1e2e3e4e5e6e7e8e9e0'})
     self.assertEqual(
         result,
         "My name is ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']")
Exemplo n.º 16
0
 def test_020_002e_length_filter_with_args(self):
     """Test the Length filter with arguments (in error)"""
     template = 'My name is {{person.name|len 193}}'
     renderer = templatelite.Renderer(template_str=template, errors=True)
     with six.assertRaisesRegex(
             self, templatelite.UnexpectedFilterArguments,
             r"Unexpected filter arguments in '{{person.name|len 193}}'"):
         result = renderer.from_context({'person': {'name': 'Tony'}})
Exemplo n.º 17
0
 def test_030_010_valid_if_endif(self):
     """Valid template - if with endif"""
     template = """{% if dummy %}
     Hello
     {% endif %}"""
     renderer = templatelite.Renderer(template_str=template)
     result = renderer.from_context({'dummy': False})
     self.assertEqual(result, '')
Exemplo n.º 18
0
 def test_040_006_endfor_outside_loop(self):
     """Invalid for loop - endfor outside a loop"""
     template = '{% endfor %}'
     with six.assertRaisesRegex(
             self, templatelite.TemplateSyntaxError,
             r"Syntax Error : Unexpected directive - found \'{% endfor %}\' outside \'{% for %}\' block"
     ):
         renderer = templatelite.Renderer(template_str=template)
Exemplo n.º 19
0
 def test_100_000_invalid_directive(self):
     """Invalid Template - unknown directive token"""
     template = """{% frooble %}"""
     with six.assertRaisesRegex(
             self, templatelite.TemplateSyntaxError,
             r'Syntax Error : Unexpected directive \'{% frooble %}\' found'
     ):
         renderer = templatelite.Renderer(template_str=template)
Exemplo n.º 20
0
    def test_040_040_for_loop_variable_error(self):
        """For looop - error fetching iterator"""
        template = """{% for n in doesnt_exist %}
inside loop
            {% endfor %}"""
        renderer = templatelite.Renderer(template_str=template,
                                         remove_indentation=True)
        self.assertEqual(renderer.from_context({}).strip(), '')
Exemplo n.º 21
0
    def test_010_041_DottedNameAttribute(self):
        """Context variable is an object with a dotted attribute name in template"""
        c = type('person', (object, ), {'name': 'Tony', 'age': 53})()

        template = 'My name is {{person.name}}'
        renderer = templatelite.Renderer(template_str=template,
                                         default='!!Error!!')
        result = renderer.from_context({'person': c})
        self.assertEqual(result, 'My name is Tony')
Exemplo n.º 22
0
 def test_040_045_for_loop_variable_singleline(self):
     """For loop - for loop on a single line"""
     template = "{% for n in l %} {{ n }} {% endfor %}"
     renderer = templatelite.Renderer(template_str=template,
                                      remove_indentation=True)
     self.assertEqual(
         renderer.from_context({
             'l': [0, 1, 2, 3, 4, 5, 6]
         }).strip(), '0123456')
Exemplo n.º 23
0
    def test_040_010_syntax_check_for_loop(self):
        """Prove that for loop completes syntax check"""
        template =\
"""{% for z, plip in dummy%}
    block
{% endfor %}
outer block
"""
        renderer = templatelite.Renderer(template_str=template)
Exemplo n.º 24
0
 def test_030_004_invalid_lone_elif(self):
     """Invalid template - elif without a starting if"""
     template = """{% elif True %}
     {% endif %}"""
     with six.assertRaisesRegex(
             self, templatelite.TemplateSyntaxError,
             r"Syntax Error : Unexpected directive - found \'{% elif %}\' outside an \'{% if %}\' block"
     ):
         renderer = templatelite.Renderer(template_str=template)
Exemplo n.º 25
0
 def test_030_005_invalid_elif(self):
     """Invalid template - elif without an expression"""
     template = """{% if True %}
     {% elif %}
     {% endif %}"""
     with six.assertRaisesRegex(
             self, templatelite.TemplateSyntaxError,
             r"Syntax Error : Invalid elif statement \'{% elif %}\'"):
         renderer = templatelite.Renderer(template_str=template)
Exemplo n.º 26
0
 def test_010_051_DottedNameDictionaryMissingKeyNoDefault(self):
     """Context variable is a dictionary with a with a missing key replace by token"""
     template = 'My name is {{person.name}}'
     renderer = templatelite.Renderer(template_str=template)
     result = renderer.from_context(
         {'person': {
             'full-name': 'Tony Flury',
             'age': 53
         }})
     self.assertEqual(result, 'My name is {{person.name}}')
Exemplo n.º 27
0
 def test_040_031_for_loop_indentation_intact(self):
     """For looop - filter used on loop target variable"""
     template = """{% for n in dummy%}
 {{ n }}
         {% endfor %}"""
     renderer = templatelite.Renderer(template_str=template,
                                      remove_indentation=False)
     self.assertEqual(
         renderer.from_context({'dummy': ['0', '1', '2', '3', '4']}),
         '    0\n    1\n    2\n    3\n    4\n')
Exemplo n.º 28
0
    def test_000_004_comments_embedded_in_text_only(self):
        """Text with embedded comments"""
        template =\
"""This is text {# with an embedded comment #}
and this is mo{# no whitespace inserted #}re text
{#Final comment #}This is it."""
        renderer = templatelite.Renderer(template)
        self.assertEqual(renderer.from_context(),
                         """This is text and this is more text
This is it.""")
Exemplo n.º 29
0
 def test_020_002_length_filter(self):
     """Test the Length filter"""
     template = 'My name is {{person.name|len}}'
     renderer = templatelite.Renderer(template_str=template, errors=True)
     result = renderer.from_context(
         {'person': {
             'name': 'Tony Flury',
             'age': 53
         }})
     self.assertEqual(result, 'My name is 10')
Exemplo n.º 30
0
 def test_020_010_split_filter(self):
     """Test the split filter with default"""
     template = 'My name is {{person.name|split}}'
     renderer = templatelite.Renderer(template_str=template, errors=True)
     result = renderer.from_context(
         {'person': {
             'name': 'tony flury',
             'age': 53
         }})
     self.assertEqual(result, 'My name is [\'tony\', \'flury\']')