test_constants_and_functions.py 2.39 KB
Newer Older
Dean Moldovan's avatar
Dean Moldovan committed
1
2
3
4
5
6
7
8
9
import pytest


def test_constants():
    from pybind11_tests import some_constant

    assert some_constant == 14


10
def test_function_overloading():
Dean Moldovan's avatar
Dean Moldovan committed
11
12
    from pybind11_tests import EMyEnumeration, test_function

13
14
15
16
    assert test_function() == "test_function()"
    assert test_function(7) == "test_function(7)"
    assert test_function(EMyEnumeration.EFirstEntry) == "test_function(enum=1)"
    assert test_function(EMyEnumeration.ESecondEntry) == "test_function(enum=2)"
Dean Moldovan's avatar
Dean Moldovan committed
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34


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"


35
def test_scoped_enum():
Dean Moldovan's avatar
Dean Moldovan committed
36
37
    from pybind11_tests import ECMyEnum, test_ecenum

38
    assert test_ecenum(ECMyEnum.Three) == "test_ecenum(ECMyEnum::Three)"
Dean Moldovan's avatar
Dean Moldovan committed
39
    z = ECMyEnum.Two
40
    assert test_ecenum(z) == "test_ecenum(ECMyEnum::Two)"
Dean Moldovan's avatar
Dean Moldovan committed
41
42
43
44
45
46
47
48

    # expected TypeError exceptions for scoped enum ==/!= int comparisons
    with pytest.raises(TypeError):
        assert z == 2
    with pytest.raises(TypeError):
        assert z != 3


49
def test_implicit_conversion():
Dean Moldovan's avatar
Dean Moldovan committed
50
51
52
53
54
55
56
57
58
    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

59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
    assert f(first) == 1

    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))

    # noinspection PyDictCreation
    x = {f(first): 1, f(second): 2}
    x[f(first)] = 3
    x[f(second)] = 4
Dean Moldovan's avatar
Dean Moldovan committed
77
78
79
80
    # Hashing test
    assert str(x) == "{EMode.EFirstMode: 3, EMode.ESecondMode: 4}"


81
def test_bytes():
Dean Moldovan's avatar
Dean Moldovan committed
82
83
    from pybind11_tests import return_bytes, print_bytes

84
    assert print_bytes(return_bytes()) == "bytes[1 0 2 0]"