Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_string.ModuleTest Class Reference

List of all members.

Public Member Functions

def test_attrs
def test_capwords
def test_formatter
def test_vformat_assert
def test_convert_field
def test_get_field

Public Attributes

 namespace

Detailed Description

Definition at line 5 of file test_string.py.


Member Function Documentation

Definition at line 7 of file test_string.py.

00007 
00008     def test_attrs(self):
00009         string.whitespace
00010         string.ascii_lowercase
00011         string.ascii_uppercase
00012         string.ascii_letters
00013         string.digits
00014         string.hexdigits
00015         string.octdigits
00016         string.punctuation
00017         string.printable

Definition at line 18 of file test_string.py.

00018 
00019     def test_capwords(self):
00020         self.assertEqual(string.capwords('abc def ghi'), 'Abc Def Ghi')
00021         self.assertEqual(string.capwords('abc\tdef\nghi'), 'Abc Def Ghi')
00022         self.assertEqual(string.capwords('abc\t   def  \nghi'), 'Abc Def Ghi')
00023         self.assertEqual(string.capwords('ABC DEF GHI'), 'Abc Def Ghi')
00024         self.assertEqual(string.capwords('ABC-DEF-GHI', '-'), 'Abc-Def-Ghi')
00025         self.assertEqual(string.capwords('ABC-def DEF-ghi GHI'), 'Abc-def Def-ghi Ghi')
00026         self.assertEqual(string.capwords('   aBc  DeF   '), 'Abc Def')
00027         self.assertEqual(string.capwords('\taBc\tDeF\t'), 'Abc Def')
00028         self.assertEqual(string.capwords('\taBc\tDeF\t', '\t'), '\tAbc\tDef\t')

Here is the call graph for this function:

Definition at line 123 of file test_string.py.

00123 
00124     def test_convert_field(self):
00125         cls = string.Formatter()
00126         self.assertEqual(cls.format("{0!s}", 'foo'), 'foo')
00127         self.assertRaises(ValueError, cls.format, "{0!h}", 'foo')

Here is the call graph for this function:

Definition at line 29 of file test_string.py.

00029 
00030     def test_formatter(self):
00031         fmt = string.Formatter()
00032         self.assertEqual(fmt.format("foo"), "foo")
00033 
00034         self.assertEqual(fmt.format("foo{0}", "bar"), "foobar")
00035         self.assertEqual(fmt.format("foo{1}{0}-{1}", "bar", 6), "foo6bar-6")
00036         self.assertEqual(fmt.format("-{arg!r}-", arg='test'), "-'test'-")
00037 
00038         # override get_value ############################################
00039         class NamespaceFormatter(string.Formatter):
00040             def __init__(self, namespace={}):
00041                 string.Formatter.__init__(self)
00042                 self.namespace = namespace
00043 
00044             def get_value(self, key, args, kwds):
00045                 if isinstance(key, str):
00046                     try:
00047                         # Check explicitly passed arguments first
00048                         return kwds[key]
00049                     except KeyError:
00050                         return self.namespace[key]
00051                 else:
00052                     string.Formatter.get_value(key, args, kwds)
00053 
00054         fmt = NamespaceFormatter({'greeting':'hello'})
00055         self.assertEqual(fmt.format("{greeting}, world!"), 'hello, world!')
00056 
00057 
00058         # override format_field #########################################
00059         class CallFormatter(string.Formatter):
00060             def format_field(self, value, format_spec):
00061                 return format(value(), format_spec)
00062 
00063         fmt = CallFormatter()
00064         self.assertEqual(fmt.format('*{0}*', lambda : 'result'), '*result*')
00065 
00066 
00067         # override convert_field ########################################
00068         class XFormatter(string.Formatter):
00069             def convert_field(self, value, conversion):
00070                 if conversion == 'x':
00071                     return None
00072                 return super(XFormatter, self).convert_field(value, conversion)
00073 
00074         fmt = XFormatter()
00075         self.assertEqual(fmt.format("{0!r}:{0!x}", 'foo', 'foo'), "'foo':None")
00076 
00077 
00078         # override parse ################################################
00079         class BarFormatter(string.Formatter):
00080             # returns an iterable that contains tuples of the form:
00081             # (literal_text, field_name, format_spec, conversion)
00082             def parse(self, format_string):
00083                 for field in format_string.split('|'):
00084                     if field[0] == '+':
00085                         # it's markup
00086                         field_name, _, format_spec = field[1:].partition(':')
00087                         yield '', field_name, format_spec, None
00088                     else:
00089                         yield field, None, None, None
00090 
00091         fmt = BarFormatter()
00092         self.assertEqual(fmt.format('*|+0:^10s|*', 'foo'), '*   foo    *')
00093 
00094         # test all parameters used
00095         class CheckAllUsedFormatter(string.Formatter):
00096             def check_unused_args(self, used_args, args, kwargs):
00097                 # Track which arguments actually got used
00098                 unused_args = set(kwargs.keys())
00099                 unused_args.update(range(0, len(args)))
00100 
00101                 for arg in used_args:
00102                     unused_args.remove(arg)
00103 
00104                 if unused_args:
00105                     raise ValueError("unused arguments")
00106 
00107         fmt = CheckAllUsedFormatter()
00108         self.assertEqual(fmt.format("{0}", 10), "10")
00109         self.assertEqual(fmt.format("{0}{i}", 10, i=100), "10100")
00110         self.assertEqual(fmt.format("{0}{i}{1}", 10, 20, i=100), "1010020")
00111         self.assertRaises(ValueError, fmt.format, "{0}{i}{1}", 10, 20, i=100, j=0)
00112         self.assertRaises(ValueError, fmt.format, "{0}", 10, 20)
00113         self.assertRaises(ValueError, fmt.format, "{0}", 10, 20, i=100)
00114         self.assertRaises(ValueError, fmt.format, "{i}", 10, 20, i=100)

Here is the call graph for this function:

Definition at line 128 of file test_string.py.

00128 
00129     def test_get_field(self):
00130         cls = string.Formatter()
00131         class MyClass:
00132             name = 'lumberjack'
00133         x = MyClass()
00134         self.assertEqual(cls.format("{0.name}", x), 'lumberjack')
00135 
00136         lookup = ["eggs", "and", "spam"]
00137         self.assertEqual(cls.format("{0[2]}", lookup), 'spam')
00138 

Here is the call graph for this function:

Definition at line 115 of file test_string.py.

00115 
00116     def test_vformat_assert(self):
00117         cls = string.Formatter()
00118         kwargs = {
00119             "i": 100
00120         }
00121         self.assertRaises(ValueError, cls._vformat,
00122                 cls.format, "{0}", kwargs, set(), -2)

Here is the call graph for this function:


Member Data Documentation

Definition at line 41 of file test_string.py.


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