Back to index

python3.2  3.2.2
Public Member Functions
tkinter.test.test_ttk.test_functions.InternalFunctionsTest Class Reference

List of all members.

Public Member Functions

def test_format_optdict
def test_format_mapdict
def test_format_elemcreate
def test_format_layoutlist
def test_script_from_settings
def test_dict_from_tcltuple
def test_list_from_statespec
def test_list_from_layouttuple
def test_val_or_dict
def test_convert_stringval

Detailed Description

Definition at line 25 of file test_functions.py.


Member Function Documentation

Definition at line 383 of file test_functions.py.

00383 
00384     def test_convert_stringval(self):
00385         tests = (
00386             (0, 0), ('09', 9), ('a', 'a'), ('áÚ', 'áÚ'), ([], '[]'),
00387             (None, 'None')
00388         )
00389         for orig, expected in tests:
00390             self.assertEqual(ttk._convert_stringval(orig), expected)
00391 

Here is the call graph for this function:

Definition at line 285 of file test_functions.py.

00285 
00286     def test_dict_from_tcltuple(self):
00287         fakettuple = ('-a', '{1 2 3}', '-something', 'foo')
00288 
00289         self.assertEqual(ttk._dict_from_tcltuple(fakettuple, False),
00290             {'-a': '{1 2 3}', '-something': 'foo'})
00291 
00292         self.assertEqual(ttk._dict_from_tcltuple(fakettuple),
00293             {'a': '{1 2 3}', 'something': 'foo'})
00294 
00295         # passing a tuple with a single item should return an empty dict,
00296         # since it tries to break the tuple by pairs.
00297         self.assertFalse(ttk._dict_from_tcltuple(('single', )))
00298 
00299         sspec = MockStateSpec('a', 'b')
00300         self.assertEqual(ttk._dict_from_tcltuple(('-a', (sspec, 'val'))),
00301             {'a': [('a', 'b', 'val')]})
00302 
00303         self.assertEqual(ttk._dict_from_tcltuple((MockTclObj('-padding'),
00304             [MockTclObj('1'), 2, MockTclObj('3m')])),
00305             {'padding': [1, 2, '3m']})
00306 

Here is the call graph for this function:

Definition at line 126 of file test_functions.py.

00126 
00127     def test_format_elemcreate(self):
00128         self.assertTrue(ttk._format_elemcreate(None), (None, ()))
00129 
00130         ## Testing type = image
00131         # image type expects at least an image name, so this should raise
00132         # IndexError since it tries to access the index 0 of an empty tuple
00133         self.assertRaises(IndexError, ttk._format_elemcreate, 'image')
00134 
00135         # don't format returned values as a tcl script
00136         # minimum acceptable for image type
00137         self.assertEqual(ttk._format_elemcreate('image', False, 'test'),
00138             ("test ", ()))
00139         # specifying a state spec
00140         self.assertEqual(ttk._format_elemcreate('image', False, 'test',
00141             ('', 'a')), ("test {} a", ()))
00142         # state spec with multiple states
00143         self.assertEqual(ttk._format_elemcreate('image', False, 'test',
00144             ('a', 'b', 'c')), ("test {a b} c", ()))
00145         # state spec and options
00146         self.assertEqual(ttk._format_elemcreate('image', False, 'test',
00147             ('a', 'b'), a='x', b='y'), ("test a b", ("-a", "x", "-b", "y")))
00148         # format returned values as a tcl script
00149         # state spec with multiple states and an option with a multivalue
00150         self.assertEqual(ttk._format_elemcreate('image', True, 'test',
00151             ('a', 'b', 'c', 'd'), x=[2, 3]), ("{test {a b c} d}", "-x {2 3}"))
00152 
00153         ## Testing type = vsapi
00154         # vsapi type expects at least a class name and a part_id, so this
00155         # should raise an ValueError since it tries to get two elements from
00156         # an empty tuple
00157         self.assertRaises(ValueError, ttk._format_elemcreate, 'vsapi')
00158 
00159         # don't format returned values as a tcl script
00160         # minimum acceptable for vsapi
00161         self.assertEqual(ttk._format_elemcreate('vsapi', False, 'a', 'b'),
00162             ("a b ", ()))
00163         # now with a state spec with multiple states
00164         self.assertEqual(ttk._format_elemcreate('vsapi', False, 'a', 'b',
00165             ('a', 'b', 'c')), ("a b {a b} c", ()))
00166         # state spec and option
00167         self.assertEqual(ttk._format_elemcreate('vsapi', False, 'a', 'b',
00168             ('a', 'b'), opt='x'), ("a b a b", ("-opt", "x")))
00169         # format returned values as a tcl script
00170         # state spec with a multivalue and an option
00171         self.assertEqual(ttk._format_elemcreate('vsapi', True, 'a', 'b',
00172             ('a', 'b', [1, 2]), opt='x'), ("{a b {a b} {1 2}}", "-opt x"))
00173 
00174         # Testing type = from
00175         # from type expects at least a type name
00176         self.assertRaises(IndexError, ttk._format_elemcreate, 'from')
00177 
00178         self.assertEqual(ttk._format_elemcreate('from', False, 'a'),
00179             ('a', ()))
00180         self.assertEqual(ttk._format_elemcreate('from', False, 'a', 'b'),
00181             ('a', ('b', )))
00182         self.assertEqual(ttk._format_elemcreate('from', True, 'a', 'b'),
00183             ('{a}', 'b'))
00184 

