Commit faec30c4 authored by Wenzel Jakob's avatar Wenzel Jakob Committed by GitHub
Browse files

Merge pull request #321 from dean0x7d/pytest

Port test suite to pytest
parents bf099587 99dbdc16
/*
example/example-keep-alive.cpp -- keep_alive modifier (pybind11's version
tests/test_keep_alive.cpp -- keep_alive modifier (pybind11's version
of Boost.Python's with_custodian_and_ward / with_custodian_and_ward_postcall)
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
......@@ -8,7 +8,7 @@
BSD-style license that can be found in the LICENSE file.
*/
#include "example.h"
#include "pybind11_tests.h"
class Child {
public:
......
import gc
def test_keep_alive_argument(capture):
from pybind11_tests import Parent, Child
with capture:
p = Parent()
assert capture == "Allocating parent."
with capture:
p.addChild(Child())
gc.collect()
assert capture == """
Allocating child.
Releasing child.
"""
with capture:
del p
gc.collect()
assert capture == "Releasing parent."
with capture:
p = Parent()
assert capture == "Allocating parent."
with capture:
p.addChildKeepAlive(Child())
gc.collect()
assert capture == "Allocating child."
with capture:
del p
gc.collect()
assert capture == """
Releasing parent.
Releasing child.
"""
def test_keep_alive_return_value(capture):
from pybind11_tests import Parent
with capture:
p = Parent()
assert capture == "Allocating parent."
with capture:
p.returnChild()
gc.collect()
assert capture == """
Allocating child.
Releasing child.
"""
with capture:
del p
gc.collect()
assert capture == "Releasing parent."
with capture:
p = Parent()
assert capture == "Allocating parent."
with capture:
p.returnChildKeepAlive()
gc.collect()
assert capture == "Allocating child."
with capture:
del p
gc.collect()
assert capture == """
Releasing parent.
Releasing child.
"""
def test_return_none(capture):
from pybind11_tests import Parent
with capture:
p = Parent()
assert capture == "Allocating parent."
with capture:
p.returnNullChildKeepAliveChild()
gc.collect()
assert capture == ""
with capture:
del p
gc.collect()
assert capture == "Releasing parent."
with capture:
p = Parent()
assert capture == "Allocating parent."
with capture:
p.returnNullChildKeepAliveParent()
gc.collect()
assert capture == ""
with capture:
del p
gc.collect()
assert capture == "Releasing parent."
/*
example/example-arg-keywords-and-defaults.cpp -- keyword arguments and default values
tests/test_kwargs_and_defaults.cpp -- keyword arguments and default values
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
......@@ -7,16 +7,17 @@
BSD-style license that can be found in the LICENSE file.
*/
#include "example.h"
#include "pybind11_tests.h"
#include <pybind11/stl.h>
void kw_func(int x, int y) { std::cout << "kw_func(x=" << x << ", y=" << y << ")" << std::endl; }
std::string kw_func(int x, int y) { return "x=" + std::to_string(x) + ", y=" + std::to_string(y); }
void kw_func4(const std::vector<int> &entries) {
std::cout << "kw_func4: ";
std::string kw_func4(const std::vector<int> &entries) {
std::string ret = "{";
for (int i : entries)
std::cout << i << " ";
std::cout << endl;
ret += std::to_string(i) + " ";
ret.back() = '}';
return ret;
}
py::object call_kw_func(py::function f) {
......@@ -26,18 +27,12 @@ py::object call_kw_func(py::function f) {
return f(*args, **kwargs);
}
void args_function(py::args args) {
for (size_t it=0; it<args.size(); ++it)
std::cout << "got argument: " << py::object(args[it]) << std::endl;
py::tuple args_function(py::args args) {
return args;
}
void args_kwargs_function(py::args args, py::kwargs kwargs) {
for (auto item : args)
std::cout << "got argument: " << item << std::endl;
if (kwargs) {
for (auto item : kwargs)
std::cout << "got keyword argument: " << item.first << " -> " << item.second << std::endl;
}
py::tuple args_kwargs_function(py::args args, py::kwargs kwargs) {
return py::make_tuple(args, kwargs);
}
struct KWClass {
......
import pytest
from pybind11_tests import (kw_func0, kw_func1, kw_func2, kw_func3, kw_func4, call_kw_func,
args_function, args_kwargs_function, kw_func_udl, kw_func_udl_z,
KWClass)
def test_function_signatures(doc):
assert doc(kw_func0) == "kw_func0(arg0: int, arg1: int) -> str"
assert doc(kw_func1) == "kw_func1(x: int, y: int) -> str"
assert doc(kw_func2) == "kw_func2(x: int=100, y: int=200) -> str"
assert doc(kw_func3) == "kw_func3(data: str='Hello world!') -> None"
assert doc(kw_func4) == "kw_func4(myList: List[int]=[13, 17]) -> str"
assert doc(kw_func_udl) == "kw_func_udl(x: int, y: int=300) -> str"
assert doc(kw_func_udl_z) == "kw_func_udl_z(x: int, y: int=0) -> str"
assert doc(args_function) == "args_function(*args) -> tuple"
assert doc(args_kwargs_function) == "args_kwargs_function(*args, **kwargs) -> tuple"
assert doc(KWClass.foo0) == "foo0(self: m.KWClass, arg0: int, arg1: float) -> None"
assert doc(KWClass.foo1) == "foo1(self: m.KWClass, x: int, y: float) -> None"
def test_named_arguments(msg):
assert kw_func0(5, 10) == "x=5, y=10"
assert kw_func1(5, 10) == "x=5, y=10"
assert kw_func1(5, y=10) == "x=5, y=10"
assert kw_func1(y=10, x=5) == "x=5, y=10"
assert kw_func2() == "x=100, y=200"
assert kw_func2(5) == "x=5, y=200"
assert kw_func2(x=5) == "x=5, y=200"
assert kw_func2(y=10) == "x=100, y=10"
assert kw_func2(5, 10) == "x=5, y=10"
assert kw_func2(x=5, y=10) == "x=5, y=10"
with pytest.raises(TypeError) as excinfo:
# noinspection PyArgumentList
kw_func2(x=5, y=10, z=12)
assert msg(excinfo.value) == """
Incompatible function arguments. The following argument types are supported:
1. (x: int=100, y: int=200) -> str
Invoked with:
"""
assert kw_func4() == "{13 17}"
assert kw_func4(myList=[1, 2, 3]) == "{1 2 3}"
assert kw_func_udl(x=5, y=10) == "x=5, y=10"
assert kw_func_udl_z(x=5) == "x=5, y=0"
def test_arg_and_kwargs():
assert call_kw_func(kw_func2) == "x=1234, y=5678"
args = 'arg1_value', 'arg2_value', 3
assert args_function(*args) == args
args = 'a1', 'a2'
kwargs = dict(arg3='a3', arg4=4)
assert args_kwargs_function(*args, **kwargs) == (args, kwargs)
/*
example/example-methods-and-attributes.cpp -- constructors, deconstructors, attribute access,
tests/test_methods_and_attributes.cpp -- constructors, deconstructors, attribute access,
__str__, argument and return value conventions
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
......@@ -8,8 +8,8 @@
BSD-style license that can be found in the LICENSE file.
*/
#include "example.h"
#include "constructor-stats.h"
#include "pybind11_tests.h"
#include "constructor_stats.h"
class ExampleMandA {
public:
......
from pybind11_tests import ExampleMandA, ConstructorStats
def test_methods_and_attributes():
instance1 = ExampleMandA()
instance2 = ExampleMandA(32)
instance1.add1(instance2)
instance1.add2(instance2)
instance1.add3(instance2)
instance1.add4(instance2)
instance1.add5(instance2)
instance1.add6(32)
instance1.add7(32)
instance1.add8(32)
instance1.add9(32)
instance1.add10(32)
assert str(instance1) == "ExampleMandA[value=320]"
assert str(instance2) == "ExampleMandA[value=32]"
assert str(instance1.self1()) == "ExampleMandA[value=320]"
assert str(instance1.self2()) == "ExampleMandA[value=320]"
assert str(instance1.self3()) == "ExampleMandA[value=320]"
assert str(instance1.self4()) == "ExampleMandA[value=320]"
assert str(instance1.self5()) == "ExampleMandA[value=320]"
assert instance1.internal1() == 320
assert instance1.internal2() == 320
assert instance1.internal3() == 320
assert instance1.internal4() == 320
assert instance1.internal5() == 320
assert instance1.value == 320
instance1.value = 100
assert str(instance1) == "ExampleMandA[value=100]"
cstats = ConstructorStats.get(ExampleMandA)
assert cstats.alive() == 2
del instance1, instance2
assert cstats.alive() == 0
assert cstats.values() == ["32"]
assert cstats.default_constructions == 1
assert cstats.copy_constructions == 3
assert cstats.move_constructions >= 1
assert cstats.copy_assignments == 0
assert cstats.move_assignments == 0
/*
example/example-modules.cpp -- nested modules, importing modules, and
tests/test_modules.cpp -- nested modules, importing modules, and
internal references
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
......@@ -8,11 +8,11 @@
BSD-style license that can be found in the LICENSE file.
*/
#include "example.h"
#include "constructor-stats.h"
#include "pybind11_tests.h"
#include "constructor_stats.h"
void submodule_func() {
std::cout << "submodule_func()" << std::endl;
std::string submodule_func() {
return "submodule_func()";
}
class A {
......
def test_nested_modules():
import pybind11_tests
from pybind11_tests.submodule import submodule_func
assert pybind11_tests.__name__ == "pybind11_tests"
assert pybind11_tests.submodule.__name__ == "pybind11_tests.submodule"
assert submodule_func() == "submodule_func()"
def test_reference_internal():
from pybind11_tests import ConstructorStats
from pybind11_tests.submodule import A, B
b = B()
assert str(b.get_a1()) == "A[1]"
assert str(b.a1) == "A[1]"
assert str(b.get_a2()) == "A[2]"
assert str(b.a2) == "A[2]"
b.a1 = A(42)
b.a2 = A(43)
assert str(b.get_a1()) == "A[42]"
assert str(b.a1) == "A[42]"
assert str(b.get_a2()) == "A[43]"
assert str(b.a2) == "A[43]"
astats, bstats = ConstructorStats.get(A), ConstructorStats.get(B)
assert astats.alive() == 2
assert bstats.alive() == 1
del b
assert astats.alive() == 0
assert bstats.alive() == 0
assert astats.values() == ['1', '2', '42', '43']
assert bstats.values() == []
assert astats.default_constructions == 0
assert bstats.default_constructions == 1
assert astats.copy_constructions == 0
assert bstats.copy_constructions == 0
# assert astats.move_constructions >= 0 # Don't invoke any
# assert bstats.move_constructions >= 0 # Don't invoke any
assert astats.copy_assignments == 2
assert bstats.copy_assignments == 0
assert astats.move_assignments == 0
assert bstats.move_assignments == 0
def test_importing():
from pybind11_tests import OD
from collections import OrderedDict
assert OD is OrderedDict
assert str(OD([(1, 'a'), (2, 'b')])) == "OrderedDict([(1, 'a'), (2, 'b')])"
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment