-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtests.py
618 lines (513 loc) · 21.8 KB
/
tests.py
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
# -*- coding: utf-8 -*-
#
# Many tests were converted from the Python 2.7 standard library test suite.
import sys
import unittest
from stringformat import FormattableString as f, _strformat
# object.__format__ does not exist in Python 2.5
has_object_format = hasattr(object, '__format__')
try:
u = unicode
except NameError:
u = unicode = str
python_3 = (str is unicode)
def repr_exc():
return repr(sys.exc_info()[1])
class BaseFormatterTest(unittest.TestCase):
type2test = None
def assertEqual(self, first, second, msg=None):
# strict assertEqual method: verify value and type
assert_equal = unittest.TestCase.assertEqual
assert_equal(self, first, second, msg)
assert_equal(self, type(first), type(second))
def _prepare(self, *args):
if len(args) == 1:
return self.type2test(args[0])
return tuple(self.type2test(v) for v in args)
def _check_strformat(self, expected, fmt, value):
value, expected = self._prepare(value, expected)
# test both with and without the trailing 's'
self.assertEqual(_strformat(value, fmt), expected)
self.assertEqual(_strformat(value, fmt + 's'), expected)
def _check_format(self, expected, fmt, *args, **kwargs):
fmt, expected = self._prepare(fmt, expected)
self.assertEqual(f(fmt).format(*args, **kwargs), expected)
def _check_raises(self, expected_exception, fmt, *args, **kwargs):
fmt = self._prepare(fmt)
try:
f(fmt).format(*args, **kwargs)
except expected_exception:
pass
else:
raise self.failureException('%s not raised' %
expected_exception.__name__)
assert_raises_25 = _check_raises
def test_strformat(self):
# from Python 2.7 test suite
test = self._check_strformat
# def test(expected, fmt, value):
# assert _strformat(value, fmt) == expected
test('', '', '')
test('abc', '', 'abc')
test('abc', '.3', 'abc')
test('ab', '.3', 'ab')
test('abc', '.3', 'abcdef')
test('', '.0', 'abcdef')
test('abc', '3.3', 'abc')
test('abc', '2.3', 'abc')
test('ab', '2.2', 'abc')
test('ab ', '3.2', 'abc')
test('result', 'x<0', 'result')
test('result', 'x<5', 'result')
test('result', 'x<6', 'result')
test('resultx', 'x<7', 'result')
test('resultxx', 'x<8', 'result')
test('result ', ' <7', 'result')
test('result ', '<7', 'result')
test(' result', '>7', ' result')
test(' result', '>8', 'result')
test(' result ', '^8', 'result')
test(' result ', '^9', 'result')
test(' result ', '^10', 'result')
test('a' + ' ' * 9999, '10000', 'a')
test(' ' * 10000, '10000', '')
test(' ' * 10000000, '10000000', '')
def test_format(self):
# from Python 2.7 test suite
test = self._check_format
# Safety check
self.assertTrue(hasattr(f(''), 'format'))
# def test(expected, fmt, *args, **kwargs):
# assert f(fmt).format(*args, **kwargs) == expected
test('', '')
test('a', 'a')
test('ab', 'ab')
test('a{', 'a{{')
test('a}', 'a}}')
test('{b', '{{b')
test('}b', '}}b')
test('a{b', 'a{{b')
# examples from the PEP:
test("My name is Fred", "My name is {0}", "Fred")
test("My name is Fred", "My name is {0[name]}", dict(name="Fred"))
test("My name is Fred :-{}", "My name is {0} :-{{}}", "Fred")
import datetime
d = datetime.date(2007, 8, 18)
test("The year is 2007", "The year is {0.year}", d)
# classes we'll use for testing
class C:
def __init__(self, x=100):
self._x = x
def __format__(self, spec):
return spec
class D:
def __init__(self, x):
self.x = x
def __format__(self, spec):
return str(self.x)
# class with __str__, but no __format__
class E:
def __init__(self, x):
self.x = x
def __str__(self):
return 'E(' + self.x + ')'
# class with __repr__, but no __format__ or __str__
class F:
def __init__(self, x):
self.x = x
def __repr__(self):
return 'F(' + self.x + ')'
# class with __format__ that forwards to string, for some format_spec's
class G:
def __init__(self, x):
self.x = x
def __str__(self):
return "string is " + self.x
def __format__(self, format_spec):
if format_spec == 'd':
return 'G(' + self.x + ')'
return object.__format__(self, format_spec)
# class that returns a bad type from __format__
class H:
def __format__(self, format_spec):
return 1.0
class I(datetime.date):
def __format__(self, format_spec):
return self.strftime(str(format_spec))
class J(int):
def __format__(self, format_spec):
return int.__format__(self * 2, format_spec)
test('abc', 'abc')
test('abc', '{0}', 'abc')
test('abc', '{0:}', 'abc')
test('Xabc', 'X{0}', 'abc')
test('abcX', '{0}X', 'abc')
test('XabcY', 'X{0}Y', 'abc')
test('abc', '{1}', 1, 'abc')
test('Xabc', 'X{1}', 1, 'abc')
test('abcX', '{1}X', 1, 'abc')
test('XabcY', 'X{1}Y', 1, 'abc')
test('-15', '{0}', -15)
test('-15abc', '{0}{1}', -15, 'abc')
test('-15Xabc', '{0}X{1}', -15, 'abc')
test('{', '{{')
test('}', '}}')
test('{}', '{{}}')
test('{x}', '{{x}}')
test('{123}', '{{{0}}}', 123)
test('{{0}}', '{{{{0}}}}')
test('}{', '}}{{')
test('}x{', '}}x{{')
# weird field names
test('baz', '{0[foo-bar]}', {'foo-bar': 'baz'})
test('baz', '{0[foo bar]}', {'foo bar': 'baz'})
test('3', '{0[ ]}', {' ': 3})
test('20', '{foo._x}', foo=C(20))
test('2010', '{1}{0}', D(10), D(20))
test('abc', '{0._x.x}', C(D('abc')))
test('abc', '{0[0]}', ['abc', 'def'])
test('def', '{0[1]}', ['abc', 'def'])
test('def', '{0[1][0]}', ['abc', ['def']])
test('def', '{0[1][0].x}', ['abc', [D('def')]])
# strings
test('abc', '{0:.3s}', 'abc')
test('ab', '{0:.3s}', 'ab')
test('abc', '{0:.3s}', 'abcdef')
test('', '{0:.0s}', 'abcdef')
test('abc', '{0:3.3s}', 'abc')
test('abc', '{0:2.3s}', 'abc')
test('ab ', '{0:3.2s}', 'abc')
test('result', '{0:x<0s}', 'result')
test('result', '{0:x<5s}', 'result')
test('result', '{0:x<6s}', 'result')
test('resultx', '{0:x<7s}', 'result')
test('resultxx', '{0:x<8s}', 'result')
test('result ', '{0: <7s}', 'result')
test('result ', '{0:<7s}', 'result')
test(' result', '{0:>7s}', 'result')
test(' result', '{0:>8s}', 'result')
test(' result ', '{0:^8s}', 'result')
test(' result ', '{0:^9s}', 'result')
test(' result ', '{0:^10s}', 'result')
test('a' + ' ' * 9999, '{0:10000}', 'a')
test(' ' * 10000, '{0:10000}', '')
test(' ' * 10000000, '{0:10000000}', '')
# format specifiers for user defined type
test('abc', '{0:abc}', C())
# !r and !s coercions
test('Hello', '{0!s}', 'Hello')
test('Hello', '{0!s}', 'Hello')
test('Hello ', '{0!s:15}', 'Hello')
test('Hello ', '{0!s:15s}', 'Hello')
test("'Hello'", '{0!r}', 'Hello')
test("'Hello'", '{0!r:}', 'Hello')
test('F(Hello)', '{0!r}', F('Hello'))
# test fallback to object.__format__
test('{}', '{0}', {})
test('[]', '{0}', [])
test('[1]', '{0}', [1])
test('E(data)', '{0}', E('data'))
# XXX pending deprecation
# * object.__format__ with a non-empty format string is deprecated
test(' E(data) ', '{0:^10}', E('data'))
test(' E(data) ', '{0:^10s}', E('data'))
if has_object_format:
test(' string is data', '{0:>15s}', G('data'))
test('G(data)', '{0:d}', G('data'))
test('string is data', '{0!s}', G('data'))
test('date: 2007-08-27', '{0:date: %Y-%m-%d}',
I(year=2007, month=8, day=27))
if has_object_format:
# test deriving from a builtin type and overriding __format__
test('20', '{0}', J(10))
# string format specifiers
test('a', '{0:}', 'a')
# computed format specifiers
test('hello', '{0:.{1}}', 'hello world', 5)
test('hello', '{0:.{1}s}', 'hello world', 5)
test('hello', '{0:.{precision}s}', 'hello world', precision=5)
test('hello ', '{0:{width}.{precision}s}',
'hello world', width=10, precision=5)
test('hello ', '{0:{width}.{precision}s}',
'hello world', width='10', precision='5')
def test_format_auto_numbering(self):
# from Python 2.7 test suite
test = self._check_format
class C:
def __init__(self, x=100):
self._x = x
def __format__(self, spec):
return spec
test('10', '{}', 10)
test('s ', '{:5}', 's')
test("'s'", '{!r}', 's')
test('10', '{._x}', C(10))
test('2', '{[1]}', [1, 2])
test('4', '{[a]}', {'a': 4, 'b': 2})
test('a0b1c', 'a{}b{}c', 0, 1)
test('a x b', 'a{:{}}b', 'x', '^10')
test('a0x14b', 'a{:{}x}b', 20, '#')
# can mix and match auto-numbering and named
test('test4', '{f}{}', 4, f='test')
test('4test', '{}{f}', 4, f='test')
test(' 1g3', '{:{f}}{g}{}', 1, 3, g='g', f=2)
test(' 14g', '{f:{}}{}{g}', 2, 4, f=1, g='g')
def test_format_errors(self):
# from Python 2.7 test suite
assert_raises = self._check_raises
# test various errors
# XXX failing tests are commented
# assert_raises(ValueError, '{')
# assert_raises(ValueError, '}')
# assert_raises(ValueError, 'a{')
# assert_raises(ValueError, 'a}')
# assert_raises(ValueError, '{a')
# assert_raises(ValueError, '}a')
# assert_raises(IndexError, '{0}') # KeyError
# assert_raises(IndexError, '{1}', 'abc') # KeyError
assert_raises(KeyError, '{x}')
# assert_raises(ValueError, '}{')
# assert_raises(ValueError, 'abc{0:{}') # KeyError
# assert_raises(ValueError, '{0')
# assert_raises(IndexError, '{0.}') # ValueError
assert_raises(ValueError, '{0.}', 0)
# assert_raises(IndexError, '{0[}') # ValueError
# assert_raises(ValueError, '{0[}', [])
assert_raises(KeyError, '{0]}')
assert_raises(ValueError, '{0.[]}', 0)
assert_raises(ValueError, '{0..foo}', 0)
assert_raises(ValueError, '{0[0}', 0)
assert_raises(ValueError, '{0[0:foo}', 0)
assert_raises(KeyError, '{c]}')
# assert_raises(ValueError, '{{ {{{0}}', 0)
# assert_raises(ValueError, '{0}}', 0)
assert_raises(KeyError, '{foo}', bar=3)
assert_raises(ValueError, '{0!x}', 3)
assert_raises(ValueError, '{0!}', 0)
assert_raises(ValueError, '{0!rs}', 0)
assert_raises(ValueError, '{!}')
# assert_raises(IndexError, '{:}') # KeyError
# assert_raises(IndexError, '{:s}') # KeyError
# assert_raises(IndexError, '{}') # KeyError
# issue 6089
assert_raises(ValueError, '{0[0]x}', [None])
assert_raises(ValueError, '{0[0](10)}', [None])
# can't have a replacement on the field name portion
assert_raises(TypeError, '{0[{1}]}', 'abcdefg', 4)
# exceed maximum recursion depth
# assert_raises(ValueError, '{0:{1:{2}}}', 'abc', 's', '')
# assert_raises(ValueError, '{0:{1:{2:{3:{4:{5:{6}}}}}}}',
# 0, 1, 2, 3, 4, 5, 6, 7)
# string format spec errors
assert_raises(ValueError, '{0:-s}', '')
self.assertRaises(ValueError, _strformat, "", "-")
assert_raises(ValueError, '{0:=s}', '')
# can't mix and match numbering and auto-numbering
assert_raises(ValueError, '{}{1}', 1, 2)
assert_raises(ValueError, '{1}{}', 1, 2)
assert_raises(ValueError, '{:{1}}', 1, 2)
assert_raises(ValueError, '{0:{}}', 1, 2)
def test_incompatibilities(self):
# Differences with Python 2.7
# KeyError instead of IndexError (or ValueError)
self.assert_raises_25(KeyError, '{0}')
self.assert_raises_25(KeyError, '{1}', 'abc')
self.assert_raises_25(KeyError, 'abc{0:{}') # ValueError
self.assert_raises_25(KeyError, '{:}')
self.assert_raises_25(KeyError, '{:s}')
self.assert_raises_25(KeyError, '{}')
# ValueError instead of IndexError
self.assert_raises_25(ValueError, '{0.}')
self.assert_raises_25(ValueError, '{0[}')
self.assert_raises_25(ValueError, '{0.[]}')
self.assert_raises_25(ValueError, '{0..foo}')
self.assert_raises_25(ValueError, '{0[0}')
self.assert_raises_25(ValueError, '{0[0:foo}')
def test_format_extra(self):
# Additional tests
test = self._check_format
import datetime
d = datetime.date(2007, 8, 18)
test('Recorded on 2007-08-18', 'Recorded on {0:%Y-%m-%d}', d)
# An Enum-like structure.
class Enum(dict):
__getattr__ = dict.__getitem__
def __getitem__(self, attr):
raise AssertionError
test('42', '{ ] [ . [ ]}', **{' ] ': {' . [ ': 42}})
test('{ 42 }', '{{{ ] [ . [ ]:0< 2n} }}', **{' ] ': {' . [ ': 42}})
test('42', '{a]b[c[d].e]f.g[h.i]}',
**{'a]b': {'c[d': Enum({'e]f': Enum(g={'h.i': 42})})}})
test('42', '{.qsd]er[fe].QsD[ER].e].az.r[t[u][[X][Y][Z.]}',
Enum({'qsd]er': {'fe': Enum(QsD={'ER': Enum({'e]':
Enum(az=Enum(r={'t[u': {'[X': {'Y': {'Z.': 42}}}}))})})}}))
# The old-formatting '%s' is ignored.
test('42%s', '{}%s', 42)
test('abc: %s', 'abc: %s', 42)
def test_format_numeric(self):
test = self._check_format
assert_raises = self._check_raises
test(' 42', '{0:=5}', 42)
test('___42', '{0:_=5}', 42)
test('- 42', '{0:=5}', -42)
test('+ 42', '{0:=+5}', 42)
test('-__42', '{0:_= 5}', -42)
test('+__42', '{0:_=+5}', 42)
test('__+42', '{0:_>+5}', 42)
test('00042', '{0:05}', 42)
test('-0042', '{0:05}', -42)
test('00042', '{0:=05}', 42)
test('-0042', '{0:=05}', -42)
test(' -42', '{0:#5}', -42)
test('000-42.', '{0:>07}', '-42.')
test('-000042', '{0:07}', -42)
test('+000042', '{0:+07}', 42)
test('###42', '{0:#=5n}', 42.)
test('00042.0', '{0:>07}', 42.)
test('-##42', '{0:#=5n}', -42.)
test('00-42.0', '{0:>07}', -42.)
test('+##42.0', '{0:#=+7}', 42.)
test('00+42.0', '{0:>+07}', 42.)
test('*', '{0:c}', 42)
result = python_3 and chr(810) or '*'
test(result, '{0:c}', 810)
# XXX broken
# test('42.000000%', '{0:%}', .42) # TypeError
# test('+42,000', '{0:>+07,}', 42000) # '0+42000'
# test('4,398,046,511,104', '{0:,}', 1 << 42)
# test('1,024.03', '{0:,}', 1024.03)
assert_raises(ValueError, '{0:>+07K}', 42.)
assert_raises(ValueError, '{0:>07s}', .42)
assert_raises(ValueError, '{0:>+07s}', '42')
assert_raises(ValueError, '{0:<+05c}', 42)
# For Python >= 2.6, compare the result with the standard Python formatting
if has_object_format:
builtin_function_or_method = type(len)
# Python 2.6 has some limitations (no auto-numbering, no thousand)
_python26_limitations = []
for fmt in ('{}', '{.real}', '{0:,}'):
try:
fmt.format(42)
except ValueError:
# Python 2.6 raises ValueErrors:
# * "zero length field name in format"
# * "empty field name"
# * "Unknown format code ',' for object of type 'int'"
_python26_limitations.append(repr_exc())
def _test_format(string, *args, **kwargs):
"""Compare with standard implementation.
Return a pair (tuple) of results (standard_result, actual_result).
"""
# Monkey patch the _format_field to skip builtin method __format__
def _format_field(value, parts, conv, spec, want_bytes=False):
for k, part, _ in parts:
if k:
if part.isdigit():
value = value[int(part)]
else:
value = value[part]
else:
value = getattr(value, part)
if conv:
value = ((conv == 'r') and '%r' or '%s') % (value,)
format_value = getattr(value, '__format__', None)
if format_value and (hasattr(value, 'strftime') or
not isinstance(format_value, builtin_function_or_method)):
value = format_value(spec)
else:
# Skip the __format__ method for builtin types
value = _strformat(value, spec)
if want_bytes and isinstance(value, unicode):
return str(value)
return value
# Monkey patch the _format_field to skip builtin method __format__
import stringformat as mod
original_format_field = mod._format_field
mod._format_field = _format_field
try:
s1 = repr(string.format(*args, **kwargs))
except Exception:
s1 = repr_exc()
try:
try:
s2 = repr(f(string).format(*args, **kwargs))
except Exception:
s2 = repr_exc()
finally:
mod._format_field = original_format_field
return s1, s2
_BaseFormatterTest25 = BaseFormatterTest
class BaseFormatterTest(_BaseFormatterTest25):
def _check_strformat(self, expected, fmt, value):
check_strformat = super(BaseFormatterTest, self)._check_strformat
check_strformat(expected, fmt, value)
value, expected = self._prepare(value, expected)
# test the built-in format() function
self.assertEqual(format(value, fmt), expected)
self.assertEqual(format(value, fmt + 's'), expected)
def _compare_with_standard(self, fmt, *args, **kwargs):
# Compare with standard formatting
fmt = self._prepare(fmt)
std_result, result = _test_format(fmt, *args, **kwargs)
# Check same result or same error message
if std_result not in _python26_limitations:
self.assertEqual(std_result, result)
return result
def _check_format(self, expected, fmt, *args, **kwargs):
rv = self._compare_with_standard(fmt, *args, **kwargs)
if expected is None:
print('\n%r.format(*%r, **%r)' % (fmt, args, kwargs))
print('*** ' + rv)
else:
check_format = super(BaseFormatterTest, self)._check_format
check_format(expected, fmt, *args, **kwargs)
def _check_raises(self, expected_exception, fmt, *args, **kwargs):
rv = self._compare_with_standard(fmt, *args, **kwargs)
if expected_exception is None:
print('\n%r.format(*%r, **%r)' % (fmt, args, kwargs))
print('*** ' + rv)
else:
check_raises = super(BaseFormatterTest, self)._check_raises
check_raises(expected_exception, fmt, *args, **kwargs)
del _BaseFormatterTest25
class StringFormatterTest(BaseFormatterTest):
type2test = str
class UnicodeFormatterTest(BaseFormatterTest):
type2test = unicode
if not python_3:
def test_mixed_unicode_str(self):
# from Python 2.7 test suite
def test(expected, fmt, *args, **kwargs):
self.assertEqual(f(fmt).format(*args, **kwargs), expected)
# test mixing unicode and str
self.assertEqual(_strformat(u('abc'), 's'), u('abc'))
self.assertEqual(_strformat(u('abc'), '->10s'), u('-------abc'))
# test combining string and unicode
test(u('foobar'), u("foo{0}"), 'bar')
# This will try to convert the argument from unicode to str,
# which will succeed
test('foobar', "foo{0}", u('bar'))
# This will try to convert the argument from unicode to str,
# which will fail
s = '\u1000bar'.decode('unicode_escape')
self.assertRaises(UnicodeEncodeError, f("foo{0}").format, s)
if has_object_format: # specific to Python >= 2.6
def test_format_subclass(self):
# from Python 2.7 test suite
class U(unicode):
if python_3:
def __str__(self):
return '__unicode__ overridden'
else:
def __unicode__(self):
return u('__unicode__ overridden')
s = U('xxx')
self.assertEqual(f("{}").format(s), '__unicode__ overridden')
self.assertEqual("%s" % s, u('__unicode__ overridden'))
def suite():
suite = unittest.TestSuite()
suite.addTest(unittest.makeSuite(StringFormatterTest))
suite.addTest(unittest.makeSuite(UnicodeFormatterTest))
return suite
if __name__ == "__main__":
unittest.main(defaultTest='suite')