Here is the call graph for this function:

Definition at line 185 of file test_functions.py.

00185 
00186     def test_format_layoutlist(self):
00187         def sample(indent=0, indent_size=2):
00188             return ttk._format_layoutlist(
00189             [('a', {'other': [1, 2, 3], 'children':
00190                 [('b', {'children':
00191                     [('c', {'children':
00192                         [('d', {'nice': 'opt'})], 'something': (1, 2)
00193                     })]
00194                 })]
00195             })], indent=indent, indent_size=indent_size)[0]
00196 
00197         def sample_expected(indent=0, indent_size=2):
00198             spaces = lambda amount=0: ' ' * (amount + indent)
00199             return (
00200                 "%sa -other {1 2 3} -children {\n"
00201                 "%sb -children {\n"
00202                 "%sc -something {1 2} -children {\n"
00203                 "%sd -nice opt\n"
00204                 "%s}\n"
00205                 "%s}\n"
00206                 "%s}" % (spaces(), spaces(indent_size),
00207                     spaces(2 * indent_size), spaces(3 * indent_size),
00208                     spaces(2 * indent_size), spaces(indent_size), spaces()))
00209 
00210         # empty layout
00211         self.assertEqual(ttk._format_layoutlist([])[0], '')
00212 
00213         # _format_layoutlist always expects the second item (in every item)
00214         # to act like a dict (except when the value evalutes to False).
00215         self.assertRaises(AttributeError,
00216             ttk._format_layoutlist, [('a', 'b')])
00217 
00218         smallest = ttk._format_layoutlist([('a', None)], indent=0)
00219         self.assertEqual(smallest,
00220             ttk._format_layoutlist([('a', '')], indent=0))
00221         self.assertEqual(smallest[0], 'a')
00222 
00223         # testing indentation levels
00224         self.assertEqual(sample(), sample_expected())
00225         for i in range(4):
00226             self.assertEqual(sample(i), sample_expected(i))
00227             self.assertEqual(sample(i, i), sample_expected(i, i))
00228 
00229         # invalid layout format, different kind of exceptions will be
00230         # raised by internal functions
00231 
00232         # plain wrong format
00233         self.assertRaises(ValueError, ttk._format_layoutlist,
00234             ['bad', 'format'])
00235         # will try to use iteritems in the 'bad' string
00236         self.assertRaises(AttributeError, ttk._format_layoutlist,
00237            [('name', 'bad')])
00238         # bad children formatting
00239         self.assertRaises(ValueError, ttk._format_layoutlist,
00240             [('name', {'children': {'a': None}})])
00241 

Here is the call graph for this function:

Definition at line 85 of file test_functions.py.

