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')
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')
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)
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)
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)
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)
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)
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)
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'})
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')
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!!')
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)
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)
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'})
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']")
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'}})
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, '')
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)
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)
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(), '')
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')
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')
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)
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)
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)
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}}')
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')
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.""")
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')
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\']')