def test_convert_stringval(self): tests = ((0, 0), ('09', 9), ('a', 'a'), (text('áÚ'), text('áÚ')), ([], '[]'), (None, 'None')) for orig, expected in tests: self.assertEqual(ttk._convert_stringval(orig), expected) if sys.getdefaultencoding() == 'ascii': self.assertRaises(UnicodeDecodeError, ttk._convert_stringval, 'á')
def test_convert_stringval(self): tests = ( (0, 0), ('09', 9), ('a', 'a'), (text('áÚ'), text('áÚ')), ([], '[]'), (None, 'None') ) for orig, expected in tests: self.assertEqual(ttk._convert_stringval(orig), expected) if sys.getdefaultencoding() == 'ascii': self.assertRaises(UnicodeDecodeError, ttk._convert_stringval, 'á')
def test_multivalues(self): adict = {'opt': [1, 2, 3, 4]} self.assertEqual(ttk.tclobjs_to_py(adict), {'opt': [1, 2, 3, 4]}) adict['opt'] = [1, 'xm', 3] self.assertEqual(ttk.tclobjs_to_py(adict), {'opt': [1, 'xm', 3]}) adict['opt'] = (MockStateSpec('a', 'b'), text('válũè')) self.assertEqual(ttk.tclobjs_to_py(adict), {'opt': [('a', 'b', text('válũè'))]}) self.assertEqual(ttk.tclobjs_to_py({'x': ['y z']}), {'x': ['y z']})
def test_insert_item(self): # parent 'none' doesn't exist self.assertRaises(Tkinter.TclError, self.tv.insert, 'none', 'end') # open values self.assertRaises(Tkinter.TclError, self.tv.insert, '', 'end', open='') self.assertRaises(Tkinter.TclError, self.tv.insert, '', 'end', open='please') self.assertFalse(self.tv.delete(self.tv.insert('', 'end', open=True))) self.assertFalse(self.tv.delete(self.tv.insert('', 'end', open=False))) # invalid index self.assertRaises(Tkinter.TclError, self.tv.insert, '', 'middle') # trying to duplicate item id is invalid itemid = self.tv.insert('', 'end', 'first-item') self.assertEqual(itemid, 'first-item') self.assertRaises(Tkinter.TclError, self.tv.insert, '', 'end', 'first-item') self.assertRaises(Tkinter.TclError, self.tv.insert, '', 'end', MockTclObj('first-item')) # unicode values value = text('ába') item = self.tv.insert('', 'end', values=(value, )) self.assertEqual(self.tv.item(item, 'values'), (value, )) self.assertEqual(self.tv.item(item, values=None), (value, )) self.tv.item(item, values=list(self.tv.item(item, values=None))) self.assertEqual(self.tv.item(item, values=None), (value, )) self.assertTrue(isinstance(self.tv.item(item), dict)) # erase item values self.tv.item(item, values='') self.assertFalse(self.tv.item(item, values=None)) # item tags item = self.tv.insert('', 'end', tags=[1, 2, value]) self.assertEqual(self.tv.item(item, tags=None), ('1', '2', value)) self.tv.item(item, tags=[]) self.assertFalse(self.tv.item(item, tags=None)) self.tv.item(item, tags=(1, 2)) self.assertEqual(self.tv.item(item, tags=None), ('1', '2')) # values with spaces item = self.tv.insert('', 'end', values=('a b c', '%s %s' % (value, value))) self.assertEqual(self.tv.item(item, values=None), ('a b c', '%s %s' % (value, value))) # text self.assertEqual(self.tv.item( self.tv.insert('', 'end', text="Label here"), text=None), "Label here") self.assertEqual(self.tv.item( self.tv.insert('', 'end', text=value), text=None), value)
def test_script_from_settings(self): # empty options self.assertFalse(ttk._script_from_settings({'name': {'configure': None, 'map': None, 'element create': None}})) # empty layout self.assertEqual( ttk._script_from_settings({'name': {'layout': None}}), "ttk::style layout name {\nnull\n}") configdict = {text('αβγ'): True, text('á'): False} self.assertTrue( ttk._script_from_settings({'name': {'configure': configdict}})) mapdict = {text('üñíćódè'): [(text('á'), text('vãl'))]} self.assertTrue( ttk._script_from_settings({'name': {'map': mapdict}})) # invalid image element self.assertRaises(IndexError, ttk._script_from_settings, {'name': {'element create': ['image']}}) # minimal valid image self.assertTrue(ttk._script_from_settings({'name': {'element create': ['image', 'name']}})) image = {'thing': {'element create': ['image', 'name', ('state1', 'state2', 'val')]}} self.assertEqual(ttk._script_from_settings(image), "ttk::style element create thing image {name {state1 state2} val} ") image['thing']['element create'].append({'opt': 30}) self.assertEqual(ttk._script_from_settings(image), "ttk::style element create thing image {name {state1 state2} val} " "-opt 30") image['thing']['element create'][-1]['opt'] = [MockTclObj(3), MockTclObj('2m')] self.assertEqual(ttk._script_from_settings(image), "ttk::style element create thing image {name {state1 state2} val} " "-opt {3 2m}")
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 = {text('αβγ'): True, text('á'): False} orig_opts = opts.copy() # check if giving unicode keys is fine check_against(ttk._format_optdict(opts), {text('-αβγ'): True, text('-á'): 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=(text('á')))) / 2 self.assertEqual(amount_opts, len(opts) - 1) # ignore non-existing options amount_opts = len(ttk._format_optdict(opts, ignore=(text('á'), 'b'))) / 2 self.assertEqual(amount_opts, len(opts) - 1) # ignore every option self.assertFalse(ttk._format_optdict(opts, ignore=opts.keys()))
def test_format_mapdict(self): opts = {'a': [('b', 'c', 'val'), ('d', 'otherval'), ('', 'single')]} result = ttk._format_mapdict(opts) self.assertEqual(len(result), len(opts.keys()) * 2) self.assertEqual(result, ('-a', '{b c} val d otherval {} single')) self.assertEqual(ttk._format_mapdict(opts, script=True), ('-a', '{{b c} val d otherval {} single}')) self.assertEqual(ttk._format_mapdict({2: []}), ('-2', '')) opts = {text('üñíćódè'): [(text('á'), text('vãl'))]} result = ttk._format_mapdict(opts) self.assertEqual(result, (text('-üñíćódè'), text('á vãl'))) # empty states valid = {'opt': [('', text(''), 'hi')]} self.assertEqual(ttk._format_mapdict(valid), ('-opt', '{ } hi')) # when passing multiple states, they all must be strings invalid = {'opt': [(1, 2, 'valid val')]} self.assertRaises(TypeError, ttk._format_mapdict, invalid) invalid = {'opt': [([1], '2', 'valid val')]} self.assertRaises(TypeError, ttk._format_mapdict, invalid) # but when passing a single state, it can be anything valid = {'opt': [[1, 'value']]} self.assertEqual(ttk._format_mapdict(valid), ('-opt', '1 value')) # special attention to single states which evalute to False for stateval in (None, 0, False, '', set()): # just some samples valid = {'opt': [(stateval, 'value')]} self.assertEqual(ttk._format_mapdict(valid), ('-opt', '{} value')) # values must be iterable opts = {'a': None} self.assertRaises(TypeError, ttk._format_mapdict, opts) # items in the value must have size >= 2 self.assertRaises(IndexError, ttk._format_mapdict, {'a': [('invalid', )]})
def test_unicode(self): adict = {'opt': text('välúè')} self.assertEqual(ttk.tclobjs_to_py(adict), {'opt': text('välúè')}) adict['opt'] = MockTclObj(adict['opt']) self.assertEqual(ttk.tclobjs_to_py(adict), {'opt': text('välúè')})
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 = {text('αβγ'): True, text('á'): False} orig_opts = opts.copy() # check if giving unicode keys is fine check_against(ttk._format_optdict(opts), { text('-αβγ'): True, text('-á'): 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=(text('á')))) / 2 self.assertEqual(amount_opts, len(opts) - 1) # ignore non-existing options amount_opts = len(ttk._format_optdict(opts, ignore=(text('á'), 'b'))) / 2 self.assertEqual(amount_opts, len(opts) - 1) # ignore every option self.assertFalse(ttk._format_optdict(opts, ignore=opts.keys()))
def test_script_from_settings(self): # empty options self.assertFalse( ttk._script_from_settings({ 'name': { 'configure': None, 'map': None, 'element create': None } })) # empty layout self.assertEqual(ttk._script_from_settings({'name': { 'layout': None }}), "ttk::style layout name {\nnull\n}") configdict = {text('αβγ'): True, text('á'): False} self.assertTrue( ttk._script_from_settings({'name': { 'configure': configdict }})) mapdict = {text('üñíćódè'): [(text('á'), text('vãl'))]} self.assertTrue(ttk._script_from_settings({'name': {'map': mapdict}})) # invalid image element self.assertRaises(IndexError, ttk._script_from_settings, {'name': { 'element create': ['image'] }}) # minimal valid image self.assertTrue( ttk._script_from_settings( {'name': { 'element create': ['image', 'name'] }})) image = { 'thing': { 'element create': ['image', 'name', ('state1', 'state2', 'val')] } } self.assertEqual( ttk._script_from_settings(image), "ttk::style element create thing image {name {state1 state2} val} " ) image['thing']['element create'].append({'opt': 30}) self.assertEqual( ttk._script_from_settings(image), "ttk::style element create thing image {name {state1 state2} val} " "-opt 30") image['thing']['element create'][-1]['opt'] = [ MockTclObj(3), MockTclObj('2m') ] self.assertEqual( ttk._script_from_settings(image), "ttk::style element create thing image {name {state1 state2} val} " "-opt {3 2m}")