00085 
00086     def test_format_mapdict(self):
00087         opts = {'a': [('b', 'c', 'val'), ('d', 'otherval'), ('', 'single')]}
00088         result = ttk._format_mapdict(opts)
00089         self.assertEqual(len(result), len(list(opts.keys())) * 2)
00090         self.assertEqual(result, ('-a', '{b c} val d otherval {} single'))
00091         self.assertEqual(ttk._format_mapdict(opts, script=True),
00092             ('-a', '{{b c} val d otherval {} single}'))
00093 
00094         self.assertEqual(ttk._format_mapdict({2: []}), ('-2', ''))
00095 
00096         opts = {'üñíćódè': [('á', 'vãl')]}
00097         result = ttk._format_mapdict(opts)
00098         self.assertEqual(result, ('-üñíćódè', 'á vãl'))
00099 
00100         # empty states
00101         valid = {'opt': [('', '', 'hi')]}
00102         self.assertEqual(ttk._format_mapdict(valid), ('-opt', '{ } hi'))
00103 
00104         # when passing multiple states, they all must be strings
00105         invalid = {'opt': [(1, 2, 'valid val')]}
00106         self.assertRaises(TypeError, ttk._format_mapdict, invalid)
00107         invalid = {'opt': [([1], '2', 'valid val')]}
00108         self.assertRaises(TypeError, ttk._format_mapdict, invalid)
00109         # but when passing a single state, it can be anything
00110         valid = {'opt': [[1, 'value']]}
00111         self.assertEqual(ttk._format_mapdict(valid), ('-opt', '1 value'))
00112         # special attention to single states which evalute to False
00113         for stateval in (None, 0, False, '', set()): # just some samples
00114             valid = {'opt': [(stateval, 'value')]}
00115             self.assertEqual(ttk._format_mapdict(valid),
00116                 ('-opt', '{} value'))
00117 
00118         # values must be iterable
00119         opts = {'a': None}
00120         self.assertRaises(TypeError, ttk._format_mapdict, opts)
00121 
00122         # items in the value must have size >= 2
00123         self.assertRaises(IndexError, ttk._format_mapdict,
00124             {'a': [('invalid', )]})
00125 

Here is the call graph for this function:

Definition at line 27 of file test_functions.py.

00027 
00028     def test_format_optdict(self):
00029         def check_against(fmt_opts, result):
00030             for i in range(0, len(fmt_opts), 2):
00031                 self.assertEqual(result.pop(fmt_opts[i]), fmt_opts[i + 1])
00032             if result:
00033                 self.fail("result still got elements: %s" % result)
00034 
00035         # passing an empty dict should return an empty object (tuple here)
00036         self.assertFalse(ttk._format_optdict({}))
00037 
00038         # check list formatting
00039         check_against(
00040             ttk._format_optdict({'fg': 'blue', 'padding': [1, 2, 3, 4]}),
00041             {'-fg': 'blue', '-padding': '1 2 3 4'})
00042 
00043         # check tuple formatting (same as list)
00044         check_against(
00045             ttk._format_optdict({'test': (1, 2, '', 0)}),
00046             {'-test': '1 2 {} 0'})
00047 
00048         # check untouched values
00049         check_against(
00050             ttk._format_optdict({'test': {'left': 'as is'}}),
00051             {'-test': {'left': 'as is'}})
00052 
00053         # check script formatting and untouched value(s)
00054         check_against(
00055             ttk._format_optdict(
00056                 {'test': [1, -1, '', '2m', 0], 'nochange1': 3,
00057                  'nochange2': 'abc def'}, script=True),
00058             {'-test': '{1 -1 {} 2m 0}', '-nochange1': 3,
00059              '-nochange2': 'abc def' })
00060 
00061         opts = {'αβγ': True, 'á': False}
00062         orig_opts = opts.copy()
00063         # check if giving unicode keys is fine
00064         check_against(ttk._format_optdict(opts), {'-αβγ': True, '-á': False})
00065         # opts should remain unchanged
00066         self.assertEqual(opts, orig_opts)
00067 
00068         # passing values with spaces inside a tuple/list
00069         check_against(
00070             ttk._format_optdict(
00071                 {'option': ('one two', 'three')}),
00072             {'-option': '{one two} three'})
00073 
00074         # ignore an option
00075         amount_opts = len(ttk._format_optdict(opts, ignore=('á'))) / 2
00076         self.assertEqual(amount_opts, len(opts) - 1)
00077 
00078         # ignore non-existing options
00079         amount_opts = len(ttk._format_optdict(opts, ignore=('á', 'b'))) / 2
00080         self.assertEqual(amount_opts, len(opts) - 1)
00081 
00082         # ignore every option
00083         self.assertFalse(ttk._format_optdict(opts, ignore=list(opts.keys())))
00084 

Here is the call graph for this function:

Definition at line 324 of file test_functions.py.

