def test_format_optdict(self): def check_against(fmt_opts, result): for i in range(0, len(fmt_opts), 2): self.assertEqual(result.pop(fmt_opts[i]), fmt_opts[i + 1]) if result: self.fail("result still got elements: %s" % result) # passing an empty dict should return an empty object (tuple here) self.assertFalse(ttk._format_optdict({})) # check list formatting check_against( ttk._format_optdict({'fg': 'blue', 'padding': [1, 2, 3, 4]}), {'-fg': 'blue', '-padding': '1 2 3 4'}) # check tuple formatting (same as list) check_against( ttk._format_optdict({'test': (1, 2, '', 0)}), {'-test': '1 2 {} 0'}) # check untouched values check_against( ttk._format_optdict({'test': {'left': 'as is'}}), {'-test': {'left': 'as is'}}) # check script formatting and untouched value(s) check_against( ttk._format_optdict( {'test': [1, -1, '', '2m', 0], 'nochange1': 3, 'nochange2': 'abc def'}, script=True), {'-test': '{1 -1 {} 2m 0}', '-nochange1': 3, '-nochange2': 'abc def' }) opts = {'αβγ': True, 'á': False} orig_opts = opts.copy() # check if giving unicode keys is fine check_against(ttk._format_optdict(opts), {'-αβγ': True, '-á': False}) # opts should remain unchanged self.assertEqual(opts, orig_opts) # passing values with spaces inside a tuple/list check_against( ttk._format_optdict( {'option': ('one two', 'three')}), {'-option': '{one two} three'}) # ignore an option amount_opts = len(ttk._format_optdict(opts, ignore=('á'))) / 2 self.assertEqual(amount_opts, len(opts) - 1) # ignore non-existing options amount_opts = len(ttk._format_optdict(opts, ignore=('á', 'b'))) / 2 self.assertEqual(amount_opts, len(opts) - 1) # ignore every option self.assertFalse(ttk._format_optdict(opts, ignore=list(opts.keys())))
def test_format_optdict(self): def check_against(fmt_opts, result): for i in range(0, len(fmt_opts), 2): self.failUnlessEqual(result.pop(fmt_opts[i]), fmt_opts[i + 1]) if result: self.fail("result still got elements: %s" % result) # passing an empty dict should return an empty object (tuple here) self.failIf(ttk._format_optdict({})) # check list formatting check_against( ttk._format_optdict({"fg": "blue", "padding": [1, 2, 3, 4]}), {"-fg": "blue", "-padding": "1 2 3 4"} ) # check tuple formatting (same as list) check_against(ttk._format_optdict({"test": (1, 2, "", 0)}), {"-test": "1 2 {} 0"}) # check untouched values check_against(ttk._format_optdict({"test": {"left": "as is"}}), {"-test": {"left": "as is"}}) # check script formatting and untouched value(s) check_against( ttk._format_optdict({"test": [1, -1, "", "2m", 0], "nochange1": 3, "nochange2": "abc def"}, script=True), {"-test": "{1 -1 {} 2m 0}", "-nochange1": 3, "-nochange2": "abc def"}, ) opts = {"αβγ": True, "á": False} orig_opts = opts.copy() # check if giving unicode keys is fine check_against(ttk._format_optdict(opts), {"-αβγ": True, "-á": False}) # opts should remain unchanged self.failUnlessEqual(opts, orig_opts) # passing values with spaces inside a tuple/list check_against(ttk._format_optdict({"option": ("one two", "three")}), {"-option": "{one two} three"}) # ignore an option amount_opts = len(ttk._format_optdict(opts, ignore=("á"))) / 2 self.failUnlessEqual(amount_opts, len(opts) - 1) # ignore non-existing options amount_opts = len(ttk._format_optdict(opts, ignore=("á", "b"))) / 2 self.failUnlessEqual(amount_opts, len(opts) - 1) # ignore every option self.failIf(ttk._format_optdict(opts, ignore=list(opts.keys())))
def insert(self, parent, index, iid=None, **kw): opts = ttk._format_optdict(kw) if iid is not None: res = self.tk.call(self._w, "insert", parent, index, "-id", iid, *opts) else: if self.custom_insert: iid = 'I{iid}'.format(iid=format( len(self.get_children()) + 1, '03x')) #hex(len(self.get_children())).split('x')[-1] res = self.tk.call(self._w, "insert", parent, index, "-id", iid, *opts) else: res = self.tk.call(self._w, "insert", parent, index, *opts) return res
def test_format_optdict(self): def check_against(fmt_opts, result): for i in range(0, len(fmt_opts), 2): self.assertEqual(result.pop(fmt_opts[i]), fmt_opts[i + 1]) if result: self.fail('result still got elements: %s' % result) self.assertFalse(ttk._format_optdict({})) check_against( ttk._format_optdict({ 'fg': 'blue', 'padding': [1, 2, 3, 4] }), { '-fg': 'blue', '-padding': '1 2 3 4' }) check_against(ttk._format_optdict({'test': (1, 2, '', 0)}), {'-test': '1 2 {} 0'}) check_against(ttk._format_optdict({'test': { 'left': 'as is' }}), {'-test': { 'left': 'as is' }}) check_against( ttk._format_optdict( { 'test': [1, -1, '', '2m', 0], 'test2': 3, 'test3': '', 'test4': 'abc def', 'test5': '"abc"', 'test6': '{}', 'test7': '} -spam {' }, script=True), { '-test': '{1 -1 {} 2m 0}', '-test2': '3', '-test3': '{}', '-test4': '{abc def}', '-test5': '{"abc"}', '-test6': '\\{\\}', '-test7': '\\}\\ -spam\\ \\{' }) opts = {'αβγ': True, 'á': False} orig_opts = opts.copy() check_against(ttk._format_optdict(opts), {'-αβγ': True, '-á': False}) self.assertEqual(opts, orig_opts) check_against(ttk._format_optdict({'option': ('one two', 'three')}), {'-option': '{one two} three'}) check_against(ttk._format_optdict({'option': ('one\ttwo', 'three')}), {'-option': '{one\ttwo} three'}) check_against(ttk._format_optdict({'option': ('', 'one')}), {'-option': '{} one'}) check_against(ttk._format_optdict({'option': ('one} {two', 'three')}), {'-option': 'one\\}\\ \\{two three'}) check_against(ttk._format_optdict({'option': ('"one"', 'two')}), {'-option': '{"one"} two'}) check_against(ttk._format_optdict({'option': ('{one}', 'two')}), {'-option': '\\{one\\} two'}) amount_opts = len(ttk._format_optdict(opts, ignore='á')) / 2 self.assertEqual(amount_opts, len(opts) - 1) amount_opts = len(ttk._format_optdict(opts, ignore=('á', 'b'))) / 2 self.assertEqual(amount_opts, len(opts) - 1) self.assertFalse(ttk._format_optdict(opts, ignore=list(opts.keys())))