1 from __future__
import absolute_import
8 from gi
.repository
import Gio
, GObject
9 from gi
._compat
import cmp
12 class Item(GObject
.Object
):
15 def __init__(self
, **kwargs
):
16 super(Item
, self
).__init
__(**kwargs
)
24 class NamedItem(Item
):
26 name
= GObject
.Property(type=str, default
='')
29 return self
.props
.name
32 def test_list_store_sort():
33 store
= Gio
.ListStore()
34 items
= [NamedItem(name
=n
) for n
in "cabx"]
35 sorted_items
= sorted(items
, key
=lambda i
: i
.props
.name
)
37 user_data
= [object(), object()]
39 def sort_func(a
, b
, *args
):
40 assert list(args
) == user_data
41 assert isinstance(a
, NamedItem
)
42 assert isinstance(b
, NamedItem
)
43 return cmp(a
.props
.name
, b
.props
.name
)
46 assert store
[:] != sorted_items
47 store
.sort(sort_func
, *user_data
)
48 assert store
[:] == sorted_items
51 def test_list_store_insert_sorted():
52 store
= Gio
.ListStore()
53 items
= [NamedItem(name
=n
) for n
in "cabx"]
54 sorted_items
= sorted(items
, key
=lambda i
: i
.props
.name
)
56 user_data
= [object(), object()]
58 def sort_func(a
, b
, *args
):
59 assert list(args
) == user_data
60 assert isinstance(a
, NamedItem
)
61 assert isinstance(b
, NamedItem
)
62 return cmp(a
.props
.name
, b
.props
.name
)
65 index
= store
.insert_sorted(item
, sort_func
, *user_data
)
66 assert isinstance(index
, int)
67 assert store
[:] == sorted_items
70 def test_list_model_len():
71 model
= Gio
.ListStore
.new(Item
)
72 assert len(model
) == 0
74 for i
in range(1, 10):
76 assert len(model
) == i
80 assert len(model
) == 0
83 def test_list_model_get_item_simple():
84 model
= Gio
.ListStore
.new(Item
)
85 with pytest
.raises(IndexError):
88 model
.append(first_item
)
89 assert model
[0] is first_item
90 assert model
[-1] is first_item
92 model
.append(second_item
)
93 assert model
[1] is second_item
94 assert model
[-1] is second_item
95 assert model
[-2] is first_item
96 with pytest
.raises(IndexError):
99 with pytest
.raises(TypeError):
103 def test_list_model_get_item_slice():
104 model
= Gio
.ListStore
.new(Item
)
105 source
= [Item() for i
in range(30)]
108 assert model
[1:10] == source
[1:10]
109 assert model
[1:-2] == source
[1:-2]
110 assert model
[-4:-1] == source
[-4:-1]
111 assert model
[-100:-1] == source
[-100:-1]
112 assert model
[::-1] == source
[::-1]
113 assert model
[:] == source
[:]
116 def test_list_model_contains():
117 model
= Gio
.ListStore
.new(Item
)
121 assert Item() not in model
122 with pytest
.raises(TypeError):
124 with pytest
.raises(TypeError):
128 def test_list_model_iter():
129 model
= Gio
.ListStore
.new(Item
)
134 assert next(it
) is item
138 def test_list_store_delitem_simple():
139 store
= Gio
.ListStore
.new(Item
)
143 with pytest
.raises(IndexError):
145 with pytest
.raises(IndexError):
149 with pytest
.raises(IndexError):
154 source
= [Item(), Item()]
155 store
.append(source
[0])
156 store
.append(source
[1])
158 assert store
[:] == [source
[0]]
160 with pytest
.raises(TypeError):
164 def test_list_store_delitem_slice():
166 def do_del(count
, key
):
170 def on_changed(m
, *args
):
173 store
= Gio
.ListStore
.new(Item
)
174 source
= [Item() for i
in range(count
)]
177 store
.connect("items-changed", on_changed
)
178 source
.__delitem
__(key
)
179 store
.__delitem
__(key
)
180 assert source
== store
[:]
183 values
= [None, 1, -15, 3, -2, 0, -3, 5, 7]
186 start
= random
.choice(values
)
187 stop
= random
.choice(values
)
188 step
= random
.choice(values
)
189 length
= abs(random
.choice(values
) or 0)
192 variants
.add((length
, start
, stop
, step
))
194 for length
, start
, stop
, step
in variants
:
195 do_del(length
, slice(start
, stop
, step
))
198 do_del(10, slice(None, None, None))
199 do_del(10, slice(None, None, None))
200 do_del(10, slice(None, None, -1))
201 do_del(10, slice(0, 5, None))
202 do_del(10, slice(0, 10, 1))
203 do_del(10, slice(0, 10, 2))
204 do_del(10, slice(14, 2, -1))
206 # test some fast paths
207 assert do_del(100, slice(None, None, None)) == [(0, 100, 0)]
208 assert do_del(100, slice(None, None, -1)) == [(0, 100, 0)]
209 assert do_del(100, slice(0, 50, 1)) == [(0, 50, 0)]
212 def test_list_store_setitem_simple():
214 store
= Gio
.ListStore
.new(Item
)
218 class Wrong(GObject
.Object
):
221 with pytest
.raises(TypeError):
223 with pytest
.raises(TypeError):
225 with pytest
.raises(TypeError):
228 assert store
[:] == [first
]
232 assert len(store
) == 1
234 assert len(store
) == 1
236 with pytest
.raises(IndexError):
238 with pytest
.raises(IndexError):
241 store
= Gio
.ListStore
.new(Item
)
242 source
= [Item(), Item(), Item()]
247 assert store
[:] == [source
[0], new
, source
[2]]
249 with pytest
.raises(TypeError):
250 store
[object()] = Item()
253 def test_list_store_setitem_slice():
255 def do_set(count
, key
, new_count
):
256 if count
== 0 and key
.step
is not None \
257 and platform
.python_implementation() == "PyPy":
258 # https://bitbucket.org/pypy/pypy/issues/2804
260 store
= Gio
.ListStore
.new(Item
)
261 source
= [Item() for i
in range(count
)]
262 new
= [Item() for i
in range(new_count
)]
267 source
.__setitem
__(key
, new
)
268 except ValueError as e
:
269 source_error
= type(e
)
273 store
.__setitem
__(key
, new
)
274 except Exception as e
:
275 store_error
= type(e
)
277 assert source_error
== store_error
278 assert source
== store
[:]
280 values
= [None, 1, -15, 3, -2, 0, 3, 4, 100]
283 start
= random
.choice(values
)
284 stop
= random
.choice(values
)
285 step
= random
.choice(values
)
286 length
= abs(random
.choice(values
) or 0)
287 new
= random
.choice(values
) or 0
290 variants
.add((length
, start
, stop
, step
, new
))
292 for length
, start
, stop
, step
, new
in variants
:
293 do_set(length
, slice(start
, stop
, step
), new
)
296 do_set(10, slice(None, None, None), 20)
297 do_set(10, slice(None, None, None), 0)
298 do_set(10, slice(None, None, -1), 20)
299 do_set(10, slice(None, None, -1), 10)
300 do_set(10, slice(0, 5, None), 20)
301 do_set(10, slice(0, 10, 1), 0)
304 store
= Gio
.ListStore
.new(Item
)
305 store
[:] = iter([Item() for i
in range(10)])
306 assert len(store
) == 10
308 # make sure we do all or nothing
309 store
= Gio
.ListStore
.new(Item
)
310 with pytest
.raises(TypeError):
311 store
[:] = [Item(), object()]
312 assert len(store
) == 0
315 def test_action_map_add_action_entries():
316 actionmap
= Gio
.SimpleActionGroup()
320 def f(action
, parameter
, data
):
321 test_data
.append('test back')
323 actionmap
.add_action_entries((
325 ("with_type", f
, "i"),
326 ("with_state", f
, "s", "'left'", f
),
328 assert actionmap
.has_action("simple")
329 assert actionmap
.has_action("with_type")
330 assert actionmap
.has_action("with_state")
331 actionmap
.add_action_entries((
332 ("with_user_data", f
),
334 assert actionmap
.has_action("with_user_data")
336 with pytest
.raises(TypeError):
337 actionmap
.add_action_entries((
338 ("invaild_type_string", f
, 'asdf'),
340 with pytest
.raises(ValueError):
341 actionmap
.add_action_entries((
342 ("stateless_with_change_state", f
, None, None, f
),
345 actionmap
.activate_action("simple")
346 assert test_data
[0] == 'test back'