This file contains corner case testing for non-field-specifier code, to augment the test cases in the unittests. XXX - This is definitely still a work in progress! >>> from pep3101 import format as f ######################################################### Make sure useall works correctly under lots of conditions >>> print f('{!useall}Hi there', 1, 2, 3, 4, 5) Traceback (most recent call last): ValueError: Not all arguments consumed at end of format_string >>> print f('{!useall}Hi there', name='Joe') Traceback (most recent call last): ValueError: Not all arguments consumed at end of format_string >>> print f('{!useall}Hi there', _dict=()) Hi there >>> print f('{!useall}Hi {there}', _dict=dict(there='there')) Hi there >>> print f('{!useall}Hi {there}', 1, _dict=dict(there='there')) Traceback (most recent call last): ValueError: Not all arguments consumed at end of format_string >>> print f('{!useall}Hi {there}', _dict=dict(there='there'), foo=3) Traceback (most recent call last): ValueError: Not all arguments consumed at end of format_string Custom __format__ objects def test_name_mapper(self): mydict = dict(foo=1, bar=2) dict2 = mydict, dict(foobar=3) foo = 27 global3 = 50 self.formatRaises(ValueError, "{foo}") self.formatRaises(ValueError, "{foo} {foobar}", _dict=mydict) self.formatEquals("1", "{foo}", _dict=mydict) self.formatEquals("1 2", "{foo} {bar}", _dict=mydict) self.formatRaises(ValueError, "{foo} {bar} {global3}", _dict=mydict) self.formatEquals("1 2 3", "{foo} {bar} {foobar}", _dict=dict2) self.formatEquals("27 2", "{foo} {bar}", _dict=mydict, foo=foo) self.assertEquals( pep3101.format("{foo} {global3} {global1}"), "27 50 10") def test_check_unused(self): mydict = dict(foo=1, foo2=1) bar = 3 bar2 = 4 result = ' a b 1 3' s = ' {0} {1} {foo} {bar}' s2 = '{!useall}' + s s3 = '{!useall}\r\n' + s s4 = '{!useall}\n' + s s5 = '{!useall}\r\n' + s self.formatEquals(result, s, 'a', 'b', bar=bar, _dict=mydict) self.formatEquals(result, s2, 'a', 'b', bar=bar, _dict=mydict) self.formatEquals(result, s3, 'a', 'b', bar=bar, _dict=mydict) self.formatEquals(result, s4, 'a', 'b', bar=bar, _dict=mydict) self.formatEquals(result, s5, 'a', 'b', bar=bar, _dict=mydict) self.formatEquals(result, s, 'a', 'b', 3, bar=bar, _dict=mydict) self.formatRaises(ValueError, s2, 'a', 'b', 3, bar=bar, _dict=mydict) self.formatEquals(result, s, 'a', 'b', bar=bar, _dict=mydict, sam=27) self.formatRaises(ValueError, s2, 'a', 'b', bar=bar, _dict=mydict, sam=27) self.formatEquals(result, s, 'a', 'b', bar=bar, foo=1) self.formatEquals(result, s, 'a', 'b', bar=bar, foo=1) self.formatEquals(result, s, 'a', 'b', bar=bar, foo=1, sam=27) self.formatRaises(ValueError, s2, 'a', 'b', bar=bar, foo=1, sam=27) self.formatRaises(ValueError, '{!useall}', 1) self.formatRaises(ValueError, '{!useall}', foo=1) def test_format_hook(self): def hookfunc(obj, spec): if obj > 100: return None return '_%s_' % (obj, spec)[obj==3] self.formatEquals('_a_ _4_ 123', '{!hook}{0:a} {1:b}{2:>10d}', 3, 4, 123, _hook=hookfunc) self.formatEquals('_ap_ _4_ 123', '{0:ap} {1:bp}{2:>10d}', 3, 4, 123, _hook=hookfunc) self.formatRaises(ValueError, '{0:ap}') self.formatEquals('_ap_', '{0:ap}', 3, _hook=hookfunc) self.formatRaises(ValueError, '{0:ap}', 123, _hook=hookfunc) def test_alt_syntax(self): self.formatEquals('{}1', '{!syntax1}{{}{0}', 1) self.formatEquals('{ ${ 1 $1 $${0} ${', '{!syntax2}{ $${ ${0} $$${0} $$$${0} $${', 1) self.formatEquals('1 {0} {\n0}', '{!syntax3}{0} { 0} {\n0}', 1) self.formatRaises(ValueError, '}') self.formatRaises(ValueError, '{') self.formatEquals('}', '{!syntax1}}') self.formatRaises(ValueError, '{!syntax1}{') self.formatEquals('}', '{!syntax2}}') self.formatEquals('{', '{!syntax2}{') self.formatRaises(ValueError, '{!syntax1}${') self.formatEquals('${', '{!syntax2}$${') self.formatEquals('}', '{!syntax3}}') self.formatRaises(ValueError, '{!syntax3}{') self.formatEquals('{', '{!syntax3}{ ')