MCPcopy Index your code
hub / github.com/python/cpython / test_format_optdict

Method test_format_optdict

Lib/test/test_ttk_textonly.py:43–128  ·  view source on GitHub ↗
(self)

Source from the content-addressed store, hash-verified

41class InternalFunctionsTest(unittest.TestCase):
42
43 def test_format_optdict(self):
44 def check_against(fmt_opts, result):
45 for i in range(0, len(fmt_opts), 2):
46 self.assertEqual(result.pop(fmt_opts[i]), fmt_opts[i + 1])
47 if result:
48 self.fail("result still got elements: %s" % result)
49
50 # passing an empty dict should return an empty object (tuple here)
51 self.assertFalse(ttk._format_optdict({}))
52
53 # check list formatting
54 check_against(
55 ttk._format_optdict({'fg': 'blue', 'padding': [1, 2, 3, 4]}),
56 {'-fg': 'blue', '-padding': '1 2 3 4'})
57
58 # check tuple formatting (same as list)
59 check_against(
60 ttk._format_optdict({'test': (1, 2, '', 0)}),
61 {'-test': '1 2 {} 0'})
62
63 # check untouched values
64 check_against(
65 ttk._format_optdict({'test': {'left': 'as is'}}),
66 {'-test': {'left': 'as is'}})
67
68 # check script formatting
69 check_against(
70 ttk._format_optdict(
71 {'test': [1, -1, '', '2m', 0], 'test2': 3,
72 'test3': '', 'test4': 'abc def',
73 'test5': '"abc"', 'test6': '{}',
74 'test7': '} -spam {'}, script=True),
75 {'-test': '{1 -1 {} 2m 0}', '-test2': '3',
76 '-test3': '{}', '-test4': '{abc def}',
77 '-test5': '{"abc"}', '-test6': r'\{\}',
78 '-test7': r'\}\ -spam\ \{'})
79
80 opts = {'αβγ': True, 'á': False}
81 orig_opts = opts.copy()
82 # check if giving unicode keys is fine
83 check_against(ttk._format_optdict(opts), {'-αβγ': True, '-á': False})
84 # opts should remain unchanged
85 self.assertEqual(opts, orig_opts)
86
87 # passing values with spaces inside a tuple/list
88 check_against(
89 ttk._format_optdict(
90 {'option': ('one two', 'three')}),
91 {'-option': '{one two} three'})
92 check_against(
93 ttk._format_optdict(
94 {'option': ('one\ttwo', 'three')}),
95 {'-option': '{one\ttwo} three'})
96
97 # passing empty strings inside a tuple/list
98 check_against(
99 ttk._format_optdict(
100 {'option': ('', 'one')}),

Callers

nothing calls this directly

Calls 5

listClass · 0.85
assertFalseMethod · 0.80
copyMethod · 0.45
assertEqualMethod · 0.45
keysMethod · 0.45

Tested by

no test coverage detected