[POS-commit] Kiwi/tests test_FormValidator.py,1.2,1.3

kiko at async.com.br kiko at async.com.br
Fri May 9 23:44:39 BRST 2003


Update of /cvs/Kiwi/tests
In directory anthem:/tmp/cvs-serv9528/tests

Modified Files:
	test_FormValidator.py 
Log Message:
Give Validator a shoeshine. Change the way the callback is run, passing
in the current validation state (and the last widget modified if any),
and handle the default callback correctly. I've added a simple test to
make sure both modes work nicely.

I also simplified the initialization loops and changed the call to be
done at startup to Validator.validate().

Updated documentation to go hand in hand, but this really needs some
rereading.


Index: test_FormValidator.py
===================================================================
RCS file: /cvs/Kiwi/tests/test_FormValidator.py,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- test_FormValidator.py	9 May 2003 04:57:47 -0000	1.2
+++ test_FormValidator.py	10 May 2003 02:44:36 -0000	1.3
@@ -3,8 +3,7 @@
 import re
 sys.path.insert(0,"..")
 
-from Kiwi.Views import BaseView
-from Kiwi.FrameWork import BaseController
+from Kiwi.Delegates import Delegate
 from Kiwi.Validator import FormValidator
 import gtk
 from gtk import TRUE, FALSE
@@ -25,20 +24,50 @@
         self.phone = ""
         self.email = ""
 
-class PersonDelegate(BaseView, BaseController):
+class SimpleDelegate(Delegate):
+    def __init__(self, model):
+        self.build_ui()
+        self.model = model
+        Delegate.__init__(self, delete_handler=gtk.mainquit)
+        self.validator = FormValidator(self, model, ["name"])
+        self.validator.validate()
+
+    def build_ui(self):
+        self.win = gtk.GtkWindow()
+        vbox = gtk.GtkVBox()
+        self.win.add(vbox)
+        self.name_label = gtk.GtkLabel("Name:")
+        vbox.add(self.name_label)
+        self.name = gtk.GtkEntry()
+        vbox.add(self.name)
+        self.ok_button = gtk.GtkButton("OK")
+        self.ok_button.connect("clicked", gtk.mainquit)
+        vbox.add(self.ok_button)
+        self.name.grab_focus()
+        self.win.show_all()
+
+    def after_name__changed(self, *args):
+        self.model.name = self.name.get_text()
+    
+class PersonDelegate(Delegate):
     def __init__(self, model):
         self.build_ui()
         self.model = model
         self.focus_topmost()
-        BaseView.__init__(self, delete_handler=gtk.mainquit)
-        BaseController.__init__(self, view=self)
+
+        Delegate.__init__(self, delete_handler=gtk.mainquit)
+
         self.ok_button.connect("clicked", gtk.mainquit)
         self.cancel_button.connect("clicked", gtk.mainquit)
+
         required_attrs = ["name", "email"]
         validators = {"email": email_validator, "phone": phone_validator}
+
         self.validator = FormValidator(self, model, required_attrs, 
                                        validators, self.enable_ok) 
-        self.enable_ok()
+
+        # Calling this once is required for consistency!
+        self.validator.validate()
 
     def after_name__changed(self, *args):
         self.model.name = self.name.get_text()
@@ -71,9 +100,15 @@
         self.table.attach(self.cancel_button, 1, 2, 3, 4)
         self.win.add(self.table)
 
-    def enable_ok(self, *args):
-        self.ok_button.set_sensitive(self.validator.form_done())
+    def enable_ok(self, state, widget):
+        # widget just tells us which was the last widget changed
+        self.ok_button.set_sensitive(state)
+
+person = Person()
+test = SimpleDelegate(person)
+test.show_all_and_loop()
 
+# Reset person to avoid reusing name
 person = Person()
 test = PersonDelegate(person)
 test.show_all_and_loop()



More information about the POS-commit mailing list