__author__ = "John Kirkham <kirkhamj@janelia.hhmi.org>"
__date__ = "$Mar 25, 2015 13:30:52 EDT$"


import functools

import nanshe.nanshe.generic_decorators



class TestGenericDecorators(object):
    def test_update_wrapper(self):
        def wrapper(a_callable):
            def wrapped(*args, **kwargs):
                return(a_callable(*args, **kwargs))

            return(wrapped)

        def func(a, b=2):
            return(a + b)

        func_wrapped_1 = functools.update_wrapper(wrapper, func)
        if not hasattr(func_wrapped_1, "__wrapped__"):
            setattr(func_wrapped_1, "__wrapped__", func)

        func_wrapped_2 = nanshe.nanshe.generic_decorators.update_wrapper(
            wrapper, func
        )

        assert func_wrapped_1 == func_wrapped_2


    def test_wraps(self):
        def wrapper(a_callable):
            def wrapped(*args, **kwargs):
                return(a_callable(*args, **kwargs))

            return(wrapped)

        def func(a, b=2):
            return(a + b)

        func_wrapped_1 = functools.wraps(wrapper)(func)
        if not hasattr(func_wrapped_1, "__wrapped__"):
            setattr(func_wrapped_1, "__wrapped__", func)

        func_wrapped_2 = nanshe.nanshe.generic_decorators.wraps(wrapper)(
            func
        )

        assert func_wrapped_1 == func_wrapped_2


    def test_identity_wrapper(self):
        def func(a, b=2):
            return(a + b)

        func_wrapped = nanshe.nanshe.generic_decorators.identity_wrapper(
            func
        )

        assert func_wrapped != func
        assert not hasattr(func, "__wrapped__")
        assert hasattr(func_wrapped, "__wrapped__")
        assert func_wrapped.__wrapped__ == func


    def test_static_variables(self):
        def func(a, b=2):
            return(a + b)

        func_wrapped = nanshe.nanshe.generic_decorators.static_variables(
            c = 7
        )(
            func
        )

        assert func_wrapped.__wrapped__ == func
        assert not hasattr(func, "c")
        assert hasattr(func_wrapped, "c")
        assert func_wrapped.c == 7


    def test_metaclass_0(self):
        class Meta(type):
            pass

        class Class(object):
            pass

        ClassWrapped = nanshe.nanshe.generic_decorators.metaclass(Meta)(Class)

        assert ClassWrapped != Class
        assert not hasattr(Class, "__wrapped__")
        assert hasattr(ClassWrapped, "__wrapped__")
        assert ClassWrapped.__wrapped__ == Class


    def test_metaclass_1(self):
        class Meta(type):
            pass

        class Class(object):
            __slots__ = ("__special_object__",)

            def __init__(self):
                self.__special_object__ = object

        ClassWrapped = nanshe.nanshe.generic_decorators.metaclass(Meta)(Class)

        assert ClassWrapped != Class
        assert not hasattr(Class, "__wrapped__")
        assert hasattr(ClassWrapped, "__wrapped__")
        assert ClassWrapped.__wrapped__ == Class

        a = Class()
        b = ClassWrapped()

        assert hasattr(a, "__special_object__")
        assert hasattr(b, "__special_object__")
        assert b.__special_object__ == a.__special_object__


    def test_metaclasses(self):
        class Meta1(type):
            pass

        class Meta2(type):
            pass

        class Class(object):
            pass

        ClassWrapped = nanshe.nanshe.generic_decorators.metaclasses(
            Meta1, Meta2
        )(Class)

        assert ClassWrapped != Class
        assert not hasattr(Class, "__wrapped__")
        assert hasattr(ClassWrapped, "__wrapped__")
        assert hasattr(ClassWrapped.__wrapped__, "__wrapped__")
        assert ClassWrapped.__wrapped__.__wrapped__ == Class

@@ -3221,16 +3221,18 @@
 aclasses
+_0
 (self):%0A
@@ -3723,28 +3723,894 @@
 pped__.__wrapped__ == Class%0A
+%0A%0A    def test_metaclasses_1(self):%0A        class Meta1(type):%0A            pass%0A%0A        class Meta2(type):%0A            pass%0A%0A        class Class(object):%0A            __slots__ = (%22__special_object__%22,)%0A%0A            def __init__(self):%0A                self.__special_object__ = object%0A%0A        ClassWrapped = nanshe.nanshe.generic_decorators.metaclasses(%0A            Meta1, Meta2%0A        )(Class)%0A%0A        assert ClassWrapped != Class%0A        assert not hasattr(Class, %22__wrapped__%22)%0A        assert hasattr(ClassWrapped, %22__wrapped__%22)%0A        assert hasattr(ClassWrapped.__wrapped__, %22__wrapped__%22)%0A        assert ClassWrapped.__wrapped__.__wrapped__ == Class%0A%0A        a = Class()%0A        b = ClassWrapped()%0A%0A        assert hasattr(a, %22__special_object__%22)%0A        assert hasattr(b, %22__special_object__%22)%0A        assert b.__special_object__ == a.__special_object__%0A

Write a test for `metaclasses` using `__slots__`.