00324 
00325     def test_list_from_layouttuple(self):
00326         # empty layout tuple
00327         self.assertFalse(ttk._list_from_layouttuple(()))
00328 
00329         # shortest layout tuple
00330         self.assertEqual(ttk._list_from_layouttuple(('name', )),
00331             [('name', {})])
00332 
00333         # not so interesting ltuple
00334         sample_ltuple = ('name', '-option', 'value')
00335         self.assertEqual(ttk._list_from_layouttuple(sample_ltuple),
00336             [('name', {'option': 'value'})])
00337 
00338         # empty children
00339         self.assertEqual(ttk._list_from_layouttuple(
00340             ('something', '-children', ())),
00341             [('something', {'children': []})]
00342         )
00343 
00344         # more interesting ltuple
00345         ltuple = (
00346             'name', '-option', 'niceone', '-children', (
00347                 ('otherone', '-children', (
00348                     ('child', )), '-otheropt', 'othervalue'
00349                 )
00350             )
00351         )
00352         self.assertEqual(ttk._list_from_layouttuple(ltuple),
00353             [('name', {'option': 'niceone', 'children':
00354                 [('otherone', {'otheropt': 'othervalue', 'children':
00355                     [('child', {})]
00356                 })]
00357             })]
00358         )
00359 
00360         # bad tuples
00361         self.assertRaises(ValueError, ttk._list_from_layouttuple,
00362             ('name', 'no_minus'))
00363         self.assertRaises(ValueError, ttk._list_from_layouttuple,
00364             ('name', 'no_minus', 'value'))
00365         self.assertRaises(ValueError, ttk._list_from_layouttuple,
00366             ('something', '-children')) # no children
00367         self.assertRaises(ValueError, ttk._list_from_layouttuple,
00368             ('something', '-children', 'value')) # invalid children
00369 

Here is the call graph for this function:

Definition at line 307 of file test_functions.py.

00307 
00308     def test_list_from_statespec(self):
00309         def test_it(sspec, value, res_value, states):
00310             self.assertEqual(ttk._list_from_statespec(
00311                 (sspec, value)), [states + (res_value, )])
00312 
00313         states_even = tuple('state%d' % i for i in range(6))
00314         statespec = MockStateSpec(*states_even)
00315         test_it(statespec, 'val', 'val', states_even)
00316         test_it(statespec, MockTclObj('val'), 'val', states_even)
00317 
00318         states_odd = tuple('state%d' % i for i in range(5))
00319         statespec = MockStateSpec(*states_odd)
00320         test_it(statespec, 'val', 'val', states_odd)
00321 
00322         test_it(('a', 'b', 'c'), MockTclObj('val'), 'val', ('a', 'b', 'c'))
00323 

Here is the call graph for this function:

Definition at line 242 of file test_functions.py.

00242 
00243     def test_script_from_settings(self):
00244         # empty options
00245         self.assertFalse(ttk._script_from_settings({'name':
00246             {'configure': None, 'map': None, 'element create': None}}))
00247 
00248         # empty layout
00249         self.assertEqual(
00250             ttk._script_from_settings({'name': {'layout': None}}),
00251             "ttk::style layout name {\nnull\n}")
00252 
00253         configdict = {'αβγ': True, 'á': False}
00254         self.assertTrue(
00255             ttk._script_from_settings({'name': {'configure': configdict}}))
00256 
00257         mapdict = {'üñíćódè': [('á', 'vãl')]}
00258         self.assertTrue(
00259             ttk._script_from_settings({'name': {'map': mapdict}}))
00260 
00261         # invalid image element
00262         self.assertRaises(IndexError,
00263             ttk._script_from_settings, {'name': {'element create': ['image']}})
00264 
00265         # minimal valid image
00266         self.assertTrue(ttk._script_from_settings({'name':
00267             {'element create': ['image', 'name']}}))
00268 
00269         image = {'thing': {'element create':
00270             ['image', 'name', ('state1', 'state2', 'val')]}}
00271         self.assertEqual(ttk._script_from_settings(image),
00272             "ttk::style element create thing image {name {state1 state2} val} ")
00273 
00274         image['thing']['element create'].append({'opt': 30})
00275         self.assertEqual(ttk._script_from_settings(image),
00276             "ttk::style element create thing image {name {state1 state2} val} "
00277             "-opt 30")
00278 
00279         image['thing']['element create'][-1]['opt'] = [MockTclObj(3),
00280             MockTclObj('2m')]
00281         self.assertEqual(ttk._script_from_settings(image),
00282             "ttk::style element create thing image {name {state1 state2} val} "
00283             "-opt {3 2m}")
00284 

Here is the call graph for this function:

Definition at line 370 of file test_functions.py.

00370 
00371     def test_val_or_dict(self):
00372         def func(opt, val=None):
00373             if val is None:
00374                 return "test val"
00375             return (opt, val)
00376 
00377         options = {'test': None}
00378         self.assertEqual(ttk._val_or_dict(options, func), "test val")
00379 
00380         options = {'test': 3}
00381         self.assertEqual(ttk._val_or_dict(options, func), options)
00382 

Here is the call graph for this function:


The documentation for this class was generated from the following file: