Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 1 | # -*- encoding: utf-8 -*- |
| 2 | import unittest |
Serhiy Storchaka | b49eff2 | 2014-05-28 18:38:27 +0300 | [diff] [blame] | 3 | import tkinter |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 4 | from tkinter import ttk |
| 5 | |
Serhiy Storchaka | b49eff2 | 2014-05-28 18:38:27 +0300 | [diff] [blame] | 6 | class 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 Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 17 | class 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 | |
| 27 | class 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 | |
| 37 | class 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 Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 42 | self.assertEqual(result.pop(fmt_opts[i]), fmt_opts[i + 1]) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 43 | 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 Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 47 | self.assertFalse(ttk._format_optdict({})) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 48 | |
| 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 Storchaka | b139652 | 2013-01-15 17:56:08 +0200 | [diff] [blame] | 64 | # check script formatting |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 65 | check_against( |
| 66 | ttk._format_optdict( |
Serhiy Storchaka | b139652 | 2013-01-15 17:56:08 +0200 | [diff] [blame] | 67 | {'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 Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 75 | |
| 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 Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 81 | self.assertEqual(opts, orig_opts) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 82 | |
| 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 Storchaka | b139652 | 2013-01-15 17:56:08 +0200 | [diff] [blame] | 88 | 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 Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 114 | |
| 115 | # ignore an option |
| 116 | amount_opts = len(ttk._format_optdict(opts, ignore=('á'))) / 2 |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 117 | self.assertEqual(amount_opts, len(opts) - 1) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 118 | |
| 119 | # ignore non-existing options |
| 120 | amount_opts = len(ttk._format_optdict(opts, ignore=('á', 'b'))) / 2 |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 121 | self.assertEqual(amount_opts, len(opts) - 1) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 122 | |
| 123 | # ignore every option |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 124 | self.assertFalse(ttk._format_optdict(opts, ignore=list(opts.keys()))) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 125 | |
| 126 | |
| 127 | def test_format_mapdict(self): |
| 128 | opts = {'a': [('b', 'c', 'val'), ('d', 'otherval'), ('', 'single')]} |
| 129 | result = ttk._format_mapdict(opts) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 130 | 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 Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 133 | ('-a', '{{b c} val d otherval {} single}')) |
| 134 | |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 135 | self.assertEqual(ttk._format_mapdict({2: []}), ('-2', '')) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 136 | |
| 137 | opts = {'üñíćódè': [('á', 'vãl')]} |
| 138 | result = ttk._format_mapdict(opts) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 139 | self.assertEqual(result, ('-üñíćódè', 'á vãl')) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 140 | |
| 141 | # empty states |
| 142 | valid = {'opt': [('', '', 'hi')]} |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 143 | self.assertEqual(ttk._format_mapdict(valid), ('-opt', '{ } hi')) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 144 | |
| 145 | # when passing multiple states, they all must be strings |
| 146 | invalid = {'opt': [(1, 2, 'valid val')]} |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 147 | self.assertRaises(TypeError, ttk._format_mapdict, invalid) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 148 | invalid = {'opt': [([1], '2', 'valid val')]} |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 149 | self.assertRaises(TypeError, ttk._format_mapdict, invalid) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 150 | # but when passing a single state, it can be anything |
| 151 | valid = {'opt': [[1, 'value']]} |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 152 | self.assertEqual(ttk._format_mapdict(valid), ('-opt', '1 value')) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 153 | # 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 Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 156 | self.assertEqual(ttk._format_mapdict(valid), |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 157 | ('-opt', '{} value')) |
| 158 | |
| 159 | # values must be iterable |
| 160 | opts = {'a': None} |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 161 | self.assertRaises(TypeError, ttk._format_mapdict, opts) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 162 | |
| 163 | # items in the value must have size >= 2 |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 164 | self.assertRaises(IndexError, ttk._format_mapdict, |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 165 | {'a': [('invalid', )]}) |
| 166 | |
| 167 | |
| 168 | def test_format_elemcreate(self): |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 169 | self.assertTrue(ttk._format_elemcreate(None), (None, ())) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 170 | |
| 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 Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 174 | self.assertRaises(IndexError, ttk._format_elemcreate, 'image') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 175 | |
| 176 | # don't format returned values as a tcl script |
| 177 | # minimum acceptable for image type |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 178 | self.assertEqual(ttk._format_elemcreate('image', False, 'test'), |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 179 | ("test ", ())) |
Ezio Melotti | 1392500 | 2011-03-16 11:05:33 +0200 | [diff] [blame] | 180 | # specifying a state spec |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 181 | self.assertEqual(ttk._format_elemcreate('image', False, 'test', |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 182 | ('', 'a')), ("test {} a", ())) |
| 183 | # state spec with multiple states |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 184 | self.assertEqual(ttk._format_elemcreate('image', False, 'test', |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 185 | ('a', 'b', 'c')), ("test {a b} c", ())) |
| 186 | # state spec and options |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 187 | self.assertEqual(ttk._format_elemcreate('image', False, 'test', |
Georg Brandl | 2daf6ae | 2012-02-20 19:54:16 +0100 | [diff] [blame] | 188 | ('a', 'b'), a='x'), ("test a b", ("-a", "x"))) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 189 | # format returned values as a tcl script |
| 190 | # state spec with multiple states and an option with a multivalue |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 191 | self.assertEqual(ttk._format_elemcreate('image', True, 'test', |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 192 | ('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 Storchaka | 6a7b3a7 | 2016-04-17 08:32:47 +0300 | [diff] [blame^] | 196 | # should raise a ValueError since it tries to get two elements from |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 197 | # an empty tuple |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 198 | self.assertRaises(ValueError, ttk._format_elemcreate, 'vsapi') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 199 | |
| 200 | # don't format returned values as a tcl script |
| 201 | # minimum acceptable for vsapi |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 202 | self.assertEqual(ttk._format_elemcreate('vsapi', False, 'a', 'b'), |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 203 | ("a b ", ())) |
| 204 | # now with a state spec with multiple states |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 205 | self.assertEqual(ttk._format_elemcreate('vsapi', False, 'a', 'b', |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 206 | ('a', 'b', 'c')), ("a b {a b} c", ())) |
| 207 | # state spec and option |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 208 | self.assertEqual(ttk._format_elemcreate('vsapi', False, 'a', 'b', |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 209 | ('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 Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 212 | self.assertEqual(ttk._format_elemcreate('vsapi', True, 'a', 'b', |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 213 | ('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 Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 217 | self.assertRaises(IndexError, ttk._format_elemcreate, 'from') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 218 | |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 219 | self.assertEqual(ttk._format_elemcreate('from', False, 'a'), |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 220 | ('a', ())) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 221 | self.assertEqual(ttk._format_elemcreate('from', False, 'a', 'b'), |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 222 | ('a', ('b', ))) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 223 | self.assertEqual(ttk._format_elemcreate('from', True, 'a', 'b'), |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 224 | ('{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 Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 252 | self.assertEqual(ttk._format_layoutlist([])[0], '') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 253 | |
| 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 Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 256 | self.assertRaises(AttributeError, |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 257 | ttk._format_layoutlist, [('a', 'b')]) |
| 258 | |
| 259 | smallest = ttk._format_layoutlist([('a', None)], indent=0) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 260 | self.assertEqual(smallest, |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 261 | ttk._format_layoutlist([('a', '')], indent=0)) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 262 | self.assertEqual(smallest[0], 'a') |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 263 | |
| 264 | # testing indentation levels |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 265 | self.assertEqual(sample(), sample_expected()) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 266 | for i in range(4): |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 267 | self.assertEqual(sample(i), sample_expected(i)) |
| 268 | self.assertEqual(sample(i, i), sample_expected(i, i)) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 269 | |
| 270 | # invalid layout format, different kind of exceptions will be |
| 271 | # raised by internal functions |
| 272 | |
| 273 | # plain wrong format |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 274 | self.assertRaises(ValueError, ttk._format_layoutlist, |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 275 | ['bad', 'format']) |
| 276 | # will try to use iteritems in the 'bad' string |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 277 | self.assertRaises(AttributeError, ttk._format_layoutlist, |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 278 | [('name', 'bad')]) |
| 279 | # bad children formatting |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 280 | self.assertRaises(ValueError, ttk._format_layoutlist, |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 281 | [('name', {'children': {'a': None}})]) |
| 282 | |
| 283 | |
| 284 | def test_script_from_settings(self): |
| 285 | # empty options |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 286 | self.assertFalse(ttk._script_from_settings({'name': |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 287 | {'configure': None, 'map': None, 'element create': None}})) |
| 288 | |
| 289 | # empty layout |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 290 | self.assertEqual( |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 291 | ttk._script_from_settings({'name': {'layout': None}}), |
| 292 | "ttk::style layout name {\nnull\n}") |
| 293 | |
| 294 | configdict = {'αβγ': True, 'á': False} |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 295 | self.assertTrue( |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 296 | ttk._script_from_settings({'name': {'configure': configdict}})) |
| 297 | |
| 298 | mapdict = {'üñíćódè': [('á', 'vãl')]} |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 299 | self.assertTrue( |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 300 | ttk._script_from_settings({'name': {'map': mapdict}})) |
| 301 | |
| 302 | # invalid image element |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 303 | self.assertRaises(IndexError, |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 304 | ttk._script_from_settings, {'name': {'element create': ['image']}}) |
| 305 | |
| 306 | # minimal valid image |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 307 | self.assertTrue(ttk._script_from_settings({'name': |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 308 | {'element create': ['image', 'name']}})) |
| 309 | |
| 310 | image = {'thing': {'element create': |
| 311 | ['image', 'name', ('state1', 'state2', 'val')]}} |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 312 | self.assertEqual(ttk._script_from_settings(image), |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 313 | "ttk::style element create thing image {name {state1 state2} val} ") |
| 314 | |
| 315 | image['thing']['element create'].append({'opt': 30}) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 316 | self.assertEqual(ttk._script_from_settings(image), |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 317 | "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 Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 322 | self.assertEqual(ttk._script_from_settings(image), |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 323 | "ttk::style element create thing image {name {state1 state2} val} " |
| 324 | "-opt {3 2m}") |
| 325 | |
| 326 | |
Serhiy Storchaka | 8f0a1d0 | 2014-09-06 22:47:58 +0300 | [diff] [blame] | 327 | 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 Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 334 | |
| 335 | |
| 336 | def test_list_from_statespec(self): |
| 337 | def test_it(sspec, value, res_value, states): |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 338 | self.assertEqual(ttk._list_from_statespec( |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 339 | (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 Storchaka | b49eff2 | 2014-05-28 18:38:27 +0300 | [diff] [blame] | 354 | tk = MockTkApp() |
| 355 | |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 356 | # empty layout tuple |
Serhiy Storchaka | b49eff2 | 2014-05-28 18:38:27 +0300 | [diff] [blame] | 357 | self.assertFalse(ttk._list_from_layouttuple(tk, ())) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 358 | |
| 359 | # shortest layout tuple |
Serhiy Storchaka | b49eff2 | 2014-05-28 18:38:27 +0300 | [diff] [blame] | 360 | self.assertEqual(ttk._list_from_layouttuple(tk, ('name', )), |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 361 | [('name', {})]) |
| 362 | |
| 363 | # not so interesting ltuple |
| 364 | sample_ltuple = ('name', '-option', 'value') |
Serhiy Storchaka | b49eff2 | 2014-05-28 18:38:27 +0300 | [diff] [blame] | 365 | self.assertEqual(ttk._list_from_layouttuple(tk, sample_ltuple), |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 366 | [('name', {'option': 'value'})]) |
| 367 | |
| 368 | # empty children |
Serhiy Storchaka | b49eff2 | 2014-05-28 18:38:27 +0300 | [diff] [blame] | 369 | self.assertEqual(ttk._list_from_layouttuple(tk, |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 370 | ('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 Storchaka | b49eff2 | 2014-05-28 18:38:27 +0300 | [diff] [blame] | 382 | self.assertEqual(ttk._list_from_layouttuple(tk, ltuple), |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 383 | [('name', {'option': 'niceone', 'children': |
| 384 | [('otherone', {'otheropt': 'othervalue', 'children': |
| 385 | [('child', {})] |
| 386 | })] |
| 387 | })] |
| 388 | ) |
| 389 | |
| 390 | # bad tuples |
Serhiy Storchaka | b49eff2 | 2014-05-28 18:38:27 +0300 | [diff] [blame] | 391 | self.assertRaises(ValueError, ttk._list_from_layouttuple, tk, |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 392 | ('name', 'no_minus')) |
Serhiy Storchaka | b49eff2 | 2014-05-28 18:38:27 +0300 | [diff] [blame] | 393 | self.assertRaises(ValueError, ttk._list_from_layouttuple, tk, |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 394 | ('name', 'no_minus', 'value')) |
Serhiy Storchaka | b49eff2 | 2014-05-28 18:38:27 +0300 | [diff] [blame] | 395 | self.assertRaises(ValueError, ttk._list_from_layouttuple, tk, |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 396 | ('something', '-children')) # no children |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 397 | |
| 398 | |
| 399 | def test_val_or_dict(self): |
Serhiy Storchaka | b49eff2 | 2014-05-28 18:38:27 +0300 | [diff] [blame] | 400 | def func(res, opt=None, val=None): |
| 401 | if opt is None: |
| 402 | return res |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 403 | if val is None: |
| 404 | return "test val" |
| 405 | return (opt, val) |
| 406 | |
Serhiy Storchaka | b49eff2 | 2014-05-28 18:38:27 +0300 | [diff] [blame] | 407 | tk = MockTkApp() |
| 408 | tk.call = func |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 409 | |
Serhiy Storchaka | b49eff2 | 2014-05-28 18:38:27 +0300 | [diff] [blame] | 410 | 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 Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 420 | |
| 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 Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 428 | self.assertEqual(ttk._convert_stringval(orig), expected) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 429 | |
| 430 | |
| 431 | class TclObjsToPyTest(unittest.TestCase): |
| 432 | |
| 433 | def test_unicode(self): |
| 434 | adict = {'opt': 'välúè'} |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 435 | self.assertEqual(ttk.tclobjs_to_py(adict), {'opt': 'välúè'}) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 436 | |
| 437 | adict['opt'] = MockTclObj(adict['opt']) |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 438 | self.assertEqual(ttk.tclobjs_to_py(adict), {'opt': 'välúè'}) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 439 | |
| 440 | def test_multivalues(self): |
| 441 | adict = {'opt': [1, 2, 3, 4]} |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 442 | self.assertEqual(ttk.tclobjs_to_py(adict), {'opt': [1, 2, 3, 4]}) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 443 | |
| 444 | adict['opt'] = [1, 'xm', 3] |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 445 | self.assertEqual(ttk.tclobjs_to_py(adict), {'opt': [1, 'xm', 3]}) |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 446 | |
| 447 | adict['opt'] = (MockStateSpec('a', 'b'), 'válũè') |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 448 | self.assertEqual(ttk.tclobjs_to_py(adict), |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 449 | {'opt': [('a', 'b', 'válũè')]}) |
| 450 | |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 451 | self.assertEqual(ttk.tclobjs_to_py({'x': ['y z']}), |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 452 | {'x': ['y z']}) |
| 453 | |
| 454 | def test_nosplit(self): |
Georg Brandl | ab91fde | 2009-08-13 08:51:18 +0000 | [diff] [blame] | 455 | self.assertEqual(ttk.tclobjs_to_py({'text': 'some text'}), |
Guilherme Polo | 5f23848 | 2009-01-28 14:41:10 +0000 | [diff] [blame] | 456 | {'text': 'some text'}) |
| 457 | |
| 458 | tests_nogui = (InternalFunctionsTest, TclObjsToPyTest) |
| 459 | |
| 460 | if __name__ == "__main__": |
| 461 | from test.support import run_unittest |
| 462 | run_unittest(*tests_nogui) |