Skip to content

Commit

Permalink
add tests for Property subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
itziakos committed Feb 27, 2014
1 parent 7dd4d84 commit 9ee68eb
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 2 deletions.
63 changes: 62 additions & 1 deletion traits_enaml/tests/test_trait_operators.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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):

Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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]")
2 changes: 1 addition & 1 deletion traits_enaml/traits_tracer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)))

#--------------------------------------------------------------------------
Expand Down

0 comments on commit 9ee68eb

Please sign in to comment.