Beispiel #1
0
 def test_cannot_define_macro_to_override_reserved_statements(self):
     for reserved in ('if', 'else', 'elseif', 'set', 'macro', 'foreach',
                      'parse', 'include', 'stop', 'end'):
         template = airspeed.Template('#macro ( %s $value) $value #end' %
                                      reserved)
         self.assertRaises(airspeed.TemplateSyntaxError, template.merge, {})
Beispiel #2
0
 def test_can_loop_over_numeric_ranges_backwards(self):
     template = airspeed.Template('#foreach( $v in [5..-2] )$v,#end')
     self.assertEquals('5,4,3,2,1,0,-1,-2,', template.merge({}))
Beispiel #3
0
 def test_stop_directive(self):
     template = airspeed.Template("hello #stop world")
     self.assertEquals('hello ', template.merge({}))
Beispiel #4
0
 def load_template(self, name):
     if name == 'foo.tmpl':
         return airspeed.Template('#macro(themacro)works#end')
Beispiel #5
0
 def test_can_assign_empty_string(self):
     template = airspeed.Template('#set( $v = "" )#set( $y = \'\' ).$v.$y.')
     self.assertEquals('...', template.merge({}))
Beispiel #6
0
 def test_large_areas_of_text_handled_without_error(self):
     text = "qwerty uiop asdfgh jkl zxcvbnm. 1234" * 300
     template = airspeed.Template(text)
     self.assertEquals(text, template.merge({}))
Beispiel #7
0
 def test_foreach_with_non_iterable_variable_raises_error(self):
     template = airspeed.Template('#foreach($value in $values)foo#end')
     self.assertRaises(ValueError, template.merge, {'values': 1})
Beispiel #8
0
 def test_parse_directive_gives_error_if_no_loader_provided(self):
     template = airspeed.Template('#parse ("foo.tmpl")')
     self.assertRaises(airspeed.TemplateError, template.merge, {})
Beispiel #9
0
 def load_template(self, name):
     if name == 'foo.tmpl':
         return airspeed.Template("$message")
Beispiel #10
0
 def test_use_of_macro_name_is_case_insensitive(self):
     template = airspeed.Template(
         '#macro ( bold $value)<strong>$value</strong>#end#BoLd ($text)')
     self.assertEquals('<strong>hello</strong>',
                       template.merge({'text': 'hello'}))
Beispiel #11
0
 def test_cannot_redefine_macro(self):
     template = airspeed.Template(
         '#macro ( hello)hi#end#macro(hello)again#end')
     self.assertRaises(Exception, template.merge,
                       {})  ## Should this be TemplateSyntaxError?
Beispiel #12
0
 def test_define_and_use_macro_with_one_parameter(self):
     template = airspeed.Template(
         '#macro ( bold $value)<strong>$value</strong>#end#bold ($text)')
     self.assertEquals('<strong>hello</strong>',
                       template.merge({'text': 'hello'}))
Beispiel #13
0
 def test_define_and_use_macro_with_no_parameters(self):
     template = airspeed.Template('#macro ( hello)hi#end#hello ()#hello()')
     self.assertEquals('hihi', template.merge({'text': 'hello'}))
Beispiel #14
0
 def test_cannot_call_undefined_macro(self):
     template = airspeed.Template('#undefined()')
     self.assertRaises(Exception, template.merge, {})
Beispiel #15
0
 def test_macros_expanded_in_double_quoted_strings(self):
     template = airspeed.Template(
         '#macro(hi $person)$person says hello#end#set($hello="#hi($name)")$hello'
     )
     self.assertEquals("Steve says hello", template.merge({'name':
                                                           'Steve'}))
Beispiel #16
0
 def test_local_namespace_methods_are_not_available_in_context(self):
     template = airspeed.Template('#macro(tryme)$values#end#tryme()')
     self.assertEquals('$values', template.merge({}))
Beispiel #17
0
 def test_color_spec(self):
     template = airspeed.Template('<span style="color: #13ff93">')
     self.assertEquals('<span style="color: #13ff93">', template.merge({}))
Beispiel #18
0
 def test_array_literal(self):
     template = airspeed.Template(
         'blah\n#set($valuesInList = ["Hello ", $person, ", your lucky number is ", 7])\n#foreach($value in $valuesInList)$value#end\n\nblah'
     )
     self.assertEquals('blah\nHello Chris, your lucky number is 7\nblah',
                       template.merge({'person': 'Chris'}))
Beispiel #19
0
 def test_foreach_with_unset_variable_expands_to_nothing(self):
     template = airspeed.Template('#foreach($value in $values)foo#end')
     self.assertEquals('', template.merge({}))
Beispiel #20
0
 def test_dictionary_literal(self):
     template = airspeed.Template(
         '#set($a = {"dog": "cat" , "horse":15})$a.dog')
     self.assertEquals('cat', template.merge({}))
     template = airspeed.Template('#set($a = {"dog": "$horse"})$a.dog')
     self.assertEquals('cow', template.merge({'horse': 'cow'}))
Beispiel #21
0
 def test_preserves_unicode_strings(self):
     template = airspeed.Template('$value')
     value = unicode('Grüße', 'latin1')
     self.assertEquals(value, template.merge(locals()))
Beispiel #22
0
 def test_dictionary_literal_as_parameter(self):
     template = airspeed.Template('$a({"color":"blue"})')
     ns = {'a': lambda x: x['color'] + ' food'}
     self.assertEquals('blue food', template.merge(ns))
Beispiel #23
0
 def test_modulus_operator(self):
     template = airspeed.Template('#set( $modulus = ($value % 2) )$modulus')
     self.assertEquals('1', template.merge({'value': 3}))
Beispiel #24
0
 def test_nested_array_literals(self):
     template = airspeed.Template(
         '#set($values = [["Hello ", "Steve"], ["Hello", " Chris"]])#foreach($pair in $values)#foreach($word in $pair)$word#end. #end'
     )
     self.assertEquals('Hello Steve. Hello Chris. ', template.merge({}))
Beispiel #25
0
 def test_can_loop_over_numeric_ranges(self):
     ## Test for bug #15
     template = airspeed.Template('#foreach( $v in [1..5] )$v\n#end')
     self.assertEquals('1\n2\n3\n4\n5\n', template.merge({}))
Beispiel #26
0
 def test_when_dictionary_does_not_contain_referenced_attribute_no_substitution_occurs(
         self):
     template = airspeed.Template(" $user.name ")
     self.assertEquals(" $user.name ", template.merge({'user': self}))
Beispiel #27
0
 def test_ranges_over_references(self):
     template = airspeed.Template(
         "#set($start = 1)#set($end = 5)#foreach($i in [$start .. $end])$i-#end"
     )
     self.assertEquals('1-2-3-4-5-', template.merge({}))
Beispiel #28
0
 def test_escaped_variable_references_not_expanded_in_double_quoted_strings(
         self):
     template = airspeed.Template(
         '#set($hello="hello, \\$name is my name")$hello')
     self.assertEquals("hello, $name is my name",
                       template.merge({'name': 'Steve'}))
Beispiel #29
0
 def test_assignment_of_parenthesized_math_expression_with_reference(self):
     template = airspeed.Template('#set($b = 5)#set($a = ($b + 4))$a')
     self.assertEquals('9', template.merge({}))
Beispiel #30
0
 def test_logical_negation_operator_yields_true_for_None(self):
     template = airspeed.Template('#if ( !$value )yes#end')
     self.assertEquals('yes', template.merge({'value': None}))