| 1 |
from pypy.module.cpyext.test.test_api import BaseApiTest |
| 2 |
from pypy.module.cpyext.test.test_cpyext import AppTestCpythonExtensionBase |
| 3 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 4 |
from pypy.module.cpyext.pyobject import get_w_obj_and_decref |
| 5 |
|
| 6 |
|
| 7 |
class TestMapping(BaseApiTest): |
| 8 |
def test_check(self, space, api): |
| 9 |
assert api.PyMapping_Check(space.newdict()) |
| 10 |
assert not api.PyMapping_Check(space.newlist([])) |
| 11 |
assert not api.PyMapping_Check(space.newtuple([])) |
| 12 |
|
| 13 |
def test_size(self, space, api): |
| 14 |
w_d = space.newdict() |
| 15 |
space.setitem(w_d, space.wrap("a"), space.wrap("b")) |
| 16 |
|
| 17 |
assert api.PyMapping_Size(w_d) == 1 |
| 18 |
assert api.PyMapping_Length(w_d) == 1 |
| 19 |
|
| 20 |
def test_keys(self, space, api): |
| 21 |
w_d = space.newdict() |
| 22 |
space.setitem(w_d, space.wrap("a"), space.wrap("b")) |
| 23 |
|
| 24 |
assert space.eq_w(api.PyMapping_Keys(w_d), space.wrap(["a"])) |
| 25 |
assert space.eq_w(api.PyMapping_Values(w_d), space.wrap(["b"])) |
| 26 |
assert space.eq_w(api.PyMapping_Items(w_d), space.wrap([("a", "b")])) |
| 27 |
|
| 28 |
def test_setitemstring(self, space, api): |
| 29 |
w_d = space.newdict() |
| 30 |
key = rffi.str2charp("key") |
| 31 |
api.PyMapping_SetItemString(w_d, key, space.wrap(42)) |
| 32 |
assert 42 == space.unwrap(get_w_obj_and_decref(space, |
| 33 |
api.PyMapping_GetItemString(w_d, key))) |
| 34 |
rffi.free_charp(key) |
| 35 |
|
| 36 |
def test_haskey(self, space, api): |
| 37 |
w_d = space.newdict() |
| 38 |
space.setitem(w_d, space.wrap("a"), space.wrap("b")) |
| 39 |
|
| 40 |
assert api.PyMapping_HasKey(w_d, space.wrap("a")) |
| 41 |
assert not api.PyMapping_HasKey(w_d, space.wrap("b")) |
| 42 |
|
| 43 |
assert api.PyMapping_HasKey(w_d, w_d) == 0 |
| 44 |
|
| 45 |
|