Only do real variable names.

This commit is contained in:
Jeremy Fincher 2004-08-21 02:57:47 +00:00
parent 8a9ca0bb06
commit 0f1842b18d
2 changed files with 17 additions and 1 deletions

View File

@ -262,7 +262,7 @@ def perlReToReplacer(s):
else: else:
return lambda s: r.sub(replace, s, 1) return lambda s: r.sub(replace, s, 1)
_perlVarSubstituteRe = re.compile(r'\$\{([^}]+)\}|\$(\S+)') _perlVarSubstituteRe = re.compile(r'\$\{([^}]+)\}|\$([a-zA-Z][a-zA-Z0-9]*)')
def perlVariableSubstitute(vars, text): def perlVariableSubstitute(vars, text):
def replacer(m): def replacer(m):
(braced, unbraced) = m.groups() (braced, unbraced) = m.groups()

View File

@ -183,6 +183,22 @@ class UtilsTest(SupyTestCase):
f = utils.perlReToReplacer('s/\b(\w+)\b/\1./g') f = utils.perlReToReplacer('s/\b(\w+)\b/\1./g')
self.assertEqual(f('foo bar baz'), 'foo. bar. baz.') self.assertEqual(f('foo bar baz'), 'foo. bar. baz.')
def testPerlVariableSubstitute(self):
f = utils.perlVariableSubstitute
vars = {'foo': 'bar', 'b a z': 'baz', 'b': 'c', 'i': 100,
'f': lambda: 'called'}
self.assertEqual(f(vars, '$foo'), 'bar')
self.assertEqual(f(vars, '${foo}'), 'bar')
self.assertEqual(f(vars, '$b'), 'c')
self.assertEqual(f(vars, '${b}'), 'c')
self.assertEqual(f(vars, '$i'), '100')
self.assertEqual(f(vars, '${i}'), '100')
self.assertEqual(f(vars, '$f'), 'called')
self.assertEqual(f(vars, '${f}'), 'called')
self.assertEqual(f(vars, '${b a z}'), 'baz')
self.assertEqual(f(vars, '$b:$i'), 'c:100')
def testFindBinaryInPath(self): def testFindBinaryInPath(self):
if os.name == 'posix': if os.name == 'posix':
self.assertEqual(None, utils.findBinaryInPath('asdfhjklasdfhjkl')) self.assertEqual(None, utils.findBinaryInPath('asdfhjklasdfhjkl'))