test_constants_and_functions.py 1.49 KB
Newer Older
1
# -*- coding: utf-8 -*-
2
3
4
import pytest

m = pytest.importorskip("pybind11_tests.constants_and_functions")
Dean Moldovan's avatar
Dean Moldovan committed
5
6
7


def test_constants():
8
    assert m.some_constant == 14
Dean Moldovan's avatar
Dean Moldovan committed
9
10


11
def test_function_overloading():
12
13
14
15
    assert m.test_function() == "test_function()"
    assert m.test_function(7) == "test_function(7)"
    assert m.test_function(m.MyEnum.EFirstEntry) == "test_function(enum=1)"
    assert m.test_function(m.MyEnum.ESecondEntry) == "test_function(enum=2)"
Dean Moldovan's avatar
Dean Moldovan committed
16

17
18
19
20
21
    assert m.test_function() == "test_function()"
    assert m.test_function("abcd") == "test_function(char *)"
    assert m.test_function(1, 1.0) == "test_function(int, float)"
    assert m.test_function(1, 1.0) == "test_function(int, float)"
    assert m.test_function(2.0, 2) == "test_function(float, int)"
22

Dean Moldovan's avatar
Dean Moldovan committed
23

24
def test_bytes():
25
    assert m.print_bytes(m.return_bytes()) == "bytes[1 0 2 0]"
26
27
28


def test_exception_specifiers():
29
    c = m.C()
30
31
32
33
34
35
36
37
38
    assert c.m1(2) == 1
    assert c.m2(3) == 1
    assert c.m3(5) == 2
    assert c.m4(7) == 3
    assert c.m5(10) == 5
    assert c.m6(14) == 8
    assert c.m7(20) == 13
    assert c.m8(29) == 21

39
40
41
42
    assert m.f1(33) == 34
    assert m.f2(53) == 55
    assert m.f3(86) == 89
    assert m.f4(140) == 144
43
44
45
46
47
48
49
50
51
52
53


def test_function_record_leaks():
    class RaisingRepr:
        def __repr__(self):
            raise RuntimeError("Surprise!")

    with pytest.raises(RuntimeError):
        m.register_large_capture_with_invalid_arguments(m)
    with pytest.raises(RuntimeError):
        m.register_with_raising_repr(m, RaisingRepr())