File: test_prefix_list.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 (162 lines) | stat: -rw-r--r-- 5,087 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# (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!

"""
Tests for the PrefixList handler.
"""

import pickle
import unittest

from traits.api import HasTraits, TraitError, PrefixList


class TestPrefixList(unittest.TestCase):
    def test_assignment(self):
        class A(HasTraits):
            foo = PrefixList(["zero", "one", "two"], default_value="one")

        a = A()

        a.foo = 'z'
        self.assertEqual(a.foo, "zero")

        with self.assertRaises(TraitError) as exception_context:
            a.foo = ''
        self.assertIn(
            "The 'foo' trait of an A instance must be 'zero' or 'one' or 'two'"
            " (or any unique prefix), but a value of ''",
            str(exception_context.exception),
        )

    def test_bad_types(self):
        class A(HasTraits):
            foo = PrefixList(["zero", "one", "two"], default_value="one")

        a = A()

        wrong_type = [[], (1, 2, 3), 1j, 2.3, 23, b"zero", None]
        for value in wrong_type:
            with self.subTest(value=value):
                with self.assertRaises(TraitError):
                    a.foo = value

    def test_repeated_prefix(self):
        class A(HasTraits):
            foo = PrefixList(("abc1", "abc2"))

        a = A()

        a.foo = "abc1"
        self.assertEqual(a.foo, "abc1")

        with self.assertRaises(TraitError):
            a.foo = "abc"

    def test_default_default(self):
        class A(HasTraits):
            foo = PrefixList(["zero", "one", "two"], default_value="zero")

        a = A()
        self.assertEqual(a.foo, "zero")

    def test_explicit_default(self):
        class A(HasTraits):
            foo = PrefixList(["zero", "one", "two"], default_value="one")

        a = A()
        self.assertEqual(a.foo, "one")

    def test_default_subject_to_completion(self):
        class A(HasTraits):
            foo = PrefixList(["zero", "one", "two"], default_value="o")

        a = A()
        self.assertEqual(a.foo, "one")

    def test_default_subject_to_validation(self):
        with self.assertRaises(ValueError):

            class A(HasTraits):
                foo = PrefixList(["zero", "one", "two"], default_value="uno")

    def test_default_legal_but_not_unique_prefix(self):
        # Corner case to exercise internal logic: the default is not a unique
        # prefix, but it is one of the list of values, so it's legal.
        class A(HasTraits):
            foo = PrefixList(["live", "modal", "livemodal"], default="live")

        a = A()
        self.assertEqual(a.foo, "live")

    def test_default_value_cant_be_passed_by_position(self):
        with self.assertRaises(TypeError):
            PrefixList(["zero", "one", "two"], "one")

    def test_values_not_sequence(self):
        # Defining values with this signature is not supported
        with self.assertRaises(TypeError):
            PrefixList("zero", "one", "two")

    def test_values_not_all_iterables(self):
        # Make sure we don't confuse other sequence types, e.g. str
        with self.assertRaises(TypeError) as exception_context:
            PrefixList("zero")

        self.assertEqual(
            str(exception_context.exception),

            "values should be a collection of strings, not 'zero'"
        )

    def test_values_is_empty(self):
        # it doesn't make sense to use a PrefixList with an empty list, so make
        # sure we raise a ValueError
        with self.assertRaises(ValueError):
            PrefixList([])

    def test_values_is_empty_with_default_value(self):
        # Raise even if we give a default value.
        with self.assertRaises(ValueError):
            PrefixList([], default_value="one")

    def test_no_nested_exception(self):
        # Regression test for enthought/traits#1155
        class A(HasTraits):
            foo = PrefixList(["zero", "one", "two"])

        a = A()
        try:
            a.foo = "three"
        except TraitError as exc:
            self.assertIsNone(exc.__context__)
            self.assertIsNone(exc.__cause__)

    def test_pickle_roundtrip(self):
        class A(HasTraits):
            foo = PrefixList(["zero", "one", "two"], default_value="one")

        a = A()
        foo_trait = a.traits()["foo"]
        reconstituted = pickle.loads(pickle.dumps(foo_trait))

        self.assertEqual(
            foo_trait.validate(a, "foo", "ze"),
            "zero",
        )
        with self.assertRaises(TraitError):
            foo_trait.validate(a, "foo", "zero-knowledge")

        self.assertEqual(
            reconstituted.validate(a, "foo", "ze"),
            "zero",
        )
        with self.assertRaises(TraitError):
            reconstituted.validate(a, "foo", "zero-knowledge")