Back to index

python3.2  3.2.2
Public Member Functions
test.test_pep292.TestTemplate Class Reference

List of all members.

Public Member Functions

def test_regular_templates
def test_regular_templates_with_braces
def test_escapes
def test_invalid
def test_percents
def test_stringification
def test_tupleargs
def test_SafeTemplate
def test_invalid_placeholders
def test_idpattern_override
def test_pattern_override
def test_braced_override
def test_braced_override_safe
def test_unicode_values
def test_keyword_arguments
def test_keyword_arguments_safe
def test_delimiter_override

Detailed Description

Definition at line 23 of file test_pep292.py.


Member Function Documentation

Definition at line 141 of file test_pep292.py.

00141 
00142     def test_braced_override(self):
00143         class MyTemplate(Template):
00144             pattern = r"""
00145             \$(?:
00146               (?P<escaped>$)                     |
00147               (?P<named>[_a-z][_a-z0-9]*)        |
00148               @@(?P<braced>[_a-z][_a-z0-9]*)@@   |
00149               (?P<invalid>)                      |
00150            )
00151            """
00152 
00153         tmpl = 'PyCon in $@@location@@'
00154         t = MyTemplate(tmpl)
00155         self.assertRaises(KeyError, t.substitute, {})
00156         val = t.substitute({'location': 'Cleveland'})
00157         self.assertEqual(val, 'PyCon in Cleveland')

Here is the call graph for this function:

Definition at line 158 of file test_pep292.py.

00158 
00159     def test_braced_override_safe(self):
00160         class MyTemplate(Template):
00161             pattern = r"""
00162             \$(?:
00163               (?P<escaped>$)                     |
00164               (?P<named>[_a-z][_a-z0-9]*)        |
00165               @@(?P<braced>[_a-z][_a-z0-9]*)@@   |
00166               (?P<invalid>)                      |
00167            )
00168            """
00169 
00170         tmpl = 'PyCon in $@@location@@'
00171         t = MyTemplate(tmpl)
00172         self.assertEqual(t.safe_substitute(), tmpl)
00173         val = t.safe_substitute({'location': 'Cleveland'})
00174         self.assertEqual(val, 'PyCon in Cleveland')

Here is the call graph for this function:

Definition at line 212 of file test_pep292.py.

00212 
00213     def test_delimiter_override(self):
00214         eq = self.assertEqual
00215         raises = self.assertRaises
00216         class AmpersandTemplate(Template):
00217             delimiter = '&'
00218         s = AmpersandTemplate('this &gift is for &{who} &&')
00219         eq(s.substitute(gift='bud', who='you'), 'this bud is for you &')
00220         raises(KeyError, s.substitute)
00221         eq(s.safe_substitute(gift='bud', who='you'), 'this bud is for you &')
00222         eq(s.safe_substitute(), 'this &gift is for &{who} &')
00223         s = AmpersandTemplate('this &gift is for &{who} &')
00224         raises(ValueError, s.substitute, dict(gift='bud', who='you'))
00225         eq(s.safe_substitute(), 'this &gift is for &{who} &')
00226 
00227         class PieDelims(Template):
00228             delimiter = '@'
00229         s = PieDelims('@who likes to eat a bag of @{what} worth $100')
00230         self.assertEqual(s.substitute(dict(who='tim', what='ham')),
00231                          'tim likes to eat a bag of ham worth $100')
00232 

Here is the call graph for this function:

Definition at line 37 of file test_pep292.py.

00037 
00038     def test_escapes(self):
00039         eq = self.assertEqual
00040         s = Template('$who likes to eat a bag of $$what worth $$100')
00041         eq(s.substitute(dict(who='tim', what='ham')),
00042            'tim likes to eat a bag of $what worth $100')
00043         s = Template('$who likes $$')
00044         eq(s.substitute(dict(who='tim', what='ham')), 'tim likes $')

Here is the call graph for this function:

Definition at line 102 of file test_pep292.py.

