| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | # -*- encoding: utf-8 -*- | 
					
						
							|  |  |  | import unittest | 
					
						
							| 
									
										
										
										
											2014-05-28 18:38:27 +03:00
										 |  |  | import tkinter | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | from tkinter import ttk | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-28 18:38:27 +03:00
										 |  |  | class MockTkApp: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def splitlist(self, arg): | 
					
						
							|  |  |  |         if isinstance(arg, tuple): | 
					
						
							|  |  |  |             return arg | 
					
						
							|  |  |  |         return arg.split(':') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def wantobjects(self): | 
					
						
							|  |  |  |         return True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | class MockTclObj(object): | 
					
						
							|  |  |  |     typename = 'test' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __init__(self, val): | 
					
						
							|  |  |  |         self.val = val | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __str__(self): | 
					
						
							|  |  |  |         return str(self.val) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class MockStateSpec(object): | 
					
						
							|  |  |  |     typename = 'StateSpec' | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __init__(self, *args): | 
					
						
							|  |  |  |         self.val = args | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def __str__(self): | 
					
						
							|  |  |  |         return ' '.join(self.val) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class InternalFunctionsTest(unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_format_optdict(self): | 
					
						
							|  |  |  |         def check_against(fmt_opts, result): | 
					
						
							|  |  |  |             for i in range(0, len(fmt_opts), 2): | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |                 self.assertEqual(result.pop(fmt_opts[i]), fmt_opts[i + 1]) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             if result: | 
					
						
							|  |  |  |                 self.fail("result still got elements: %s" % result) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # passing an empty dict should return an empty object (tuple here) | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertFalse(ttk._format_optdict({})) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # 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'}}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-01-15 17:56:08 +02:00
										 |  |  |         # check script formatting | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |         check_against( | 
					
						
							|  |  |  |             ttk._format_optdict( | 
					
						
							| 
									
										
										
										
											2013-01-15 17:56:08 +02:00
										 |  |  |                 {'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': r'\{\}', | 
					
						
							|  |  |  |              '-test7': r'\}\ -spam\ \{'}) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         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 | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(opts, orig_opts) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # passing values with spaces inside a tuple/list | 
					
						
							|  |  |  |         check_against( | 
					
						
							|  |  |  |             ttk._format_optdict( | 
					
						
							|  |  |  |                 {'option': ('one two', 'three')}), | 
					
						
							|  |  |  |             {'-option': '{one two} three'}) | 
					
						
							| 
									
										
										
										
											2013-01-15 17:56:08 +02:00
										 |  |  |         check_against( | 
					
						
							|  |  |  |             ttk._format_optdict( | 
					
						
							|  |  |  |                 {'option': ('one\ttwo', 'three')}), | 
					
						
							|  |  |  |             {'-option': '{one\ttwo} three'}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # passing empty strings inside a tuple/list | 
					
						
							|  |  |  |         check_against( | 
					
						
							|  |  |  |             ttk._format_optdict( | 
					
						
							|  |  |  |                 {'option': ('', 'one')}), | 
					
						
							|  |  |  |             {'-option': '{} one'}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # passing values with braces inside a tuple/list | 
					
						
							|  |  |  |         check_against( | 
					
						
							|  |  |  |             ttk._format_optdict( | 
					
						
							|  |  |  |                 {'option': ('one} {two', 'three')}), | 
					
						
							|  |  |  |             {'-option': r'one\}\ \{two three'}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # passing quoted strings inside a tuple/list | 
					
						
							|  |  |  |         check_against( | 
					
						
							|  |  |  |             ttk._format_optdict( | 
					
						
							|  |  |  |                 {'option': ('"one"', 'two')}), | 
					
						
							|  |  |  |             {'-option': '{"one"} two'}) | 
					
						
							|  |  |  |         check_against( | 
					
						
							|  |  |  |             ttk._format_optdict( | 
					
						
							|  |  |  |                 {'option': ('{one}', 'two')}), | 
					
						
							|  |  |  |             {'-option': r'\{one\} two'}) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # ignore an option | 
					
						
							|  |  |  |         amount_opts = len(ttk._format_optdict(opts, ignore=('á'))) / 2 | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(amount_opts, len(opts) - 1) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # ignore non-existing options | 
					
						
							|  |  |  |         amount_opts = len(ttk._format_optdict(opts, ignore=('á', 'b'))) / 2 | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(amount_opts, len(opts) - 1) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # ignore every option | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertFalse(ttk._format_optdict(opts, ignore=list(opts.keys()))) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_format_mapdict(self): | 
					
						
							|  |  |  |         opts = {'a': [('b', 'c', 'val'), ('d', 'otherval'), ('', 'single')]} | 
					
						
							|  |  |  |         result = ttk._format_mapdict(opts) | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(len(result), len(list(opts.keys())) * 2) | 
					
						
							|  |  |  |         self.assertEqual(result, ('-a', '{b c} val d otherval {} single')) | 
					
						
							|  |  |  |         self.assertEqual(ttk._format_mapdict(opts, script=True), | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             ('-a', '{{b c} val d otherval {} single}')) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(ttk._format_mapdict({2: []}), ('-2', '')) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         opts = {'üñíćódè': [('á', 'vãl')]} | 
					
						
							|  |  |  |         result = ttk._format_mapdict(opts) | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(result, ('-üñíćódè', 'á vãl')) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # empty states | 
					
						
							|  |  |  |         valid = {'opt': [('', '', 'hi')]} | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(ttk._format_mapdict(valid), ('-opt', '{ } hi')) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # when passing multiple states, they all must be strings | 
					
						
							|  |  |  |         invalid = {'opt': [(1, 2, 'valid val')]} | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertRaises(TypeError, ttk._format_mapdict, invalid) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |         invalid = {'opt': [([1], '2', 'valid val')]} | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertRaises(TypeError, ttk._format_mapdict, invalid) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |         # but when passing a single state, it can be anything | 
					
						
							|  |  |  |         valid = {'opt': [[1, 'value']]} | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(ttk._format_mapdict(valid), ('-opt', '1 value')) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |         # special attention to single states which evalute to False | 
					
						
							|  |  |  |         for stateval in (None, 0, False, '', set()): # just some samples | 
					
						
							|  |  |  |             valid = {'opt': [(stateval, 'value')]} | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |             self.assertEqual(ttk._format_mapdict(valid), | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |                 ('-opt', '{} value')) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # values must be iterable | 
					
						
							|  |  |  |         opts = {'a': None} | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertRaises(TypeError, ttk._format_mapdict, opts) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # items in the value must have size >= 2 | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertRaises(IndexError, ttk._format_mapdict, | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             {'a': [('invalid', )]}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_format_elemcreate(self): | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertTrue(ttk._format_elemcreate(None), (None, ())) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         ## Testing type = image | 
					
						
							|  |  |  |         # image type expects at least an image name, so this should raise | 
					
						
							|  |  |  |         # IndexError since it tries to access the index 0 of an empty tuple | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertRaises(IndexError, ttk._format_elemcreate, 'image') | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # don't format returned values as a tcl script | 
					
						
							|  |  |  |         # minimum acceptable for image type | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(ttk._format_elemcreate('image', False, 'test'), | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             ("test ", ())) | 
					
						
							| 
									
										
										
										
											2011-03-16 11:05:33 +02:00
										 |  |  |         # specifying a state spec | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(ttk._format_elemcreate('image', False, 'test', | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             ('', 'a')), ("test {} a", ())) | 
					
						
							|  |  |  |         # state spec with multiple states | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(ttk._format_elemcreate('image', False, 'test', | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             ('a', 'b', 'c')), ("test {a b} c", ())) | 
					
						
							|  |  |  |         # state spec and options | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(ttk._format_elemcreate('image', False, 'test', | 
					
						
							| 
									
										
										
										
											2012-02-20 19:54:16 +01:00
										 |  |  |             ('a', 'b'), a='x'), ("test a b", ("-a", "x"))) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |         # format returned values as a tcl script | 
					
						
							|  |  |  |         # state spec with multiple states and an option with a multivalue | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(ttk._format_elemcreate('image', True, 'test', | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             ('a', 'b', 'c', 'd'), x=[2, 3]), ("{test {a b c} d}", "-x {2 3}")) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         ## Testing type = vsapi | 
					
						
							|  |  |  |         # vsapi type expects at least a class name and a part_id, so this | 
					
						
							| 
									
										
										
										
											2016-04-17 08:32:47 +03:00
										 |  |  |         # should raise a ValueError since it tries to get two elements from | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |         # an empty tuple | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertRaises(ValueError, ttk._format_elemcreate, 'vsapi') | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # don't format returned values as a tcl script | 
					
						
							|  |  |  |         # minimum acceptable for vsapi | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(ttk._format_elemcreate('vsapi', False, 'a', 'b'), | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             ("a b ", ())) | 
					
						
							|  |  |  |         # now with a state spec with multiple states | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(ttk._format_elemcreate('vsapi', False, 'a', 'b', | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             ('a', 'b', 'c')), ("a b {a b} c", ())) | 
					
						
							|  |  |  |         # state spec and option | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(ttk._format_elemcreate('vsapi', False, 'a', 'b', | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             ('a', 'b'), opt='x'), ("a b a b", ("-opt", "x"))) | 
					
						
							|  |  |  |         # format returned values as a tcl script | 
					
						
							|  |  |  |         # state spec with a multivalue and an option | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(ttk._format_elemcreate('vsapi', True, 'a', 'b', | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             ('a', 'b', [1, 2]), opt='x'), ("{a b {a b} {1 2}}", "-opt x")) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # Testing type = from | 
					
						
							|  |  |  |         # from type expects at least a type name | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertRaises(IndexError, ttk._format_elemcreate, 'from') | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(ttk._format_elemcreate('from', False, 'a'), | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             ('a', ())) | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(ttk._format_elemcreate('from', False, 'a', 'b'), | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             ('a', ('b', ))) | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(ttk._format_elemcreate('from', True, 'a', 'b'), | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             ('{a}', 'b')) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_format_layoutlist(self): | 
					
						
							|  |  |  |         def sample(indent=0, indent_size=2): | 
					
						
							|  |  |  |             return ttk._format_layoutlist( | 
					
						
							|  |  |  |             [('a', {'other': [1, 2, 3], 'children': | 
					
						
							|  |  |  |                 [('b', {'children': | 
					
						
							|  |  |  |                     [('c', {'children': | 
					
						
							|  |  |  |                         [('d', {'nice': 'opt'})], 'something': (1, 2) | 
					
						
							|  |  |  |                     })] | 
					
						
							|  |  |  |                 })] | 
					
						
							|  |  |  |             })], indent=indent, indent_size=indent_size)[0] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         def sample_expected(indent=0, indent_size=2): | 
					
						
							|  |  |  |             spaces = lambda amount=0: ' ' * (amount + indent) | 
					
						
							|  |  |  |             return ( | 
					
						
							|  |  |  |                 "%sa -other {1 2 3} -children {\n" | 
					
						
							|  |  |  |                 "%sb -children {\n" | 
					
						
							|  |  |  |                 "%sc -something {1 2} -children {\n" | 
					
						
							|  |  |  |                 "%sd -nice opt\n" | 
					
						
							|  |  |  |                 "%s}\n" | 
					
						
							|  |  |  |                 "%s}\n" | 
					
						
							|  |  |  |                 "%s}" % (spaces(), spaces(indent_size), | 
					
						
							|  |  |  |                     spaces(2 * indent_size), spaces(3 * indent_size), | 
					
						
							|  |  |  |                     spaces(2 * indent_size), spaces(indent_size), spaces())) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # empty layout | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(ttk._format_layoutlist([])[0], '') | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # _format_layoutlist always expects the second item (in every item) | 
					
						
							|  |  |  |         # to act like a dict (except when the value evalutes to False). | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertRaises(AttributeError, | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             ttk._format_layoutlist, [('a', 'b')]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         smallest = ttk._format_layoutlist([('a', None)], indent=0) | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(smallest, | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             ttk._format_layoutlist([('a', '')], indent=0)) | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(smallest[0], 'a') | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # testing indentation levels | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(sample(), sample_expected()) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |         for i in range(4): | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |             self.assertEqual(sample(i), sample_expected(i)) | 
					
						
							|  |  |  |             self.assertEqual(sample(i, i), sample_expected(i, i)) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # invalid layout format, different kind of exceptions will be | 
					
						
							|  |  |  |         # raised by internal functions | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # plain wrong format | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertRaises(ValueError, ttk._format_layoutlist, | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             ['bad', 'format']) | 
					
						
							|  |  |  |         # will try to use iteritems in the 'bad' string | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertRaises(AttributeError, ttk._format_layoutlist, | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |            [('name', 'bad')]) | 
					
						
							|  |  |  |         # bad children formatting | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertRaises(ValueError, ttk._format_layoutlist, | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             [('name', {'children': {'a': None}})]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_script_from_settings(self): | 
					
						
							|  |  |  |         # empty options | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertFalse(ttk._script_from_settings({'name': | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             {'configure': None, 'map': None, 'element create': None}})) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # empty layout | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual( | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             ttk._script_from_settings({'name': {'layout': None}}), | 
					
						
							|  |  |  |             "ttk::style layout name {\nnull\n}") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         configdict = {'αβγ': True, 'á': False} | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertTrue( | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             ttk._script_from_settings({'name': {'configure': configdict}})) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         mapdict = {'üñíćódè': [('á', 'vãl')]} | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertTrue( | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             ttk._script_from_settings({'name': {'map': mapdict}})) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # invalid image element | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertRaises(IndexError, | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             ttk._script_from_settings, {'name': {'element create': ['image']}}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # minimal valid image | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertTrue(ttk._script_from_settings({'name': | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             {'element create': ['image', 'name']}})) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         image = {'thing': {'element create': | 
					
						
							|  |  |  |             ['image', 'name', ('state1', 'state2', 'val')]}} | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(ttk._script_from_settings(image), | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             "ttk::style element create thing image {name {state1 state2} val} ") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         image['thing']['element create'].append({'opt': 30}) | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(ttk._script_from_settings(image), | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             "ttk::style element create thing image {name {state1 state2} val} " | 
					
						
							|  |  |  |             "-opt 30") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         image['thing']['element create'][-1]['opt'] = [MockTclObj(3), | 
					
						
							|  |  |  |             MockTclObj('2m')] | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(ttk._script_from_settings(image), | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             "ttk::style element create thing image {name {state1 state2} val} " | 
					
						
							|  |  |  |             "-opt {3 2m}") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-06 22:47:58 +03:00
										 |  |  |     def test_tclobj_to_py(self): | 
					
						
							|  |  |  |         self.assertEqual( | 
					
						
							|  |  |  |             ttk._tclobj_to_py((MockStateSpec('a', 'b'), 'val')), | 
					
						
							|  |  |  |             [('a', 'b', 'val')]) | 
					
						
							|  |  |  |         self.assertEqual( | 
					
						
							|  |  |  |             ttk._tclobj_to_py([MockTclObj('1'), 2, MockTclObj('3m')]), | 
					
						
							|  |  |  |             [1, 2, '3m']) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_list_from_statespec(self): | 
					
						
							|  |  |  |         def test_it(sspec, value, res_value, states): | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |             self.assertEqual(ttk._list_from_statespec( | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |                 (sspec, value)), [states + (res_value, )]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         states_even = tuple('state%d' % i for i in range(6)) | 
					
						
							|  |  |  |         statespec = MockStateSpec(*states_even) | 
					
						
							|  |  |  |         test_it(statespec, 'val', 'val', states_even) | 
					
						
							|  |  |  |         test_it(statespec, MockTclObj('val'), 'val', states_even) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         states_odd = tuple('state%d' % i for i in range(5)) | 
					
						
							|  |  |  |         statespec = MockStateSpec(*states_odd) | 
					
						
							|  |  |  |         test_it(statespec, 'val', 'val', states_odd) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         test_it(('a', 'b', 'c'), MockTclObj('val'), 'val', ('a', 'b', 'c')) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_list_from_layouttuple(self): | 
					
						
							| 
									
										
										
										
											2014-05-28 18:38:27 +03:00
										 |  |  |         tk = MockTkApp() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |         # empty layout tuple | 
					
						
							| 
									
										
										
										
											2014-05-28 18:38:27 +03:00
										 |  |  |         self.assertFalse(ttk._list_from_layouttuple(tk, ())) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         # shortest layout tuple | 
					
						
							| 
									
										
										
										
											2014-05-28 18:38:27 +03:00
										 |  |  |         self.assertEqual(ttk._list_from_layouttuple(tk, ('name', )), | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             [('name', {})]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # not so interesting ltuple | 
					
						
							|  |  |  |         sample_ltuple = ('name', '-option', 'value') | 
					
						
							| 
									
										
										
										
											2014-05-28 18:38:27 +03:00
										 |  |  |         self.assertEqual(ttk._list_from_layouttuple(tk, sample_ltuple), | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             [('name', {'option': 'value'})]) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # empty children | 
					
						
							| 
									
										
										
										
											2014-05-28 18:38:27 +03:00
										 |  |  |         self.assertEqual(ttk._list_from_layouttuple(tk, | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             ('something', '-children', ())), | 
					
						
							|  |  |  |             [('something', {'children': []})] | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # more interesting ltuple | 
					
						
							|  |  |  |         ltuple = ( | 
					
						
							|  |  |  |             'name', '-option', 'niceone', '-children', ( | 
					
						
							|  |  |  |                 ('otherone', '-children', ( | 
					
						
							|  |  |  |                     ('child', )), '-otheropt', 'othervalue' | 
					
						
							|  |  |  |                 ) | 
					
						
							|  |  |  |             ) | 
					
						
							|  |  |  |         ) | 
					
						
							| 
									
										
										
										
											2014-05-28 18:38:27 +03:00
										 |  |  |         self.assertEqual(ttk._list_from_layouttuple(tk, ltuple), | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             [('name', {'option': 'niceone', 'children': | 
					
						
							|  |  |  |                 [('otherone', {'otheropt': 'othervalue', 'children': | 
					
						
							|  |  |  |                     [('child', {})] | 
					
						
							|  |  |  |                 })] | 
					
						
							|  |  |  |             })] | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         # bad tuples | 
					
						
							| 
									
										
										
										
											2014-05-28 18:38:27 +03:00
										 |  |  |         self.assertRaises(ValueError, ttk._list_from_layouttuple, tk, | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             ('name', 'no_minus')) | 
					
						
							| 
									
										
										
										
											2014-05-28 18:38:27 +03:00
										 |  |  |         self.assertRaises(ValueError, ttk._list_from_layouttuple, tk, | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             ('name', 'no_minus', 'value')) | 
					
						
							| 
									
										
										
										
											2014-05-28 18:38:27 +03:00
										 |  |  |         self.assertRaises(ValueError, ttk._list_from_layouttuple, tk, | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             ('something', '-children')) # no children | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_val_or_dict(self): | 
					
						
							| 
									
										
										
										
											2014-05-28 18:38:27 +03:00
										 |  |  |         def func(res, opt=None, val=None): | 
					
						
							|  |  |  |             if opt is None: | 
					
						
							|  |  |  |                 return res | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             if val is None: | 
					
						
							|  |  |  |                 return "test val" | 
					
						
							|  |  |  |             return (opt, val) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-28 18:38:27 +03:00
										 |  |  |         tk = MockTkApp() | 
					
						
							|  |  |  |         tk.call = func | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertEqual(ttk._val_or_dict(tk, {}, '-test:3'), | 
					
						
							|  |  |  |                          {'test': '3'}) | 
					
						
							|  |  |  |         self.assertEqual(ttk._val_or_dict(tk, {}, ('-test', 3)), | 
					
						
							|  |  |  |                          {'test': 3}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         self.assertEqual(ttk._val_or_dict(tk, {'test': None}, 'x:y'), | 
					
						
							|  |  |  |                          'test val') | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-28 18:38:27 +03:00
										 |  |  |         self.assertEqual(ttk._val_or_dict(tk, {'test': 3}, 'x:y'), | 
					
						
							|  |  |  |                          {'test': 3}) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_convert_stringval(self): | 
					
						
							|  |  |  |         tests = ( | 
					
						
							|  |  |  |             (0, 0), ('09', 9), ('a', 'a'), ('áÚ', 'áÚ'), ([], '[]'), | 
					
						
							|  |  |  |             (None, 'None') | 
					
						
							|  |  |  |         ) | 
					
						
							|  |  |  |         for orig, expected in tests: | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |             self.assertEqual(ttk._convert_stringval(orig), expected) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | class TclObjsToPyTest(unittest.TestCase): | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_unicode(self): | 
					
						
							|  |  |  |         adict = {'opt': 'välúè'} | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(ttk.tclobjs_to_py(adict), {'opt': 'välúè'}) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         adict['opt'] = MockTclObj(adict['opt']) | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(ttk.tclobjs_to_py(adict), {'opt': 'välúè'}) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |     def test_multivalues(self): | 
					
						
							|  |  |  |         adict = {'opt': [1, 2, 3, 4]} | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(ttk.tclobjs_to_py(adict), {'opt': [1, 2, 3, 4]}) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         adict['opt'] = [1, 'xm', 3] | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(ttk.tclobjs_to_py(adict), {'opt': [1, 'xm', 3]}) | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  | 
 | 
					
						
							|  |  |  |         adict['opt'] = (MockStateSpec('a', 'b'), 'válũè') | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(ttk.tclobjs_to_py(adict), | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             {'opt': [('a', 'b', 'válũè')]}) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(ttk.tclobjs_to_py({'x': ['y z']}), | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             {'x': ['y z']}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     def test_nosplit(self): | 
					
						
							| 
									
										
										
										
											2009-08-13 08:51:18 +00:00
										 |  |  |         self.assertEqual(ttk.tclobjs_to_py({'text': 'some text'}), | 
					
						
							| 
									
										
										
										
											2009-01-28 14:41:10 +00:00
										 |  |  |             {'text': 'some text'}) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | tests_nogui = (InternalFunctionsTest, TclObjsToPyTest) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | if __name__ == "__main__": | 
					
						
							|  |  |  |     from test.support import run_unittest | 
					
						
							|  |  |  |     run_unittest(*tests_nogui) |