1# -*- coding: utf-8 -*- 2import pytest 3 4m = pytest.importorskip("pybind11_tests.constants_and_functions") 5 6 7def test_constants(): 8 assert m.some_constant == 14 9 10 11def test_function_overloading(): 12 assert m.test_function() == "test_function()" 13 assert m.test_function(7) == "test_function(7)" 14 assert m.test_function(m.MyEnum.EFirstEntry) == "test_function(enum=1)" 15 assert m.test_function(m.MyEnum.ESecondEntry) == "test_function(enum=2)" 16 17 assert m.test_function() == "test_function()" 18 assert m.test_function("abcd") == "test_function(char *)" 19 assert m.test_function(1, 1.0) == "test_function(int, float)" 20 assert m.test_function(1, 1.0) == "test_function(int, float)" 21 assert m.test_function(2.0, 2) == "test_function(float, int)" 22 23 24def test_bytes(): 25 assert m.print_bytes(m.return_bytes()) == "bytes[1 0 2 0]" 26 27 28def test_exception_specifiers(): 29 c = m.C() 30 assert c.m1(2) == 1 31 assert c.m2(3) == 1 32 assert c.m3(5) == 2 33 assert c.m4(7) == 3 34 assert c.m5(10) == 5 35 assert c.m6(14) == 8 36 assert c.m7(20) == 13 37 assert c.m8(29) == 21 38 39 assert m.f1(33) == 34 40 assert m.f2(53) == 55 41 assert m.f3(86) == 89 42 assert m.f4(140) == 144 43 44 45def test_function_record_leaks(): 46 class RaisingRepr: 47 def __repr__(self): 48 raise RuntimeError("Surprise!") 49 50 with pytest.raises(RuntimeError): 51 m.register_large_capture_with_invalid_arguments(m) 52 with pytest.raises(RuntimeError): 53 m.register_with_raising_repr(m, RaisingRepr()) 54