00102 
00103     def test_idpattern_override(self):
00104         class PathPattern(Template):
00105             idpattern = r'[_a-z][._a-z0-9]*'
00106         m = Mapping()
00107         m.bag = Bag()
00108         m.bag.foo = Bag()
00109         m.bag.foo.who = 'tim'
00110         m.bag.what = 'ham'
00111         s = PathPattern('$bag.foo.who likes to eat a bag of $bag.what')
00112         self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham')

Here is the call graph for this function:

Definition at line 45 of file test_pep292.py.

00045 
00046     def test_invalid(self):
00047         class MyPattern(Template):
00048             pattern = r"""
00049             (?:
00050             (?P<invalid>)            |
00051             (?P<escaped>%(delim)s)   |
00052             @(?P<named>%(id)s)       |
00053             @{(?P<braced>%(id)s)}    
00054             )
00055             """
00056         s = MyPattern('$')
00057         self.assertRaises(ValueError, s.substitute, dict()) 

Here is the call graph for this function:

Definition at line 93 of file test_pep292.py.

00093 
00094     def test_invalid_placeholders(self):
00095         raises = self.assertRaises
00096         s = Template('$who likes $')
00097         raises(ValueError, s.substitute, dict(who='tim'))
00098         s = Template('$who likes ${what)')
00099         raises(ValueError, s.substitute, dict(who='tim'))
00100         s = Template('$who likes $100')
00101         raises(ValueError, s.substitute, dict(who='tim'))

Here is the call graph for this function:

Definition at line 180 of file test_pep292.py.

00180 
00181     def test_keyword_arguments(self):
00182         eq = self.assertEqual
00183         s = Template('$who likes $what')
00184         eq(s.substitute(who='tim', what='ham'), 'tim likes ham')
00185         eq(s.substitute(dict(who='tim'), what='ham'), 'tim likes ham')
00186         eq(s.substitute(dict(who='fred', what='kung pao'),
00187                         who='tim', what='ham'),
00188            'tim likes ham')
00189         s = Template('the mapping is $mapping')
00190         eq(s.substitute(dict(foo='none'), mapping='bozo'),
00191            'the mapping is bozo')
00192         eq(s.substitute(dict(mapping='one'), mapping='two'),
00193            'the mapping is two')

Here is the call graph for this function:

Definition at line 194 of file test_pep292.py.

00194 
00195     def test_keyword_arguments_safe(self):
00196         eq = self.assertEqual
00197         raises = self.assertRaises
00198         s = Template('$who likes $what')
00199         eq(s.safe_substitute(who='tim', what='ham'), 'tim likes ham')
00200         eq(s.safe_substitute(dict(who='tim'), what='ham'), 'tim likes ham')
00201         eq(s.safe_substitute(dict(who='fred', what='kung pao'),
00202                         who='tim', what='ham'),
00203            'tim likes ham')
00204         s = Template('the mapping is $mapping')
00205         eq(s.safe_substitute(dict(foo='none'), mapping='bozo'),
00206            'the mapping is bozo')
00207         eq(s.safe_substitute(dict(mapping='one'), mapping='two'),
00208            'the mapping is two')
00209         d = dict(mapping='one')
00210         raises(TypeError, s.substitute, d, {})
00211         raises(TypeError, s.safe_substitute, d, {})

Here is the call graph for this function:

Definition at line 113 of file test_pep292.py.

00113 
00114     def test_pattern_override(self):
00115         class MyPattern(Template):
00116             pattern = r"""
00117             (?P<escaped>@{2})                   |
00118             @(?P<named>[_a-z][._a-z0-9]*)       |
00119             @{(?P<braced>[_a-z][._a-z0-9]*)}    |
00120             (?P<invalid>@)
00121             """
00122         m = Mapping()
00123         m.bag = Bag()
00124         m.bag.foo = Bag()
00125         m.bag.foo.who = 'tim'
00126         m.bag.what = 'ham'
00127         s = MyPattern('@bag.foo.who likes to eat a bag of @bag.what')
00128         self.assertEqual(s.substitute(m), 'tim likes to eat a bag of ham')
00129 
00130         class BadPattern(Template):
00131             pattern = r"""
00132             (?P<badname>.*)                     |
00133             (?P<escaped>@{2})                   |
00134             @(?P<named>[_a-z][._a-z0-9]*)       |
00135             @{(?P<braced>[_a-z][._a-z0-9]*)}    |
00136             (?P<invalid>@)                      |
00137             """
00138         s = BadPattern('@bag.foo.who likes to eat a bag of @bag.what')
00139         self.assertRaises(ValueError, s.substitute, {})
00140         self.assertRaises(ValueError, s.safe_substitute, {})

