diff options
author | Christoph Reiter <reiter.christoph@gmail.com> | 2018-04-20 10:09:31 +0200 |
---|---|---|
committer | Christoph Reiter <reiter.christoph@gmail.com> | 2018-04-21 04:06:42 +0000 |
commit | d0bfb29fe5b34c58a190e084614fe489771bf53b (patch) | |
tree | ced676b2d6861960cfa2405906e371074e2fa2fd /tests/test_overrides_gio.py | |
parent | 00779bbaa59bfac4ddf2daa043761a3954a0ded3 (diff) | |
download | pygobject-d0bfb29fe5b34c58a190e084614fe489771bf53b.tar.gz |
Add overrides for Gio.ListStore.sort and Gio.ListStore.insert_sorted. Fixes #130gio-liststore-sort
Those functions use CompareDataFunc which works with pointers and doesn't know
anything about GObjects.
Add overrides which wrap the passed in sort function and convert the pointers
to proper Python wrappers.
Diffstat (limited to 'tests/test_overrides_gio.py')
-rw-r--r-- | tests/test_overrides_gio.py | 51 |
1 files changed, 49 insertions, 2 deletions
diff --git a/tests/test_overrides_gio.py b/tests/test_overrides_gio.py index 123653cd..8434c2b0 100644 --- a/tests/test_overrides_gio.py +++ b/tests/test_overrides_gio.py @@ -5,13 +5,14 @@ import random import pytest from gi.repository import Gio, GObject +from gi._compat import cmp class Item(GObject.Object): _id = 0 - def __init__(self): - super(Item, self).__init__() + def __init__(self, **kwargs): + super(Item, self).__init__(**kwargs) Item._id += 1 self._id = self._id @@ -19,6 +20,52 @@ class Item(GObject.Object): return str(self._id) +class NamedItem(Item): + + name = GObject.Property(type=str, default='') + + def __repr__(self): + return self.props.name + + +def test_list_store_sort(): + store = Gio.ListStore() + items = [NamedItem(name=n) for n in "cabx"] + sorted_items = sorted(items, key=lambda i: i.props.name) + + user_data = [object(), object()] + + def sort_func(a, b, *args): + assert list(args) == user_data + assert isinstance(a, NamedItem) + assert isinstance(b, NamedItem) + return cmp(a.props.name, b.props.name) + + store[:] = items + assert store[:] != sorted_items + store.sort(sort_func, *user_data) + assert store[:] == sorted_items + + +def test_list_store_insert_sorted(): + store = Gio.ListStore() + items = [NamedItem(name=n) for n in "cabx"] + sorted_items = sorted(items, key=lambda i: i.props.name) + + user_data = [object(), object()] + + def sort_func(a, b, *args): + assert list(args) == user_data + assert isinstance(a, NamedItem) + assert isinstance(b, NamedItem) + return cmp(a.props.name, b.props.name) + + for item in items: + index = store.insert_sorted(item, sort_func, *user_data) + assert isinstance(index, int) + assert store[:] == sorted_items + + def test_list_model_len(): model = Gio.ListStore.new(Item) assert len(model) == 0 |