Commit a0c1ccf0 authored by Dean Moldovan's avatar Dean Moldovan
Browse files

Port tests to pytest

Use simple asserts and pytest's powerful introspection to make testing
simpler. This merges the old .py/.ref file pairs into simple .py files
where the expected values are right next to the code being tested.

This commit does not touch the C++ part of the code and replicates the
Python tests exactly like the old .ref-file-based approach.
parent 192eb884
/*
example/example-callbacks.cpp -- callbacks
tests/test_callbacks.cpp -- callbacks
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
......@@ -7,8 +7,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"
#include <pybind11/functional.h>
......
import pytest
def test_inheritance(capture, msg):
from pybind11_tests import Pet, Dog, Rabbit, dog_bark, pet_print
roger = Rabbit('Rabbit')
assert roger.name() + " is a " + roger.species() == "Rabbit is a parrot"
with capture:
pet_print(roger)
assert capture == "Rabbit is a parrot"
polly = Pet('Polly', 'parrot')
assert polly.name() + " is a " + polly.species() == "Polly is a parrot"
with capture:
pet_print(polly)
assert capture == "Polly is a parrot"
molly = Dog('Molly')
assert molly.name() + " is a " + molly.species() == "Molly is a dog"
with capture:
pet_print(molly)
assert capture == "Molly is a dog"
with capture:
dog_bark(molly)
assert capture == "Woof!"
with pytest.raises(TypeError) as excinfo:
dog_bark(polly)
assert msg(excinfo.value) == """
Incompatible function arguments. The following argument types are supported:
1. (arg0: m.Dog) -> None
Invoked with: <m.Pet object at 0>
"""
def test_callbacks(capture):
from functools import partial
from pybind11_tests import (test_callback1, test_callback2, test_callback3,
test_callback4, test_callback5)
def func1():
print('Callback function 1 called!')
def func2(a, b, c, d):
print('Callback function 2 called : {}, {}, {}, {}'.format(a, b, c, d))
return d
def func3(a):
print('Callback function 3 called : {}'.format(a))
with capture:
assert test_callback1(func1) is False
assert capture == "Callback function 1 called!"
with capture:
assert test_callback2(func2) == 5
assert capture == "Callback function 2 called : Hello, x, True, 5"
with capture:
assert test_callback1(partial(func2, "Hello", "from", "partial", "object")) is False
assert capture == "Callback function 2 called : Hello, from, partial, object"
with capture:
assert test_callback1(partial(func3, "Partial object with one argument")) is False
assert capture == "Callback function 3 called : Partial object with one argument"
with capture:
test_callback3(lambda i: i + 1)
assert capture == "func(43) = 44"
f = test_callback4()
assert f(43) == 44
f = test_callback5()
assert f(number=43) == 44
def test_lambda_closure_cleanup():
from pybind11_tests import test_cleanup, payload_cstats
test_cleanup()
cstats = payload_cstats()
assert cstats.alive() == 0
assert cstats.copy_constructions == 1
assert cstats.move_constructions >= 1
def test_cpp_function_roundtrip(capture):
"""Test if passing a function pointer from C++ -> Python -> C++ yields the original pointer"""
from pybind11_tests import dummy_function, dummy_function2, test_dummy_function, roundtrip
with capture:
test_dummy_function(dummy_function)
assert capture == """
argument matches dummy_function
eval(1) = 2
"""
with capture:
test_dummy_function(roundtrip(dummy_function))
assert capture == """
roundtrip..
argument matches dummy_function
eval(1) = 2
"""
with capture:
assert roundtrip(None) is None
assert capture == "roundtrip (got None).."
with capture:
test_dummy_function(lambda x: x + 2)
assert capture == """
could not convert to a function pointer.
eval(1) = 3
"""
with capture:
with pytest.raises(TypeError) as excinfo:
test_dummy_function(dummy_function2)
assert "Incompatible function arguments" in str(excinfo.value)
assert capture == "could not convert to a function pointer."
with capture:
with pytest.raises(TypeError) as excinfo:
test_dummy_function(lambda x, y: x + y)
assert any(s in str(excinfo.value) for s in ("missing 1 required positional argument",
"takes exactly 2 arguments"))
assert capture == "could not convert to a function pointer."
def test_function_signatures(doc):
from pybind11_tests import test_callback3, test_callback4
assert doc(test_callback3) == "test_callback3(arg0: Callable[[int], int]) -> None"
assert doc(test_callback4) == "test_callback4() -> Callable[[int], int]"
/*
example/example-constants-and-functions.cpp -- global constants and functions, enumerations, raw byte strings
tests/test_constants_and_functions.cpp -- global constants and functions, enumerations, raw byte strings
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
......@@ -7,7 +7,7 @@
BSD-style license that can be found in the LICENSE file.
*/
#include "example.h"
#include "pybind11_tests.h"
enum EMyEnumeration {
EFirstEntry = 1,
......
import pytest
def test_constants():
from pybind11_tests import some_constant
assert some_constant == 14
def test_function_overloading(capture):
from pybind11_tests import EMyEnumeration, test_function
with capture:
assert test_function() is False
assert test_function(7) == 3.5
assert test_function(EMyEnumeration.EFirstEntry) is None
assert test_function(EMyEnumeration.ESecondEntry) is None
assert capture == """
test_function()
test_function(7)
test_function(enum=1)
test_function(enum=2)
"""
def test_unscoped_enum():
from pybind11_tests import EMyEnumeration, EFirstEntry
assert str(EMyEnumeration.EFirstEntry) == "EMyEnumeration.EFirstEntry"
assert str(EMyEnumeration.ESecondEntry) == "EMyEnumeration.ESecondEntry"
assert str(EFirstEntry) == "EMyEnumeration.EFirstEntry"
# no TypeError exception for unscoped enum ==/!= int comparisons
y = EMyEnumeration.ESecondEntry
assert y == 2
assert y != 3
assert int(EMyEnumeration.ESecondEntry) == 2
assert str(EMyEnumeration(2)) == "EMyEnumeration.ESecondEntry"
def test_scoped_enum(capture):
from pybind11_tests import ECMyEnum, test_ecenum
with capture:
test_ecenum(ECMyEnum.Three)
assert capture == "test_ecenum(ECMyEnum::Three)"
z = ECMyEnum.Two
with capture:
test_ecenum(z)
assert capture == "test_ecenum(ECMyEnum::Two)"
# expected TypeError exceptions for scoped enum ==/!= int comparisons
with pytest.raises(TypeError):
assert z == 2
with pytest.raises(TypeError):
assert z != 3
def test_implicit_conversion(capture):
from pybind11_tests import ExampleWithEnum
assert str(ExampleWithEnum.EMode.EFirstMode) == "EMode.EFirstMode"
assert str(ExampleWithEnum.EFirstMode) == "EMode.EFirstMode"
f = ExampleWithEnum.test_function
first = ExampleWithEnum.EFirstMode
second = ExampleWithEnum.ESecondMode
with capture:
f(first)
assert capture == "ExampleWithEnum::test_function(enum=1)"
with capture:
assert f(first) == f(first)
assert not f(first) != f(first)
assert f(first) != f(second)
assert not f(first) == f(second)
assert f(first) == int(f(first))
assert not f(first) != int(f(first))
assert f(first) != int(f(second))
assert not f(first) == int(f(second))
assert capture == """
ExampleWithEnum::test_function(enum=1)
ExampleWithEnum::test_function(enum=1)
ExampleWithEnum::test_function(enum=1)
ExampleWithEnum::test_function(enum=1)
ExampleWithEnum::test_function(enum=1)
ExampleWithEnum::test_function(enum=2)
ExampleWithEnum::test_function(enum=1)
ExampleWithEnum::test_function(enum=2)
ExampleWithEnum::test_function(enum=1)
ExampleWithEnum::test_function(enum=1)
ExampleWithEnum::test_function(enum=1)
ExampleWithEnum::test_function(enum=1)
ExampleWithEnum::test_function(enum=1)
ExampleWithEnum::test_function(enum=2)
ExampleWithEnum::test_function(enum=1)
ExampleWithEnum::test_function(enum=2)
"""
with capture:
# noinspection PyDictCreation
x = {f(first): 1, f(second): 2}
x[f(first)] = 3
x[f(second)] = 4
assert capture == """
ExampleWithEnum::test_function(enum=1)
ExampleWithEnum::test_function(enum=2)
ExampleWithEnum::test_function(enum=1)
ExampleWithEnum::test_function(enum=2)
"""
# Hashing test
assert str(x) == "{EMode.EFirstMode: 3, EMode.ESecondMode: 4}"
def test_bytes(capture):
from pybind11_tests import return_bytes, print_bytes
with capture:
print_bytes(return_bytes())
assert capture == """
bytes[0]=1
bytes[1]=0
bytes[2]=2
bytes[3]=0
"""
/*
example/eigen.cpp -- automatic conversion of Eigen types
tests/eigen.cpp -- automatic conversion of Eigen types
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
......@@ -7,7 +7,7 @@
BSD-style license that can be found in the LICENSE file.
*/
#include "example.h"
#include "pybind11_tests.h"
#include <pybind11/eigen.h>
#include <Eigen/Cholesky>
......
import pytest
with pytest.suppress(ImportError):
import numpy as np
ref = np.array([[ 0, 3, 0, 0, 0, 11],
[22, 0, 0, 0, 17, 11],
[ 7, 5, 0, 1, 0, 11],
[ 0, 0, 0, 0, 0, 11],
[ 0, 0, 14, 0, 8, 11]])
def assert_equal_ref(mat):
np.testing.assert_array_equal(mat, ref)
def assert_sparse_equal_ref(sparse_mat):
assert_equal_ref(sparse_mat.todense())
@pytest.requires_eigen_and_numpy
def test_fixed():
from pybind11_tests import fixed_r, fixed_c, fixed_passthrough_r, fixed_passthrough_c
assert_equal_ref(fixed_c())
assert_equal_ref(fixed_r())
assert_equal_ref(fixed_passthrough_r(fixed_r()))
assert_equal_ref(fixed_passthrough_c(fixed_c()))
assert_equal_ref(fixed_passthrough_r(fixed_c()))
assert_equal_ref(fixed_passthrough_c(fixed_r()))
@pytest.requires_eigen_and_numpy
def test_dense():
from pybind11_tests import dense_r, dense_c, dense_passthrough_r, dense_passthrough_c
assert_equal_ref(dense_r())
assert_equal_ref(dense_c())
assert_equal_ref(dense_passthrough_r(dense_r()))
assert_equal_ref(dense_passthrough_c(dense_c()))
assert_equal_ref(dense_passthrough_r(dense_c()))
assert_equal_ref(dense_passthrough_c(dense_r()))
@pytest.requires_eigen_and_numpy
def test_nonunit_stride_from_python():
from pybind11_tests import double_row, double_col, double_mat_cm, double_mat_rm
counting_mat = np.arange(9.0, dtype=np.float32).reshape((3, 3))
first_row = counting_mat[0, :]
first_col = counting_mat[:, 0]
assert np.array_equal(double_row(first_row), 2.0 * first_row)
assert np.array_equal(double_col(first_row), 2.0 * first_row)
assert np.array_equal(double_row(first_col), 2.0 * first_col)
assert np.array_equal(double_col(first_col), 2.0 * first_col)
counting_3d = np.arange(27.0, dtype=np.float32).reshape((3, 3, 3))
slices = [counting_3d[0, :, :], counting_3d[:, 0, :], counting_3d[:, :, 0]]
for slice_idx, ref_mat in enumerate(slices):
assert np.array_equal(double_mat_cm(ref_mat), 2.0 * ref_mat)
assert np.array_equal(double_mat_rm(ref_mat), 2.0 * ref_mat)
@pytest.requires_eigen_and_numpy
def test_nonunit_stride_to_python():
from pybind11_tests import diagonal, diagonal_1, diagonal_n, block
assert np.all(diagonal(ref) == ref.diagonal())
assert np.all(diagonal_1(ref) == ref.diagonal(1))
for i in range(-5, 7):
assert np.all(diagonal_n(ref, i) == ref.diagonal(i)), "diagonal_n({})".format(i)
assert np.all(block(ref, 2, 1, 3, 3) == ref[2:5, 1:4])
assert np.all(block(ref, 1, 4, 4, 2) == ref[1:, 4:])
assert np.all(block(ref, 1, 4, 3, 2) == ref[1:4, 4:])
@pytest.requires_eigen_and_numpy
def test_eigen_ref_to_python():
from pybind11_tests import cholesky1, cholesky2, cholesky3, cholesky4, cholesky5, cholesky6
chols = [cholesky1, cholesky2, cholesky3, cholesky4, cholesky5, cholesky6]
for i, chol in enumerate(chols, start=1):
mymat = chol(np.array([[1, 2, 4], [2, 13, 23], [4, 23, 77]]))
assert np.all(mymat == np.array([[1, 0, 0], [2, 3, 0], [4, 5, 6]])), "cholesky{}".format(i)
@pytest.requires_eigen_and_numpy
def test_special_matrix_objects():
from pybind11_tests import incr_diag, symmetric_upper, symmetric_lower
assert np.all(incr_diag(7) == np.diag([1, 2, 3, 4, 5, 6, 7]))
asymm = np.array([[ 1, 2, 3, 4],
[ 5, 6, 7, 8],
[ 9, 10, 11, 12],
[13, 14, 15, 16]])
symm_lower = np.array(asymm)
symm_upper = np.array(asymm)
for i in range(4):
for j in range(i + 1, 4):
symm_lower[i, j] = symm_lower[j, i]
symm_upper[j, i] = symm_upper[i, j]
assert np.all(symmetric_lower(asymm) == symm_lower)
assert np.all(symmetric_upper(asymm) == symm_upper)
@pytest.requires_eigen_and_numpy
def test_dense_signature(doc):
from pybind11_tests import double_col, double_row, double_mat_rm
assert doc(double_col) == "double_col(arg0: numpy.ndarray[float32[m, 1]]) -> numpy.ndarray[float32[m, 1]]"
assert doc(double_row) == "double_row(arg0: numpy.ndarray[float32[1, n]]) -> numpy.ndarray[float32[1, n]]"
assert doc(double_mat_rm) == "double_mat_rm(arg0: numpy.ndarray[float32[m, n]]) -> numpy.ndarray[float32[m, n]]"
@pytest.requires_eigen_and_scipy
def test_sparse():
from pybind11_tests import sparse_r, sparse_c, sparse_passthrough_r, sparse_passthrough_c
assert_sparse_equal_ref(sparse_r())
assert_sparse_equal_ref(sparse_c())
assert_sparse_equal_ref(sparse_passthrough_r(sparse_r()))
assert_sparse_equal_ref(sparse_passthrough_c(sparse_c()))
assert_sparse_equal_ref(sparse_passthrough_r(sparse_c()))
assert_sparse_equal_ref(sparse_passthrough_c(sparse_r()))
@pytest.requires_eigen_and_scipy
def test_sparse_signature(doc):
from pybind11_tests import sparse_passthrough_r, sparse_passthrough_c
assert doc(sparse_passthrough_r) == "sparse_passthrough_r(arg0: scipy.sparse.csr_matrix[float32]) -> scipy.sparse.csr_matrix[float32]"
assert doc(sparse_passthrough_c) == "sparse_passthrough_c(arg0: scipy.sparse.csc_matrix[float32]) -> scipy.sparse.csc_matrix[float32]"
/*
example/example-eval.cpp -- Usage of eval() and eval_file()
tests/test_eval.cpp -- Usage of eval() and eval_file()
Copyright (c) 2016 Klemens D. Morgenstern
......@@ -9,7 +9,7 @@
#include <pybind11/eval.h>
#include "example.h"
#include "pybind11_tests.h"
void example_eval() {
py::module main_module = py::module::import("__main__");
......@@ -59,9 +59,9 @@ void example_eval() {
main_module.def("call_test2", [&](int value) {val_out = value;});
try {
result = py::eval_file("example-eval_call.py", main_namespace);
result = py::eval_file("test_eval_call.py", main_namespace);
} catch (...) {
result = py::eval_file("example/example-eval_call.py", main_namespace);
result = py::eval_file("tests/test_eval_call.py", main_namespace);
}
if (val_out == 42 && result == py::none())
......
def test_eval(capture):
from pybind11_tests import example_eval
with capture:
example_eval()
assert capture == """
eval_statements test
Hello World!
eval_statements passed
eval test
eval passed
eval_single_statement test
eval_single_statement passed
eval_file test
eval_file passed
eval failure test
eval failure test passed
eval_file failure test
eval_file failure test passed
"""
# This file is called from 'test_eval.py'
if 'call_test2' in globals():
call_test2(y)
/*
example/example-custom-exceptions.cpp -- exception translation
tests/test_custom-exceptions.cpp -- exception translation
Copyright (c) 2016 Pim Schellart <P.Schellart@princeton.edu>
......@@ -7,7 +7,7 @@
BSD-style license that can be found in the LICENSE file.
*/
#include "example.h"
#include "pybind11_tests.h"
// A type that should be raised as an exeption in Python
class MyException : public std::exception {
......
import pytest
def test_custom(msg):
from pybind11_tests import (MyException, throws1, throws2, throws3, throws4,
throws_logic_error)
# Can we catch a MyException?"
with pytest.raises(MyException) as excinfo:
throws1()
assert msg(excinfo.value) == "this error should go to a custom type"
# Can we translate to standard Python exceptions?
with pytest.raises(RuntimeError) as excinfo:
throws2()
assert msg(excinfo.value) == "this error should go to a standard Python exception"
# Can we handle unknown exceptions?
with pytest.raises(RuntimeError) as excinfo:
throws3()
assert msg(excinfo.value) == "Caught an unknown exception!"
# Can we delegate to another handler by rethrowing?
with pytest.raises(MyException) as excinfo:
throws4()
assert msg(excinfo.value) == "this error is rethrown"
# "Can we fall-through to the default handler?"
with pytest.raises(RuntimeError) as excinfo:
throws_logic_error()
assert msg(excinfo.value) == "this error should fall through to the standard handler"
/*
example/example-inheritance.cpp -- inheritance, automatic upcasting for polymorphic types
tests/test_inheritance.cpp -- inheritance, automatic upcasting for polymorphic types
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
......@@ -7,7 +7,7 @@
BSD-style license that can be found in the LICENSE file.
*/
#include "example.h"
#include "pybind11_tests.h"
class Pet {
public:
......
def test_automatic_upcasting():
from pybind11_tests import return_class_1, return_class_2, return_none
assert type(return_class_1()).__name__ == "DerivedClass1"
assert type(return_class_2()).__name__ == "DerivedClass2"
assert type(return_none()).__name__ == "NoneType"
/*
example/issues.cpp -- collection of testcases for miscellaneous issues
tests/test_issues.cpp -- collection of testcases for miscellaneous issues
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
......@@ -7,8 +7,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"
#include <pybind11/stl.h>
#include <pybind11/operators.h>
......
import pytest
import gc
def test_regressions(capture):
from pybind11_tests.issues import print_cchar, print_char
with capture:
print_cchar("const char *") # #137: const char* isn't handled properly
assert capture == "const char *"
with capture:
print_char("c") # #150: char bindings broken
assert capture == "c"
def test_dispatch_issue(capture, msg):
"""#159: virtual function dispatch has problems with similar-named functions"""
from pybind11_tests.issues import DispatchIssue, dispatch_issue_go
class PyClass1(DispatchIssue):
def dispatch(self):
print("Yay..")
class PyClass2(DispatchIssue):
def dispatch(self):
with pytest.raises(RuntimeError) as excinfo:
super(PyClass2, self).dispatch()
assert msg(excinfo.value) == 'Tried to call pure virtual function "Base::dispatch"'
p = PyClass1()
dispatch_issue_go(p)
b = PyClass2()
with capture:
dispatch_issue_go(b)
assert capture == "Yay.."
def test_reference_wrapper():
"""#171: Can't return reference wrappers (or STL data structures containing them)"""
from pybind11_tests.issues import Placeholder, return_vec_of_reference_wrapper
assert str(return_vec_of_reference_wrapper(Placeholder(4))) == \
"[Placeholder[1], Placeholder[2], Placeholder[3], Placeholder[4]]"
def test_iterator_passthrough():
"""#181: iterator passthrough did not compile"""
from pybind11_tests.issues import iterator_passthrough
assert list(iterator_passthrough(iter([3, 5, 7, 9, 11, 13, 15]))) == [3, 5, 7, 9, 11, 13, 15]
def test_shared_ptr_gc():
"""// #187: issue involving std::shared_ptr<> return value policy & garbage collection"""
from pybind11_tests.issues import ElementList, ElementA
el = ElementList()
for i in range(10):
el.add(ElementA(i))
gc.collect()
for i, v in enumerate(el.get()):
assert i == v.value()
def test_no_id(capture, msg):
from pybind11_tests.issues import print_element, expect_float, expect_int
with pytest.raises(TypeError) as excinfo:
print_element(None)
assert msg(excinfo.value) == """
Incompatible function arguments. The following argument types are supported:
1. (arg0: m.issues.ElementA) -> None
Invoked with: None
"""
with pytest.raises(TypeError) as excinfo:
expect_int(5.2)
assert msg(excinfo.value) == """
Incompatible function arguments. The following argument types are supported:
1. (arg0: int) -> int
Invoked with: 5.2
"""
assert expect_float(12) == 12
from pybind11_tests.issues import A, call_f
class B(A):
def __init__(self):
super(B, self).__init__()
def f(self):
print("In python f()")
# C++ version
with capture:
a = A()
call_f(a)
assert capture == "A.f()"
# Python version
with capture:
b = B()
call_f(b)
assert capture == """
PyA.PyA()
PyA.f()
In python f()
"""
def test_str_issue(capture, msg):
"""Issue #283: __str__ called on uninitialized instance when constructor arguments invalid"""
from pybind11_tests.issues import StrIssue
with capture:
assert str(StrIssue(3)) == "StrIssue[3]"
assert capture == "StrIssue.__str__ called"
with pytest.raises(TypeError) as excinfo:
str(StrIssue("no", "such", "constructor"))
assert msg(excinfo.value) == """
Incompatible constructor arguments. The following argument types are supported:
1. m.issues.StrIssue(arg0: int)
2. m.issues.StrIssue()
Invoked with: no, such, constructor
"""
def test_nested(capture):
""" #328: first member in a class can't be used in operators"""
from pybind11_tests.issues import NestA, NestB, NestC, print_NestA, print_NestB, print_NestC
a = NestA()
b = NestB()
c = NestC()
a += 10
b.a += 100
c.b.a += 1000
b -= 1
c.b -= 3
c *= 7
with capture:
print_NestA(a)
print_NestA(b.a)
print_NestA(c.b.a)
print_NestB(b)
print_NestB(c.b)
print_NestC(c)
assert capture == """
13
103
1003
3
1
35
"""
abase = a.as_base()
assert abase.value == -2
a.as_base().value += 44
assert abase.value == 42
assert c.b.a.as_base().value == -2
c.b.a.as_base().value += 44
assert c.b.a.as_base().value == 42
del c
gc.collect()
del a # Should't delete while abase is still alive
gc.collect()
assert abase.value == 42
del abase, b
gc.collect()
/*
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,7 +7,7 @@
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; }
......
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) -> None"
assert doc(kw_func1) == "kw_func1(x: int, y: int) -> None"
assert doc(kw_func2) == "kw_func2(x: int=100, y: int=200) -> None"
assert doc(kw_func3) == "kw_func3(data: str='Hello world!') -> None"
assert doc(kw_func4) == "kw_func4(myList: List[int]=[13, 17]) -> None"
assert doc(kw_func_udl) == "kw_func_udl(x: int, y: int=300) -> None"
assert doc(kw_func_udl_z) == "kw_func_udl_z(x: int, y: int=0) -> None"
assert doc(args_function) == "args_function(*args) -> None"
assert doc(args_kwargs_function) == "args_kwargs_function(*args, **kwargs) -> None"
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(capture, msg):
with capture:
kw_func1(5, 10)
assert capture == "kw_func(x=5, y=10)"
with capture:
kw_func1(5, y=10)
assert capture == "kw_func(x=5, y=10)"
with capture:
kw_func1(y=10, x=5)
assert capture == "kw_func(x=5, y=10)"
with capture:
kw_func2()
assert capture == "kw_func(x=100, y=200)"
with capture:
kw_func2(5)
assert capture == "kw_func(x=5, y=200)"
with capture:
kw_func2(x=5)
assert capture == "kw_func(x=5, y=200)"
with capture:
kw_func2(y=10)
assert capture == "kw_func(x=100, y=10)"
with capture:
kw_func2(5, 10)
assert capture == "kw_func(x=5, y=10)"
with capture:
kw_func2(x=5, y=10)
assert capture == "kw_func(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) -> None
Invoked with:
"""
with capture:
kw_func4()
assert capture == "kw_func4: 13 17"
with capture:
kw_func4(myList=[1, 2, 3])
assert capture == "kw_func4: 1 2 3"
with capture:
kw_func_udl(x=5, y=10)
assert capture == "kw_func(x=5, y=10)"
with capture:
kw_func_udl_z(x=5)
assert capture == "kw_func(x=5, y=0)"
def test_arg_and_kwargs(capture):
with capture:
call_kw_func(kw_func2)
assert capture == "kw_func(x=1234, y=5678)"
with capture:
args_function('arg1_value', 'arg2_value', 3)
assert capture.unordered == """
got argument: arg1_value
got argument: arg2_value
got argument: 3
"""
with capture:
args_kwargs_function('arg1_value', 'arg2_value', arg3='arg3_value', arg4=4)
assert capture.unordered == """
got argument: arg1_value
got argument: arg2_value
got keyword argument: arg3 -> arg3_value
got keyword argument: arg4 -> 4
"""
/*
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:
......
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