Here is the call graph for this function:

Definition at line 58 of file test_pep292.py.

00058 
00059     def test_percents(self):
00060         eq = self.assertEqual
00061         s = Template('%(foo)s $foo ${foo}')
00062         d = dict(foo='baz')
00063         eq(s.substitute(d), '%(foo)s baz baz')
00064         eq(s.safe_substitute(d), '%(foo)s baz baz')

Here is the call graph for this function:

Definition at line 24 of file test_pep292.py.

00024 
00025     def test_regular_templates(self):
00026         s = Template('$who likes to eat a bag of $what worth $$100')
00027         self.assertEqual(s.substitute(dict(who='tim', what='ham')),
00028                          'tim likes to eat a bag of ham worth $100')
00029         self.assertRaises(KeyError, s.substitute, dict(who='tim'))

Here is the call graph for this function:

Definition at line 30 of file test_pep292.py.

00030 
00031     def test_regular_templates_with_braces(self):
00032         s = Template('$who likes ${what} for ${meal}')
00033         d = dict(who='tim', what='ham', meal='dinner')
00034         self.assertEqual(s.substitute(d), 'tim likes ham for dinner')
00035         self.assertRaises(KeyError, s.substitute,
00036                           dict(who='tim', what='ham'))

Here is the call graph for this function:

Definition at line 81 of file test_pep292.py.

00081 
00082     def test_SafeTemplate(self):
00083         eq = self.assertEqual
00084         s = Template('$who likes ${what} for ${meal}')
00085         eq(s.safe_substitute(dict(who='tim')), 'tim likes ${what} for ${meal}')
00086         eq(s.safe_substitute(dict(what='ham')), '$who likes ham for ${meal}')
00087         eq(s.safe_substitute(dict(what='ham', meal='dinner')),
00088            '$who likes ham for dinner')
00089         eq(s.safe_substitute(dict(who='tim', what='ham')),
00090            'tim likes ham for ${meal}')
00091         eq(s.safe_substitute(dict(who='tim', what='ham', meal='dinner')),
00092            'tim likes ham for dinner')

Here is the call graph for this function:

Definition at line 65 of file test_pep292.py.

00065 
00066     def test_stringification(self):
00067         eq = self.assertEqual
00068         s = Template('tim has eaten $count bags of ham today')
00069         d = dict(count=7)
00070         eq(s.substitute(d), 'tim has eaten 7 bags of ham today')
00071         eq(s.safe_substitute(d), 'tim has eaten 7 bags of ham today')
00072         s = Template('tim has eaten ${count} bags of ham today')
00073         eq(s.substitute(d), 'tim has eaten 7 bags of ham today')

Here is the call graph for this function:

Definition at line 74 of file test_pep292.py.

00074 
00075     def test_tupleargs(self):
00076         eq = self.assertEqual
00077         s = Template('$who ate ${meal}')
00078         d = dict(who=('tim', 'fred'), meal=('ham', 'kung pao'))
00079         eq(s.substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')")
00080         eq(s.safe_substitute(d), "('tim', 'fred') ate ('ham', 'kung pao')")

Here is the call graph for this function:

Definition at line 175 of file test_pep292.py.

00175 
00176     def test_unicode_values(self):
00177         s = Template('$who likes $what')
00178         d = dict(who='t\xffm', what='f\xfe\fed')
00179         self.assertEqual(s.substitute(d), 't\xffm likes f\xfe\x0ced')

Here is the call graph for this function:


The documentation for this class was generated from the following file: