/usr/share/pyshared/genshi/template/tests/interpolation.py is in python-genshi 0.7-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 | # -*- coding: utf-8 -*-
#
# Copyright (C) 2007-2008 Edgewall Software
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution. The terms
# are also available at http://genshi.edgewall.org/wiki/License.
#
# This software consists of voluntary contributions made by many
# individuals. For the exact contribution history, see the revision
# history and logs, available at http://genshi.edgewall.org/log/.
import doctest
import sys
import unittest
from genshi.core import TEXT
from genshi.template.base import TemplateSyntaxError, EXPR
from genshi.template.interpolation import interpolate
class InterpolateTestCase(unittest.TestCase):
def test_interpolate_string(self):
parts = list(interpolate('bla'))
self.assertEqual(1, len(parts))
self.assertEqual(TEXT, parts[0][0])
self.assertEqual('bla', parts[0][1])
def test_interpolate_simple(self):
parts = list(interpolate('${bla}'))
self.assertEqual(1, len(parts))
self.assertEqual(EXPR, parts[0][0])
self.assertEqual('bla', parts[0][1].source)
def test_interpolate_escaped(self):
parts = list(interpolate('$${bla}'))
self.assertEqual(1, len(parts))
self.assertEqual(TEXT, parts[0][0])
self.assertEqual('${bla}', parts[0][1])
def test_interpolate_dobuleescaped(self):
parts = list(interpolate('$$${bla}'))
self.assertEqual(2, len(parts))
self.assertEqual(TEXT, parts[0][0])
self.assertEqual('$', parts[0][1])
self.assertEqual(EXPR, parts[1][0])
self.assertEqual('bla', parts[1][1].source)
def test_interpolate_short(self):
parts = list(interpolate('$bla'))
self.assertEqual(1, len(parts))
self.assertEqual(EXPR, parts[0][0])
self.assertEqual('bla', parts[0][1].source)
def test_interpolate_short_escaped(self):
parts = list(interpolate('$$bla'))
self.assertEqual(1, len(parts))
self.assertEqual(TEXT, parts[0][0])
self.assertEqual('$bla', parts[0][1])
def test_interpolate_short_escaped_2(self):
parts = list(interpolate('my $$bla = 2'))
self.assertEqual(1, len(parts))
self.assertEqual(TEXT, parts[0][0])
self.assertEqual('my $bla = 2', parts[0][1])
def test_interpolate_short_doubleescaped(self):
parts = list(interpolate('$$$bla'))
self.assertEqual(2, len(parts))
self.assertEqual(TEXT, parts[0][0])
self.assertEqual('$', parts[0][1])
self.assertEqual(EXPR, parts[1][0])
self.assertEqual('bla', parts[1][1].source)
def test_interpolate_short_starting_with_underscore(self):
parts = list(interpolate('$_bla'))
self.assertEqual(1, len(parts))
self.assertEqual(EXPR, parts[0][0])
self.assertEqual('_bla', parts[0][1].source)
def test_interpolate_short_containing_underscore(self):
parts = list(interpolate('$foo_bar'))
self.assertEqual(1, len(parts))
self.assertEqual(EXPR, parts[0][0])
self.assertEqual('foo_bar', parts[0][1].source)
def test_interpolate_short_starting_with_dot(self):
parts = list(interpolate('$.bla'))
self.assertEqual(1, len(parts))
self.assertEqual(TEXT, parts[0][0])
self.assertEqual('$.bla', parts[0][1])
def test_interpolate_short_containing_dot(self):
parts = list(interpolate('$foo.bar'))
self.assertEqual(1, len(parts))
self.assertEqual(EXPR, parts[0][0])
self.assertEqual('foo.bar', parts[0][1].source)
def test_interpolate_short_starting_with_digit(self):
parts = list(interpolate('$0bla'))
self.assertEqual(1, len(parts))
self.assertEqual(TEXT, parts[0][0])
self.assertEqual('$0bla', parts[0][1])
def test_interpolate_short_containing_digit(self):
parts = list(interpolate('$foo0'))
self.assertEqual(1, len(parts))
self.assertEqual(EXPR, parts[0][0])
self.assertEqual('foo0', parts[0][1].source)
def test_interpolate_short_starting_with_digit(self):
parts = list(interpolate('$0bla'))
self.assertEqual(1, len(parts))
self.assertEqual(TEXT, parts[0][0])
self.assertEqual('$0bla', parts[0][1])
def test_interpolate_short_containing_digit(self):
parts = list(interpolate('$foo0'))
self.assertEqual(1, len(parts))
self.assertEqual(EXPR, parts[0][0])
self.assertEqual('foo0', parts[0][1].source)
def test_interpolate_full_nested_brackets(self):
parts = list(interpolate('${{1:2}}'))
self.assertEqual(1, len(parts))
self.assertEqual(EXPR, parts[0][0])
self.assertEqual('{1:2}', parts[0][1].source)
def test_interpolate_full_mismatched_brackets(self):
try:
list(interpolate('${{1:2}'))
except TemplateSyntaxError, e:
pass
else:
self.fail('Expected TemplateSyntaxError')
def test_interpolate_quoted_brackets_1(self):
parts = list(interpolate('${"}"}'))
self.assertEqual(1, len(parts))
self.assertEqual(EXPR, parts[0][0])
self.assertEqual('"}"', parts[0][1].source)
def test_interpolate_quoted_brackets_2(self):
parts = list(interpolate("${'}'}"))
self.assertEqual(1, len(parts))
self.assertEqual(EXPR, parts[0][0])
self.assertEqual("'}'", parts[0][1].source)
def test_interpolate_quoted_brackets_3(self):
parts = list(interpolate("${'''}'''}"))
self.assertEqual(1, len(parts))
self.assertEqual(EXPR, parts[0][0])
self.assertEqual("'''}'''", parts[0][1].source)
def test_interpolate_quoted_brackets_4(self):
parts = list(interpolate("${'''}\"\"\"'''}"))
self.assertEqual(1, len(parts))
self.assertEqual(EXPR, parts[0][0])
self.assertEqual("'''}\"\"\"'''", parts[0][1].source)
def test_interpolate_quoted_brackets_5(self):
parts = list(interpolate(r"${'\'}'}"))
self.assertEqual(1, len(parts))
self.assertEqual(EXPR, parts[0][0])
self.assertEqual(r"'\'}'", parts[0][1].source)
def test_interpolate_mixed1(self):
parts = list(interpolate('$foo bar $baz'))
self.assertEqual(3, len(parts))
self.assertEqual(EXPR, parts[0][0])
self.assertEqual('foo', parts[0][1].source)
self.assertEqual(TEXT, parts[1][0])
self.assertEqual(' bar ', parts[1][1])
self.assertEqual(EXPR, parts[2][0])
self.assertEqual('baz', parts[2][1].source)
def test_interpolate_mixed2(self):
parts = list(interpolate('foo $bar baz'))
self.assertEqual(3, len(parts))
self.assertEqual(TEXT, parts[0][0])
self.assertEqual('foo ', parts[0][1])
self.assertEqual(EXPR, parts[1][0])
self.assertEqual('bar', parts[1][1].source)
self.assertEqual(TEXT, parts[2][0])
self.assertEqual(' baz', parts[2][1])
def test_interpolate_triplequoted(self):
parts = list(interpolate('${"""foo\nbar"""}'))
self.assertEqual(1, len(parts))
self.assertEqual('"""foo\nbar"""', parts[0][1].source)
def suite():
suite = unittest.TestSuite()
suite.addTest(doctest.DocTestSuite(interpolate.__module__))
suite.addTest(unittest.makeSuite(InterpolateTestCase, 'test'))
return suite
if __name__ == '__main__':
unittest.main(defaultTest='suite')
|