pypy / pypy / objspace / std / test / test_proxy_object.py

""" test proxy object
"""

from pypy.objspace.std.test.test_proxy import AppProxyBasic

class AppTestProxyNoDict(AppProxyBasic):
    def setup_method(self, meth):
        super(AppTestProxyNoDict, self).setup_method(meth)
        self.w_A = self.space.appexec([], """():
        class A(object):
            __slots__ = []
        return A
        """)
        self.w_proxy = self.space.appexec([], """():
        from __pypy__ import tproxy
        return tproxy
        """)
    
    def test_write_dict(self):
        c = self.Controller(self.A())
        obj = self.proxy(self.A, c.perform)
        raises(AttributeError, "obj.__dict__ = {}")

class AppTestProxyObj(AppProxyBasic):
    def setup_method(self, meth):
        super(AppTestProxyObj, self).setup_method(meth)
        self.w_A = self.space.appexec([], """():
        class A(object):
            pass
        return A
        """)
        
    def test_simple_obj(self):
        class AT(self.A):
            pass

        c = self.Controller(self.A())
        obj = self.proxy(AT, c.perform)
        
        assert type(obj) is AT
        assert obj.__class__ is AT

    def test__class__override(self):
        c = self.Controller(self.A())
        obj = self.proxy(self.A, c.perform)
        
        raises(TypeError, "obj.__class__ = self.A")

    def test_attribute_access(self):
        a = self.A()
        a.x = 3
        c = self.Controller(a)
        obj = self.proxy(self.A, c.perform)
        
        assert obj.x == 3

    def test_nonexistant_attribuite_access(self):
        c = self.Controller(self.A())
        obj = self.proxy(self.A, c.perform)
        raises(AttributeError, "obj.x")
    
    def test_setattr(self):
        a = self.A()
        c = self.Controller(a)
        obj = self.proxy(self.A, c.perform)
        obj.x = 1
        assert obj.x == 1
        assert a.x == 1

    def test_delattr(self):
        a = self.A()
        a.f = 3
        c = self.Controller(a)
        obj = self.proxy(self.A, c.perform)
        del obj.f
        raises(AttributeError, "obj.f")
    
    def test__dict__(self):
        a = self.A()
        a.x = 3
        c = self.Controller(a)
        obj = self.proxy(self.A, c.perform)
        assert 'x' in obj.__dict__
    
    def test_set__dict__(self):
        a = self.A()
        c = self.Controller(a)
        obj = self.proxy(self.A, c.perform)
        obj.__dict__ = {'x':3}
        assert obj.x == 3
        assert obj.__dict__.keys() == ['x']
    
    def test_repr(self):
        a = self.A()
        c = self.Controller(a)
        obj = self.proxy(self.A, c.perform)
        assert repr(obj)[:6] == repr(a)[:6]
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.