File: tuple_test_mixin.py

package info (click to toggle)
python-traits 6.4.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 8,648 kB
  • sloc: python: 34,801; ansic: 4,266; makefile: 102
file content (81 lines) | stat: -rw-r--r-- 2,690 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# (C) Copyright 2005-2023 Enthought, Inc., Austin, TX
# All rights reserved.
#
# This software is provided without warranty under the terms of the BSD
# license included in LICENSE.txt and may be redistributed only under
# the conditions described in the aforementioned license. The license
# is also available online at http://www.enthought.com/licenses/BSD.txt
#
# Thanks for using Enthought open source!

from traits.api import HasTraits, TraitError
from traits.testing.unittest_tools import UnittestTools

VALUES = ("value1", 33, None)


class TupleTestMixin(UnittestTools):
    """ A mixin class for testing tuple like traits.

    TestCases should set the self.trait attribute during setUp for the tests
    to run.

    """

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

        dummy = self._create_class()
        self.assertEqual(dummy.t1, VALUES)
        self.assertEqual(dummy.t2, VALUES)

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

        dummy = self._create_class()
        with self.assertTraitChanges(dummy, "t1"):
            dummy.t1 = ("other value 1", 77, None)
        with self.assertTraitChanges(dummy, "t2"):
            dummy.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.
        dummy = self._create_class()
        other_tuple = ("other value", 75, True)
        with self.assertRaises(TraitError):
            dummy.t1 = other_tuple
        self.assertEqual(dummy.t1, VALUES)

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

    def _assign_invalid_values_length(self, values):
        dummy = self._create_class()
        with self.assertRaises(TraitError):
            dummy.t1 = values
        self.assertEqual(dummy.t1, VALUES)
        with self.assertRaises(TraitError):
            dummy.t2 = values
        self.assertEqual(dummy.t2, VALUES)

    def _create_class(self):
        trait = self.trait

        class Dummy(HasTraits):

            t1 = trait(VALUES)

            t2 = trait(*VALUES)

        return Dummy()