blob: c68a650559e91edd5c5bdb44ce419e8bc0111b21 [file] [log] [blame]
Guilherme Polo5f238482009-01-28 14:41:10 +00001# -*- encoding: utf-8 -*-
2import unittest
Serhiy Storchakab49eff22014-05-28 18:38:27 +03003import tkinter
Guilherme Polo5f238482009-01-28 14:41:10 +00004from tkinter import ttk
5
Serhiy Storchakab49eff22014-05-28 18:38:27 +03006class MockTkApp:
7
8 def splitlist(self, arg):
9 if isinstance(arg, tuple):
10 return arg
11 return arg.split(':')
12
13 def wantobjects(self):
14 return True
15
16
Guilherme Polo5f238482009-01-28 14:41:10 +000017class MockTclObj(object):
18 typename = 'test'
19
20 def __init__(self, val):
21 self.val = val
22
23 def __str__(self):
24 return str(self.val)
25
26
27class MockStateSpec(object):
28 typename = 'StateSpec'
29
30 def __init__(self, *args):
31 self.val = args
32
33 def __str__(self):
34 return ' '.join(self.val)
35
36
37class InternalFunctionsTest(unittest.TestCase):
38
39 def test_format_optdict(self):
40 def check_against(fmt_opts, result):
41 for i in range(0, len(fmt_opts), 2):
Georg Brandlab91fde2009-08-13 08:51:18 +000042 self.assertEqual(result.pop(fmt_opts[i]), fmt_opts[i + 1])
Guilherme Polo5f238482009-01-28 14:41:10 +000043 if result:
44 self.fail("result still got elements: %s" % result)
45
46 # passing an empty dict should return an empty object (tuple here)
Georg Brandlab91fde2009-08-13 08:51:18 +000047 self.assertFalse(ttk._format_optdict({}))
Guilherme Polo5f238482009-01-28 14:41:10 +000048
49 # check list formatting
50 check_against(
51 ttk._format_optdict({'fg': 'blue', 'padding': [1, 2, 3, 4]}),
52 {'-fg': 'blue', '-padding': '1 2 3 4'})
53
54 # check tuple formatting (same as list)
55 check_against(
56 ttk._format_optdict({'test': (1, 2, '', 0)}),
57 {'-test': '1 2 {} 0'})
58
59 # check untouched values
60 check_against(
61 ttk._format_optdict({'test': {'left': 'as is'}}),
62 {'-test': {'left': 'as is'}})
63
Serhiy Storchakab1396522013-01-15 17:56:08 +020064 # check script formatting
Guilherme Polo5f238482009-01-28 14:41:10 +000065 check_against(
66 ttk._format_optdict(
Serhiy Storchakab1396522013-01-15 17:56:08 +020067 {'test': [1, -1, '', '2m', 0], 'test2': 3,
68 'test3': '', 'test4': 'abc def',
69 'test5': '"abc"', 'test6': '{}',
70 'test7': '} -spam {'}, script=True),
71 {'-test': '{1 -1 {} 2m 0}', '-test2': '3',
72 '-test3': '{}', '-test4': '{abc def}',
73 '-test5': '{"abc"}', '-test6': r'\{\}',
74 '-test7': r'\}\ -spam\ \{'})
Guilherme Polo5f238482009-01-28 14:41:10 +000075
76 opts = {'αβγ': True, 'á': False}
77 orig_opts = opts.copy()
78 # check if giving unicode keys is fine
79 check_against(ttk._format_optdict(opts), {'-αβγ': True, '-á': False})
80 # opts should remain unchanged
Georg Brandlab91fde2009-08-13 08:51:18 +000081 self.assertEqual(opts, orig_opts)
Guilherme Polo5f238482009-01-28 14:41:10 +000082
83 # passing values with spaces inside a tuple/list
84 check_against(
85 ttk._format_optdict(
86 {'option': ('one two', 'three')}),
87 {'-option': '{one two} three'})
Serhiy Storchakab1396522013-01-15 17:56:08 +020088 check_against(
89 ttk._format_optdict(
90 {'option': ('one\ttwo', 'three')}),
91 {'-option': '{one\ttwo} three'})
92
93 # passing empty strings inside a tuple/list
94 check_against(
95 ttk._format_optdict(
96 {'option': ('', 'one')}),
97 {'-option': '{} one'})
98
99 # passing values with braces inside a tuple/list
100 check_against(
101 ttk._format_optdict(
102 {'option': ('one} {two', 'three')}),
103 {'-option': r'one\}\ \{two three'})
104
105 # passing quoted strings inside a tuple/list
106 check_against(
107 ttk._format_optdict(
108 {'option': ('"one"', 'two')}),
109 {'-option': '{"one"} two'})
110 check_against(
111 ttk._format_optdict(
112 {'option': ('{one}', 'two')}),
113 {'-option': r'\{one\} two'})
Guilherme Polo5f238482009-01-28 14:41:10 +0000114
115 # ignore an option
116 amount_opts = len(ttk._format_optdict(opts, ignore=('á'))) / 2
Georg Brandlab91fde2009-08-13 08:51:18 +0000117 self.assertEqual(amount_opts, len(opts) - 1)
Guilherme Polo5f238482009-01-28 14:41:10 +0000118
119 # ignore non-existing options
120 amount_opts = len(ttk._format_optdict(opts, ignore=('á', 'b'))) / 2
Georg Brandlab91fde2009-08-13 08:51:18 +0000121 self.assertEqual(amount_opts, len(opts) - 1)
Guilherme Polo5f238482009-01-28 14:41:10 +0000122
123 # ignore every option
Georg Brandlab91fde2009-08-13 08:51:18 +0000124 self.assertFalse(ttk._format_optdict(opts, ignore=list(opts.keys())))
Guilherme Polo5f238482009-01-28 14:41:10 +0000125
126
127 def test_format_mapdict(self):
128 opts = {'a': [('b', 'c', 'val'), ('d', 'otherval'), ('', 'single')]}
129 result = ttk._format_mapdict(opts)
Georg Brandlab91fde2009-08-13 08:51:18 +0000130 self.assertEqual(len(result), len(list(opts.keys())) * 2)
131 self.assertEqual(result, ('-a', '{b c} val d otherval {} single'))
132 self.assertEqual(ttk._format_mapdict(opts, script=True),
Guilherme Polo5f238482009-01-28 14:41:10 +0000133 ('-a', '{{b c} val d otherval {} single}'))
134
Georg Brandlab91fde2009-08-13 08:51:18 +0000135 self.assertEqual(ttk._format_mapdict({2: []}), ('-2', ''))
Guilherme Polo5f238482009-01-28 14:41:10 +0000136
137 opts = {'üñíćódè': [('á', 'vãl')]}
138 result = ttk._format_mapdict(opts)
Georg Brandlab91fde2009-08-13 08:51:18 +0000139 self.assertEqual(result, ('-üñíćódè', 'á vãl'))
Guilherme Polo5f238482009-01-28 14:41:10 +0000140
141 # empty states
142 valid = {'opt': [('', '', 'hi')]}
Georg Brandlab91fde2009-08-13 08:51:18 +0000143 self.assertEqual(ttk._format_mapdict(valid), ('-opt', '{ } hi'))
Guilherme Polo5f238482009-01-28 14:41:10 +0000144
145 # when passing multiple states, they all must be strings
146 invalid = {'opt': [(1, 2, 'valid val')]}
Georg Brandlab91fde2009-08-13 08:51:18 +0000147 self.assertRaises(TypeError, ttk._format_mapdict, invalid)
Guilherme Polo5f238482009-01-28 14:41:10 +0000148 invalid = {'opt': [([1], '2', 'valid val')]}
Georg Brandlab91fde2009-08-13 08:51:18 +0000149 self.assertRaises(TypeError, ttk._format_mapdict, invalid)
Guilherme Polo5f238482009-01-28 14:41:10 +0000150 # but when passing a single state, it can be anything
151 valid = {'opt': [[1, 'value']]}
Georg Brandlab91fde2009-08-13 08:51:18 +0000152 self.assertEqual(ttk._format_mapdict(valid), ('-opt', '1 value'))
Guilherme Polo5f238482009-01-28 14:41:10 +0000153 # special attention to single states which evalute to False
154 for stateval in (None, 0, False, '', set()): # just some samples
155 valid = {'opt': [(stateval, 'value')]}
Georg Brandlab91fde2009-08-13 08:51:18 +0000156 self.assertEqual(ttk._format_mapdict(valid),
Guilherme Polo5f238482009-01-28 14:41:10 +0000157 ('-opt', '{} value'))
158
159 # values must be iterable
160 opts = {'a': None}
Georg Brandlab91fde2009-08-13 08:51:18 +0000161 self.assertRaises(TypeError, ttk._format_mapdict, opts)
Guilherme Polo5f238482009-01-28 14:41:10 +0000162
163 # items in the value must have size >= 2
Georg Brandlab91fde2009-08-13 08:51:18 +0000164 self.assertRaises(IndexError, ttk._format_mapdict,
Guilherme Polo5f238482009-01-28 14:41:10 +0000165 {'a': [('invalid', )]})
166
167
168 def test_format_elemcreate(self):
Georg Brandlab91fde2009-08-13 08:51:18 +0000169 self.assertTrue(ttk._format_elemcreate(None), (None, ()))
Guilherme Polo5f238482009-01-28 14:41:10 +0000170
171 ## Testing type = image
172 # image type expects at least an image name, so this should raise
173 # IndexError since it tries to access the index 0 of an empty tuple
Georg Brandlab91fde2009-08-13 08:51:18 +0000174 self.assertRaises(IndexError, ttk._format_elemcreate, 'image')
Guilherme Polo5f238482009-01-28 14:41:10 +0000175
176 # don't format returned values as a tcl script
177 # minimum acceptable for image type
Georg Brandlab91fde2009-08-13 08:51:18 +0000178 self.assertEqual(ttk._format_elemcreate('image', False, 'test'),
Guilherme Polo5f238482009-01-28 14:41:10 +0000179 ("test ", ()))
Ezio Melotti13925002011-03-16 11:05:33 +0200180 # specifying a state spec
Georg Brandlab91fde2009-08-13 08:51:18 +0000181 self.assertEqual(ttk._format_elemcreate('image', False, 'test',
Guilherme Polo5f238482009-01-28 14:41:10 +0000182 ('', 'a')), ("test {} a", ()))
183 # state spec with multiple states
Georg Brandlab91fde2009-08-13 08:51:18 +0000184 self.assertEqual(ttk._format_elemcreate('image', False, 'test',
Guilherme Polo5f238482009-01-28 14:41:10 +0000185 ('a', 'b', 'c')), ("test {a b} c", ()))
186 # state spec and options
Georg Brandlab91fde2009-08-13 08:51:18 +0000187 self.assertEqual(ttk._format_elemcreate('image', False, 'test',
Georg Brandl2daf6ae2012-02-20 19:54:16 +0100188 ('a', 'b'), a='x'), ("test a b", ("-a", "x")))
Guilherme Polo5f238482009-01-28 14:41:10 +0000189 # format returned values as a tcl script
190 # state spec with multiple states and an option with a multivalue
Georg Brandlab91fde2009-08-13 08:51:18 +0000191 self.assertEqual(ttk._format_elemcreate('image', True, 'test',
Guilherme Polo5f238482009-01-28 14:41:10 +0000192 ('a', 'b', 'c', 'd'), x=[2, 3]), ("{test {a b c} d}", "-x {2 3}"))
193
194 ## Testing type = vsapi
195 # vsapi type expects at least a class name and a part_id, so this
Serhiy Storchaka6a7b3a72016-04-17 08:32:47 +0300196 # should raise a ValueError since it tries to get two elements from
Guilherme Polo5f238482009-01-28 14:41:10 +0000197 # an empty tuple
Georg Brandlab91fde2009-08-13 08:51:18 +0000198 self.assertRaises(ValueError, ttk._format_elemcreate, 'vsapi')
Guilherme Polo5f238482009-01-28 14:41:10 +0000199
200 # don't format returned values as a tcl script
201 # minimum acceptable for vsapi
Georg Brandlab91fde2009-08-13 08:51:18 +0000202 self.assertEqual(ttk._format_elemcreate('vsapi', False, 'a', 'b'),
Guilherme Polo5f238482009-01-28 14:41:10 +0000203 ("a b ", ()))
204 # now with a state spec with multiple states
Georg Brandlab91fde2009-08-13 08:51:18 +0000205 self.assertEqual(ttk._format_elemcreate('vsapi', False, 'a', 'b',
Guilherme Polo5f238482009-01-28 14:41:10 +0000206 ('a', 'b', 'c')), ("a b {a b} c", ()))
207 # state spec and option
Georg Brandlab91fde2009-08-13 08:51:18 +0000208 self.assertEqual(ttk._format_elemcreate('vsapi', False, 'a', 'b',
Guilherme Polo5f238482009-01-28 14:41:10 +0000209 ('a', 'b'), opt='x'), ("a b a b", ("-opt", "x")))
210 # format returned values as a tcl script
211 # state spec with a multivalue and an option
Georg Brandlab91fde2009-08-13 08:51:18 +0000212 self.assertEqual(ttk._format_elemcreate('vsapi', True, 'a', 'b',
Guilherme Polo5f238482009-01-28 14:41:10 +0000213 ('a', 'b', [1, 2]), opt='x'), ("{a b {a b} {1 2}}", "-opt x"))
214
215 # Testing type = from
216 # from type expects at least a type name
Georg Brandlab91fde2009-08-13 08:51:18 +0000217 self.assertRaises(IndexError, ttk._format_elemcreate, 'from')
Guilherme Polo5f238482009-01-28 14:41:10 +0000218
Georg Brandlab91fde2009-08-13 08:51:18 +0000219 self.assertEqual(ttk._format_elemcreate('from', False, 'a'),
Guilherme Polo5f238482009-01-28 14:41:10 +0000220 ('a', ()))
Georg Brandlab91fde2009-08-13 08:51:18 +0000221 self.assertEqual(ttk._format_elemcreate('from', False, 'a', 'b'),
Guilherme Polo5f238482009-01-28 14:41:10 +0000222 ('a', ('b', )))
Georg Brandlab91fde2009-08-13 08:51:18 +0000223 self.assertEqual(ttk._format_elemcreate('from', True, 'a', 'b'),
Guilherme Polo5f238482009-01-28 14:41:10 +0000224 ('{a}', 'b'))
225
226
227 def test_format_layoutlist(self):
228 def sample(indent=0, indent_size=2):
229 return ttk._format_layoutlist(
230 [('a', {'other': [1, 2, 3], 'children':
231 [('b', {'children':
232 [('c', {'children':
233 [('d', {'nice': 'opt'})], 'something': (1, 2)
234 })]
235 })]
236 })], indent=indent, indent_size=indent_size)[0]
237
238 def sample_expected(indent=0, indent_size=2):
239 spaces = lambda amount=0: ' ' * (amount + indent)
240 return (
241 "%sa -other {1 2 3} -children {\n"
242 "%sb -children {\n"
243 "%sc -something {1 2} -children {\n"
244 "%sd -nice opt\n"
245 "%s}\n"
246 "%s}\n"
247 "%s}" % (spaces(), spaces(indent_size),
248 spaces(2 * indent_size), spaces(3 * indent_size),
249 spaces(2 * indent_size), spaces(indent_size), spaces()))
250
251 # empty layout
Georg Brandlab91fde2009-08-13 08:51:18 +0000252 self.assertEqual(ttk._format_layoutlist([])[0], '')
Guilherme Polo5f238482009-01-28 14:41:10 +0000253
254 # _format_layoutlist always expects the second item (in every item)
255 # to act like a dict (except when the value evalutes to False).
Georg Brandlab91fde2009-08-13 08:51:18 +0000256 self.assertRaises(AttributeError,
Guilherme Polo5f238482009-01-28 14:41:10 +0000257 ttk._format_layoutlist, [('a', 'b')])
258
259 smallest = ttk._format_layoutlist([('a', None)], indent=0)
Georg Brandlab91fde2009-08-13 08:51:18 +0000260 self.assertEqual(smallest,
Guilherme Polo5f238482009-01-28 14:41:10 +0000261 ttk._format_layoutlist([('a', '')], indent=0))
Georg Brandlab91fde2009-08-13 08:51:18 +0000262 self.assertEqual(smallest[0], 'a')
Guilherme Polo5f238482009-01-28 14:41:10 +0000263
264 # testing indentation levels
Georg Brandlab91fde2009-08-13 08:51:18 +0000265 self.assertEqual(sample(), sample_expected())
Guilherme Polo5f238482009-01-28 14:41:10 +0000266 for i in range(4):
Georg Brandlab91fde2009-08-13 08:51:18 +0000267 self.assertEqual(sample(i), sample_expected(i))
268 self.assertEqual(sample(i, i), sample_expected(i, i))
Guilherme Polo5f238482009-01-28 14:41:10 +0000269
270 # invalid layout format, different kind of exceptions will be
271 # raised by internal functions
272
273 # plain wrong format
Georg Brandlab91fde2009-08-13 08:51:18 +0000274 self.assertRaises(ValueError, ttk._format_layoutlist,
Guilherme Polo5f238482009-01-28 14:41:10 +0000275 ['bad', 'format'])
276 # will try to use iteritems in the 'bad' string
Georg Brandlab91fde2009-08-13 08:51:18 +0000277 self.assertRaises(AttributeError, ttk._format_layoutlist,
Guilherme Polo5f238482009-01-28 14:41:10 +0000278 [('name', 'bad')])
279 # bad children formatting
Georg Brandlab91fde2009-08-13 08:51:18 +0000280 self.assertRaises(ValueError, ttk._format_layoutlist,
Guilherme Polo5f238482009-01-28 14:41:10 +0000281 [('name', {'children': {'a': None}})])
282
283
284 def test_script_from_settings(self):
285 # empty options
Georg Brandlab91fde2009-08-13 08:51:18 +0000286 self.assertFalse(ttk._script_from_settings({'name':
Guilherme Polo5f238482009-01-28 14:41:10 +0000287 {'configure': None, 'map': None, 'element create': None}}))
288
289 # empty layout
Georg Brandlab91fde2009-08-13 08:51:18 +0000290 self.assertEqual(
Guilherme Polo5f238482009-01-28 14:41:10 +0000291 ttk._script_from_settings({'name': {'layout': None}}),
292 "ttk::style layout name {\nnull\n}")
293
294 configdict = {'αβγ': True, 'á': False}
Georg Brandlab91fde2009-08-13 08:51:18 +0000295 self.assertTrue(
Guilherme Polo5f238482009-01-28 14:41:10 +0000296 ttk._script_from_settings({'name': {'configure': configdict}}))
297
298 mapdict = {'üñíćódè': [('á', 'vãl')]}
Georg Brandlab91fde2009-08-13 08:51:18 +0000299 self.assertTrue(
Guilherme Polo5f238482009-01-28 14:41:10 +0000300 ttk._script_from_settings({'name': {'map': mapdict}}))
301
302 # invalid image element
Georg Brandlab91fde2009-08-13 08:51:18 +0000303 self.assertRaises(IndexError,
Guilherme Polo5f238482009-01-28 14:41:10 +0000304 ttk._script_from_settings, {'name': {'element create': ['image']}})
305
306 # minimal valid image
Georg Brandlab91fde2009-08-13 08:51:18 +0000307 self.assertTrue(ttk._script_from_settings({'name':
Guilherme Polo5f238482009-01-28 14:41:10 +0000308 {'element create': ['image', 'name']}}))
309
310 image = {'thing': {'element create':
311 ['image', 'name', ('state1', 'state2', 'val')]}}
Georg Brandlab91fde2009-08-13 08:51:18 +0000312 self.assertEqual(ttk._script_from_settings(image),
Guilherme Polo5f238482009-01-28 14:41:10 +0000313 "ttk::style element create thing image {name {state1 state2} val} ")
314
315 image['thing']['element create'].append({'opt': 30})
Georg Brandlab91fde2009-08-13 08:51:18 +0000316 self.assertEqual(ttk._script_from_settings(image),
Guilherme Polo5f238482009-01-28 14:41:10 +0000317 "ttk::style element create thing image {name {state1 state2} val} "
318 "-opt 30")
319
320 image['thing']['element create'][-1]['opt'] = [MockTclObj(3),
321 MockTclObj('2m')]
Georg Brandlab91fde2009-08-13 08:51:18 +0000322 self.assertEqual(ttk._script_from_settings(image),
Guilherme Polo5f238482009-01-28 14:41:10 +0000323 "ttk::style element create thing image {name {state1 state2} val} "
324 "-opt {3 2m}")
325
326
Serhiy Storchaka8f0a1d02014-09-06 22:47:58 +0300327 def test_tclobj_to_py(self):
328 self.assertEqual(
329 ttk._tclobj_to_py((MockStateSpec('a', 'b'), 'val')),
330 [('a', 'b', 'val')])
331 self.assertEqual(
332 ttk._tclobj_to_py([MockTclObj('1'), 2, MockTclObj('3m')]),
333 [1, 2, '3m'])
Guilherme Polo5f238482009-01-28 14:41:10 +0000334
335
336 def test_list_from_statespec(self):
337 def test_it(sspec, value, res_value, states):
Georg Brandlab91fde2009-08-13 08:51:18 +0000338 self.assertEqual(ttk._list_from_statespec(
Guilherme Polo5f238482009-01-28 14:41:10 +0000339 (sspec, value)), [states + (res_value, )])
340
341 states_even = tuple('state%d' % i for i in range(6))
342 statespec = MockStateSpec(*states_even)
343 test_it(statespec, 'val', 'val', states_even)
344 test_it(statespec, MockTclObj('val'), 'val', states_even)
345
346 states_odd = tuple('state%d' % i for i in range(5))
347 statespec = MockStateSpec(*states_odd)
348 test_it(statespec, 'val', 'val', states_odd)
349
350 test_it(('a', 'b', 'c'), MockTclObj('val'), 'val', ('a', 'b', 'c'))
351
352
353 def test_list_from_layouttuple(self):
Serhiy Storchakab49eff22014-05-28 18:38:27 +0300354 tk = MockTkApp()
355
Guilherme Polo5f238482009-01-28 14:41:10 +0000356 # empty layout tuple
Serhiy Storchakab49eff22014-05-28 18:38:27 +0300357 self.assertFalse(ttk._list_from_layouttuple(tk, ()))
Guilherme Polo5f238482009-01-28 14:41:10 +0000358
359 # shortest layout tuple
Serhiy Storchakab49eff22014-05-28 18:38:27 +0300360 self.assertEqual(ttk._list_from_layouttuple(tk, ('name', )),
Guilherme Polo5f238482009-01-28 14:41:10 +0000361 [('name', {})])
362
363 # not so interesting ltuple
364 sample_ltuple = ('name', '-option', 'value')
Serhiy Storchakab49eff22014-05-28 18:38:27 +0300365 self.assertEqual(ttk._list_from_layouttuple(tk, sample_ltuple),
Guilherme Polo5f238482009-01-28 14:41:10 +0000366 [('name', {'option': 'value'})])
367
368 # empty children
Serhiy Storchakab49eff22014-05-28 18:38:27 +0300369 self.assertEqual(ttk._list_from_layouttuple(tk,
Guilherme Polo5f238482009-01-28 14:41:10 +0000370 ('something', '-children', ())),
371 [('something', {'children': []})]
372 )
373
374 # more interesting ltuple
375 ltuple = (
376 'name', '-option', 'niceone', '-children', (
377 ('otherone', '-children', (
378 ('child', )), '-otheropt', 'othervalue'
379 )
380 )
381 )
Serhiy Storchakab49eff22014-05-28 18:38:27 +0300382 self.assertEqual(ttk._list_from_layouttuple(tk, ltuple),
Guilherme Polo5f238482009-01-28 14:41:10 +0000383 [('name', {'option': 'niceone', 'children':
384 [('otherone', {'otheropt': 'othervalue', 'children':
385 [('child', {})]
386 })]
387 })]
388 )
389
390 # bad tuples
Serhiy Storchakab49eff22014-05-28 18:38:27 +0300391 self.assertRaises(ValueError, ttk._list_from_layouttuple, tk,
Guilherme Polo5f238482009-01-28 14:41:10 +0000392 ('name', 'no_minus'))
Serhiy Storchakab49eff22014-05-28 18:38:27 +0300393 self.assertRaises(ValueError, ttk._list_from_layouttuple, tk,
Guilherme Polo5f238482009-01-28 14:41:10 +0000394 ('name', 'no_minus', 'value'))
Serhiy Storchakab49eff22014-05-28 18:38:27 +0300395 self.assertRaises(ValueError, ttk._list_from_layouttuple, tk,
Guilherme Polo5f238482009-01-28 14:41:10 +0000396 ('something', '-children')) # no children
Guilherme Polo5f238482009-01-28 14:41:10 +0000397
398
399 def test_val_or_dict(self):
Serhiy Storchakab49eff22014-05-28 18:38:27 +0300400 def func(res, opt=None, val=None):
401 if opt is None:
402 return res
Guilherme Polo5f238482009-01-28 14:41:10 +0000403 if val is None:
404 return "test val"
405 return (opt, val)
406
Serhiy Storchakab49eff22014-05-28 18:38:27 +0300407 tk = MockTkApp()
408 tk.call = func
Guilherme Polo5f238482009-01-28 14:41:10 +0000409
Serhiy Storchakab49eff22014-05-28 18:38:27 +0300410 self.assertEqual(ttk._val_or_dict(tk, {}, '-test:3'),
411 {'test': '3'})
412 self.assertEqual(ttk._val_or_dict(tk, {}, ('-test', 3)),
413 {'test': 3})
414
415 self.assertEqual(ttk._val_or_dict(tk, {'test': None}, 'x:y'),
416 'test val')
417
418 self.assertEqual(ttk._val_or_dict(tk, {'test': 3}, 'x:y'),
419 {'test': 3})
Guilherme Polo5f238482009-01-28 14:41:10 +0000420
421
422 def test_convert_stringval(self):
423 tests = (
424 (0, 0), ('09', 9), ('a', 'a'), ('áÚ', 'áÚ'), ([], '[]'),
425 (None, 'None')
426 )
427 for orig, expected in tests:
Georg Brandlab91fde2009-08-13 08:51:18 +0000428 self.assertEqual(ttk._convert_stringval(orig), expected)
Guilherme Polo5f238482009-01-28 14:41:10 +0000429
430
431class TclObjsToPyTest(unittest.TestCase):
432
433 def test_unicode(self):
434 adict = {'opt': 'välúè'}
Georg Brandlab91fde2009-08-13 08:51:18 +0000435 self.assertEqual(ttk.tclobjs_to_py(adict), {'opt': 'välúè'})
Guilherme Polo5f238482009-01-28 14:41:10 +0000436
437 adict['opt'] = MockTclObj(adict['opt'])
Georg Brandlab91fde2009-08-13 08:51:18 +0000438 self.assertEqual(ttk.tclobjs_to_py(adict), {'opt': 'välúè'})
Guilherme Polo5f238482009-01-28 14:41:10 +0000439
440 def test_multivalues(self):
441 adict = {'opt': [1, 2, 3, 4]}
Georg Brandlab91fde2009-08-13 08:51:18 +0000442 self.assertEqual(ttk.tclobjs_to_py(adict), {'opt': [1, 2, 3, 4]})
Guilherme Polo5f238482009-01-28 14:41:10 +0000443
444 adict['opt'] = [1, 'xm', 3]
Georg Brandlab91fde2009-08-13 08:51:18 +0000445 self.assertEqual(ttk.tclobjs_to_py(adict), {'opt': [1, 'xm', 3]})
Guilherme Polo5f238482009-01-28 14:41:10 +0000446
447 adict['opt'] = (MockStateSpec('a', 'b'), 'válũè')
Georg Brandlab91fde2009-08-13 08:51:18 +0000448 self.assertEqual(ttk.tclobjs_to_py(adict),
Guilherme Polo5f238482009-01-28 14:41:10 +0000449 {'opt': [('a', 'b', 'válũè')]})
450
Georg Brandlab91fde2009-08-13 08:51:18 +0000451 self.assertEqual(ttk.tclobjs_to_py({'x': ['y z']}),
Guilherme Polo5f238482009-01-28 14:41:10 +0000452 {'x': ['y z']})
453
454 def test_nosplit(self):
Georg Brandlab91fde2009-08-13 08:51:18 +0000455 self.assertEqual(ttk.tclobjs_to_py({'text': 'some text'}),
Guilherme Polo5f238482009-01-28 14:41:10 +0000456 {'text': 'some text'})
457
458tests_nogui = (InternalFunctionsTest, TclObjsToPyTest)
459
460if __name__ == "__main__":
461 from test.support import run_unittest
462 run_unittest(*tests_nogui)