add tests

This commit is contained in:
Taiju Yamada 2022-06-15 14:58:04 +09:00
parent c19ac6a55c
commit d77c95d068
2 changed files with 29 additions and 1 deletions

View File

@ -71,6 +71,20 @@ TEST_SUBMODULE(pytypes, m) {
py::print("list item {}: {}"_s.format(index++, item)); py::print("list item {}: {}"_s.format(index++, item));
} }
}); });
m.def("access_list", []() {
py::list l1 = py::list();
l1.append(1);
l1.append(2);
return l1[1];
});
m.def("access_list_as_object", []() {
py::list l1 = py::list();
l1.append(1);
l1.append(2);
py::object l2 = l1;
return l2[1];
});
// test_none // test_none
m.def("get_none", [] { return py::none(); }); m.def("get_none", [] { return py::none(); });
m.def("print_none", [](const py::none &none) { py::print("none: {}"_s.format(none)); }); m.def("print_none", [](const py::none &none) { py::print("none: {}"_s.format(none)); });
@ -121,12 +135,22 @@ TEST_SUBMODULE(pytypes, m) {
m.def("dict_contains", m.def("dict_contains",
[](const py::dict &dict, const char *val) { return dict.contains(val); }); [](const py::dict &dict, const char *val) { return dict.contains(val); });
m.def("access_dict_with_str", []() { m.def("access_dict_with_str", []() {
py::dict d1 = py::dict();
d1["x"] = 1;
return d1["x"];
});
m.def("access_dict_with_int", []() {
py::dict d1 = py::dict();
d1[1] = 1;
return d1[1];
});
m.def("access_dict_as_object_with_str", []() {
py::dict d1 = py::dict(); py::dict d1 = py::dict();
d1["x"] = 1; d1["x"] = 1;
py::object d2 = d1; py::object d2 = d1;
return d2["x"]; return d2["x"];
}); });
m.def("access_dict_with_int", []() { m.def("access_dict_as_object_with_int", []() {
py::dict d1 = py::dict(); py::dict d1 = py::dict();
d1[1] = 1; d1[1] = 1;
py::object d2 = d1; py::object d2 = d1;

View File

@ -59,6 +59,8 @@ def test_list(capture, doc):
assert doc(m.get_list) == "get_list() -> list" assert doc(m.get_list) == "get_list() -> list"
assert doc(m.print_list) == "print_list(arg0: list) -> None" assert doc(m.print_list) == "print_list(arg0: list) -> None"
assert m.access_list() == 2
assert m.access_list_as_object() == 2
def test_none(capture, doc): def test_none(capture, doc):
assert doc(m.get_none) == "get_none() -> None" assert doc(m.get_none) == "get_none() -> None"
@ -148,6 +150,8 @@ def test_dict(capture, doc):
assert m.access_dict_with_str() == 1 assert m.access_dict_with_str() == 1
assert m.access_dict_with_int() == 1 assert m.access_dict_with_int() == 1
assert m.access_dict_as_object_with_str() == 1
assert m.access_dict_as_object_with_int() == 1
def test_tuple(): def test_tuple():