<commit_before>from __future__ import unicode_literals
from ben10.foundation.weak_ref import (GetRealObj, GetWeakProxy, GetWeakRef, IsSame, IsWeakProxy,
    IsWeakRef, WeakList, WeakMethodProxy, WeakMethodRef, WeakSet)
import pytest
import sys
import weakref



#===================================================================================================
# _Stub
#===================================================================================================
class _Stub(object):

    def __hash__(self):
        return 1

    def __eq__(self, o):
        return True  # always equal

    def __ne__(self, o):
        return not self == o

    def Method(self):
        pass


#===================================================================================================
# Test
#===================================================================================================
class Test():

    def testStub(self):
        a = _Stub()
        b = _Stub()
        assert not a != a
        assert not a != b
        assert a == a
        assert a == b
        a.Method()


    def testIsSame(self):
        s1 = _Stub()
        s2 = _Stub()

        r1 = weakref.ref(s1)
        r2 = weakref.ref(s2)

        p1 = weakref.proxy(s1)
        p2 = weakref.proxy(s2)

        assert IsSame(s1, s1)
        assert not IsSame(s1, s2)

        assert IsSame(s1, r1)
        assert IsSame(s1, p1)

        assert not IsSame(s1, r2)
        assert not IsSame(s1, p2)


        assert IsSame(p2, r2)
        assert IsSame(r1, p1)
        assert not IsSame(r1, p2)

        with pytest.raises(ReferenceError):
            IsSame(p1, p2)


    def testGetWeakRef(self):
        b = GetWeakRef(None)
        assert b() is None


    def testGeneral(self):
        b = _Stub()
        r = GetWeakRef(b.Method)
        assert r() is not None  # should not be a regular weak ref here (but a weak method ref)

        assert IsWeakRef(r)
        assert not IsWeakProxy(r)

        r = GetWeakProxy(b.Method)
        r()
        assert IsWeakProxy(r)
        assert not IsWeakRef(r)

        r = weakref.ref(b)
        b2 = _Stub()
        r2 = weakref.ref(b2)
        assert r == r2
        assert hash(r) == hash(r2)

        r = GetWeakRef(b.Method)
        r2 = GetWeakRef(b.Method)
        assert r == r2
        assert hash(r) == hash(r2)



    def testGetRealObj(self):
        b = _Stub()
        r = GetWeakRef(b)
        assert GetRealObj(r) is b

        r = GetWeakRef(None)
        assert GetRealObj(r) is None


    def testGetWeakProxyFromWeakRef(self):
        b = _Stub()
        r = GetWeakRef(b)
        proxy = GetWeakProxy(r)
        assert IsWeakProxy(proxy)


    def testWeakSet(self):
        weak_set = WeakSet()
        s1 = _Stub()
        s2 = _Stub()

        weak_set.add(s1)
        assert isinstance(iter(weak_set).next(), _Stub)

        assert s1 in weak_set
        self.CustomAssertEqual(len(weak_set), 1)
        del s1
        self.CustomAssertEqual(len(weak_set), 0)

