| 1 |
import gc |
| 2 |
import weakref |
| 3 |
|
| 4 |
import pytest |
| 5 |
from gi.repository import GObject, Regress |
| 6 |
|
| 7 |
try: |
| 8 |
from gi.repository import Gtk |
| 9 |
|
| 10 |
GTK4 = Gtk._version == "4.0" |
| 11 |
except ImportError: |
| 12 |
Gtk = None |
| 13 |
GTK4 = False |
| 14 |
|
| 15 |
|
| 16 |
def test_constructor_no_data(): |
| 17 |
obj = Regress.TestFundamentalSubObject() |
| 18 |
|
| 19 |
assert isinstance(obj, Regress.TestFundamentalSubObject) |
| 20 |
assert isinstance(obj, Regress.TestFundamentalObject) |
| 21 |
assert obj.refcount == 1 |
| 22 |
assert obj.data is None |
| 23 |
|
| 24 |
|
| 25 |
def test_constructor_with_data(): |
| 26 |
with pytest.raises(TypeError): |
| 27 |
Regress.TestFundamentalSubObject(data="foo") |
| 28 |
|
| 29 |
|
| 30 |
def test_create_fundamental_new_with_data(): |
| 31 |
obj = Regress.TestFundamentalSubObject.new("foo") |
| 32 |
|
| 33 |
assert isinstance(obj, Regress.TestFundamentalSubObject) |
| 34 |
assert isinstance(obj, Regress.TestFundamentalObject) |
| 35 |
assert obj.refcount == 1 |
| 36 |
assert obj.data == "foo" |
| 37 |
|
| 38 |
|
| 39 |
def test_change_field(): |
| 40 |
obj = Regress.TestFundamentalObjectNoGetSetFunc.new("foo") |
| 41 |
|
| 42 |
obj.data = "bar" |
| 43 |
|
| 44 |
assert obj.get_data() == "bar" |
| 45 |
|
| 46 |
|
| 47 |
def test_call_method(): |
| 48 |
obj = Regress.TestFundamentalObjectNoGetSetFunc.new("foo") |
| 49 |
|
| 50 |
assert obj.get_data() == "foo" |
| 51 |
|
| 52 |
|
| 53 |
def test_create_fundamental_hidden_class_instance(): |
| 54 |
obj = Regress.test_create_fundamental_hidden_class_instance() |
| 55 |
|
| 56 |
assert isinstance(obj, Regress.TestFundamentalObject) |
| 57 |
|
| 58 |
|
| 59 |
def test_create_fundamental_refcount(): |
| 60 |
obj = Regress.TestFundamentalSubObject.new("foo") |
| 61 |
|
| 62 |
assert obj.refcount == 1 |
| 63 |
|
| 64 |
|
| 65 |
def test_delete_fundamental_refcount(): |
| 66 |
obj = Regress.TestFundamentalSubObject.new("foo") |
| 67 |
del obj |
| 68 |
|
| 69 |
gc.collect() |
| 70 |
|
| 71 |
|
| 72 |
def test_value_set_fundamental_object(): |
| 73 |
val = GObject.Value(Regress.TestFundamentalSubObject.__gtype__) |
| 74 |
obj = Regress.TestFundamentalSubObject() |
| 75 |
|
| 76 |
val.set_value(obj) |
| 77 |
|
| 78 |
assert val.get_value() == obj |
| 79 |
|
| 80 |
|
| 81 |
def test_value_set_wrong_value(): |
| 82 |
val = GObject.Value(Regress.TestFundamentalSubObject.__gtype__) |
| 83 |
|
| 84 |
with pytest.raises(TypeError, match="Fundamental type is required"): |
| 85 |
val.set_value(1) |
| 86 |
|
| 87 |
|
| 88 |
def test_value_set_wrong_fundamental(): |
| 89 |
class MyCustomFundamentalObject(Regress.TestFundamentalObject): |
| 90 |
pass |
| 91 |
|
| 92 |
val = GObject.Value(Regress.TestFundamentalSubObject.__gtype__) |
| 93 |
|
| 94 |
with pytest.raises(TypeError, match="Invalid fundamental type for assignment"): |
| 95 |
val.set_value(MyCustomFundamentalObject()) |
| 96 |
|
| 97 |
|
| 98 |
def test_array_of_fundamental_objects_in(): |
| 99 |
assert Regress.test_array_of_fundamental_objects_in( |
| 100 |
[Regress.TestFundamentalSubObject()] |
| 101 |
) |
| 102 |
|
| 103 |
|
| 104 |
def test_array_of_fundamental_objects_out(): |
| 105 |
objs = Regress.test_array_of_fundamental_objects_out() |
| 106 |
|
| 107 |
assert len(objs) == 2 |
| 108 |
assert all(isinstance(o, Regress.TestFundamentalObject) for o in objs) |
| 109 |
|
| 110 |
|
| 111 |
def test_fundamental_argument_in(): |
| 112 |
obj = Regress.TestFundamentalSubObject() |
| 113 |
|
| 114 |
assert Regress.test_fundamental_argument_in(obj) |
| 115 |
|
| 116 |
|
| 117 |
def test_abstract_fundamental_type(): |
| 118 |
with pytest.raises(TypeError): |
| 119 |
Regress.TestFundamentalObject() |
| 120 |
|
| 121 |
|
| 122 |
def test_fundamental_argument_out(): |
| 123 |
obj = Regress.TestFundamentalSubObject.new("data") |
| 124 |
other = Regress.test_fundamental_argument_out(obj) |
| 125 |
|
| 126 |
assert type(obj) is type(other) |
| 127 |
assert obj is not other |
| 128 |
assert obj.data == other.data |
| 129 |
|
| 130 |
|
| 131 |
def test_multiple_objects(): |
| 132 |
obj1 = Regress.TestFundamentalSubObject() |
| 133 |
obj2 = Regress.TestFundamentalSubObject() |
| 134 |
|
| 135 |
assert obj1 != obj2 |
| 136 |
|
| 137 |
|
| 138 |
def test_fundamental_weak_ref(): |
| 139 |
obj = Regress.TestFundamentalSubObject() |
| 140 |
weak = weakref.ref(obj) |
| 141 |
|
| 142 |
assert weak() == obj |
| 143 |
|
| 144 |
del obj |
| 145 |
gc.collect() |
| 146 |
|
| 147 |
assert weak() is None |
| 148 |
|
| 149 |
|
| 150 |
def test_fundamental_primitive_object(): |
| 151 |
bitmask = Regress.Bitmask(2) |
| 152 |
|
| 153 |
assert bitmask.v == 2 |
| 154 |
|
| 155 |
|
| 156 |
def test_custom_fundamental_type_vfunc_override(): |
| 157 |
class MyCustomFundamentalObject(Regress.TestFundamentalObject): |
| 158 |
def __init__(self): |
| 159 |
super().__init__() |
| 160 |
invocations["__init__"] = True |
| 161 |
|
| 162 |
def do_finalize(self): |
| 163 |
invocations["do_finalize"] = True |
| 164 |
|
| 165 |
invocations = {} |
| 166 |
obj = MyCustomFundamentalObject() |
| 167 |
del obj |
| 168 |
gc.collect() |
| 169 |
|
| 170 |
assert "__init__" in invocations |
| 171 |
assert "do_finalize" in invocations |
| 172 |
|
| 173 |
|
| 174 |
@pytest.mark.skipif(not GTK4, reason="requires GTK 4") |
| 175 |
def test_gtk_expression(): |
| 176 |
obj = object() |
| 177 |
con = Gtk.ConstantExpression.new_for_value(obj) |
| 178 |
|
| 179 |
assert con.get_value() is obj |
| 180 |
|
| 181 |
|
| 182 |
@pytest.mark.skipif(not GTK4, reason="requires GTK 4") |
| 183 |
def test_gtk_string_filter_fundamental_property(): |
| 184 |
expr = Gtk.ConstantExpression.new_for_value("one") |
| 185 |
filter = Gtk.StringFilter.new(expr) |
| 186 |
filter.props.expression = expr |
| 187 |
|
| 188 |
assert filter.get_expression() == expr |
| 189 |
assert filter.props.expression == expr |