1# comprehensive functionality test for {} format string 2 3int_tests = False # these take a while 4char_tests = True 5str_tests = True 6 7def test(fmt, *args): 8 print('{:8s}'.format(fmt) + '>' + fmt.format(*args) + '<') 9 10def test_fmt(conv, fill, alignment, sign, prefix, width, precision, type, arg): 11 fmt = '{' 12 if conv: 13 fmt += '!' 14 fmt += conv 15 fmt += ':' 16 if alignment: 17 fmt += fill 18 fmt += alignment 19 fmt += sign 20 fmt += prefix 21 fmt += width 22 if precision: 23 fmt += '.' 24 fmt += precision 25 fmt += type 26 fmt += '}' 27 test(fmt, arg) 28 if fill == '0' and alignment == '=': 29 fmt = '{:' 30 fmt += sign 31 fmt += prefix 32 fmt += width 33 if precision: 34 fmt += '.' 35 fmt += precision 36 fmt += type 37 fmt += '}' 38 test(fmt, arg) 39 40if int_tests: 41 int_nums = (-1234, -123, -12, -1, 0, 1, 12, 123, 1234, True, False) 42 #int_nums = (-12, -1, 0, 1, 12, True, False) 43 for type in ('', 'b', 'd', 'o', 'x', 'X'): 44 for width in ('', '1', '3', '5', '7'): 45 for alignment in ('', '<', '>', '=', '^'): 46 for fill in ('', ' ', '0', '@'): 47 for sign in ('', '+', '-', ' '): 48 for prefix in ('', '#'): 49 for num in int_nums: 50 test_fmt('', fill, alignment, sign, prefix, width, '', type, num) 51 52if char_tests: 53 for width in ('', '1', '2'): 54 for alignment in ('', '<', '>', '^'): 55 for fill in ('', ' ', '0', '@'): 56 test_fmt('', fill, alignment, '', '', width, '', 'c', 48) 57 58if str_tests: 59 for conv in ('', 'r', 's'): 60 for width in ('', '1', '4', '10'): 61 for alignment in ('', '<', '>', '^'): 62 for fill in ('', ' ', '0', '@'): 63 for str in ('', 'a', 'bcd', 'This is a test with a longer string'): 64 test_fmt(conv, fill, alignment, '', '', width, '', 's', str) 65