[Python-checkins] r66075 - in sandbox/trunk/ttk-gsoc/src: 2.x/test/test_base_widget.py 2.x/test/test_other_widgets.py 2.x/ttk.py 3.x/test/test_base_widget.py 3.x/test/test_other_widgets.py 3.x/ttk.py
guilherme.polo
python-checkins at python.org
Sat Aug 30 20:05:45 CEST 2008
Author: guilherme.polo
Date: Sat Aug 30 20:05:44 2008
New Revision: 66075
Log:
Renamed test_base_widget to test_other_widgets;
Added tests for ttk.Button and ttk.Combobox;
Added a test for the identify method in ttk.Widget;
ttk.Combobox correctly accepts empty string as a value now.
Added:
sandbox/trunk/ttk-gsoc/src/2.x/test/test_other_widgets.py
- copied, changed from r66068, /sandbox/trunk/ttk-gsoc/src/2.x/test/test_base_widget.py
sandbox/trunk/ttk-gsoc/src/3.x/test/test_other_widgets.py
- copied, changed from r66068, /sandbox/trunk/ttk-gsoc/src/3.x/test/test_base_widget.py
Removed:
sandbox/trunk/ttk-gsoc/src/2.x/test/test_base_widget.py
sandbox/trunk/ttk-gsoc/src/3.x/test/test_base_widget.py
Modified:
sandbox/trunk/ttk-gsoc/src/2.x/ttk.py
sandbox/trunk/ttk-gsoc/src/3.x/ttk.py
Deleted: sandbox/trunk/ttk-gsoc/src/2.x/test/test_base_widget.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/2.x/test/test_base_widget.py Sat Aug 30 20:05:44 2008
+++ (empty file)
@@ -1,53 +0,0 @@
-import sys
-import unittest
-import Tkinter
-import ttk
-
-import support
-
-class WidgetTest(unittest.TestCase):
- """Tests methods available in every ttk widget."""
-
- def setUp(self):
- self.widget = ttk.Button()
- self.widget.pack()
- self.widget.wait_visibility()
-
- def tearDown(self):
- self.widget.destroy()
-
-
- def test_widget_state(self):
- # XXX not sure about the portability of all these tests
- self.failUnlessEqual(self.widget.state(), ())
- self.failUnlessEqual(self.widget.instate(['!disabled']), True)
-
- # changing from !disabled to disabled
- self.failUnlessEqual(self.widget.state(['disabled']), ('!disabled', ))
- # no state change
- self.failUnlessEqual(self.widget.state(['disabled']), ())
- # change back to !disable but also active
- self.failUnlessEqual(self.widget.state(['!disabled', 'active']),
- ('!active', 'disabled'))
- # no state changes, again
- self.failUnlessEqual(self.widget.state(['!disabled', 'active']), ())
- self.failUnlessEqual(self.widget.state(['active', '!disabled']), ())
-
- def test_cb(arg1, **kw):
- return arg1, kw
- self.failUnlessEqual(self.widget.instate(['!disabled'],
- test_cb, "hi", **{"msg": "there"}),
- ('hi', {'msg': 'there'}))
-
- # attempt to set invalid statespec
- self.failUnlessRaises(Tkinter.TclError, self.widget.instate,
- ['badstate'])
- self.failUnlessRaises(Tkinter.TclError, self.widget.instate,
- ['disabled', 'badstate'])
-
-
-def test_main():
- support.run(WidgetTest)
-
-if __name__ == "__main__":
- test_main()
Copied: sandbox/trunk/ttk-gsoc/src/2.x/test/test_other_widgets.py (from r66068, /sandbox/trunk/ttk-gsoc/src/2.x/test/test_base_widget.py)
==============================================================================
--- /sandbox/trunk/ttk-gsoc/src/2.x/test/test_base_widget.py (original)
+++ sandbox/trunk/ttk-gsoc/src/2.x/test/test_other_widgets.py Sat Aug 30 20:05:44 2008
@@ -1,4 +1,3 @@
-import sys
import unittest
import Tkinter
import ttk
@@ -17,6 +16,22 @@
self.widget.destroy()
+ def test_identify(self):
+ def identify(event):
+ btn.update_idletasks()
+ should_be_label = btn.identify(5, 5)
+ should_be_empty = btn.identify(-1, -1)
+ btn.destroy()
+ btn.quit()
+ self.failUnlessEqual(should_be_label, "label")
+ self.failUnlessEqual(should_be_empty, "")
+
+ btn = ttk.Button()
+ btn.pack()
+ btn.bind('<Map>', identify)
+ btn.mainloop()
+
+
def test_widget_state(self):
# XXX not sure about the portability of all these tests
self.failUnlessEqual(self.widget.state(), ())
@@ -46,8 +61,123 @@
['disabled', 'badstate'])
+class ButtonTest(unittest.TestCase):
+
+ def test_invoke(self):
+ success = []
+ def cb_test():
+ success.append(1)
+ btn = ttk.Button(command=cb_test)
+ btn.invoke()
+ self.failUnless(success)
+
+
+class CheckbuttonTest(unittest.TestCase):
+ # XXX broken for now
+
+ def test_invoke(self):
+ success = []
+ def cb_test():
+ success.append(1)
+ cbtn = ttk.Checkbutton(command=cb_test)
+ print cbtn.tk.globalgetvar(cbtn['variable'])
+ print cbtn['variable'], "<<"
+ cbtn.invoke()
+ self.failUnless(success)
+
+
+class ComboboxTest(unittest.TestCase):
+
+ def setUp(self):
+ self.combo = ttk.Combobox()
+
+ def tearDown(self):
+ self.combo.destroy()
+
+ def _show_drop_down_listbox(self):
+ width = self.combo.winfo_width()
+ self.combo.event_generate('<ButtonPress-1>', x=width - 5, y=5)
+ self.combo.event_generate('<ButtonRelease-1>', x=width - 5, y=5)
+ self.combo.update_idletasks()
+
+
+ def test_virtual_event(self):
+ success = []
+ def cb_test(evt):
+ success.append(True)
+
+ self.combo['values'] = [1]
+ self.combo.bind('<<ComboboxSelected>>', cb_test)
+ self.combo.pack()
+ self.combo.wait_visibility()
+
+ height = self.combo.winfo_height()
+ self._show_drop_down_listbox()
+ self.combo.event_generate('<Return>')
+
+ self.failUnless(success)
+
+
+ def test_postcommand(self):
+ success = []
+ def cb_test():
+ success.append(True)
+
+ self.combo['postcommand'] = cb_test
+ self.combo.pack()
+ self.combo.wait_visibility()
+
+ self._show_drop_down_listbox()
+ self.failUnless(success)
+
+ # testing postcommand removal
+ self.combo['postcommand'] = ''
+ self._show_drop_down_listbox()
+ self.failUnlessEqual(len(success), 1)
+
+
+ def test_values(self):
+ def check_get_current(getval, currval):
+ self.failUnlessEqual(self.combo.get(), getval)
+ self.failUnlessEqual(self.combo.current(), currval)
+
+ check_get_current('', -1)
+
+ self.combo['values'] = ['a', 1, 'c']
+
+ self.combo.set('c')
+ check_get_current('c', 2)
+
+ self.combo.current(0)
+ check_get_current('a', 0)
+
+ self.combo.set('d')
+ check_get_current('d', -1)
+
+ # testing values with empty string
+ self.combo.set('')
+ self.combo['values'] = (1, 2, '', 3)
+ check_get_current('', 2)
+
+ # testing values with empty string set through configure
+ self.combo.configure(values=[1, '', 2])
+ self.failUnlessEqual(self.combo['values'], ('1', '', '2'))
+
+ # out of range
+ self.failUnlessRaises(Tkinter.TclError, self.combo.current,
+ len(self.combo['values']))
+ # it expects an integer (or something that can be converted to int)
+ self.failUnlessRaises(Tkinter.TclError, self.combo.current, '')
+
+ # testing creating combobox with empty string in values
+ combo2 = ttk.Combobox(values=[1, 2, ''])
+ self.failUnlessEqual(combo2['values'], ('1', '2', ''))
+ combo2.destroy()
+
+
def test_main():
- support.run(WidgetTest)
+ support.run(WidgetTest, ButtonTest, #CheckbuttonTest,
+ ComboboxTest)
if __name__ == "__main__":
test_main()
Modified: sandbox/trunk/ttk-gsoc/src/2.x/ttk.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/2.x/ttk.py (original)
+++ sandbox/trunk/ttk-gsoc/src/2.x/ttk.py Sat Aug 30 20:05:44 2008
@@ -687,9 +687,31 @@
exportselection, justify, height, postcommand, state,
textvariable, values, width
"""
+ if "values" in kw:
+ # may need special formatting if any value is an empty string
+ kw["values"] = _format_optdict({'v': kw["values"]})[1]
+
Entry.__init__(self, master, "ttk::combobox", **kw)
+ def __setitem__(self, item, value):
+ if item == "values":
+ # may need special formatting if any value is an empty string
+ value = _format_optdict({item: value})[1]
+
+ Entry.__setitem__(self, item, value)
+
+
+ def configure(self, cnf=None, **kw):
+ """Custom Combobox configure, created to properly format the values
+ option."""
+ if "values" in kw:
+ # may need special formatting if any value is an empty string
+ kw["values"] = _format_optdict({'v': kw["values"]})[1]
+
+ return Entry.configure(self, cnf, **kw)
+
+
def current(self, newindex=None):
"""If newindex is supplied, sets the combobox value to the
element at position newindex in the list of values. Otherwise,
Deleted: sandbox/trunk/ttk-gsoc/src/3.x/test/test_base_widget.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/3.x/test/test_base_widget.py Sat Aug 30 20:05:44 2008
+++ (empty file)
@@ -1,53 +0,0 @@
-import sys
-import unittest
-import tkinter
-import ttk
-
-import support
-
-class WidgetTest(unittest.TestCase):
- """Tests methods available in every ttk widget."""
-
- def setUp(self):
- self.widget = ttk.Button()
- self.widget.pack()
- self.widget.wait_visibility()
-
- def tearDown(self):
- self.widget.destroy()
-
-
- def test_widget_state(self):
- # XXX not sure about the portability of all these tests
- self.failUnlessEqual(self.widget.state(), ())
- self.failUnlessEqual(self.widget.instate(['!disabled']), True)
-
- # changing from !disabled to disabled
- self.failUnlessEqual(self.widget.state(['disabled']), ('!disabled', ))
- # no state change
- self.failUnlessEqual(self.widget.state(['disabled']), ())
- # change back to !disable but also active
- self.failUnlessEqual(self.widget.state(['!disabled', 'active']),
- ('!active', 'disabled'))
- # no state changes, again
- self.failUnlessEqual(self.widget.state(['!disabled', 'active']), ())
- self.failUnlessEqual(self.widget.state(['active', '!disabled']), ())
-
- def test_cb(arg1, **kw):
- return arg1, kw
- self.failUnlessEqual(self.widget.instate(['!disabled'],
- test_cb, "hi", **{"msg": "there"}),
- ('hi', {'msg': 'there'}))
-
- # attempt to set invalid statespec
- self.failUnlessRaises(tkinter.TclError, self.widget.instate,
- ['badstate'])
- self.failUnlessRaises(tkinter.TclError, self.widget.instate,
- ['disabled', 'badstate'])
-
-
-def test_main():
- support.run(WidgetTest)
-
-if __name__ == "__main__":
- test_main()
Copied: sandbox/trunk/ttk-gsoc/src/3.x/test/test_other_widgets.py (from r66068, /sandbox/trunk/ttk-gsoc/src/3.x/test/test_base_widget.py)
==============================================================================
--- /sandbox/trunk/ttk-gsoc/src/3.x/test/test_base_widget.py (original)
+++ sandbox/trunk/ttk-gsoc/src/3.x/test/test_other_widgets.py Sat Aug 30 20:05:44 2008
@@ -1,4 +1,3 @@
-import sys
import unittest
import tkinter
import ttk
@@ -17,6 +16,22 @@
self.widget.destroy()
+ def test_identify(self):
+ def identify(event):
+ btn.update_idletasks()
+ should_be_label = btn.identify(5, 5)
+ should_be_empty = btn.identify(-1, -1)
+ btn.destroy()
+ btn.quit()
+ self.failUnlessEqual(should_be_label, "label")
+ self.failUnlessEqual(should_be_empty, "")
+
+ btn = ttk.Button()
+ btn.pack()
+ btn.bind('<Map>', identify)
+ btn.mainloop()
+
+
def test_widget_state(self):
# XXX not sure about the portability of all these tests
self.failUnlessEqual(self.widget.state(), ())
@@ -46,8 +61,123 @@
['disabled', 'badstate'])
+class ButtonTest(unittest.TestCase):
+
+ def test_invoke(self):
+ success = []
+ def cb_test():
+ success.append(1)
+ btn = ttk.Button(command=cb_test)
+ btn.invoke()
+ self.failUnless(success)
+
+
+class CheckbuttonTest(unittest.TestCase):
+ # XXX broken for now
+
+ def test_invoke(self):
+ success = []
+ def cb_test():
+ success.append(1)
+ cbtn = ttk.Checkbutton(command=cb_test)
+ print(cbtn.tk.globalgetvar(cbtn['variable']))
+ print(cbtn['variable'], "<<")
+ cbtn.invoke()
+ self.failUnless(success)
+
+
+class ComboboxTest(unittest.TestCase):
+
+ def setUp(self):
+ self.combo = ttk.Combobox()
+
+ def tearDown(self):
+ self.combo.destroy()
+
+ def _show_drop_down_listbox(self):
+ width = self.combo.winfo_width()
+ self.combo.event_generate('<ButtonPress-1>', x=width - 5, y=5)
+ self.combo.event_generate('<ButtonRelease-1>', x=width - 5, y=5)
+ self.combo.update_idletasks()
+
+
+ def test_virtual_event(self):
+ success = []
+ def cb_test(evt):
+ success.append(True)
+
+ self.combo['values'] = [1]
+ self.combo.bind('<<ComboboxSelected>>', cb_test)
+ self.combo.pack()
+ self.combo.wait_visibility()
+
+ height = self.combo.winfo_height()
+ self._show_drop_down_listbox()
+ self.combo.event_generate('<Return>')
+
+ self.failUnless(success)
+
+
+ def test_postcommand(self):
+ success = []
+ def cb_test():
+ success.append(True)
+
+ self.combo['postcommand'] = cb_test
+ self.combo.pack()
+ self.combo.wait_visibility()
+
+ self._show_drop_down_listbox()
+ self.failUnless(success)
+
+ # testing postcommand removal
+ self.combo['postcommand'] = ''
+ self._show_drop_down_listbox()
+ self.failUnlessEqual(len(success), 1)
+
+
+ def test_values(self):
+ def check_get_current(getval, currval):
+ self.failUnlessEqual(self.combo.get(), getval)
+ self.failUnlessEqual(self.combo.current(), currval)
+
+ check_get_current('', -1)
+
+ self.combo['values'] = ['a', 1, 'c']
+
+ self.combo.set('c')
+ check_get_current('c', 2)
+
+ self.combo.current(0)
+ check_get_current('a', 0)
+
+ self.combo.set('d')
+ check_get_current('d', -1)
+
+ # testing values with empty string
+ self.combo.set('')
+ self.combo['values'] = (1, 2, '', 3)
+ check_get_current('', 2)
+
+ # testing values with empty string set through configure
+ self.combo.configure(values=[1, '', 2])
+ self.failUnlessEqual(self.combo['values'], ('1', '', '2'))
+
+ # out of range
+ self.failUnlessRaises(tkinter.TclError, self.combo.current,
+ len(self.combo['values']))
+ # it expects an integer (or something that can be converted to int)
+ self.failUnlessRaises(tkinter.TclError, self.combo.current, '')
+
+ # testing creating combobox with empty string in values
+ combo2 = ttk.Combobox(values=[1, 2, ''])
+ self.failUnlessEqual(combo2['values'], ('1', '2', ''))
+ combo2.destroy()
+
+
def test_main():
- support.run(WidgetTest)
+ support.run(WidgetTest, ButtonTest, #CheckbuttonTest,
+ ComboboxTest)
if __name__ == "__main__":
test_main()
Modified: sandbox/trunk/ttk-gsoc/src/3.x/ttk.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/3.x/ttk.py (original)
+++ sandbox/trunk/ttk-gsoc/src/3.x/ttk.py Sat Aug 30 20:05:44 2008
@@ -687,9 +687,31 @@
exportselection, justify, height, postcommand, state,
textvariable, values, width
"""
+ if "values" in kw:
+ # may need special formatting if any value is an empty string
+ kw["values"] = _format_optdict({'v': kw["values"]})[1]
+
Entry.__init__(self, master, "ttk::combobox", **kw)
+ def __setitem__(self, item, value):
+ if item == "values":
+ # may need special formatting if any value is an empty string
+ value = _format_optdict({item: value})[1]
+
+ Entry.__setitem__(self, item, value)
+
+
+ def configure(self, cnf=None, **kw):
+ """Custom Combobox configure, created to properly format the values
+ option."""
+ if "values" in kw:
+ # may need special formatting if any value is an empty string
+ kw["values"] = _format_optdict({'v': kw["values"]})[1]
+
+ return Entry.configure(self, cnf, **kw)
+
+
def current(self, newindex=None):
"""If newindex is supplied, sets the combobox value to the
element at position newindex in the list of values. Otherwise,
More information about the Python-checkins
mailing list