test_weakfuncref.py 1.82 KB
Newer Older
Paul McCarthy's avatar
Paul McCarthy committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#!/usr/bin/env python
#
# test_weakfuncref.py -
#
# Author: Paul McCarthy <pauldmccarthy@gmail.com>
#

import fsl.utils.weakfuncref as weakfuncref


def make_weakfuncref_that_will_get_gcd():

    def thefunc():
        pass

    return weakfuncref.WeakFunctionRef(thefunc)

def make_weakfuncref_method_that_will_get_gcd():

    class Thing(object):
        def method(self):
            pass

Paul McCarthy's avatar
Paul McCarthy committed
24
25
26
27
28
29
30
31
32
33
34
35
36
    return weakfuncref.WeakFunctionRef(Thing.method)


def test_weakfuncref_call():

    def func():
        pass

    non_gcd_func = weakfuncref.WeakFunctionRef(func)
    gcd_func     = make_weakfuncref_that_will_get_gcd()

    assert gcd_func()     is None
    assert non_gcd_func() is func
Paul McCarthy's avatar
Paul McCarthy committed
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57


def test_weakfuncref_function():

    def func():
        pass

    non_gcd_func = weakfuncref.WeakFunctionRef(func)
    gcd_func     = make_weakfuncref_that_will_get_gcd()

    assert gcd_func.function()     is None
    assert non_gcd_func.function() is func


def test_weakfuncref_method():

    class Thing(object):
        def method(self):
            return 'existent!'

        def __priv_method(self):
Paul McCarthy's avatar
Paul McCarthy committed
58
59
            return 'existent!'

Paul McCarthy's avatar
Paul McCarthy committed
60
61
        @classmethod
        def clsmethod(clsself):
Paul McCarthy's avatar
Paul McCarthy committed
62
            return 'existent!'
Paul McCarthy's avatar
Paul McCarthy committed
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84


    t = Thing()

    gcd_methref = make_weakfuncref_that_will_get_gcd()
    methref     = weakfuncref.WeakFunctionRef(t.method)
    privmethref = weakfuncref.WeakFunctionRef(t._Thing__priv_method)
    clsmethref  = weakfuncref.WeakFunctionRef(t.clsmethod)

    assert gcd_methref.function()   is None
    assert privmethref.function()() == 'existent!'
    assert methref    .function()() == 'existent!'
    assert clsmethref .function()() == 'existent!'

    print(gcd_methref)
    print(methref)

    t = None

    assert methref    .function()   is None
    assert privmethref.function()   is None
    assert clsmethref .function()() == 'existent!'