From 0cad6dc96afc2ebd4d03beb14ed01bc155dfa322 Mon Sep 17 00:00:00 2001 From: mike Date: Mon, 06 Apr 2009 02:38:10 +0000 Subject: LP 348570 Core : Extending actions with the new properties, adding min / max size to arrays --- diff --git a/src/sugar/tutorius/actions.py b/src/sugar/tutorius/actions.py index 2c76bd7..b8c21e0 100644 --- a/src/sugar/tutorius/actions.py +++ b/src/sugar/tutorius/actions.py @@ -23,7 +23,7 @@ from dialog import TutoriusDialog import overlayer from sugar.tutorius.editor import WidgetIdentifier from sugar.tutorius.services import ObjectStore - +from sugar.tutorius.properties import * class Action(object): """Base class for Actions""" @@ -45,8 +45,8 @@ class Action(object): def get_properties(self): if not hasattr(self, "_props") or self._props is None: self._props = [] - for i in dir(self.__class__): - if type(getattr(self.__class__,i)) is property: + for i in dir(self): + if isinstance(getattr(self,i), TutoriusProperty): self._props.append(i) return self._props @@ -94,31 +94,18 @@ class DialogMessage(Action): self._message = message self._position = pos self._dialog = None - - def set_message(self, msg): - self._message = msg - - def get_message(self): - return self._message - - message = property(fget=get_message, fset=set_message) - - def set_pos(self, x, y): - self._position = [x, y] - def get_pos(self): - return self._position - - position = property(fget=get_pos, fset=set_pos) + self.message = TStringProperty("") + self.position = TArrayProperty([0,0], 2) def do(self): """ Show the dialog """ - self._dialog = TutoriusDialog(self._message) + self._dialog = TutoriusDialog(self.message.value) self._dialog.set_button_clicked_cb(self._dialog.close_self) self._dialog.set_modal(False) - self._dialog.move(self.position[0], self.position[1]) + self._dialog.move(self.position.value[0], self.position.value[1]) self._dialog.show() def undo(self): @@ -137,34 +124,20 @@ class BubbleMessage(Action): @param message A string to display to the user @param pos A list of the form [x, y] @param speaker treeish representation of the speaking widget + @param tailpos The position of the tail of the bubble; useful to point to + specific elements of the interface """ def __init__(self, message, pos=[0,0], speaker=None, tailpos=None): Action.__init__(self) - self._message = message - self._position = pos - + self.message = TStringProperty(message) + # Create the position as an array of fixed-size 2 + self.position = TArrayProperty(pos, 2, 2) + # Do the same for the tail position + self.tail_pos = TArrayProperty(tailpos, 2, 2) + self.overlay = None self._bubble = None self._speaker = None - self._tailpos = tailpos - - def set_message(self, msg): - self._message = msg - def get_message(self): - return self._message - message = property(fget=get_message, fset=set_message, doc="Message displayed to the user") - - def set_pos(self, x, y): - self._position = [x, y] - def get_pos(self): - return self.position - position = property(fget=get_pos, fset=set_pos, doc="Position in [x, y] on the screen") - - def set_tail_pos(self, x, y): - self._tailpos = [x, y] - def get_tail_pos(self): - return self._tailpos - tail_pos = property(fget=get_tail_pos, fset=set_tail_pos, doc="Position the tail of the bubble must point to") def do(self): """ diff --git a/src/sugar/tutorius/constraints.py b/src/sugar/tutorius/constraints.py index a666ecb..0e09664 100644 --- a/src/sugar/tutorius/constraints.py +++ b/src/sugar/tutorius/constraints.py @@ -81,10 +81,10 @@ class LowerLimitConstraint(ValueConstraint): raise LowerLimitConstraintError() return -class SizeConstraintError(Exception): +class MaxSizeConstraintError(Exception): pass -class SizeConstraint(ValueConstraint): +class MaxSizeConstraint(ValueConstraint): def validate(self, value): """ Evaluate whether a given object is smaller than the given size when @@ -94,9 +94,27 @@ class SizeConstraint(ValueConstraint): bigger than the limit. """ if self.limit is not None: - if self.limit > len(value): + if self.limit >= len(value): return - raise SizeConstraintError("Setter : trying to set value of length %d while limit is %d"%(len(value), self.limit)) + raise MaxSizeConstraintError("Setter : trying to set value of length %d while limit is %d"%(len(value), self.limit)) + return + +class MinSizeConstraintError(Exception): + pass + +class MinSizeConstraint(ValueConstraint): + def validate(self, value): + """ + Evaluate whether a given object is smaller than the given size when + run through len(). Great for string, lists and the like. ;) + + @raise SizeConstraintError If the length of the value is strictly + bigger than the limit. + """ + if self.limit is not None: + if self.limit <= len(value): + return + raise MinSizeConstraintError("Setter : trying to set value of length %d while limit is %d"%(len(value), self.limit)) return class ColorConstraintError(Exception): diff --git a/src/sugar/tutorius/properties.py b/src/sugar/tutorius/properties.py index 5be7e1c..cd12f90 100644 --- a/src/sugar/tutorius/properties.py +++ b/src/sugar/tutorius/properties.py @@ -106,7 +106,7 @@ class TStringProperty(TutoriusProperty): def __init__(self, value, size_limit=None): TutoriusProperty.__init__(self) self._type = "string" - self.size_limit = SizeConstraint(size_limit) + self.size_limit = MaxSizeConstraint(size_limit) self.set(value) @@ -115,11 +115,11 @@ class TArrayProperty(TutoriusProperty): Represents an array of properties. Can have a maximum number of element limit, but there are no constraints on the content of the array. """ - def __init__(self, value, size_limit=None): + def __init__(self, value, min_size_limit=None, max_size_limit=None): TutoriusProperty.__init__(self) self._type = "array" - self.size_limit = SizeConstraint(size_limit) - + self.max_size_limit = MaxSizeConstraint(max_size_limit) + self.min_size_limit = MinSizeConstraint(min_size_limit) self.set(value) class TColorProperty(TutoriusProperty): diff --git a/src/sugar/tutorius/tests/actiontests.py b/src/sugar/tutorius/tests/actiontests.py index d1de5fd..cfb6114 100644 --- a/src/sugar/tutorius/tests/actiontests.py +++ b/src/sugar/tutorius/tests/actiontests.py @@ -29,15 +29,7 @@ from sugar.tutorius.services import ObjectStore class PropertyAction(Action): def __init__(self, na): - self._a = na - - def set_a(self, na): - self._a = na - - def get_a(self): - return self._a - - a = property(fget=get_a, fset=set_a) + self.a = TIntProperty(na) def has_function(obj, function_name): """ @@ -54,6 +46,7 @@ class PropsTest(unittest.TestCase): assert prop.get_properties() == ['a'], "Action does not contain property 'a'" +class OnceWrapperTest(unittest.TestCase): def validate_wrapper(self, wrapper_instance): """ Ensures that a given instance of a wrapper implements the Action @@ -67,7 +60,7 @@ class PropsTest(unittest.TestCase): assert has_function(wrapper_instance, "get_properties"),\ "The wrapper does not have the 'get_properties' function." - + def test_once_wrapper_interface(self): """ Tests that the interface of the OnceWrapper actually conforms to the of diff --git a/src/sugar/tutorius/tests/constraintstests.py b/src/sugar/tutorius/tests/constraintstests.py index 407cc24..b7b0a47 100644 --- a/src/sugar/tutorius/tests/constraintstests.py +++ b/src/sugar/tutorius/tests/constraintstests.py @@ -77,28 +77,50 @@ class LowerLimitConstraintTest(unittest.TestCase): except LowerLimitConstraintError: assert True, "Validation of LowerLimit(10) on 20 should not raise an exception" -class SizeConstraintTest(unittest.TestCase): +class MaxSizeConstraintTest(unittest.TestCase): def test_empty_constraint(self): - cons = SizeConstraint(None) + cons = MaxSizeConstraint(None) try: cons.validate(20) - except SizeConstraintError: + except MaxSizeConstraintError: assert False, "Empty contraint should not raise an exception" def test_validate(self): - cons = SizeConstraint(10) + cons = MaxSizeConstraint(10) try: cons.validate(range(0, 20)) - assert False, "Validation of SizeLimit(10) on list of length 20 should raise an exception" - except SizeConstraintError: + assert False, "Validation of MaxSizeConstraint(10) on list of length 20 should raise an exception" + except MaxSizeConstraintError: pass try: cons.validate(range(0,5)) - except SizeConstraintError: - assert True, "Validation of SizeLimit(10) on list of length 5 should not raise an exception" + except MaxSizeConstraintError: + assert True, "Validation of MaxSizeConstraint(10) on list of length 5 should not raise an exception" +class MinSizeConstraintTest(unittest.TestCase): + def test_empty_constraint(self): + cons = MinSizeConstraint(None) + try: + cons.validate(20) + except MinSizeConstraintError: + assert False, "Empty contraint should not raise an exception" + + def test_validate(self): + cons = MinSizeConstraint(10) + + try: + cons.validate(range(0, 5)) + assert False, "Validation of MinSizeConstraint(10) on list of length 20 should raise an exception" + except MinSizeConstraintError: + pass + + try: + cons.validate(range(0,20)) + except MinSizeConstraintError: + assert True, "Validation of MinSizeConstraint(10) on list of length 5 should not raise an exception" + class ColorConstraintTest(unittest.TestCase): def test_validate(self): cons = ColorConstraint() diff --git a/src/sugar/tutorius/tests/propertiestests.py b/src/sugar/tutorius/tests/propertiestests.py index 52a9a75..b54f849 100644 --- a/src/sugar/tutorius/tests/propertiestests.py +++ b/src/sugar/tutorius/tests/propertiestests.py @@ -204,7 +204,7 @@ class TStringPropertyTest(unittest.TestCase): try: prop.set("My string is too big!") assert False, "String should not set to longer than max size" - except SizeConstraintError: + except MaxSizeConstraintError: pass except: assert False, "Wrong exception type thrown" @@ -218,7 +218,7 @@ class TStringPropertyTest(unittest.TestCase): try: prop = TStringProperty("This is normal", 5) assert False, "Creation of the property should fail." - except SizeConstraintError: + except MaxSizeConstraintError: pass except: assert False, "Wrong exception type on failed constructor" @@ -236,26 +236,34 @@ class TArrayPropertyTest(unittest.TestCase): try_wrong_values(prop) - def test_size_limit(self): - prop = TArrayProperty([1,2], 4) + def test_size_limits(self): + prop = TArrayProperty([1,2], None, 4) try: prop.set([1,2,4,5,6,7]) - assert False, "Size limit constraint was not properly applied" - except SizeConstraintError: + assert False, "Maximum size limit constraint was not properly applied" + except MaxSizeConstraintError: + pass + + prop = TArrayProperty([1,2,3,4], 2) + + try: + prop.set([1]) + assert False, "Minimum size limit constraint was not properly applied" + except MinSizeConstraintError: pass - except: - assert False, "Wrong type of exception thrown" - def test_failing_constructor(self): try: - prop = TArrayProperty([100, 0, 20], 2) + prop = TArrayProperty([100, 0, 20], None, 2) assert False, "Creation of the property should fail." - except SizeConstraintError: + except MaxSizeConstraintError: + pass + try: + prop = TArrayProperty([100, 0, 20], 4, None) + assert False, "Creation of the property should fail." + except MinSizeConstraintError: pass - except: - assert False, "Wrong exception type on failed constructor" class TColorPropertyTest(unittest.TestCase): def test_basic_color(self): diff --git a/src/sugar/tutorius/tests/run-tests.py b/src/sugar/tutorius/tests/run-tests.py index 63a8de1..bf5a57e 100755 --- a/src/sugar/tutorius/tests/run-tests.py +++ b/src/sugar/tutorius/tests/run-tests.py @@ -69,5 +69,7 @@ if __name__=='__main__': from linear_creatortests import * from uamtests import * from filterstests import * + from constraintstests import * + from propertiestests import * unittest.main() -- cgit v0.9.1