#         weak_set.add(s2)
#         self.CustomAssertEqual(len(weak_set), 1)
#         weak_set.remove(s2)
#         self.CustomAssertEqual(len(weak_set), 0)
#
#         weak_set.add(s2)
#         weak_set.clear()
#         self.CustomAssertEqual(len(weak_set), 0)
#
#         weak_set.add(s2)
#         weak_set.add(s2)
#         weak_set.add(s2)
#         self.CustomAssertEqual(len(weak_set), 1)
#         del s2
#         self.CustomAssertEqual(len(weak_set), 0)
#
#         # >>> Testing with FUNCTION
#
#         # Adding twice, having one
#         def function():
#             pass
#         weak_set.add(function)
#         weak_set.add(function)
#         self.CustomAssertEqual(len(weak_set), 1)


    def testRemove(self):
        weak_set = WeakSet()

        s1 = _Stub()

        self.CustomAssertEqual(len(weak_set), 0)

        # Trying remove, raises KeyError
        with pytest.raises(KeyError):
            weak_set.remove(s1)
        self.CustomAssertEqual(len(weak_set), 0)

        # Trying discard, no exception raised
        weak_set.discard(s1)
        self.CustomAssertEqual(len(weak_set), 0)


    def testWeakSet2(self):
        weak_set = WeakSet()

        # >>> Removing with DEL
        s1 = _Stub()
        weak_set.add(s1.Method)
        self.CustomAssertEqual(len(weak_set), 1)
        del s1
        self.CustomAssertEqual(len(weak_set), 0)

        # >>> Removing with REMOVE
        s2 = _Stub()
        weak_set.add(s2.Method)
        self.CustomAssertEqual(len(weak_set), 1)
        weak_set.remove(s2.Method)
        self.CustomAssertEqual(len(weak_set), 0)


    def testWithError(self):
        weak_set = WeakSet()

        # Not WITH, everything ok
        s1 = _Stub()
        weak_set.add(s1.Method)
        self.CustomAssertEqual(len(weak_set), 1)
        del s1
        self.CustomAssertEqual(len(weak_set), 0)

        # Using WITH, s2 is not deleted from weak_set
        s2 = _Stub()
        with pytest.raises(KeyError):
            raise KeyError('key')
        self.CustomAssertEqual(len(weak_set), 0)

        weak_set.add(s2.Method)
        self.CustomAssertEqual(len(weak_set), 1)
        del s2
        self.CustomAssertEqual(len(weak_set), 0)


    def testFunction(self):
        weak_set = WeakSet()

        def function():
            'Never called'

        # Adding twice, having one.
        weak_set.add(function)
        weak_set.add(function)
        self.CustomAssertEqual(len(weak_set), 1)

        # Removing function
        weak_set.remove(function)
        assert len(weak_set) == 0


    def CustomAssertEqual(self, a, b):
        '''
        Avoiding using "assert a == b" because it adds another reference to the ref-count.
        '''
        if a == b:
            pass
        else:
            assert False, "%s != %s" % (a, b)


    def SetupTestAttributes(self):

        class C(object):
            def f(self, y=0):
                return self.x + y

        class D(object):
            def f(self):
                'Never called'

        self.C = C
        self.c = C()
        self.c.x = 1
        self.d = D()


    def testCustomAssertEqual(self):
        with pytest.raises(AssertionError) as excinfo:
            self.CustomAssertEqual(1, 2)
        assert unicode(excinfo.value) == '1 != 2\nassert False'


    def testRefcount(self):
        self.SetupTestAttributes()

        self.CustomAssertEqual(sys.getrefcount(self.c), 2)  # 2: one in self, and one as argument to getrefcount()
        cf = self.c.f
        self.CustomAssertEqual(sys.getrefcount(self.c), 3)  # 3: as above, plus cf
        rf = WeakMethodRef(self.c.f)
        pf = WeakMethodProxy(self.c.f)
        self.CustomAssertEqual(sys.getrefcount(self.c), 3)
        del cf
        self.CustomAssertEqual(sys.getrefcount(self.c), 2)
        rf2 = WeakMethodRef(self.c.f)
        self.CustomAssertEqual(sys.getrefcount(self.c), 2)
        del rf
        del rf2
        del pf
        self.CustomAssertEqual(sys.getrefcount(self.c), 2)


    def testDies(self):
        self.SetupTestAttributes()

        rf = WeakMethodRef(self.c.f)<commit_msg>Fix tests after pytest-2.6.4 upgrade.<commit_after>from __future__ import unicode_literals
from ben10.foundation.weak_ref import (GetRealObj, GetWeakProxy, GetWeakRef, IsSame, IsWeakProxy,
    IsWeakRef, WeakList, WeakMethodProxy, WeakMethodRef, WeakSet)
import pytest
import sys
import weakref



#===================================================================================================
# _Stub
#===================================================================================================
class _Stub(object):

    def __hash__(self):
        return 1

    def __eq__(self, o):
        return True  # always equal

    def __ne__(self, o):
        return not self == o

    def Method(self):
        pass


#===================================================================================================
# Test
#===================================================================================================
class Test():

    def testStub(self):
        a = _Stub()
        b = _Stub()
        assert not a != a
        assert not a != b
        assert a == a
        assert a == b
        a.Method()


    def testIsSame(self):
        s1 = _Stub()
        s2 = _Stub()

        r1 = weakref.ref(s1)
        r2 = weakref.ref(s2)

        p1 = weakref.proxy(s1)
        p2 = weakref.proxy(s2)

        assert IsSame(s1, s1)
        assert not IsSame(s1, s2)

        assert IsSame(s1, r1)
        assert IsSame(s1, p1)

        assert not IsSame(s1, r2)
        assert not IsSame(s1, p2)


        assert IsSame(p2, r2)
        assert IsSame(r1, p1)
        assert not IsSame(r1, p2)

        with pytest.raises(ReferenceError):
            IsSame(p1, p2)


    def testGetWeakRef(self):
        b = GetWeakRef(None)
        assert b() is None


    def testGeneral(self):
        b = _Stub()
        r = GetWeakRef(b.Method)
        assert r() is not None  # should not be a regular weak ref here (but a weak method ref)

        assert IsWeakRef(r)
        assert not IsWeakProxy(r)

        r = GetWeakProxy(b.Method)
        r()
        assert IsWeakProxy(r)
        assert not IsWeakRef(r)

        r = weakref.ref(b)
        b2 = _Stub()
        r2 = weakref.ref(b2)
        assert r == r2
        assert hash(r) == hash(r2)

        r = GetWeakRef(b.Method)
        r2 = GetWeakRef(b.Method)
        assert r == r2
        assert hash(r) == hash(r2)



    def testGetRealObj(self):
        b = _Stub()
        r = GetWeakRef(b)
        assert GetRealObj(r) is b

        r = GetWeakRef(None)
        assert GetRealObj(r) is None


    def testGetWeakProxyFromWeakRef(self):
        b = _Stub()
        r = GetWeakRef(b)
        proxy = GetWeakProxy(r)
        assert IsWeakProxy(proxy)


    def testWeakSet(self):
        weak_set = WeakSet()
        s1 = _Stub()
        s2 = _Stub()

        weak_set.add(s1)
        assert isinstance(iter(weak_set).next(), _Stub)

        assert s1 in weak_set
        self.CustomAssertEqual(len(weak_set), 1)
        del s1
        self.CustomAssertEqual(len(weak_set), 0)

