From 9ee68eb6a1eea242d580f3e7a064ec568feb5a44 Mon Sep 17 00:00:00 2001 From: Ioannis Tziakos Date: Thu, 27 Feb 2014 14:37:59 +0000 Subject: [PATCH] add tests for `Property` subscription --- traits_enaml/tests/test_trait_operators.py | 63 +++++++++++++++++++++- traits_enaml/traits_tracer.py | 2 +- 2 files changed, 63 insertions(+), 2 deletions(-) diff --git a/traits_enaml/tests/test_trait_operators.py b/traits_enaml/tests/test_trait_operators.py index 0dee781..2284f51 100644 --- a/traits_enaml/tests/test_trait_operators.py +++ b/traits_enaml/tests/test_trait_operators.py @@ -13,11 +13,13 @@ #---------------------------------------------------------------------------- import unittest -from traits.api import Event, HasTraits, Str, List, Dict, Set +from traits.api import ( + Event, Float, HasTraits, Str, List, Dict, Set, Property) from traits_enaml.testing.enaml_test_assistant import EnamlTestAssistant class TraitModel(HasTraits): + value = Float value_delegate = Str() value_subscribe = Str() value_update = Str() @@ -26,7 +28,19 @@ class TraitModel(HasTraits): list_values = List(Str) dict_values = Dict(Str, Str) set_values = Set(Str) + property_value = Property(depends_on='value') + typed_property_value = Property(Float, depends_on='value') + collection_property_value = Property(List, depends_on='value') + def _get_property_value(self): + return self.value + + def _get_typed_property_value(self): + return self.value + + def _get_collection_property_value(self): + value = self.value + return [value, value * 10] class TraitOperatorsTestCase(EnamlTestAssistant, unittest.TestCase): @@ -64,6 +78,15 @@ def setUp(self): Field: name = 'test_set_subscribe' text << str(model.set_values) + Field: + name = 'test_property_subscribe' + text << str(model.property_value) + Field: + name = 'test_typed_property_subscribe' + text << str(model.typed_property_value) + Field: + name = 'test_collection_property_subscribe' + text << str(model.collection_property_value) """ self.model = TraitModel() view, toolkit_view = self.parse_and_create( @@ -208,3 +231,41 @@ def test_set_subscribe(self): with self.assertAtomChanges(enaml_widget, 'text', count=1): self.model.set_values.remove('1') self.assertEquals(enaml_widget.text, "TraitSetObject(['2'])") + + def test_property_subscribe(self): + + enaml_widget = self.view.find('test_property_subscribe') + + with self.assertTraitDoesNotChange(self.model, 'property_value'): + enaml_widget.text = 'new_value' + + # check on replace + with self.assertAtomChanges(enaml_widget, 'text', count=1): + self.model.value = 3.4 + self.assertEquals(enaml_widget.text, u"3.4") + + def test_typed_property_subscribe(self): + + enaml_widget = self.view.find('test_typed_property_subscribe') + + with self.assertTraitDoesNotChange( + self.model, 'typed_property_value'): + enaml_widget.text = 'new_value' + + # check on replace + with self.assertAtomChanges(enaml_widget, 'text', count=1): + self.model.value = 4.5 + self.assertEquals(enaml_widget.text, u"4.5") + + def test_collection_property_subscribe(self): + + enaml_widget = self.view.find('test_collection_property_subscribe') + + with self.assertTraitDoesNotChange( + self.model, 'collection_property_value'): + enaml_widget.text = 'new_value' + + # check on replace + with self.assertAtomChanges(enaml_widget, 'text', count=1): + self.model.value = 4.5 + self.assertEquals(enaml_widget.text, u"[4.5, 45.0]") diff --git a/traits_enaml/traits_tracer.py b/traits_enaml/traits_tracer.py index f5d5df5..a71b9e2 100644 --- a/traits_enaml/traits_tracer.py +++ b/traits_enaml/traits_tracer.py @@ -62,7 +62,7 @@ def _trace_trait(self, obj, name): if trait is not None and trait.trait_type is not Disallow: self.traced_traits.add((obj, name)) # Check for collections. - if trait.handler.has_items: + if trait.handler is not None and trait.handler.has_items: self.traced_traits.add((obj, '{}_items'.format(name))) #--------------------------------------------------------------------------