Beispiel #1
0
    def test_memoized_property(self):
        from xoutil.inspect import getattr_static
        from xoutil.decorator import memoized_property, reset_memoized

        class Foobar(object):
            @memoized_property
            def prop(self):
                return self

            with self.assertRaises(AttributeError):
                @prop.setter
                def prop(self, value):
                    pass

            with self.assertRaises(AttributeError):
                @prop.deleter
                def prop(self, value):
                    pass

        foo = Foobar()
        self.assertNotEquals(getattr_static(foo, 'prop'), foo)
        self.assertIs(foo.prop, foo)
        self.assertIs(getattr_static(foo, 'prop'), foo)
        # After the first invocation, the static attr is the result.
        reset_memoized(foo, 'prop')
        self.assertNotEquals(getattr_static(foo, 'prop'), foo)
Beispiel #2
0
 def test_module_level_memoized_props(self):
     import customizetestbed as m
     from xoutil.inspect import getattr_static
     self.assertNotEquals(getattr_static(m, 'memoized'), m)
     self.assertIs(m.memoized, m)
     self.assertIs(getattr_static(m, 'memoized'), m)