-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #118 from enthought/tst-more-unit-tests
More unit tests
- Loading branch information
Showing
4 changed files
with
158 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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')) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |