| 1 | n/a | # -*- encoding: utf-8 -*- |
|---|
| 2 | n/a | import unittest |
|---|
| 3 | n/a | from tkinter import ttk |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | class MockTkApp: |
|---|
| 6 | n/a | |
|---|
| 7 | n/a | def splitlist(self, arg): |
|---|
| 8 | n/a | if isinstance(arg, tuple): |
|---|
| 9 | n/a | return arg |
|---|
| 10 | n/a | return arg.split(':') |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | def wantobjects(self): |
|---|
| 13 | n/a | return True |
|---|
| 14 | n/a | |
|---|
| 15 | n/a | |
|---|
| 16 | n/a | class MockTclObj(object): |
|---|
| 17 | n/a | typename = 'test' |
|---|
| 18 | n/a | |
|---|
| 19 | n/a | def __init__(self, val): |
|---|
| 20 | n/a | self.val = val |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | def __str__(self): |
|---|
| 23 | n/a | return str(self.val) |
|---|
| 24 | n/a | |
|---|
| 25 | n/a | |
|---|
| 26 | n/a | class MockStateSpec(object): |
|---|
| 27 | n/a | typename = 'StateSpec' |
|---|
| 28 | n/a | |
|---|
| 29 | n/a | def __init__(self, *args): |
|---|
| 30 | n/a | self.val = args |
|---|
| 31 | n/a | |
|---|
| 32 | n/a | def __str__(self): |
|---|
| 33 | n/a | return ' '.join(self.val) |
|---|
| 34 | n/a | |
|---|
| 35 | n/a | |
|---|
| 36 | n/a | class InternalFunctionsTest(unittest.TestCase): |
|---|
| 37 | n/a | |
|---|
| 38 | n/a | def test_format_optdict(self): |
|---|
| 39 | n/a | def check_against(fmt_opts, result): |
|---|
| 40 | n/a | for i in range(0, len(fmt_opts), 2): |
|---|
| 41 | n/a | self.assertEqual(result.pop(fmt_opts[i]), fmt_opts[i + 1]) |
|---|
| 42 | n/a | if result: |
|---|
| 43 | n/a | self.fail("result still got elements: %s" % result) |
|---|
| 44 | n/a | |
|---|
| 45 | n/a | # passing an empty dict should return an empty object (tuple here) |
|---|
| 46 | n/a | self.assertFalse(ttk._format_optdict({})) |
|---|
| 47 | n/a | |
|---|
| 48 | n/a | # check list formatting |
|---|
| 49 | n/a | check_against( |
|---|
| 50 | n/a | ttk._format_optdict({'fg': 'blue', 'padding': [1, 2, 3, 4]}), |
|---|
| 51 | n/a | {'-fg': 'blue', '-padding': '1 2 3 4'}) |
|---|
| 52 | n/a | |
|---|
| 53 | n/a | # check tuple formatting (same as list) |
|---|
| 54 | n/a | check_against( |
|---|
| 55 | n/a | ttk._format_optdict({'test': (1, 2, '', 0)}), |
|---|
| 56 | n/a | {'-test': '1 2 {} 0'}) |
|---|
| 57 | n/a | |
|---|
| 58 | n/a | # check untouched values |
|---|
| 59 | n/a | check_against( |
|---|
| 60 | n/a | ttk._format_optdict({'test': {'left': 'as is'}}), |
|---|
| 61 | n/a | {'-test': {'left': 'as is'}}) |
|---|
| 62 | n/a | |
|---|
| 63 | n/a | # check script formatting |
|---|
| 64 | n/a | check_against( |
|---|
| 65 | n/a | ttk._format_optdict( |
|---|
| 66 | n/a | {'test': [1, -1, '', '2m', 0], 'test2': 3, |
|---|
| 67 | n/a | 'test3': '', 'test4': 'abc def', |
|---|
| 68 | n/a | 'test5': '"abc"', 'test6': '{}', |
|---|
| 69 | n/a | 'test7': '} -spam {'}, script=True), |
|---|
| 70 | n/a | {'-test': '{1 -1 {} 2m 0}', '-test2': '3', |
|---|
| 71 | n/a | '-test3': '{}', '-test4': '{abc def}', |
|---|
| 72 | n/a | '-test5': '{"abc"}', '-test6': r'\{\}', |
|---|
| 73 | n/a | '-test7': r'\}\ -spam\ \{'}) |
|---|
| 74 | n/a | |
|---|
| 75 | n/a | opts = {'αβγ': True, 'á': False} |
|---|
| 76 | n/a | orig_opts = opts.copy() |
|---|
| 77 | n/a | # check if giving unicode keys is fine |
|---|
| 78 | n/a | check_against(ttk._format_optdict(opts), {'-αβγ': True, '-á': False}) |
|---|
| 79 | n/a | # opts should remain unchanged |
|---|
| 80 | n/a | self.assertEqual(opts, orig_opts) |
|---|
| 81 | n/a | |
|---|
| 82 | n/a | # passing values with spaces inside a tuple/list |
|---|
| 83 | n/a | check_against( |
|---|
| 84 | n/a | ttk._format_optdict( |
|---|
| 85 | n/a | {'option': ('one two', 'three')}), |
|---|
| 86 | n/a | {'-option': '{one two} three'}) |
|---|
| 87 | n/a | check_against( |
|---|
| 88 | n/a | ttk._format_optdict( |
|---|
| 89 | n/a | {'option': ('one\ttwo', 'three')}), |
|---|
| 90 | n/a | {'-option': '{one\ttwo} three'}) |
|---|
| 91 | n/a | |
|---|
| 92 | n/a | # passing empty strings inside a tuple/list |
|---|
| 93 | n/a | check_against( |
|---|
| 94 | n/a | ttk._format_optdict( |
|---|
| 95 | n/a | {'option': ('', 'one')}), |
|---|
| 96 | n/a | {'-option': '{} one'}) |
|---|
| 97 | n/a | |
|---|
| 98 | n/a | # passing values with braces inside a tuple/list |
|---|
| 99 | n/a | check_against( |
|---|
| 100 | n/a | ttk._format_optdict( |
|---|
| 101 | n/a | {'option': ('one} {two', 'three')}), |
|---|
| 102 | n/a | {'-option': r'one\}\ \{two three'}) |
|---|
| 103 | n/a | |
|---|
| 104 | n/a | # passing quoted strings inside a tuple/list |
|---|
| 105 | n/a | check_against( |
|---|
| 106 | n/a | ttk._format_optdict( |
|---|
| 107 | n/a | {'option': ('"one"', 'two')}), |
|---|
| 108 | n/a | {'-option': '{"one"} two'}) |
|---|
| 109 | n/a | check_against( |
|---|
| 110 | n/a | ttk._format_optdict( |
|---|
| 111 | n/a | {'option': ('{one}', 'two')}), |
|---|
| 112 | n/a | {'-option': r'\{one\} two'}) |
|---|
| 113 | n/a | |
|---|
| 114 | n/a | # ignore an option |
|---|
| 115 | n/a | amount_opts = len(ttk._format_optdict(opts, ignore=('á'))) / 2 |
|---|
| 116 | n/a | self.assertEqual(amount_opts, len(opts) - 1) |
|---|
| 117 | n/a | |
|---|
| 118 | n/a | # ignore non-existing options |
|---|
| 119 | n/a | amount_opts = len(ttk._format_optdict(opts, ignore=('á', 'b'))) / 2 |
|---|
| 120 | n/a | self.assertEqual(amount_opts, len(opts) - 1) |
|---|
| 121 | n/a | |
|---|
| 122 | n/a | # ignore every option |
|---|
| 123 | n/a | self.assertFalse(ttk._format_optdict(opts, ignore=list(opts.keys()))) |
|---|
| 124 | n/a | |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | def test_format_mapdict(self): |
|---|
| 127 | n/a | opts = {'a': [('b', 'c', 'val'), ('d', 'otherval'), ('', 'single')]} |
|---|
| 128 | n/a | result = ttk._format_mapdict(opts) |
|---|
| 129 | n/a | self.assertEqual(len(result), len(list(opts.keys())) * 2) |
|---|
| 130 | n/a | self.assertEqual(result, ('-a', '{b c} val d otherval {} single')) |
|---|
| 131 | n/a | self.assertEqual(ttk._format_mapdict(opts, script=True), |
|---|
| 132 | n/a | ('-a', '{{b c} val d otherval {} single}')) |
|---|
| 133 | n/a | |
|---|
| 134 | n/a | self.assertEqual(ttk._format_mapdict({2: []}), ('-2', '')) |
|---|
| 135 | n/a | |
|---|
| 136 | n/a | opts = {'üñÃÄódè': [('á', 'vãl')]} |
|---|
| 137 | n/a | result = ttk._format_mapdict(opts) |
|---|
| 138 | n/a | self.assertEqual(result, ('-üñÃÄódè', 'á vãl')) |
|---|
| 139 | n/a | |
|---|
| 140 | n/a | # empty states |
|---|
| 141 | n/a | valid = {'opt': [('', '', 'hi')]} |
|---|
| 142 | n/a | self.assertEqual(ttk._format_mapdict(valid), ('-opt', '{ } hi')) |
|---|
| 143 | n/a | |
|---|
| 144 | n/a | # when passing multiple states, they all must be strings |
|---|
| 145 | n/a | invalid = {'opt': [(1, 2, 'valid val')]} |
|---|
| 146 | n/a | self.assertRaises(TypeError, ttk._format_mapdict, invalid) |
|---|
| 147 | n/a | invalid = {'opt': [([1], '2', 'valid val')]} |
|---|
| 148 | n/a | self.assertRaises(TypeError, ttk._format_mapdict, invalid) |
|---|
| 149 | n/a | # but when passing a single state, it can be anything |
|---|
| 150 | n/a | valid = {'opt': [[1, 'value']]} |
|---|
| 151 | n/a | self.assertEqual(ttk._format_mapdict(valid), ('-opt', '1 value')) |
|---|
| 152 | n/a | # special attention to single states which evalute to False |
|---|
| 153 | n/a | for stateval in (None, 0, False, '', set()): # just some samples |
|---|
| 154 | n/a | valid = {'opt': [(stateval, 'value')]} |
|---|
| 155 | n/a | self.assertEqual(ttk._format_mapdict(valid), |
|---|
| 156 | n/a | ('-opt', '{} value')) |
|---|
| 157 | n/a | |
|---|
| 158 | n/a | # values must be iterable |
|---|
| 159 | n/a | opts = {'a': None} |
|---|
| 160 | n/a | self.assertRaises(TypeError, ttk._format_mapdict, opts) |
|---|
| 161 | n/a | |
|---|
| 162 | n/a | # items in the value must have size >= 2 |
|---|
| 163 | n/a | self.assertRaises(IndexError, ttk._format_mapdict, |
|---|
| 164 | n/a | {'a': [('invalid', )]}) |
|---|
| 165 | n/a | |
|---|
| 166 | n/a | |
|---|
| 167 | n/a | def test_format_elemcreate(self): |
|---|
| 168 | n/a | self.assertTrue(ttk._format_elemcreate(None), (None, ())) |
|---|
| 169 | n/a | |
|---|
| 170 | n/a | ## Testing type = image |
|---|
| 171 | n/a | # image type expects at least an image name, so this should raise |
|---|
| 172 | n/a | # IndexError since it tries to access the index 0 of an empty tuple |
|---|
| 173 | n/a | self.assertRaises(IndexError, ttk._format_elemcreate, 'image') |
|---|
| 174 | n/a | |
|---|
| 175 | n/a | # don't format returned values as a tcl script |
|---|
| 176 | n/a | # minimum acceptable for image type |
|---|
| 177 | n/a | self.assertEqual(ttk._format_elemcreate('image', False, 'test'), |
|---|
| 178 | n/a | ("test ", ())) |
|---|
| 179 | n/a | # specifying a state spec |
|---|
| 180 | n/a | self.assertEqual(ttk._format_elemcreate('image', False, 'test', |
|---|
| 181 | n/a | ('', 'a')), ("test {} a", ())) |
|---|
| 182 | n/a | # state spec with multiple states |
|---|
| 183 | n/a | self.assertEqual(ttk._format_elemcreate('image', False, 'test', |
|---|
| 184 | n/a | ('a', 'b', 'c')), ("test {a b} c", ())) |
|---|
| 185 | n/a | # state spec and options |
|---|
| 186 | n/a | self.assertEqual(ttk._format_elemcreate('image', False, 'test', |
|---|
| 187 | n/a | ('a', 'b'), a='x'), ("test a b", ("-a", "x"))) |
|---|
| 188 | n/a | # format returned values as a tcl script |
|---|
| 189 | n/a | # state spec with multiple states and an option with a multivalue |
|---|
| 190 | n/a | self.assertEqual(ttk._format_elemcreate('image', True, 'test', |
|---|
| 191 | n/a | ('a', 'b', 'c', 'd'), x=[2, 3]), ("{test {a b c} d}", "-x {2 3}")) |
|---|
| 192 | n/a | |
|---|
| 193 | n/a | ## Testing type = vsapi |
|---|
| 194 | n/a | # vsapi type expects at least a class name and a part_id, so this |
|---|
| 195 | n/a | # should raise a ValueError since it tries to get two elements from |
|---|
| 196 | n/a | # an empty tuple |
|---|
| 197 | n/a | self.assertRaises(ValueError, ttk._format_elemcreate, 'vsapi') |
|---|
| 198 | n/a | |
|---|
| 199 | n/a | # don't format returned values as a tcl script |
|---|
| 200 | n/a | # minimum acceptable for vsapi |
|---|
| 201 | n/a | self.assertEqual(ttk._format_elemcreate('vsapi', False, 'a', 'b'), |
|---|
| 202 | n/a | ("a b ", ())) |
|---|
| 203 | n/a | # now with a state spec with multiple states |
|---|
| 204 | n/a | self.assertEqual(ttk._format_elemcreate('vsapi', False, 'a', 'b', |
|---|
| 205 | n/a | ('a', 'b', 'c')), ("a b {a b} c", ())) |
|---|
| 206 | n/a | # state spec and option |
|---|
| 207 | n/a | self.assertEqual(ttk._format_elemcreate('vsapi', False, 'a', 'b', |
|---|
| 208 | n/a | ('a', 'b'), opt='x'), ("a b a b", ("-opt", "x"))) |
|---|
| 209 | n/a | # format returned values as a tcl script |
|---|
| 210 | n/a | # state spec with a multivalue and an option |
|---|
| 211 | n/a | self.assertEqual(ttk._format_elemcreate('vsapi', True, 'a', 'b', |
|---|
| 212 | n/a | ('a', 'b', [1, 2]), opt='x'), ("{a b {a b} {1 2}}", "-opt x")) |
|---|
| 213 | n/a | |
|---|
| 214 | n/a | # Testing type = from |
|---|
| 215 | n/a | # from type expects at least a type name |
|---|
| 216 | n/a | self.assertRaises(IndexError, ttk._format_elemcreate, 'from') |
|---|
| 217 | n/a | |
|---|
| 218 | n/a | self.assertEqual(ttk._format_elemcreate('from', False, 'a'), |
|---|
| 219 | n/a | ('a', ())) |
|---|
| 220 | n/a | self.assertEqual(ttk._format_elemcreate('from', False, 'a', 'b'), |
|---|
| 221 | n/a | ('a', ('b', ))) |
|---|
| 222 | n/a | self.assertEqual(ttk._format_elemcreate('from', True, 'a', 'b'), |
|---|
| 223 | n/a | ('{a}', 'b')) |
|---|
| 224 | n/a | |
|---|
| 225 | n/a | |
|---|
| 226 | n/a | def test_format_layoutlist(self): |
|---|
| 227 | n/a | def sample(indent=0, indent_size=2): |
|---|
| 228 | n/a | return ttk._format_layoutlist( |
|---|
| 229 | n/a | [('a', {'other': [1, 2, 3], 'children': |
|---|
| 230 | n/a | [('b', {'children': |
|---|
| 231 | n/a | [('c', {'children': |
|---|
| 232 | n/a | [('d', {'nice': 'opt'})], 'something': (1, 2) |
|---|
| 233 | n/a | })] |
|---|
| 234 | n/a | })] |
|---|
| 235 | n/a | })], indent=indent, indent_size=indent_size)[0] |
|---|
| 236 | n/a | |
|---|
| 237 | n/a | def sample_expected(indent=0, indent_size=2): |
|---|
| 238 | n/a | spaces = lambda amount=0: ' ' * (amount + indent) |
|---|
| 239 | n/a | return ( |
|---|
| 240 | n/a | "%sa -other {1 2 3} -children {\n" |
|---|
| 241 | n/a | "%sb -children {\n" |
|---|
| 242 | n/a | "%sc -something {1 2} -children {\n" |
|---|
| 243 | n/a | "%sd -nice opt\n" |
|---|
| 244 | n/a | "%s}\n" |
|---|
| 245 | n/a | "%s}\n" |
|---|
| 246 | n/a | "%s}" % (spaces(), spaces(indent_size), |
|---|
| 247 | n/a | spaces(2 * indent_size), spaces(3 * indent_size), |
|---|
| 248 | n/a | spaces(2 * indent_size), spaces(indent_size), spaces())) |
|---|
| 249 | n/a | |
|---|
| 250 | n/a | # empty layout |
|---|
| 251 | n/a | self.assertEqual(ttk._format_layoutlist([])[0], '') |
|---|
| 252 | n/a | |
|---|
| 253 | n/a | # _format_layoutlist always expects the second item (in every item) |
|---|
| 254 | n/a | # to act like a dict (except when the value evalutes to False). |
|---|
| 255 | n/a | self.assertRaises(AttributeError, |
|---|
| 256 | n/a | ttk._format_layoutlist, [('a', 'b')]) |
|---|
| 257 | n/a | |
|---|
| 258 | n/a | smallest = ttk._format_layoutlist([('a', None)], indent=0) |
|---|
| 259 | n/a | self.assertEqual(smallest, |
|---|
| 260 | n/a | ttk._format_layoutlist([('a', '')], indent=0)) |
|---|
| 261 | n/a | self.assertEqual(smallest[0], 'a') |
|---|
| 262 | n/a | |
|---|
| 263 | n/a | # testing indentation levels |
|---|
| 264 | n/a | self.assertEqual(sample(), sample_expected()) |
|---|
| 265 | n/a | for i in range(4): |
|---|
| 266 | n/a | self.assertEqual(sample(i), sample_expected(i)) |
|---|
| 267 | n/a | self.assertEqual(sample(i, i), sample_expected(i, i)) |
|---|
| 268 | n/a | |
|---|
| 269 | n/a | # invalid layout format, different kind of exceptions will be |
|---|
| 270 | n/a | # raised by internal functions |
|---|
| 271 | n/a | |
|---|
| 272 | n/a | # plain wrong format |
|---|
| 273 | n/a | self.assertRaises(ValueError, ttk._format_layoutlist, |
|---|
| 274 | n/a | ['bad', 'format']) |
|---|
| 275 | n/a | # will try to use iteritems in the 'bad' string |
|---|
| 276 | n/a | self.assertRaises(AttributeError, ttk._format_layoutlist, |
|---|
| 277 | n/a | [('name', 'bad')]) |
|---|
| 278 | n/a | # bad children formatting |
|---|
| 279 | n/a | self.assertRaises(ValueError, ttk._format_layoutlist, |
|---|
| 280 | n/a | [('name', {'children': {'a': None}})]) |
|---|
| 281 | n/a | |
|---|
| 282 | n/a | |
|---|
| 283 | n/a | def test_script_from_settings(self): |
|---|
| 284 | n/a | # empty options |
|---|
| 285 | n/a | self.assertFalse(ttk._script_from_settings({'name': |
|---|
| 286 | n/a | {'configure': None, 'map': None, 'element create': None}})) |
|---|
| 287 | n/a | |
|---|
| 288 | n/a | # empty layout |
|---|
| 289 | n/a | self.assertEqual( |
|---|
| 290 | n/a | ttk._script_from_settings({'name': {'layout': None}}), |
|---|
| 291 | n/a | "ttk::style layout name {\nnull\n}") |
|---|
| 292 | n/a | |
|---|
| 293 | n/a | configdict = {'αβγ': True, 'á': False} |
|---|
| 294 | n/a | self.assertTrue( |
|---|
| 295 | n/a | ttk._script_from_settings({'name': {'configure': configdict}})) |
|---|
| 296 | n/a | |
|---|
| 297 | n/a | mapdict = {'üñÃÄódè': [('á', 'vãl')]} |
|---|
| 298 | n/a | self.assertTrue( |
|---|
| 299 | n/a | ttk._script_from_settings({'name': {'map': mapdict}})) |
|---|
| 300 | n/a | |
|---|
| 301 | n/a | # invalid image element |
|---|
| 302 | n/a | self.assertRaises(IndexError, |
|---|
| 303 | n/a | ttk._script_from_settings, {'name': {'element create': ['image']}}) |
|---|
| 304 | n/a | |
|---|
| 305 | n/a | # minimal valid image |
|---|
| 306 | n/a | self.assertTrue(ttk._script_from_settings({'name': |
|---|
| 307 | n/a | {'element create': ['image', 'name']}})) |
|---|
| 308 | n/a | |
|---|
| 309 | n/a | image = {'thing': {'element create': |
|---|
| 310 | n/a | ['image', 'name', ('state1', 'state2', 'val')]}} |
|---|
| 311 | n/a | self.assertEqual(ttk._script_from_settings(image), |
|---|
| 312 | n/a | "ttk::style element create thing image {name {state1 state2} val} ") |
|---|
| 313 | n/a | |
|---|
| 314 | n/a | image['thing']['element create'].append({'opt': 30}) |
|---|
| 315 | n/a | self.assertEqual(ttk._script_from_settings(image), |
|---|
| 316 | n/a | "ttk::style element create thing image {name {state1 state2} val} " |
|---|
| 317 | n/a | "-opt 30") |
|---|
| 318 | n/a | |
|---|
| 319 | n/a | image['thing']['element create'][-1]['opt'] = [MockTclObj(3), |
|---|
| 320 | n/a | MockTclObj('2m')] |
|---|
| 321 | n/a | self.assertEqual(ttk._script_from_settings(image), |
|---|
| 322 | n/a | "ttk::style element create thing image {name {state1 state2} val} " |
|---|
| 323 | n/a | "-opt {3 2m}") |
|---|
| 324 | n/a | |
|---|
| 325 | n/a | |
|---|
| 326 | n/a | def test_tclobj_to_py(self): |
|---|
| 327 | n/a | self.assertEqual( |
|---|
| 328 | n/a | ttk._tclobj_to_py((MockStateSpec('a', 'b'), 'val')), |
|---|
| 329 | n/a | [('a', 'b', 'val')]) |
|---|
| 330 | n/a | self.assertEqual( |
|---|
| 331 | n/a | ttk._tclobj_to_py([MockTclObj('1'), 2, MockTclObj('3m')]), |
|---|
| 332 | n/a | [1, 2, '3m']) |
|---|
| 333 | n/a | |
|---|
| 334 | n/a | |
|---|
| 335 | n/a | def test_list_from_statespec(self): |
|---|
| 336 | n/a | def test_it(sspec, value, res_value, states): |
|---|
| 337 | n/a | self.assertEqual(ttk._list_from_statespec( |
|---|
| 338 | n/a | (sspec, value)), [states + (res_value, )]) |
|---|
| 339 | n/a | |
|---|
| 340 | n/a | states_even = tuple('state%d' % i for i in range(6)) |
|---|
| 341 | n/a | statespec = MockStateSpec(*states_even) |
|---|
| 342 | n/a | test_it(statespec, 'val', 'val', states_even) |
|---|
| 343 | n/a | test_it(statespec, MockTclObj('val'), 'val', states_even) |
|---|
| 344 | n/a | |
|---|
| 345 | n/a | states_odd = tuple('state%d' % i for i in range(5)) |
|---|
| 346 | n/a | statespec = MockStateSpec(*states_odd) |
|---|
| 347 | n/a | test_it(statespec, 'val', 'val', states_odd) |
|---|
| 348 | n/a | |
|---|
| 349 | n/a | test_it(('a', 'b', 'c'), MockTclObj('val'), 'val', ('a', 'b', 'c')) |
|---|
| 350 | n/a | |
|---|
| 351 | n/a | |
|---|
| 352 | n/a | def test_list_from_layouttuple(self): |
|---|
| 353 | n/a | tk = MockTkApp() |
|---|
| 354 | n/a | |
|---|
| 355 | n/a | # empty layout tuple |
|---|
| 356 | n/a | self.assertFalse(ttk._list_from_layouttuple(tk, ())) |
|---|
| 357 | n/a | |
|---|
| 358 | n/a | # shortest layout tuple |
|---|
| 359 | n/a | self.assertEqual(ttk._list_from_layouttuple(tk, ('name', )), |
|---|
| 360 | n/a | [('name', {})]) |
|---|
| 361 | n/a | |
|---|
| 362 | n/a | # not so interesting ltuple |
|---|
| 363 | n/a | sample_ltuple = ('name', '-option', 'value') |
|---|
| 364 | n/a | self.assertEqual(ttk._list_from_layouttuple(tk, sample_ltuple), |
|---|
| 365 | n/a | [('name', {'option': 'value'})]) |
|---|
| 366 | n/a | |
|---|
| 367 | n/a | # empty children |
|---|
| 368 | n/a | self.assertEqual(ttk._list_from_layouttuple(tk, |
|---|
| 369 | n/a | ('something', '-children', ())), |
|---|
| 370 | n/a | [('something', {'children': []})] |
|---|
| 371 | n/a | ) |
|---|
| 372 | n/a | |
|---|
| 373 | n/a | # more interesting ltuple |
|---|
| 374 | n/a | ltuple = ( |
|---|
| 375 | n/a | 'name', '-option', 'niceone', '-children', ( |
|---|
| 376 | n/a | ('otherone', '-children', ( |
|---|
| 377 | n/a | ('child', )), '-otheropt', 'othervalue' |
|---|
| 378 | n/a | ) |
|---|
| 379 | n/a | ) |
|---|
| 380 | n/a | ) |
|---|
| 381 | n/a | self.assertEqual(ttk._list_from_layouttuple(tk, ltuple), |
|---|
| 382 | n/a | [('name', {'option': 'niceone', 'children': |
|---|
| 383 | n/a | [('otherone', {'otheropt': 'othervalue', 'children': |
|---|
| 384 | n/a | [('child', {})] |
|---|
| 385 | n/a | })] |
|---|
| 386 | n/a | })] |
|---|
| 387 | n/a | ) |
|---|
| 388 | n/a | |
|---|
| 389 | n/a | # bad tuples |
|---|
| 390 | n/a | self.assertRaises(ValueError, ttk._list_from_layouttuple, tk, |
|---|
| 391 | n/a | ('name', 'no_minus')) |
|---|
| 392 | n/a | self.assertRaises(ValueError, ttk._list_from_layouttuple, tk, |
|---|
| 393 | n/a | ('name', 'no_minus', 'value')) |
|---|
| 394 | n/a | self.assertRaises(ValueError, ttk._list_from_layouttuple, tk, |
|---|
| 395 | n/a | ('something', '-children')) # no children |
|---|
| 396 | n/a | |
|---|
| 397 | n/a | |
|---|
| 398 | n/a | def test_val_or_dict(self): |
|---|
| 399 | n/a | def func(res, opt=None, val=None): |
|---|
| 400 | n/a | if opt is None: |
|---|
| 401 | n/a | return res |
|---|
| 402 | n/a | if val is None: |
|---|
| 403 | n/a | return "test val" |
|---|
| 404 | n/a | return (opt, val) |
|---|
| 405 | n/a | |
|---|
| 406 | n/a | tk = MockTkApp() |
|---|
| 407 | n/a | tk.call = func |
|---|
| 408 | n/a | |
|---|
| 409 | n/a | self.assertEqual(ttk._val_or_dict(tk, {}, '-test:3'), |
|---|
| 410 | n/a | {'test': '3'}) |
|---|
| 411 | n/a | self.assertEqual(ttk._val_or_dict(tk, {}, ('-test', 3)), |
|---|
| 412 | n/a | {'test': 3}) |
|---|
| 413 | n/a | |
|---|
| 414 | n/a | self.assertEqual(ttk._val_or_dict(tk, {'test': None}, 'x:y'), |
|---|
| 415 | n/a | 'test val') |
|---|
| 416 | n/a | |
|---|
| 417 | n/a | self.assertEqual(ttk._val_or_dict(tk, {'test': 3}, 'x:y'), |
|---|
| 418 | n/a | {'test': 3}) |
|---|
| 419 | n/a | |
|---|
| 420 | n/a | |
|---|
| 421 | n/a | def test_convert_stringval(self): |
|---|
| 422 | n/a | tests = ( |
|---|
| 423 | n/a | (0, 0), ('09', 9), ('a', 'a'), ('áÃ', 'áÃ'), ([], '[]'), |
|---|
| 424 | n/a | (None, 'None') |
|---|
| 425 | n/a | ) |
|---|
| 426 | n/a | for orig, expected in tests: |
|---|
| 427 | n/a | self.assertEqual(ttk._convert_stringval(orig), expected) |
|---|
| 428 | n/a | |
|---|
| 429 | n/a | |
|---|
| 430 | n/a | class TclObjsToPyTest(unittest.TestCase): |
|---|
| 431 | n/a | |
|---|
| 432 | n/a | def test_unicode(self): |
|---|
| 433 | n/a | adict = {'opt': 'välúè'} |
|---|
| 434 | n/a | self.assertEqual(ttk.tclobjs_to_py(adict), {'opt': 'välúè'}) |
|---|
| 435 | n/a | |
|---|
| 436 | n/a | adict['opt'] = MockTclObj(adict['opt']) |
|---|
| 437 | n/a | self.assertEqual(ttk.tclobjs_to_py(adict), {'opt': 'välúè'}) |
|---|
| 438 | n/a | |
|---|
| 439 | n/a | def test_multivalues(self): |
|---|
| 440 | n/a | adict = {'opt': [1, 2, 3, 4]} |
|---|
| 441 | n/a | self.assertEqual(ttk.tclobjs_to_py(adict), {'opt': [1, 2, 3, 4]}) |
|---|
| 442 | n/a | |
|---|
| 443 | n/a | adict['opt'] = [1, 'xm', 3] |
|---|
| 444 | n/a | self.assertEqual(ttk.tclobjs_to_py(adict), {'opt': [1, 'xm', 3]}) |
|---|
| 445 | n/a | |
|---|
| 446 | n/a | adict['opt'] = (MockStateSpec('a', 'b'), 'válũè') |
|---|
| 447 | n/a | self.assertEqual(ttk.tclobjs_to_py(adict), |
|---|
| 448 | n/a | {'opt': [('a', 'b', 'válũè')]}) |
|---|
| 449 | n/a | |
|---|
| 450 | n/a | self.assertEqual(ttk.tclobjs_to_py({'x': ['y z']}), |
|---|
| 451 | n/a | {'x': ['y z']}) |
|---|
| 452 | n/a | |
|---|
| 453 | n/a | def test_nosplit(self): |
|---|
| 454 | n/a | self.assertEqual(ttk.tclobjs_to_py({'text': 'some text'}), |
|---|
| 455 | n/a | {'text': 'some text'}) |
|---|
| 456 | n/a | |
|---|
| 457 | n/a | tests_nogui = (InternalFunctionsTest, TclObjsToPyTest) |
|---|
| 458 | n/a | |
|---|
| 459 | n/a | if __name__ == "__main__": |
|---|
| 460 | n/a | from test.support import run_unittest |
|---|
| 461 | n/a | run_unittest(*tests_nogui) |
|---|