File: test_callableref.py

package info (click to toggle)
python-enaml 0.19.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 13,284 kB
  • sloc: python: 31,443; cpp: 4,499; makefile: 140; javascript: 68; lisp: 53; sh: 20
file content (120 lines) | stat: -rw-r--r-- 2,738 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# ------------------------------------------------------------------------------
# Copyright (c) 2020-2024, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
# ------------------------------------------------------------------------------
import gc
from weakref import ref

import pytest

from enaml.callableref import CallableRef
from enaml.weakmethod import WeakMethod

class CObj:
    def __init__(self):
        self.counter = 0
        self.last_args = None
        self.last_kwargs = None

    def __call__(self, *args, **kwargs):
        self.counter += 1
        self.last_args = args
        self.last_kwargs = kwargs
        return 1


def test_callableref_lifetime():
    """Test creating a callable ref and calling it before and after the object death.

    """
    obj = CObj()
    cr = CallableRef(obj)
    assert cr(1, 2, a=1, b=2) == 1
    assert obj.counter == 1
    assert obj.last_args == (1, 2)
    assert obj.last_kwargs == dict(a=1, b=2)

    del obj
    gc.collect()
    assert cr() is None


def test_callableref_call():
    """Test calling a callable ref with different args/kwargs.

    """
    obj = CObj()
    cr = CallableRef(obj)
    cr()
    assert obj.counter == 1
    assert obj.last_args == ()
    assert obj.last_kwargs == {}

    cr(1, 2)
    assert obj.counter == 2
    assert obj.last_args == (1, 2)
    assert obj.last_kwargs == {}

    cr(a=1, b=2)
    assert obj.counter == 3
    assert obj.last_args == ()
    assert obj.last_kwargs == dict(a=1, b=2)


def test_callableref_call_weakmethod():
    """Test calling a callable ref with different args/kwargs.

    """
    obj = CObj()
    cr = CallableRef(WeakMethod(obj.__call__))
    cr()
    assert obj.counter == 1
    assert obj.last_args == ()
    assert obj.last_kwargs == {}

    cr(1, 2)
    assert obj.counter == 2
    assert obj.last_args == (1, 2)
    assert obj.last_kwargs == {}

    cr(a=1, b=2)
    assert obj.counter == 3
    assert obj.last_args == ()
    assert obj.last_kwargs == dict(a=1, b=2)


def test_callableref_callback():
    """Test that callback are properly sets.

    """
    obj = CObj()
    cb = CObj()
    cr = CallableRef(obj, cb)

    del obj
    gc.collect()
    assert cr() is None
    assert cb.counter == 1


def test_callableref_comparison():
    """Test comparison of CallableRef.

    """
    obj1 = CObj()
    obj2 = CObj()
    cr1 = CallableRef(obj1)
    cr1_2 = CallableRef(obj1)
    cr2 = CallableRef(obj2)

    assert cr1 == cr1_2
    assert cr1 != cr2
    assert cr1 == ref(obj1)
    assert cr1 != ref(obj2)
    assert cr1 != None

    with pytest.raises(TypeError):
        cr1 > cr2