| 1 |
import sys |
| 2 |
import unittest |
| 3 |
import pytest |
| 4 |
|
| 5 |
from gi.repository import GLib, GObject |
| 6 |
|
| 7 |
import testhelper |
| 8 |
|
| 9 |
|
| 10 |
class PyGObject(GObject.GObject): |
| 11 |
__gtype_name__ = "PyGObject" |
| 12 |
__gproperties__ = { |
| 13 |
"label": ( |
| 14 |
GObject.TYPE_STRING, |
| 15 |
"label property", |
| 16 |
"the label of the object", |
| 17 |
"default", |
| 18 |
GObject.ParamFlags.READABLE | GObject.ParamFlags.WRITABLE, |
| 19 |
), |
| 20 |
} |
| 21 |
|
| 22 |
def __init__(self): |
| 23 |
self._props = {} |
| 24 |
GObject.GObject.__init__(self) |
| 25 |
self.set_property("label", "hello") |
| 26 |
|
| 27 |
def do_set_property(self, name, value): |
| 28 |
self._props[name] = value |
| 29 |
|
| 30 |
def do_get_property(self, name): |
| 31 |
return self._props[name] |
| 32 |
|
| 33 |
|
| 34 |
def test_parse_constructor_args(): |
| 35 |
assert testhelper.test_parse_constructor_args("foo") == 1 |
| 36 |
|
| 37 |
|
| 38 |
class TestObject(unittest.TestCase): |
| 39 |
def test_create_ctor(self): |
| 40 |
o = PyGObject() |
| 41 |
self.assertTrue(isinstance(o, GObject.Object)) |
| 42 |
self.assertTrue(isinstance(o, PyGObject)) |
| 43 |
|
| 44 |
# has expected property |
| 45 |
self.assertEqual(o.props.label, "hello") |
| 46 |
o.props.label = "goodbye" |
| 47 |
self.assertEqual(o.props.label, "goodbye") |
| 48 |
self.assertRaises(AttributeError, getattr, o.props, "nosuchprop") |
| 49 |
|
| 50 |
def test_pyobject_new_test_type(self): |
| 51 |
o = testhelper.create_test_type() |
| 52 |
self.assertTrue(isinstance(o, PyGObject)) |
| 53 |
|
| 54 |
# has expected property |
| 55 |
self.assertEqual(o.props.label, "hello") |
| 56 |
o.props.label = "goodbye" |
| 57 |
self.assertEqual(o.props.label, "goodbye") |
| 58 |
self.assertRaises(AttributeError, getattr, o.props, "nosuchprop") |
| 59 |
|
| 60 |
@unittest.skipIf(sys.implementation.name == "pypy", reason="GC is unpredictable") |
| 61 |
def test_new_refcount(self): |
| 62 |
self.assertEqual(testhelper.test_g_object_new(), 1) |
| 63 |
|
| 64 |
|
| 65 |
class TestGValueConversion(unittest.TestCase): |
| 66 |
def test_int(self): |
| 67 |
self.assertEqual(testhelper.test_value(0), 0) |
| 68 |
self.assertEqual(testhelper.test_value(5), 5) |
| 69 |
self.assertEqual(testhelper.test_value(-5), -5) |
| 70 |
self.assertEqual(testhelper.test_value(GLib.MAXINT32), GLib.MAXINT32) |
| 71 |
self.assertEqual(testhelper.test_value(GLib.MININT32), GLib.MININT32) |
| 72 |
|
| 73 |
def test_str(self): |
| 74 |
self.assertEqual(testhelper.test_value("hello"), "hello") |
| 75 |
|
| 76 |
def test_int_array(self): |
| 77 |
self.assertEqual(testhelper.test_value_array([]), []) |
| 78 |
self.assertEqual(testhelper.test_value_array([0]), [0]) |
| 79 |
ar = list(range(100)) |
| 80 |
self.assertEqual(testhelper.test_value_array(ar), ar) |
| 81 |
|
| 82 |
def test_str_array(self): |
| 83 |
self.assertEqual(testhelper.test_value_array([]), []) |
| 84 |
self.assertEqual(testhelper.test_value_array(["a"]), ["a"]) |
| 85 |
ar = ("aa " * 1000).split() |
| 86 |
self.assertEqual(testhelper.test_value_array(ar), ar) |
| 87 |
|
| 88 |
|
| 89 |
class TestErrors(unittest.TestCase): |
| 90 |
def test_gerror(self): |
| 91 |
def callable_(): |
| 92 |
return GLib.file_get_contents("/nonexisting ") |
| 93 |
|
| 94 |
self.assertRaises(GLib.GError, testhelper.test_gerror_exception, callable_) |
| 95 |
|
| 96 |
def test_no_gerror(self): |
| 97 |
def callable_(): |
| 98 |
return GLib.file_get_contents(__file__) |
| 99 |
|
| 100 |
self.assertEqual(testhelper.test_gerror_exception(callable_), None) |
| 101 |
|
| 102 |
|
| 103 |
def test_to_unichar_conv(): |
| 104 |
assert testhelper.test_to_unichar_conv("A") == 65 |
| 105 |
assert testhelper.test_to_unichar_conv("Ä") == 196 |
| 106 |
|
| 107 |
with pytest.raises(TypeError): |
| 108 |
assert testhelper.test_to_unichar_conv(b"\x65") |
| 109 |
|
| 110 |
with pytest.raises(TypeError): |
| 111 |
testhelper.test_to_unichar_conv(object()) |
| 112 |
|
| 113 |
with pytest.raises(TypeError): |
| 114 |
testhelper.test_to_unichar_conv("AA") |
| 115 |
|
| 116 |
|
| 117 |
def test_constant_strip_prefix(): |
| 118 |
assert testhelper.constant_strip_prefix("foo", "bar") == "foo" |
| 119 |
assert testhelper.constant_strip_prefix("foo", "f") == "oo" |
| 120 |
assert testhelper.constant_strip_prefix("foo", "f") == "oo" |
| 121 |
assert testhelper.constant_strip_prefix("ha2foo", "ha") == "a2foo" |
| 122 |
assert testhelper.constant_strip_prefix("2foo", "ha") == "2foo" |
| 123 |
assert testhelper.constant_strip_prefix("bla_foo", "bla") == "_foo" |
| 124 |
|
| 125 |
|
| 126 |
def test_state_ensure_release(): |
| 127 |
testhelper.test_state_ensure_release() |