Skip to content

Commit

Permalink
Merge pull request #118 from enthought/tst-more-unit-tests
Browse files Browse the repository at this point in the history
More unit tests
  • Loading branch information
mdickinson committed Jan 12, 2014
2 parents 2999c77 + 6160596 commit 071dfc9
Show file tree
Hide file tree
Showing 4 changed files with 158 additions and 12 deletions.
56 changes: 56 additions & 0 deletions traits/tests/test_class_traits.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"""
Unit tests for the `HasTraits.class_traits` class function.
"""

from __future__ import absolute_import

from traits import _py2to3

from traits.testing.unittest_tools import unittest

from ..api import HasTraits, Int, List, Str


class A(HasTraits):

x = Int

name = Str(marked=True)


class B(A):

pass


class C(B):

lst = List(marked=False)

y = Int(marked=True)


class TestClassTraits(unittest.TestCase):

def test_all_class_traits(self):
expected = ['x', 'name', 'trait_added', 'trait_modified']
_py2to3.assertCountEqual(self, A.class_traits(), expected)

# Check that derived classes report the correct traits.
_py2to3.assertCountEqual(self, B.class_traits(), expected)

expected.extend(('lst', 'y'))
_py2to3.assertCountEqual(self, C.class_traits(), expected)

def test_class_traits_with_metadata(self):

# Retrieve all traits that have the `marked` metadata
# attribute set to True.
traits = C.class_traits(marked=True)
_py2to3.assertCountEqual(self, traits.keys(), ('y', 'name'))

# Retrieve all traits that have a `marked` metadata attribute,
# regardless of its value.
marked_traits = C.class_traits(marked=lambda attr: attr is not None)
_py2to3.assertCountEqual(self, marked_traits, ('y', 'name', 'lst'))
46 changes: 35 additions & 11 deletions traits/tests/test_range.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@

from traits.testing.unittest_tools import unittest

from ..api import HasTraits, Int, Range, Str
from ..api import HasTraits, Int, Range, Str, TraitError


class WithFloatRange(HasTraits):
r = Range(0.0, 100.0)
Expand Down Expand Up @@ -46,6 +47,16 @@ def _r_changed(self, old, new):
self.r = 0


class WithDynamicRange(HasTraits):
low = Int(0)
high = Int(10)
value = Int(3)

r = Range(value='value', low='low', high='high', exclude_high=True)

def _r_changed(self, old, new):
self._changed_handler_calls += 1


class RangeTestCase(unittest.TestCase):

Expand All @@ -55,30 +66,43 @@ def test_non_ui_events(self):
obj._changed_handler_calls = 0

obj.r = 10
self.failUnlessEqual(1, obj._changed_handler_calls)
self.assertEqual(1, obj._changed_handler_calls)

obj._changed_handler_calls = 0
obj.r = 34.56
self.failUnlessEqual(2, obj._changed_handler_calls)
self.failUnlessEqual(40, obj.r)
self.assertEqual(obj._changed_handler_calls, 2)
self.assertEqual(obj.r, 40)

def test_non_ui_int_events(self):

# Even thou the range is configured for 0..1000, the handler resets
# Even though the range is configured for 0..1000, the handler resets
# the value to 0 when it exceeds 100.
obj = WithLargeIntRange()
obj._changed_handler_calls = 0

obj.r = 10
self.failUnlessEqual(1, obj._changed_handler_calls)
self.failUnlessEqual(10, obj.r)
self.assertEqual(obj._changed_handler_calls, 1)
self.assertEqual(obj.r, 10)

obj.r = 100
self.failUnlessEqual(2, obj._changed_handler_calls)
self.failUnlessEqual(100, obj.r)
self.assertEqual(obj._changed_handler_calls, 2)
self.assertEqual(obj.r, 100)

obj.r = 101
self.failUnlessEqual(4, obj._changed_handler_calls)
self.failUnlessEqual(0, obj.r)
self.assertEqual(obj._changed_handler_calls, 4)
self.assertEqual(obj.r, 0)

def test_dynamic_events(self):

obj = WithDynamicRange()
obj._changed_handler_calls = 0

obj.r = 5
self.assertEqual(obj._changed_handler_calls, 1)
self.assertEqual(obj.r, 5)

with self.assertRaises(TraitError):
obj.r = obj.high
self.assertEqual(obj.r, 5)

### EOF
2 changes: 1 addition & 1 deletion traits/tests/test_sync_traits.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Test that the `sync_trait` member function of the `HasTraits`
""" Test that the `sync_trait` member function of `HasTraits` instances
functions correctly.
"""
Expand Down
66 changes: 66 additions & 0 deletions traits/tests/test_tuple.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
""" Unit tests for the Tuple trait type.
"""

from traits.testing.unittest_tools import unittest, UnittestTools

from traits.api import HasTraits, Tuple, TraitError

VALUES = ('value1', 33, None)


class E(HasTraits):

t1 = Tuple(VALUES)

t2 = Tuple(*VALUES)


class TupleTestCase(unittest.TestCase, UnittestTools):

def test_default_values(self):
# Check that the default values for t1 and t2 are correctly
# derived from the VALUES tuple.

e = E()
self.assertEqual(e.t1, VALUES)
self.assertEqual(e.t2, VALUES)

def test_simple_assignment(self):
# Check that we can assign different values of the correct type.

e = E()
with self.assertTraitChanges(e, 't1'):
e.t1 = ('other value 1', 77, None)
with self.assertTraitChanges(e, 't2'):
e.t2 = ('other value 2', 99, None)

def test_invalid_assignment_length(self):
# Check that assigning a tuple of incorrect length
# raises a TraitError.
self._assign_invalid_values_length(('str', 44))
self._assign_invalid_values_length(('str', 33, None, []))

def test_type_checking(self):
# Test that type checking is done for the 't1' attribute.
e = E()
other_tuple = ('other value', 75, True)
with self.assertRaises(TraitError):
e.t1 = other_tuple
self.assertEqual(e.t1, VALUES)

# Test that no type checking is done for the 't2' attribute.
try:
e.t2 = other_tuple
except TraitError:
self.fail('Unexpected TraitError when assigning to tuple.')
self.assertEqual(e.t2, other_tuple)

def _assign_invalid_values_length(self, values):

e = E()
with self.assertRaises(TraitError):
e.t1 = values
self.assertEqual(e.t1, VALUES)
with self.assertRaises(TraitError):
e.t2 = values
self.assertEqual(e.t2, VALUES)

0 comments on commit 071dfc9

Please sign in to comment.