#         weak_set.add(s2)
#         self.CustomAssertEqual(len(weak_set), 1)
#         weak_set.remove(s2)
#         self.CustomAssertEqual(len(weak_set), 0)
#
#         weak_set.add(s2)
#         weak_set.clear()
#         self.CustomAssertEqual(len(weak_set), 0)
#
#         weak_set.add(s2)
#         weak_set.add(s2)
#         weak_set.add(s2)
#         self.CustomAssertEqual(len(weak_set), 1)
#         del s2
#         self.CustomAssertEqual(len(weak_set), 0)
#
#         # >>> Testing with FUNCTION
#
#         # Adding twice, having one
#         def function():
#             pass
#         weak_set.add(function)
#         weak_set.add(function)
#         self.CustomAssertEqual(len(weak_set), 1)


    def testRemove(self):
        weak_set = WeakSet()

        s1 = _Stub()

        self.CustomAssertEqual(len(weak_set), 0)

        # Trying remove, raises KeyError
        with pytest.raises(KeyError):
            weak_set.remove(s1)
        self.CustomAssertEqual(len(weak_set), 0)

        # Trying discard, no exception raised
        weak_set.discard(s1)
        self.CustomAssertEqual(len(weak_set), 0)


    def testWeakSet2(self):
        weak_set = WeakSet()

        # >>> Removing with DEL
        s1 = _Stub()
        weak_set.add(s1.Method)
        self.CustomAssertEqual(len(weak_set), 1)
        del s1
        self.CustomAssertEqual(len(weak_set), 0)

        # >>> Removing with REMOVE
        s2 = _Stub()
        weak_set.add(s2.Method)
        self.CustomAssertEqual(len(weak_set), 1)
        weak_set.remove(s2.Method)
        self.CustomAssertEqual(len(weak_set), 0)


    def testWithError(self):
        weak_set = WeakSet()

        # Not WITH, everything ok
        s1 = _Stub()
        weak_set.add(s1.Method)
        self.CustomAssertEqual(len(weak_set), 1)
        del s1
        self.CustomAssertEqual(len(weak_set), 0)

        # Using WITH, s2 is not deleted from weak_set
        s2 = _Stub()
        with pytest.raises(KeyError):
            raise KeyError('key')
        self.CustomAssertEqual(len(weak_set), 0)

        weak_set.add(s2.Method)
        self.CustomAssertEqual(len(weak_set), 1)
        del s2
        self.CustomAssertEqual(len(weak_set), 0)


    def testFunction(self):
        weak_set = WeakSet()

        def function():
            'Never called'

        # Adding twice, having one.
        weak_set.add(function)
        weak_set.add(function)
        self.CustomAssertEqual(len(weak_set), 1)

        # Removing function
        weak_set.remove(function)
        assert len(weak_set) == 0


    def CustomAssertEqual(self, a, b):
        '''
        Avoiding using "assert a == b" because it adds another reference to the ref-count.
        '''
        if a == b:
            pass
        else:
            assert False, "%s != %s" % (a, b)


    def SetupTestAttributes(self):

        class C(object):
            def f(self, y=0):
                return self.x + y

        class D(object):
            def f(self):
                'Never called'

        self.C = C
        self.c = C()
        self.c.x = 1
        self.d = D()


    def testCustomAssertEqual(self):
        with pytest.raises(AssertionError) as excinfo:
            self.CustomAssertEqual(1, 2)
        # TODO: Remove multiple options after Dist-12.0.29
        assert unicode(excinfo.value) in (
            '1 != 2',
            '1 != 2\nassert False'
        )


    def testRefcount(self):
        self.SetupTestAttributes()

        self.CustomAssertEqual(sys.getrefcount(self.c), 2)  # 2: one in self, and one as argument to getrefcount()
        cf = self.c.f
        self.CustomAssertEqual(sys.getrefcount(self.c), 3)  # 3: as above, plus cf
        rf = WeakMethodRef(self.c.f)
        pf = WeakMethodProxy(self.c.f)
        self.CustomAssertEqual(sys.getrefcount(self.c), 3)
        del cf
        self.CustomAssertEqual(sys.getrefcount(self.c), 2)
        rf2 = WeakMethodRef(self.c.f)
        self.CustomAssertEqual(sys.getrefcount(self.c), 2)
        del rf
        del rf2
        del pf
        self.CustomAssertEqual(sys.getrefcount(self.c), 2)


    def testDies(self):
        self.SetupTestAttributes()

        rf = WeakMethodRef(self.c.f)
{'fraction_non_alphanumeric': 0.15726899812278383, 'fraction_numerical': 0.015712994507404574, 'mean_word_length': 2.0748182984181276, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7484212', 'n_tokens_mistral': 4788, 'n_tokens_neox': 4488, 'n_words': 1008}