[Python-checkins] r66087 - in sandbox/trunk/ttk-gsoc/src: 2.x/test/test_other_widgets.py 2.x/ttk.py 3.x/test/test_other_widgets.py 3.x/ttk.py
guilherme.polo
python-checkins at python.org
Sun Aug 31 15:48:01 CEST 2008
Author: guilherme.polo
Date: Sun Aug 31 15:48:00 2008
New Revision: 66087
Log:
Fixed a problem in Widget.state that would change the widget state when passing
a None statespec (tests added);
Added tests for the Entry widget.
Modified:
sandbox/trunk/ttk-gsoc/src/2.x/test/test_other_widgets.py
sandbox/trunk/ttk-gsoc/src/2.x/ttk.py
sandbox/trunk/ttk-gsoc/src/3.x/test/test_other_widgets.py
sandbox/trunk/ttk-gsoc/src/3.x/ttk.py
Modified: sandbox/trunk/ttk-gsoc/src/2.x/test/test_other_widgets.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/2.x/test/test_other_widgets.py (original)
+++ sandbox/trunk/ttk-gsoc/src/2.x/test/test_other_widgets.py Sun Aug 31 15:48:00 2008
@@ -17,19 +17,13 @@
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()
+ self.widget.update_idletasks()
+ self.failUnlessEqual(self.widget.identify(5, 5), "label")
+ self.failUnlessEqual(self.widget.identify(-1, -1), "")
+
+ self.failUnlessRaises(Tkinter.TclError, self.widget.identify, None, 5)
+ self.failUnlessRaises(Tkinter.TclError, self.widget.identify, 5, None)
+ self.failUnlessRaises(Tkinter.TclError, self.widget.identify, 5, '')
def test_widget_state(self):
@@ -55,10 +49,17 @@
('hi', {'msg': 'there'}))
# attempt to set invalid statespec
+ currstate = self.widget.state()
self.failUnlessRaises(Tkinter.TclError, self.widget.instate,
['badstate'])
self.failUnlessRaises(Tkinter.TclError, self.widget.instate,
['disabled', 'badstate'])
+ # verify that widget didn't change its state
+ self.failUnlessEqual(currstate, self.widget.state())
+
+ # ensuring that passing None as state doesn't modify current state
+ self.widget.state(['active', '!disabled'])
+ self.failUnlessEqual(self.widget.state(), ('active', ))
class ButtonTest(unittest.TestCase):
@@ -175,9 +176,112 @@
combo2.destroy()
+class EntryTest(unittest.TestCase):
+
+ def setUp(self):
+ self.entry = ttk.Entry()
+
+ def tearDown(self):
+ self.entry.destroy()
+
+
+ def test_bbox(self):
+ self.failUnlessEqual(len(self.entry.bbox(0)), 4)
+ for item in self.entry.bbox(0):
+ self.failUnless(isinstance(item, int))
+
+ self.failUnlessRaises(Tkinter.TclError, self.entry.bbox, 'noindex')
+ self.failUnlessRaises(Tkinter.TclError, self.entry.bbox, None)
+
+
+ def test_identify(self):
+ self.entry.pack()
+ self.entry.wait_visibility()
+ self.entry.update_idletasks()
+
+ self.failUnlessEqual(self.entry.identify(5, 5), "textarea")
+ self.failUnlessEqual(self.entry.identify(-1, -1), "")
+
+ self.failUnlessRaises(Tkinter.TclError, self.entry.identify, None, 5)
+ self.failUnlessRaises(Tkinter.TclError, self.entry.identify, 5, None)
+ self.failUnlessRaises(Tkinter.TclError, self.entry.identify, 5, '')
+
+
+ def test_validation_options(self):
+ success = []
+ def test_invalid():
+ success.append(True)
+
+ self.entry['validate'] = 'none'
+ self.entry['validatecommand'] = lambda: False
+
+ self.entry['invalidcommand'] = test_invalid
+ self.entry.validate()
+ self.failUnless(success)
+
+ self.entry['invalidcommand'] = ''
+ self.entry.validate()
+ self.failUnlessEqual(len(success), 1)
+
+ self.entry['invalidcommand'] = test_invalid
+ self.entry['validatecommand'] = lambda: True
+ self.entry.validate()
+ self.failUnlessEqual(len(success), 1)
+
+ self.entry['validatecommand'] = ''
+ self.entry.validate()
+ self.failUnlessEqual(len(success), 1)
+
+ self.entry['validatecommand'] = True
+ self.failUnlessRaises(Tkinter.TclError, self.entry.validate)
+
+
+ def test_validation(self):
+ validation = []
+ def validate(to_insert):
+ if not 'a' <= to_insert.lower() <= 'z':
+ validation.append(False)
+ return False
+ validation.append(True)
+ return True
+
+ self.entry['validate'] = 'key'
+ self.entry['validatecommand'] = self.entry.register(validate), '%S'
+
+ self.entry.insert('end', 1)
+ self.entry.insert('end', 'a')
+ self.failUnlessEqual(validation, [False, True])
+ self.failUnlessEqual(self.entry.get(), 'a')
+
+
+ def test_revalidation(self):
+ def validate(content):
+ for letter in content:
+ if not 'a' <= letter.lower() <= 'z':
+ return False
+ return True
+
+ self.entry['validatecommand'] = self.entry.register(validate), '%P'
+
+ self.entry.insert('end', 'avocado')
+ self.failUnlessEqual(self.entry.validate(), True)
+ self.failUnlessEqual(self.entry.state(), ())
+
+ self.entry.delete(0, 'end')
+ self.failUnlessEqual(self.entry.get(), '')
+
+ self.entry.insert('end', 'a1b')
+ self.failUnlessEqual(self.entry.validate(), False)
+ self.failUnlessEqual(self.entry.state(), ('invalid', ))
+
+ self.entry.delete(1)
+ self.failUnlessEqual(self.entry.validate(), True)
+ self.failUnlessEqual(self.entry.state(), ())
+
+
def test_main():
support.run(WidgetTest, ButtonTest, #CheckbuttonTest,
- ComboboxTest)
+ ComboboxTest, EntryTest)
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 Sun Aug 31 15:48:00 2008
@@ -12,7 +12,7 @@
of the widgets appearance lies at Themes.
"""
-__version__ = "0.2.2"
+__version__ = "0.2.3"
__author__ = "Guilherme Polo <ggpolo at gmail.com>"
@@ -570,10 +570,10 @@
set according to the statespec flags and then a new state spec
is returned indicating which flags were changed. statespec is
expected to be a sequence."""
- statespec = statespec or ''
+ if statespec is not None:
+ statespec = ' '.join(statespec)
- return self.tk.splitlist(
- str(self.tk.call(self._w, "state", ' '.join(statespec))))
+ return self.tk.splitlist(str(self.tk.call(self._w, "state", statespec)))
class Button(Widget):
@@ -666,9 +666,9 @@
def validate(self):
"""Force revalidation, independent of the conditions specified
- by the validate option. Returns 0 if validation fails, 1 if it
- succeeds. Sets or clears the invalid state accordingly."""
- return self.tk.call(self._w, "validate")
+ by the validate option. Returns False if validation fails, True
+ if it succeeds. Sets or clears the invalid state accordingly."""
+ return bool(self.tk.call(self._w, "validate"))
class Combobox(Entry):
Modified: sandbox/trunk/ttk-gsoc/src/3.x/test/test_other_widgets.py
==============================================================================
--- sandbox/trunk/ttk-gsoc/src/3.x/test/test_other_widgets.py (original)
+++ sandbox/trunk/ttk-gsoc/src/3.x/test/test_other_widgets.py Sun Aug 31 15:48:00 2008
@@ -17,19 +17,13 @@
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()
+ self.widget.update_idletasks()
+ self.failUnlessEqual(self.widget.identify(5, 5), "label")
+ self.failUnlessEqual(self.widget.identify(-1, -1), "")
+
+ self.failUnlessRaises(tkinter.TclError, self.widget.identify, None, 5)
+ self.failUnlessRaises(tkinter.TclError, self.widget.identify, 5, None)
+ self.failUnlessRaises(tkinter.TclError, self.widget.identify, 5, '')
def test_widget_state(self):
@@ -55,10 +49,17 @@
('hi', {'msg': 'there'}))
# attempt to set invalid statespec
+ currstate = self.widget.state()
self.failUnlessRaises(tkinter.TclError, self.widget.instate,
['badstate'])
self.failUnlessRaises(tkinter.TclError, self.widget.instate,
['disabled', 'badstate'])
+ # verify that widget didn't change its state
+ self.failUnlessEqual(currstate, self.widget.state())
+
+ # ensuring that passing None as state doesn't modify current state
+ self.widget.state(['active', '!disabled'])
+ self.failUnlessEqual(self.widget.state(), ('active', ))
class ButtonTest(unittest.TestCase):
@@ -175,9 +176,112 @@
combo2.destroy()
+class EntryTest(unittest.TestCase):
+
+ def setUp(self):
+ self.entry = ttk.Entry()
+
+ def tearDown(self):
+ self.entry.destroy()
+
+
+ def test_bbox(self):
+ self.failUnlessEqual(len(self.entry.bbox(0)), 4)
+ for item in self.entry.bbox(0):
+ self.failUnless(isinstance(item, int))
+
+ self.failUnlessRaises(tkinter.TclError, self.entry.bbox, 'noindex')
+ self.failUnlessRaises(tkinter.TclError, self.entry.bbox, None)
+
+
+ def test_identify(self):
+ self.entry.pack()
+ self.entry.wait_visibility()
+ self.entry.update_idletasks()
+
+ self.failUnlessEqual(self.entry.identify(5, 5), "textarea")
+ self.failUnlessEqual(self.entry.identify(-1, -1), "")
+
+ self.failUnlessRaises(tkinter.TclError, self.entry.identify, None, 5)
+ self.failUnlessRaises(tkinter.TclError, self.entry.identify, 5, None)
+ self.failUnlessRaises(tkinter.TclError, self.entry.identify, 5, '')
+
+
+ def test_validation_options(self):
+ success = []
+ def test_invalid():
+ success.append(True)
+
+ self.entry['validate'] = 'none'
+ self.entry['validatecommand'] = lambda: False
+
+ self.entry['invalidcommand'] = test_invalid
+ self.entry.validate()
+ self.failUnless(success)
+
+ self.entry['invalidcommand'] = ''
+ self.entry.validate()
+ self.failUnlessEqual(len(success), 1)
+
+ self.entry['invalidcommand'] = test_invalid
+ self.entry['validatecommand'] = lambda: True
+ self.entry.validate()
+ self.failUnlessEqual(len(success), 1)
+
+ self.entry['validatecommand'] = ''
+ self.entry.validate()
+ self.failUnlessEqual(len(success), 1)
+
+ self.entry['validatecommand'] = True
+ self.failUnlessRaises(tkinter.TclError, self.entry.validate)
+
+
+ def test_validation(self):
+ validation = []
+ def validate(to_insert):
+ if not 'a' <= to_insert.lower() <= 'z':
+ validation.append(False)
+ return False
+ validation.append(True)
+ return True
+
+ self.entry['validate'] = 'key'
+ self.entry['validatecommand'] = self.entry.register(validate), '%S'
+
+ self.entry.insert('end', 1)
+ self.entry.insert('end', 'a')
+ self.failUnlessEqual(validation, [False, True])
+ self.failUnlessEqual(self.entry.get(), 'a')
+
+
+ def test_revalidation(self):
+ def validate(content):
+ for letter in content:
+ if not 'a' <= letter.lower() <= 'z':
+ return False
+ return True
+
+ self.entry['validatecommand'] = self.entry.register(validate), '%P'
+
+ self.entry.insert('end', 'avocado')
+ self.failUnlessEqual(self.entry.validate(), True)
+ self.failUnlessEqual(self.entry.state(), ())
+
+ self.entry.delete(0, 'end')
+ self.failUnlessEqual(self.entry.get(), '')
+
+ self.entry.insert('end', 'a1b')
+ self.failUnlessEqual(self.entry.validate(), False)
+ self.failUnlessEqual(self.entry.state(), ('invalid', ))
+
+ self.entry.delete(1)
+ self.failUnlessEqual(self.entry.validate(), True)
+ self.failUnlessEqual(self.entry.state(), ())
+
+
def test_main():
support.run(WidgetTest, ButtonTest, #CheckbuttonTest,
- ComboboxTest)
+ ComboboxTest, EntryTest)
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 Sun Aug 31 15:48:00 2008
@@ -12,7 +12,7 @@
of the widgets appearance lies at Themes.
"""
-__version__ = "0.2.2"
+__version__ = "0.2.3"
__author__ = "Guilherme Polo <ggpolo at gmail.com>"
@@ -570,10 +570,10 @@
set according to the statespec flags and then a new state spec
is returned indicating which flags were changed. statespec is
expected to be a sequence."""
- statespec = statespec or ''
+ if statespec is not None:
+ statespec = ' '.join(statespec)
- return self.tk.splitlist(
- str(self.tk.call(self._w, "state", ' '.join(statespec))))
+ return self.tk.splitlist(str(self.tk.call(self._w, "state", statespec)))
class Button(Widget):
@@ -666,9 +666,9 @@
def validate(self):
"""Force revalidation, independent of the conditions specified
- by the validate option. Returns 0 if validation fails, 1 if it
- succeeds. Sets or clears the invalid state accordingly."""
- return self.tk.call(self._w, "validate")
+ by the validate option. Returns False if validation fails, True
+ if it succeeds. Sets or clears the invalid state accordingly."""
+ return bool(self.tk.call(self._w, "validate"))
class Combobox(Entry):
More information about the Python-checkins
mailing list