1# basic functionality test for {} format string
2
3def test(fmt, *args):
4    print('{:8s}'.format(fmt) + '>' +  fmt.format(*args) + '<')
5
6test("}}{{")
7test("{}-{}", 1, [4, 5])
8test("{0}-{1}", 1, [4, 5])
9test("{1}-{0}", 1, [4, 5])
10test("{:x}", 1)
11test("{!r}", 2)
12test("{:x}", 0x10)
13test("{!r}", "foo")
14test("{!s}", "foo")
15test("{0!r:>10s} {0!s:>10s}", "foo")
16
17test("{:4b}", 10)
18test("{:4c}", 48)
19test("{:4d}", 123)
20test("{:4n}", 123)
21test("{:4o}", 123)
22test("{:4x}", 123)
23test("{:4X}", 123)
24
25test("{:4,d}", 12345678)
26
27test("{:#4b}", 10)
28test("{:#4o}", 123)
29test("{:#4x}", 123)
30test("{:#4X}", 123)
31
32test("{:#4d}", 0)
33test("{:#4b}", 0)
34test("{:#4o}", 0)
35test("{:#4x}", 0)
36test("{:#4X}", 0)
37
38test("{:<6s}", "ab")
39test("{:>6s}", "ab")
40test("{:^6s}", "ab")
41test("{:.1s}", "ab")
42
43test("{: <6d}", 123)
44test("{: <6d}", -123)
45test("{:0<6d}", 123)
46test("{:0<6d}", -123)
47test("{:@<6d}", 123)
48test("{:@<6d}", -123)
49
50test("{:@< 6d}", 123)
51test("{:@< 6d}", -123)
52test("{:@<+6d}", 123)
53test("{:@<+6d}", -123)
54test("{:@<-6d}", 123)
55test("{:@<-6d}", -123)
56
57test("{:@>6d}",  -123)
58test("{:@<6d}",  -123)
59test("{:@=6d}",  -123)
60test("{:06d}",  -123)
61
62test("{:>20}", "foo")
63test("{:^20}", "foo")
64test("{:<20}", "foo")
65
66# formatting bool as int
67test('{:d}', False)
68test('{:20}', False)
69test('{:d}', True)
70test('{:20}', True)
71
72# nested format specifiers
73print("{:{}}".format(123, '#>10'))
74print("{:{}{}{}}".format(123, '#', '>', '10'))
75print("{0:{1}{2}}".format(123, '#>', '10'))
76print("{text:{align}{width}}".format(text="foo", align="<", width=20))
77print("{text:{align}{width}}".format(text="foo", align="^", width=10))
78print("{text:{align}{width}}".format(text="foo", align=">", width=30))
79
80print("{foo}/foo".format(foo="bar"))
81print("{}".format(123, foo="bar"))
82print("{}-{foo}".format(123, foo="bar"))
83