From ed75b67abbd06b76648a6ea644b92d4ab1e5ca82 Mon Sep 17 00:00:00 2001 From: tshirtman Date: Tue, 9 Oct 2012 00:01:24 +0200 Subject: [PATCH] add tests for errorvalue/errorhandler of Properties and update versionchanged --- kivy/properties.pyx | 4 ++-- kivy/tests/test_properties.py | 45 +++++++++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/kivy/properties.pyx b/kivy/properties.pyx index b5a0be9d6..65de8540c 100644 --- a/kivy/properties.pyx +++ b/kivy/properties.pyx @@ -213,8 +213,8 @@ cdef class Property: `errorvalue`: object If set, will replace an invalid property value (overrides errorhandler) - .. versionchanged:: 1.4.0 - Parameters errorhandler and errorvalue added + .. versionchanged:: 1.4.2 + Parameters errorhandler and errorvalue added ''' def __cinit__(self): diff --git a/kivy/tests/test_properties.py b/kivy/tests/test_properties.py index 8fcb33319..df3ff4894 100644 --- a/kivy/tests/test_properties.py +++ b/kivy/tests/test_properties.py @@ -311,3 +311,48 @@ class PropertiesTestCase(unittest.TestCase): self.assertEqual(wid.basevalue, 2) self.assertEqual(wid.prop, 4) self.assertEqual(observe_called, 3) + + def test_bounded_numeric_property_error_value(self): + from kivy.properties import BoundedNumericProperty + + bnp = BoundedNumericProperty(0, min=-5, max=5, errorvalue=1) + bnp.link(wid, 'bnp') + + bnp.set(wid, 1) + self.assertEqual(bnp.get(wid), 1) + + bnp.set(wid, 5) + self.assertEqual(bnp.get(wid), 5) + + bnp.set(wid, 6) + self.assertEqual(bnp.get(wid), 1) + + bnp.set(wid, -5) + self.assertEqual(bnp.get(wid), -5) + + bnp.set(wid, -6) + self.assertEqual(bnp.get(wid), 1) + + def test_bounded_numeric_property_error_handler(self): + from kivy.properties import BoundedNumericProperty + + bnp = BoundedNumericProperty( + 0, min=-5, max=5, + errorhandler=lambda x: 5 if x > 5 else -5) + + bnp.link(wid, 'bnp') + + bnp.set(wid, 1) + self.assertEqual(bnp.get(wid), 1) + + bnp.set(wid, 5) + self.assertEqual(bnp.get(wid), 5) + + bnp.set(wid, 10) + self.assertEqual(bnp.get(wid), 5) + + bnp.set(wid, -5) + self.assertEqual(bnp.get(wid), -5) + + bnp.set(wid, -10) + self.assertEqual(bnp.get(wid), -5)