diff options
author | Johan Dahlin <johan@src.gnome.org> | 2007-05-01 15:54:05 +0000 |
---|---|---|
committer | Johan Dahlin <johan@src.gnome.org> | 2007-05-01 15:54:05 +0000 |
commit | a57fde73b509adcf56a73ae1fb97474d136fd2af (patch) | |
tree | 808278f44be0091211dd0113d6293ea0d6510583 /tests | |
parent | ecbf8b6315dda927f4f90d8651033eb67c46a187 (diff) | |
download | pygobject-a57fde73b509adcf56a73ae1fb97474d136fd2af.tar.gz pygobject-a57fde73b509adcf56a73ae1fb97474d136fd2af.tar.xz pygobject-a57fde73b509adcf56a73ae1fb97474d136fd2af.zip |
Add a property helper, fixes #338098
* examples/properties.py:
* gobject/Makefile.am:
* gobject/__init__.py:
* gobject/constants.py.in:
* gobject/generate-constants.c: (main):
* gobject/propertyhelper.py:
* tests/Makefile.am:
* tests/test_enum.py:
* tests/test_interface.py:
* tests/test_properties.py:
Add a property helper, fixes #338098
svn path=/trunk/; revision=662
Diffstat (limited to 'tests')
-rw-r--r-- | tests/Makefile.am | 6 | ||||
-rw-r--r-- | tests/test_enum.py | 15 | ||||
-rw-r--r-- | tests/test_interface.py | 11 | ||||
-rw-r--r-- | tests/test_properties.py | 170 |
4 files changed, 135 insertions, 67 deletions
diff --git a/tests/Makefile.am b/tests/Makefile.am index fe627c2..3b05018 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -40,10 +40,10 @@ testhelper.la: $(testhelper_la_OBJECTS) $(testhelper_la_DEPENDENCIES) check-local: $(top_srcdir)/gobject/__init__.py @if test "$(top_builddir)" != "$(top_srcdir)"; then \ - cp $(top_srcdir)/gobject/__init__.py $(top_builddir)/gobject/__init__.py; \ + cp $(top_srcdir)/gobject/*.py $(top_builddir)/gobject; \ fi @$(PYTHON) $(srcdir)/runtests.py $(top_builddir) $(top_srcdir) @if test "$(top_builddir)" != "$(top_srcdir)"; then \ - rm -f $(top_builddir)/gobject/__init__.py*; \ + rm -f $(top_builddir)/gobject/*.py; \ fi - @rm -fr *.pyc + @rm -fr $(top_builddir)/gobject/*.pyc diff --git a/tests/test_enum.py b/tests/test_enum.py index 88d8287..6543cec 100644 --- a/tests/test_enum.py +++ b/tests/test_enum.py @@ -1,20 +1,15 @@ import unittest import warnings +import gobject from gobject import GEnum, GFlags, GObject, GType, PARAM_READWRITE from common import gobject, atk, pango, gtk, gdk class PObject(GObject): - __gproperties__ = { - 'enum': (gtk.WindowType, 'blurb', 'description', - gtk.WINDOW_TOPLEVEL, PARAM_READWRITE), - 'enum2': (gtk.WindowType, 'blurb', 'description', - int(gtk.WINDOW_TOPLEVEL), PARAM_READWRITE), - 'flags': (gtk.AttachOptions, 'blurb', 'description', - gtk.EXPAND, PARAM_READWRITE), - 'flags2': (gtk.AttachOptions, 'blurb', 'description', - int(gtk.EXPAND), PARAM_READWRITE), - } + enum = gobject.property(type=gtk.WindowType, default=gtk.WINDOW_TOPLEVEL) + enum2 = gobject.property(type=gtk.WindowType, default=int(gtk.WINDOW_TOPLEVEL)) + flags = gobject.property(type=gtk.AttachOptions, default=gtk.EXPAND) + flags2 = gobject.property(type=gtk.AttachOptions, default=int(gtk.EXPAND)) class EnumTest(unittest.TestCase): def testEnums(self): diff --git a/tests/test_interface.py b/tests/test_interface.py index d54515d..4413d64 100644 --- a/tests/test_interface.py +++ b/tests/test_interface.py @@ -3,15 +3,13 @@ import unittest import testmodule from common import gobject, testhelper from gobject import GObject, GInterface +import gobject GUnknown = gobject.type_from_name("TestUnknown") Unknown = GUnknown.pytype class MyUnknown(Unknown, testhelper.Interface): - __gproperties__ = { - 'some-property': (str, 'blurb', 'description', 'default', - gobject.PARAM_READWRITE), - } + some_property = gobject.property(type=str) def __init__(self): Unknown.__init__(self) @@ -24,10 +22,7 @@ class MyUnknown(Unknown, testhelper.Interface): gobject.type_register(MyUnknown) class MyObject(gobject.GObject, testhelper.Interface): - __gproperties__ = { - 'some-property': (str, 'blurb', 'description', 'default', - gobject.PARAM_READWRITE), - } + some_property = gobject.property(type=str) def __init__(self): GObject.__init__(self) diff --git a/tests/test_properties.py b/tests/test_properties.py index e155911..423a8f6 100644 --- a/tests/test_properties.py +++ b/tests/test_properties.py @@ -2,54 +2,26 @@ import struct import unittest -from common import testhelper -from gobject import GObject, GType, new, PARAM_READWRITE, \ - PARAM_CONSTRUCT, PARAM_READABLE, PARAM_WRITABLE, PARAM_CONSTRUCT_ONLY, \ - TYPE_INT, TYPE_UINT, TYPE_LONG, TYPE_ULONG, \ - TYPE_INT64, TYPE_UINT64 +import gobject +from gobject import GObject, GType, GEnum, new, PARAM_READWRITE, \ + PARAM_CONSTRUCT, PARAM_READABLE, PARAM_WRITABLE, PARAM_CONSTRUCT_ONLY +from gobject.constants import \ + TYPE_INT, TYPE_UINT, TYPE_LONG, \ + TYPE_ULONG, TYPE_INT64, TYPE_UINT64 +from gobject.constants import \ + G_MININT, G_MAXINT, G_MAXUINT, G_MINLONG, G_MAXLONG, \ + G_MAXULONG class PropertyObject(GObject): - __gproperties__ = { - 'normal': (str, 'blurb', 'description', 'default', - PARAM_READWRITE), - 'construct': (str, 'blurb', 'description', 'default', - PARAM_READWRITE|PARAM_CONSTRUCT), - 'construct-only': (str, 'blurb', 'description', 'default', - PARAM_READWRITE|PARAM_CONSTRUCT_ONLY), - 'uint64': (TYPE_UINT64, 'blurb', 'description', 0, 10, 0, - PARAM_READWRITE), - } - - def __init__(self): - GObject.__init__(self) - self._value = 'default' - self._construct_only = None - self._construct = None - self._uint64 = 0L - - def do_get_property(self, pspec): - if pspec.name == 'normal': - return self._value - elif pspec.name == 'construct': - return self._construct - elif pspec.name == 'construct-only': - return self._construct_only - elif pspec.name == 'uint64': - return self._uint64 - else: - raise AssertionError - - def do_set_property(self, pspec, value): - if pspec.name == 'normal': - self._value = value - elif pspec.name == 'construct': - self._construct = value - elif pspec.name == 'construct-only': - self._construct_only = value - elif pspec.name == 'uint64': - self._uint64 = value - else: - raise AssertionError + normal = gobject.property(type=str) + construct = gobject.property( + type=str, + flags=PARAM_READWRITE|PARAM_CONSTRUCT, default='default') + construct_only = gobject.property( + type=str, + flags=PARAM_READWRITE|PARAM_CONSTRUCT_ONLY) + uint64 = gobject.property( + type=TYPE_UINT64, flags=PARAM_READWRITE|PARAM_CONSTRUCT) class TestProperties(unittest.TestCase): def testGetSet(self): @@ -200,3 +172,109 @@ class TestProperties(unittest.TestCase): normal, uint64 = obj.get_properties("normal", "uint64") self.assertEqual(normal, "foo") self.assertEqual(uint64, 7) + +class TestProperty(unittest.TestCase): + def testSimple(self): + class C(gobject.GObject): + str = gobject.property(type=str) + int = gobject.property(type=int) + float = gobject.property(type=float) + long = gobject.property(type=long) + + self.failUnless(hasattr(C.props, 'str')) + self.failUnless(hasattr(C.props, 'int')) + self.failUnless(hasattr(C.props, 'float')) + self.failUnless(hasattr(C.props, 'long')) + + o = C() + self.assertEqual(o.str, '') + o.str = 'str' + self.assertEqual(o.str, 'str') + + self.assertEqual(o.int, 0) + o.int = 1138 + self.assertEqual(o.int, 1138) + + self.assertEqual(o.float, 0.0) + o.float = 3.14 + self.assertEqual(o.float, 3.14) + + self.assertEqual(o.long, 0L) + o.long = 100L + self.assertEqual(o.long, 100L) + + def testCustomGetter(self): + class C(gobject.GObject): + def get_prop(self): + return 'value' + prop = gobject.property(getter=get_prop) + + o = C() + self.assertEqual(o.prop, 'value') + self.assertRaises(TypeError, setattr, o, 'prop', 'xxx') + + def testCustomSetter(self): + class C(gobject.GObject): + def set_prop(self, value): + self._value = value + prop = gobject.property(setter=set_prop) + + def __init__(self): + self._value = None + gobject.GObject.__init__(self) + + o = C() + self.assertEquals(o._value, None) + o.prop = 'bar' + self.assertEquals(o._value, 'bar') + self.assertRaises(TypeError, getattr, o, 'prop') + + def testErrors(self): + self.assertRaises(TypeError, gobject.property, type='str') + self.assertRaises(TypeError, gobject.property, nick=False) + self.assertRaises(TypeError, gobject.property, blurb=False) + self.assertRaises(TypeError, gobject.property, type=bool, default=0) + self.assertRaises(TypeError, gobject.property, type=GEnum) + self.assertRaises(TypeError, gobject.property, type=GEnum, default=0) + self.assertRaises(TypeError, gobject.property, type=object, default=0) + self.assertRaises(TypeError, gobject.property, type=complex) + self.assertRaises(TypeError, gobject.property, flags=-10) + + def testNameWithUnderscore(self): + class C(gobject.GObject): + prop_name = gobject.property(type=int) + o = C() + o.prop_name = 10 + self.assertEqual(o.prop_name, 10) + + def testRange(self): + maxint64 = 2 ** 62 - 1 + minint64 = -2 ** 62 - 1 + maxuint64 = 2 ** 63 - 1 + + types = [ + (TYPE_INT, G_MININT, G_MAXINT), + (TYPE_UINT, 0, G_MAXUINT), + (TYPE_LONG, G_MINLONG, G_MAXLONG), + (TYPE_ULONG, 0, G_MAXULONG), + (TYPE_INT64, minint64, maxint64), + (TYPE_UINT64, 0, maxuint64), + ] + + for gtype, min, max in types: + # Normal, everything is alright + prop = gobject.property(type=gtype, minimum=min, maximum=max) + subtype = type('', (gobject.GObject,), + dict(prop=prop)) + self.assertEqual(subtype.props.prop.minimum, min) + self.assertEqual(subtype.props.prop.maximum, max) + + # Lower than minimum + self.assertRaises(TypeError, + gobject.property, type=gtype, minimum=min-1, + maximum=max) + + # Higher than maximum + self.assertRaises(TypeError, + gobject.property, type=gtype, minimum=min, + maximum=max+1) |