[Python-checkins] python/nondist/sandbox/string/tests __init__.py,
NONE, 1.1 test_pep292.py, NONE, 1.1 test_safedict.py, NONE, 1.1
bwarsaw at users.sourceforge.net
bwarsaw at users.sourceforge.net
Mon Jun 14 13:25:24 EDT 2004
Update of /cvsroot/python/python/nondist/sandbox/string/tests
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv27618/tests
Added Files:
__init__.py test_pep292.py test_safedict.py
Log Message:
Updated code and tests for PEP 292 proposal. I still need to write some
documentation, and it's not clear we're agreed on the approach of making the
string module a backward compatible package. But at least there's now a
strawman and some unit tests.
--- NEW FILE: __init__.py ---
--- NEW FILE: test_pep292.py ---
# Copyright (C) 2004 Python Software Foundation
# Author: barry at python.org (Barry Warsaw)
# License: http://www.opensource.org/licenses/PythonSoftFoundation.php
import unittest
from string.pep292 import dstring, astring
from string.safedict import safedict, nsdict
class Bag:
pass
class TestDstrings(unittest.TestCase):
def test_basic_dstring(self):
eq = self.assertEqual
s = dstring('$who likes to eat a bag of $what worth 100ドル')
eq(s % safedict(who='tim', what='ham'),
'tim likes to eat a bag of ham worth 100ドル')
eq(s % safedict(who='tim'),
'tim likes to eat a bag of $what worth 100ドル')
def test_brace_identifiers(self):
eq = self.assertEqual
s = dstring('${who} likes to eat a bag of ${what} worth 100ドル')
eq(s % safedict(who='tim', what='ham'),
'tim likes to eat a bag of ham worth 100ドル')
eq(s % safedict(who='tim'),
'tim likes to eat a bag of ${what} worth 100ドル')
def test_embedded_brace_identifiers(self):
eq = self.assertEqual
s = dstring('${who} likes to eat a bag of ${what}ster worth 100ドル')
eq(s % safedict(who='tim', what='ham'),
'tim likes to eat a bag of hamster worth 100ドル')
eq(s % safedict(who='tim'),
'tim likes to eat a bag of ${what}ster worth 100ドル')
def test_escapes(self):
eq = self.assertEqual
s = dstring('$who likes to eat a bag of $$what worth 100ドル')
eq(s % safedict(who='tim', what='ham'),
'tim likes to eat a bag of $what worth 100ドル')
class TestAstrings(unittest.TestCase):
def setUp(self):
self.bag = Bag()
self.bag.who = 'tim'
self.bag.food = 'ham'
def test_basic_astring(self):
eq = self.assertEqual
s = astring(
'$self.bag.who likes to eat a bag of $self.bag.food worth 100ドル')
eq(s % nsdict(),
'tim likes to eat a bag of ham worth 100ドル')
def test_brace_identifiers(self):
eq = self.assertEqual
s = astring(
'${self.bag.who} likes to eat bags of ${self.bag.food} worth 100ドル')
eq(s % nsdict(),
'tim likes to eat bags of ham worth 100ドル')
def test_embedded_brace_identifiers(self):
eq = self.assertEqual
s = astring(
'${self.bag.who} likes to eat bags of ${self.bag.food}ster')
eq(s % nsdict(), 'tim likes to eat bags of hamster')
def test_escapes(self):
eq = self.assertEqual
s = astring('$self.bag.who likes to eat bags of $${self.bag.food}')
eq(s % nsdict(), 'tim likes to eat bags of ${self.bag.food}')
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestDstrings))
suite.addTest(unittest.makeSuite(TestAstrings))
return suite
if __name__ == '__main__':
unittest.main()
--- NEW FILE: test_safedict.py ---
# Copyright (C) 2004 Python Software Foundation
# Author: barry at python.org (Barry Warsaw)
# License: http://www.opensource.org/licenses/PythonSoftFoundation.php
import unittest
from string.safedict import safedict, nsdict
global_string = 'global'
class _Bag:
def __init__(self, **kws):
self.__dict__.update(kws)
global_obj = _Bag(eno='eno', owt='owt')
class TestDictSubclasses(unittest.TestCase):
def test_safedict_present_keys(self):
eq = self.assertEqual
d = safedict(one='one', two='two')
eq(d['one'], 'one')
eq(d['two'], 'two')
eq(d['{one}'], 'one')
eq(d['{two}'], 'two')
def test_safedict_missing_keys(self):
eq = self.assertEqual
d = safedict(one='one', two='two')
eq(d['three'], '$three')
eq(d['{four}'], '${four}')
def test_nsdict_present_keys(self):
eq = self.assertEqual
local_string = 'local'
local_obj = _Bag(eerht='eerht', ruof='ruof')
d = nsdict()
# Simple globals and locals
eq(d['local_string'], 'local')
eq(d['global_string'], 'global')
# Attribute paths on locals and globals
eq(d['global_obj.eno'], 'eno')
eq(d['global_obj.owt'], 'owt')
eq(d['local_obj.eerht'], 'eerht')
eq(d['local_obj.ruof'], 'ruof')
# Missing attributes on present locals and globals
eq(d['global_obj.one'], '$global_obj.one')
eq(d['local_obj.one'], '$local_obj.one')
eq(d['{global_obj.one}'], '${global_obj.one}')
eq(d['{local_obj.one}'], '${local_obj.one}')
# Missing locals and globals
eq(d['missing'], '$missing')
eq(d['{missing}'], '${missing}')
def test_suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(TestDictSubclasses))
return suite
if __name__ == '__main__':
unittest.main()
More information about the Python-checkins
mailing list