Ticket #2245: test_properties.py

File test_properties.py, 2.0 KB (added by Peaker, 7 years ago)
Line 
1# Copyright (c) 2007 Twisted Matrix Laboratories.
2# See LICENSE for details.
3
4"""
5Tests for the twisted.python.properties module
6"""
7
8from twisted.trial import unittest, util
9from twisted.python.properties import deprecated_property
10
11deprecation_message = "Deprecation Message"
12
13def _getter(self):
14    """A simple getter used as an argument to deprecated_property"""
15    return None
16
17def _setter(self, value):
18    """A simple setter that ignores its value used as an argument
19    to deprecated_property"""
20    pass
21
22def _deller(self):
23    """A simple deller that ignores used as an argument to
24    deprecated_property"""
25    pass
26
27def _getattr(c):
28    """A getattr is implemented here, allowing the deprecation
29    warnings to be issued for this __file__"""
30    return c.x
31
32def _setattr(c):
33    """A setattr wrapper is implemented here, allowing the deprecation
34    warnings to be issued for this __file__"""
35    c.x = 1
36
37def _delattr(c):
38    """A delattr is implemented here, allowing the deprecation
39    warnings to be issued for this __file__"""
40    del c.x
41
42class DeprecatedPropertyTests(unittest.TestCase):
43    """
44    Tests for deprecated properties.
45    """
46
47    def _verify(self, allowed, method, c):
48        if allowed:
49            self.assertWarns(DeprecationWarning, deprecation_message, __file__,
50                             method, c)
51        else:
52            self.assertRaises(AttributeError, method, c)
53
54    def test_access_limit(self):
55        """
56        Test that specifying no getter, setter or deller indeed
57        prevents their access.
58        """
59
60        for getter in [None, _getter]:
61            for setter in [None, _setter]:
62                for deller in [None, _deller]:
63                    class Container(object):
64                        x = deprecated_property(deprecation_message, getter, setter, deller)
65                    c = Container()
66                    self._verify(bool(getter), _getattr, c)
67                    self._verify(bool(setter), _setattr, c)
68                    self._verify(bool(deller), _delattr, c)