File: test_get_behaviors.py

package info (click to toggle)
python-atom 0.12.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,616 kB
  • sloc: cpp: 9,040; python: 6,249; makefile: 123
file content (185 lines) | stat: -rw-r--r-- 4,720 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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
# --------------------------------------------------------------------------------------
# Copyright (c) 2013-2025, Nucleic Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file LICENSE, distributed with this software.
# --------------------------------------------------------------------------------------
"""Test the get behaviors

no_op_handler: : tested here
slot_handler: standard one tested through other tests (post_get, ...)
event_handler: not tested here (see test_observe.py and
                                test_event_binder.py)
signal_handler: not tested here (see test_observe.py)
delegate_handler: not tested here (see test_delegate.py)
property_handler: not tested here (see test_property.py)
cached_property_handler: not tested here (see test_property.py)
call_object_object_handler: tested here
call_object_object_name_handler: tested here
object_method_handler: tested here
object_method_name_handler: tested here
member_method_object_handler: method defined on a Member subclass

"""

import pytest

from atom.api import Atom, GetAttr, Int, Value


def test_using_no_op_handler():
    """Test using the no_op handler."""
    v = Value()
    v.set_getattr_mode(GetAttr.NoOp, None)

    class CustomGetAtom(Atom):
        val = v

    a = CustomGetAtom()
    assert a.val is None
    a.val = 1
    assert a.val is None


def test_using_call_object_object_mode():
    """Test using call_object_object mode."""

    def getter(object):
        object.count += 1
        return object.count

    m = Int()
    m.set_getattr_mode(GetAttr.CallObject_Object, getter)

    class CustomGetAtom(Atom):
        val = m
        count = Int()

    a = CustomGetAtom()
    assert a.val == 1
    assert a.val == 2

    with pytest.raises(TypeError):
        m.set_getattr_mode(GetAttr.CallObject_Object, 1)


def test_using_call_object_object_name_mode():
    """Test using call_object_object_name mode."""

    def getter(object, name):
        object.count += 1
        return object.count, name

    m = Value()
    m.set_getattr_mode(GetAttr.CallObject_ObjectName, getter)

    class CustomGetAtom(Atom):
        val = m
        count = Int()

    a = CustomGetAtom()
    assert a.val == (1, "val")
    assert a.val == (2, "val")

    with pytest.raises(TypeError):
        m.set_getattr_mode(GetAttr.CallObject_ObjectName, 1)


def test_using_object_method_mode():
    """Test using object_method mode."""
    m = Int()
    m.set_getattr_mode(GetAttr.ObjectMethod, "getter")

    class CustomGetAtom(Atom):
        val = m
        count = Int()

        def getter(self):
            self.count += 1
            return self.count

    a = CustomGetAtom()
    assert a.val == 1
    assert a.val == 2

    with pytest.raises(TypeError):
        m.set_getattr_mode(GetAttr.ObjectMethod, 1)


def test_using_object_method_name_mode():
    """Test using object_method mode."""
    m = Value()
    m.set_getattr_mode(GetAttr.ObjectMethod_Name, "getter")

    class CustomGetAtom(Atom):
        val = m
        count = Int()

        def getter(self, name):
            self.count += 1
            return (self.count, name)

    a = CustomGetAtom()
    assert a.val == (1, "val")
    assert a.val == (2, "val")

    with pytest.raises(TypeError):
        m.set_getattr_mode(GetAttr.ObjectMethod_Name, 1)


def test_subclassing_member():
    """Test defining get in a Member subclass"""

    class ModuloInt(Int):
        def __init__(self):
            super(ModuloInt, self).__init__()
            mode = GetAttr.MemberMethod_Object
            self.set_getattr_mode(mode, "get")

        def get(self, obj):
            return self.get_slot(obj) % 2

    class GetTest(Atom):
        mi = ModuloInt()

    pvt = GetTest()
    mi = pvt.get_member("mi")
    assert mi.getattr_mode[0] == GetAttr.MemberMethod_Object
    pvt.mi = 2
    assert pvt.mi == 0
    pvt.mi = 3
    assert pvt.mi == 1

    pvt.mi = 2
    assert mi.do_getattr(pvt) == 0
    pvt.mi = 3
    assert mi.do_getattr(pvt) == 1

    with pytest.raises(TypeError):
        Int().set_getattr_mode(GetAttr.MemberMethod_Object, 1)


def test_handling_wrong_index():
    """Test handling a wrong index in the slot handler."""

    class SlotAtom(Atom):
        v = Value()

    sa = SlotAtom()

    assert SlotAtom.v.do_getattr(sa) is None

    SlotAtom.v.set_index(SlotAtom.v.index + 1)

    with pytest.raises(AttributeError):
        SlotAtom.v.do_getattr(sa)


def test_using_invalid_args():
    """Test using the invalid arguments."""
    v = Value()
    with pytest.raises(TypeError):
        v.set_getattr_mode(None)
    with pytest.raises(TypeError):
        v.set_getattr_mode(7, None)