summaryrefslogtreecommitdiff
path: root/tests/test_properties.py
diff options
context:
space:
mode:
Diffstat (limited to 'tests/test_properties.py')
-rw-r--r--tests/test_properties.py124
1 files changed, 109 insertions, 15 deletions
diff --git a/tests/test_properties.py b/tests/test_properties.py
index 490b1ae..ccb80a6 100644
--- a/tests/test_properties.py
+++ b/tests/test_properties.py
@@ -13,13 +13,14 @@ from gi.repository.GObject import \
TYPE_UINT64, TYPE_GTYPE, TYPE_INVALID, TYPE_NONE, TYPE_STRV, \
TYPE_INTERFACE, TYPE_CHAR, TYPE_UCHAR, TYPE_BOOLEAN, TYPE_FLOAT, \
TYPE_DOUBLE, TYPE_POINTER, TYPE_BOXED, TYPE_PARAM, TYPE_OBJECT, \
- TYPE_STRING, TYPE_PYOBJECT
+ TYPE_STRING, TYPE_PYOBJECT, TYPE_VARIANT
from gi.repository.GObject import \
G_MININT, G_MAXINT, G_MAXUINT, G_MINLONG, G_MAXLONG, G_MAXULONG
from gi.repository import Gio
from gi.repository import GLib
+from gi.repository import Regress
from gi.repository import GIMarshallingTests
from gi._gobject import propertyhelper
@@ -60,6 +61,42 @@ class PropertyObject(GObject.GObject):
strings = GObject.Property(
type=TYPE_STRV, flags=PARAM_READWRITE | PARAM_CONSTRUCT)
+ variant = GObject.Property(
+ type=TYPE_VARIANT, flags=PARAM_READWRITE | PARAM_CONSTRUCT)
+
+ variant_def = GObject.Property(
+ type=TYPE_VARIANT, flags=PARAM_READWRITE | PARAM_CONSTRUCT,
+ default=GLib.Variant('i', 42))
+
+
+class PropertyInheritanceObject(Regress.TestObj):
+ # override property from the base class, with a different type
+ string = GObject.Property(type=int)
+
+ # a property entirely defined at the Python level
+ python_prop = GObject.Property(type=str)
+
+
+class PropertySubClassObject(PropertyInheritanceObject):
+ # override property from the base class, with a different type
+ python_prop = GObject.Property(type=int)
+
+
+class TestPropertyInheritanceObject(unittest.TestCase):
+ def test_override_gi_property(self):
+ self.assertNotEqual(Regress.TestObj.props.string.value_type,
+ PropertyInheritanceObject.props.string.value_type)
+ obj = PropertyInheritanceObject()
+ self.assertEqual(type(obj.props.string), int)
+ obj.props.string = 4
+ self.assertEqual(obj.props.string, 4)
+
+ def test_override_python_property(self):
+ obj = PropertySubClassObject()
+ self.assertEqual(type(obj.props.python_prop), int)
+ obj.props.python_prop = 5
+ self.assertEqual(obj.props.python_prop, 5)
+
class TestPropertyObject(unittest.TestCase):
def test_get_set(self):
@@ -82,19 +119,24 @@ class TestPropertyObject(unittest.TestCase):
def test_iteration(self):
for obj in (PropertyObject.props, PropertyObject().props):
+ names = []
for pspec in obj:
gtype = GType(pspec)
self.assertEqual(gtype.parent.name, 'GParam')
- self.assertTrue(pspec.name in ['normal',
- 'construct',
- 'construct-only',
- 'uint64',
- 'enum',
- 'flags',
- 'gtype',
- 'strings',
- 'boxed'])
- self.assertEqual(len(obj), 9)
+ names.append(pspec.name)
+
+ names.sort()
+ self.assertEqual(names, ['boxed',
+ 'construct',
+ 'construct-only',
+ 'enum',
+ 'flags',
+ 'gtype',
+ 'normal',
+ 'strings',
+ 'uint64',
+ 'variant',
+ 'variant-def'])
def test_normal(self):
obj = new(PropertyObject, normal="123")
@@ -300,6 +342,52 @@ class TestPropertyObject(unittest.TestCase):
self.assertRaises(TypeError, GObject.Property, type=TYPE_STRV,
default=['hello', 1])
+ def test_variant(self):
+ obj = new(PropertyObject)
+
+ self.assertEqual(obj.props.variant, None)
+ self.assertEqual(obj.variant, None)
+
+ obj.variant = GLib.Variant('s', 'hello')
+ self.assertEqual(obj.variant.print_(True), "'hello'")
+
+ obj.variant = GLib.Variant('b', True)
+ self.assertEqual(obj.variant.print_(True), "true")
+
+ obj.props.variant = GLib.Variant('y', 2)
+ self.assertEqual(obj.variant.print_(True), "byte 0x02")
+
+ obj.variant = None
+ self.assertEqual(obj.variant, None)
+
+ # set in constructor
+ obj = new(PropertyObject, variant=GLib.Variant('u', 5))
+ self.assertEqual(obj.props.variant.print_(True), 'uint32 5')
+
+ GObject.Property(type=TYPE_VARIANT, default=GLib.Variant('i', 1))
+
+ # incompatible types
+ self.assertRaises(TypeError, setattr, obj, 'variant', 'foo')
+ self.assertRaises(TypeError, setattr, obj, 'variant', 42)
+
+ self.assertRaises(TypeError, GObject.Property, type=TYPE_VARIANT,
+ default='foo')
+ self.assertRaises(TypeError, GObject.Property, type=TYPE_VARIANT,
+ default=object())
+
+ def test_variant_default(self):
+ obj = new(PropertyObject)
+
+ self.assertEqual(obj.props.variant_def.print_(True), '42')
+ self.assertEqual(obj.variant_def.print_(True), '42')
+
+ obj.props.variant_def = GLib.Variant('y', 2)
+ self.assertEqual(obj.variant_def.print_(True), "byte 0x02")
+
+ # set in constructor
+ obj = new(PropertyObject, variant_def=GLib.Variant('u', 5))
+ self.assertEqual(obj.props.variant_def.print_(True), 'uint32 5')
+
def test_range(self):
# kiwi code
def max(c):
@@ -689,14 +777,20 @@ class TestProperty(unittest.TestCase):
del t
self.assertEqual(sys.getrefcount(o), rc)
- def test_doc_string_as_blurb(self):
+ def test_doc_strings(self):
class C(GObject.GObject):
+ foo_blurbed = GObject.Property(type=int, blurb='foo_blurbed doc string')
+
@GObject.Property
- def blurbed(self):
- """blurbed doc string"""
+ def foo_getter(self):
+ """foo_getter doc string"""
return 0
- self.assertEqual(C.blurbed.blurb, 'blurbed doc string')
+ self.assertEqual(C.foo_blurbed.blurb, 'foo_blurbed doc string')
+ self.assertEqual(C.foo_blurbed.__doc__, 'foo_blurbed doc string')
+
+ self.assertEqual(C.foo_getter.blurb, 'foo_getter doc string')
+ self.assertEqual(C.foo_getter.__doc__, 'foo_getter doc string')
def test_python_to_glib_type_mapping(self):
tester = GObject.Property()